Created
November 20, 2018 00:50
-
-
Save ramsesoriginal/1cc6f1422f3c96f19e21e440265195ed to your computer and use it in GitHub Desktop.
Tests für PA03 von CoMa 18/19 - einfach am Ende der Datei einfügen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test(): | |
def do_test(t, args): | |
lok = False | |
try: | |
if len(args) == 3: | |
p, l, r = args | |
retVal = t(p, l) | |
lok = retVal == r | |
else: | |
l, m = args | |
retVal = t(l) | |
lok = retVal == m | |
if not lok: | |
print("NICHT OK\nFehler bei {} mit Argumente: \n{}\nSollte {} sein, ist aber {}".format(t, args[:-1], args[-1], retVal), flush=True) | |
except Exception as ex: | |
print("NICHT OK\nFehler bei {} mit Argumente: \n{}\n".format(t, args[:-1]), flush=True) | |
raise ex | |
return lok | |
ok = True | |
print("\nTest get_min: ", end='', flush=True) | |
ok = ok and do_test(get_min, (None, None)) | |
ok = ok and do_test(get_min, ([], None)) | |
ok = ok and do_test(get_min, ([0], 0)) | |
ok = ok and do_test(get_min, ([0, 0, 0, 0], 0)) | |
ok = ok and do_test(get_min, ([1, 2, 3, 4], 1)) | |
ok = ok and do_test(get_min, ([4, 3, 2, 1], 1)) | |
ok = ok and do_test(get_min, ([1, 2, 3, -1], -1)) | |
ok = ok and do_test(get_min, (list(range(5000,10000)), 5000)) | |
if not ok: | |
return | |
print("OK", flush=True) | |
print("Test get_linedistance: ", end='', flush=True) | |
ok = ok and do_test(get_linedistance, ([], (0, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(0, 0)], (0, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(0, 1)], (0, 0), 1)) | |
ok = ok and do_test(get_linedistance, ([(1, 1)], (0, 0), 1)) | |
ok = ok and do_test(get_linedistance, ([(0, 2)], (0, 0), 4)) | |
ok = ok and do_test(get_linedistance, ([(2, 2)], (0, 0), 4)) | |
all_points = [] | |
for a in range(-10, 11): | |
same_height = [] | |
zero_height = [] | |
ok = ok and do_test(get_linedistance, ([(a, a)], (1, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(a, 2*a)], (2, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(a, -a)], (-1, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(0, a)], (0, a), 0)) | |
ok = ok and do_test(get_linedistance, ([(0, 0)], (a, 0), 0)) | |
ok = ok and do_test(get_linedistance, ([(0, 1)], (a, 0), 1)) | |
ok = ok and do_test(get_linedistance, ([(0, a)], (0, 0), a**2)) | |
ok = ok and do_test(get_linedistance, ([(0, a)], (1, a), 0)) | |
ok = ok and do_test(get_linedistance, ([(1, a)], (a, 0), 0)) | |
for b in range(-10, 11): | |
all_points.append((a,b)) | |
same_height.append((b,a)) | |
zero_height.append((a,0)) | |
ok = ok and do_test(get_linedistance, ([], (a, b), 0)) | |
ok = ok and do_test(get_linedistance, ([(a, 2*a+b)], (2, b), 0)) | |
ok = ok and do_test(get_linedistance, ([(a, -2*a+b)], (-2, b), 0)) | |
ok = ok and do_test(get_linedistance, ([(a, a*a+b)], (a, b), 0)) | |
ok = ok and do_test(get_linedistance, (same_height, (0, a), 0)) | |
ok = ok and do_test(get_linedistance, (same_height, (0, 0), len(same_height) * a**2)) | |
ok = ok and do_test(get_linedistance, (zero_height, (0, 0), 0)) | |
ok = ok and do_test(get_linedistance, (zero_height, (0, b), len(same_height) * b**2)) | |
ok = ok and do_test(get_linedistance, (zero_height, (0, a), len(same_height) * a**2)) | |
ok = ok and do_test(get_linedistance, ([(0, b)], (a, b), 0)) | |
ok = ok and do_test(get_linedistance, ([(1, a+b)], (a, b), 0)) | |
ok = ok and do_test(get_linedistance, ([(1, 0)], (a, b), (a+b)**2)) | |
ok = ok and do_test(get_linedistance, ([(0, b), (1, a+b)], (a, b), 0)) | |
ok = ok and do_test(get_linedistance, (all_points, (0, 0), 16170)) | |
ok = ok and do_test(get_linedistance, (all_points, (1, 2), 34104)) | |
ok = ok and do_test(get_linedistance, (all_points, (-2, 5), 91875)) | |
if not ok: | |
return | |
print("OK", flush=True) | |
print("Tests aus der Angabe: ", end='', flush=True) | |
angabe_points = [(-1, 1), (0, 2), (1, 1), (3, -1)] | |
ok = ok and do_test(linear_regression, (angabe_points, [(1, 1)], 28)) | |
ok = ok and do_test(linear_regression, (angabe_points, [(-1, 2)], 4)) | |
ok = ok and do_test(linear_regression, (angabe_points, [(1, 1) ,(-1, 2)], 4)) | |
if not ok: | |
return | |
print("OK", flush=True) | |
print("Tests mit daten aus der Angabe: ", end='', flush=True) | |
ok = ok and do_test(get_linedistance, (angabe_points, (1, 1), 28)) | |
ok = ok and do_test(get_linedistance, (angabe_points, (-1, 2), 4)) | |
ok = ok and do_test(get_min, ([28, 4], 4)) | |
if not ok: | |
return | |
print("OK", flush=True) | |
print("Test get_linedistance: ", end='', flush=True) | |
ok = ok and do_test(linear_regression, ([], [], None)) | |
ok = ok and do_test(linear_regression, ([(0, 0)], [], None)) | |
ok = ok and do_test(linear_regression, ([], [(0, 0)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 0)], [(0, 0)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 0)], [(0, 0), (0, 0)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 0), (0, 0)], [(0, 0)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 0), (0, 0)], [(0, 0), (0, 0)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 1)], [(0, 0)], 1)) | |
ok = ok and do_test(linear_regression, ([(0, 1)], [(0, 0), (0, 1)], 0)) | |
ok = ok and do_test(linear_regression, ([(0, 1)], [(0, 1), (0, 0)], 0)) | |
diagonal = {} | |
negative_diagonal = {} | |
higher_diagonal = {} | |
horizontals = {} | |
all_points = [] | |
for y in range(-10, 11): | |
diagonal[y] = [] | |
negative_diagonal[y] = [] | |
higher_diagonal[y] = [] | |
horizontals[y] = [] | |
for x in range(-10, 11): | |
all_points.append((x, y)) | |
diagonal[y].append((x, x+y)) | |
negative_diagonal[y].append((x, -x+y)) | |
higher_diagonal[y].append((x, y*x)) | |
horizontals[y].append((x, y)) | |
for a in range(-10, 11): | |
ok = ok and do_test(linear_regression, (higher_diagonal[a], [(a, 0)], 0)) | |
for b in range(-10, 11): | |
ok = ok and do_test(linear_regression, (diagonal[b], [(1, b)], 0)) | |
ok = ok and do_test(linear_regression, (diagonal[b], [(1, b+a)], len(diagonal[b]) * a**2)) | |
ok = ok and do_test(linear_regression, (diagonal[b], [(1, b-a)], len(diagonal[b]) * a**2)) | |
ok = ok and do_test(linear_regression, (negative_diagonal[b], [(-1, b)], 0)) | |
ok = ok and do_test(linear_regression, (negative_diagonal[b], [(-1, b+a)], len(diagonal[b]) * a**2)) | |
ok = ok and do_test(linear_regression, (negative_diagonal[b], [(-1, b-a)], len(diagonal[b]) * a**2)) | |
ok = ok and do_test(linear_regression, (diagonal[b], [(1, b), (b, 1)], 0)) | |
ok = ok and do_test(linear_regression, (horizontals[b], [(0, b)], 0)) | |
ok = ok and do_test(linear_regression, (horizontals[b], [(0, b), (b, 1)], 0)) | |
ok = ok and do_test(linear_regression, (horizontals[b], [(0, b), (a, b)], 0)) | |
avg_result = 16170 | |
ok = ok and do_test(linear_regression, (all_points, [(0, 0)], avg_result)) | |
ok = ok and do_test(linear_regression, (all_points, [(0, 0), (1, 0)], avg_result)) | |
ok = ok and do_test(linear_regression, (all_points, [(0, 0), (-1, 0)], avg_result)) | |
ok = ok and do_test(linear_regression, (all_points, diagonal[0], avg_result)) | |
ok = ok and do_test(linear_regression, (all_points, horizontals[0], avg_result)) | |
ok = ok and do_test(linear_regression, (all_points, negative_diagonal[0], avg_result)) | |
result_b1 = 16611 | |
ok = ok and do_test(linear_regression, (all_points, [(0, 1)], result_b1)) | |
ok = ok and do_test(linear_regression, (all_points, [(0, 1), (1, 1)], result_b1)) | |
ok = ok and do_test(linear_regression, (all_points, [(0, 1), (-1, 1)], result_b1)) | |
ok = ok and do_test(linear_regression, (all_points, diagonal[1], result_b1)) | |
ok = ok and do_test(linear_regression, (all_points, horizontals[1], result_b1)) | |
ok = ok and do_test(linear_regression, (all_points, negative_diagonal[1], result_b1)) | |
if not ok: | |
return | |
print("OK", flush=True) | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment