Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lowrank committed Sep 6, 2024
1 parent 7467e4e commit b701e86
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 57 deletions.
15 changes: 2 additions & 13 deletions tests/matlab_test.m → tests/matlab_test1.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/matlab_test2_large.m
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/matlab_test2_small.m
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions tests/matlab_test3.m
Original file line number Diff line number Diff line change
@@ -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
44 changes: 0 additions & 44 deletions tests/python_test.py

This file was deleted.

23 changes: 23 additions & 0 deletions tests/python_test1.py
Original file line number Diff line number Diff line change
@@ -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()
46 changes: 46 additions & 0 deletions tests/python_test2_large.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions tests/python_test2_small.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 18 additions & 0 deletions tests/python_test3.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b701e86

Please sign in to comment.