From b701e862bb67145cb5cece903381ce01aa26f89e Mon Sep 17 00:00:00 2001 From: Yimin Zhong Date: Fri, 6 Sep 2024 17:37:20 -0500 Subject: [PATCH] update tests --- tests/{matlab_test.m => matlab_test1.m} | 15 ++------ tests/matlab_test2_large.m | 11 ++++++ tests/matlab_test2_small.m | 11 ++++++ tests/matlab_test3.m | 13 +++++++ tests/python_test.py | 44 ----------------------- tests/python_test1.py | 23 +++++++++++++ tests/python_test2_large.py | 46 +++++++++++++++++++++++++ tests/python_test2_small.py | 23 +++++++++++++ tests/python_test3.py | 18 ++++++++++ 9 files changed, 147 insertions(+), 57 deletions(-) rename tests/{matlab_test.m => matlab_test1.m} (52%) create mode 100644 tests/matlab_test2_large.m create mode 100644 tests/matlab_test2_small.m create mode 100644 tests/matlab_test3.m delete mode 100644 tests/python_test.py create mode 100644 tests/python_test1.py create mode 100644 tests/python_test2_large.py create mode 100644 tests/python_test2_small.py create mode 100644 tests/python_test3.py diff --git a/tests/matlab_test.m b/tests/matlab_test1.m similarity index 52% rename from tests/matlab_test.m rename to tests/matlab_test1.m index 7d58ec7..09fa48e 100644 --- a/tests/matlab_test.m +++ b/tests/matlab_test1.m @@ -8,19 +8,8 @@ hw_assert(hw00_worker.p1(5) == 7) hw_assert(hw00_worker.p1(6) == 13) hw_assert(hw00_worker.p1(7) == 24) - -hw_assert(hw00_worker.p2(1) == 1) -hw_assert(hw00_worker.p2(-1) == -1) -hw_assert(hw00_worker.p2([[1 0];[0 1]]) == 1) -hw_assert(hw00_worker.p2([[1 1];[1 1]]) == 0) -hw_assert(hw00_worker.p2(magic(4)) == 0) -hw_assert(hw00_worker.p2(magic(6)) == 0) - -start_time = tic; -hw00_worker.p3(); -elapsed = toc(start_time); - -hw_assert(abs(elapsed - 1.0) < 0.05); +hw_assert(hw00_worker.p1(8) == 44) +hw_assert(hw00_worker.p1(9) == 81) function hw_assert(X) if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end diff --git a/tests/matlab_test2_large.m b/tests/matlab_test2_large.m new file mode 100644 index 0000000..fbf3ac5 --- /dev/null +++ b/tests/matlab_test2_large.m @@ -0,0 +1,11 @@ +hw00_worker = hw00(); + +hw_assert(hw00_worker.p2(5 * eye(3)) == 125) +hw_assert(hw00_worker.p2(5 * eye(6)) == 5^6) +hw_assert(hw00_worker.p2(magic(4)) == 0) +hw_assert(hw00_worker.p2(magic(6)) == 0) +hw_assert(hw00_worker.p2(magic(8)) == 0) + +function hw_assert(X) + if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end +end \ No newline at end of file diff --git a/tests/matlab_test2_small.m b/tests/matlab_test2_small.m new file mode 100644 index 0000000..4588da4 --- /dev/null +++ b/tests/matlab_test2_small.m @@ -0,0 +1,11 @@ +hw00_worker = hw00(); + +hw_assert(hw00_worker.p2(1) == 1) +hw_assert(hw00_worker.p2(-1) == -1) +hw_assert(hw00_worker.p2([[1 0];[0 1]]) == 1) +hw_assert(hw00_worker.p2([[1 1];[1 1]]) == 0) +hw_assert(hw00_worker.p2([[1 3];[3 1]]) == -8) + +function hw_assert(X) + if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end +end \ No newline at end of file diff --git a/tests/matlab_test3.m b/tests/matlab_test3.m new file mode 100644 index 0000000..5b229c9 --- /dev/null +++ b/tests/matlab_test3.m @@ -0,0 +1,13 @@ +hw00_worker = hw00(); + +for i = 1:10 + start_time = tic; + hw00_worker.p3(); + elapsed = toc(start_time); + + hw_assert(abs(elapsed - 1.0) < 0.05); +end + +function hw_assert(X) + if X; fprintf('\t PASS\n'); else; fprintf('\t FAIL\n'); end +end \ No newline at end of file diff --git a/tests/python_test.py b/tests/python_test.py deleted file mode 100644 index 7e4284e..0000000 --- a/tests/python_test.py +++ /dev/null @@ -1,44 +0,0 @@ -from hw00 import * -import numpy as np -import time - -def test_p1(): - hw_assert (p1(0) == 0) - hw_assert (p1(1) == 1) - hw_assert (p1(2) == 1) - hw_assert (p1(3) == 2) - hw_assert (p1(4) == 4) - hw_assert (p1(5) == 7) - hw_assert (p1(6) == 13) - hw_assert (p1(7) == 24) - -def test_p2(): - A = np.array([[1]]) - hw_assert (p2(A) == 1) - A = np.array([[1, 2], [3, 4]]) - hw_assert (p2(A) == -2) - A = np.array([[-1]]) - hw_assert (p2(A) == -1) - A = np.array([[1, 0], [0, 1]]) - hw_assert (p2(A) == 1) - A = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) - hw_assert (p2(A) == 1) - A = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) - hw_assert (p2(A) == 1) -def test_p3(): - start = time.time() - p3() - end = time.time() - hw_assert (abs(end - start - 1.0) < 0.05) - - -def hw_assert(X): - if X: - print('\t PASS') - else: - print('\t FAIL') - - -test_p1() -test_p2() -test_p3() \ No newline at end of file diff --git a/tests/python_test1.py b/tests/python_test1.py new file mode 100644 index 0000000..cfb0a0e --- /dev/null +++ b/tests/python_test1.py @@ -0,0 +1,23 @@ +from hw00 import p1 +import numpy as np +import time + +def test_p1(): + hw_assert (p1(0) == 0) + hw_assert (p1(1) == 1) + hw_assert (p1(2) == 1) + hw_assert (p1(3) == 2) + hw_assert (p1(4) == 4) + hw_assert (p1(5) == 7) + hw_assert (p1(6) == 13) + hw_assert (p1(7) == 24) + hw_assert (p1(8) == 44) + hw_assert (p1(9) == 81) + +def hw_assert(X): + if X: + print('\t PASS') + else: + print('\t FAIL') + +test_p1() \ No newline at end of file diff --git a/tests/python_test2_large.py b/tests/python_test2_large.py new file mode 100644 index 0000000..6a3e1de --- /dev/null +++ b/tests/python_test2_large.py @@ -0,0 +1,46 @@ +from hw00 import p2 +import numpy as np +import time + +def magic(n): + n = int(n) + if n < 3: + raise ValueError("Size must be at least 3") + if n % 2 == 1: + p = np.arange(1, n+1) + return n*np.mod(p[:, None] + p - (n+3)//2, n) + np.mod(p[:, None] + 2*p-2, n) + 1 + elif n % 4 == 0: + J = np.mod(np.arange(1, n+1), 4) // 2 + K = J[:, None] == J + M = np.arange(1, n*n+1, n)[:, None] + np.arange(n) + M[K] = n*n + 1 - M[K] + else: + p = n//2 + M = magic(p) + M = np.block([[M, M+2*p*p], [M+3*p*p, M+p*p]]) + i = np.arange(p) + k = (n-2)//4 + j = np.concatenate((np.arange(k), np.arange(n-k+1, n))) + M[np.ix_(np.concatenate((i, i+p)), j)] = M[np.ix_(np.concatenate((i+p, i)), j)] + M[np.ix_([k, k+p], [0, k])] = M[np.ix_([k+p, k], [0, k])] + return M + +def test_p2(): + A = np.array([[5, 0, 0, 0], [0, 5, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]]) + hw_assert (p2(A) == 5**4) + A = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) + hw_assert (p2(A) == 1) + A = np.array(magic(4)) + hw_assert (p2(A) == 0) + A = np.array(magic(6)) + hw_assert (p2(A) == 0) + A = np.array(magic(8)) + hw_assert (p2(A) == 0) + +def hw_assert(X): + if X: + print('\t PASS') + else: + print('\t FAIL') + +test_p2() diff --git a/tests/python_test2_small.py b/tests/python_test2_small.py new file mode 100644 index 0000000..3664979 --- /dev/null +++ b/tests/python_test2_small.py @@ -0,0 +1,23 @@ +from hw00 import p2 +import numpy as np +import time + +def test_p2(): + A = np.array([[1]]) + hw_assert (p2(A) == 1) + A = np.array([[1, 2], [3, 4]]) + hw_assert (p2(A) == -2) + A = np.array([[-1]]) + hw_assert (p2(A) == -1) + A = np.array([[1, 3], [3, 1]]) + hw_assert (p2(A) == -8) + A = np.array([[1, 0], [0, 1]]) + hw_assert (p2(A) == 1) + +def hw_assert(X): + if X: + print('\t PASS') + else: + print('\t FAIL') + +test_p2() diff --git a/tests/python_test3.py b/tests/python_test3.py new file mode 100644 index 0000000..f583580 --- /dev/null +++ b/tests/python_test3.py @@ -0,0 +1,18 @@ +from hw00 import p3 +import numpy as np +import time + +def test_p3(): + for i in range(10): + start = time.time() + p3() + end = time.time() + hw_assert (abs(end - start - 1.0) < 0.05) + +def hw_assert(X): + if X: + print('\t PASS') + else: + print('\t FAIL') + +test_p3()