diff --git a/unit_tests/doubler.py b/unit_tests/doubler.py new file mode 100644 index 0000000..4a4faa2 --- /dev/null +++ b/unit_tests/doubler.py @@ -0,0 +1,7 @@ +def d(fname="lab99_ex1_easy.expected_input"): + with open(fname) as f: + n = int(f.read().split()[0]) + return str(n * 2 + n % 2) + +if __name__ == "__main__": + print(d()) diff --git a/unit_tests/lab99_ex1_easy.expected_input b/unit_tests/lab99_ex1_easy.expected_input new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/unit_tests/lab99_ex1_easy.expected_input @@ -0,0 +1 @@ +12 diff --git a/unit_tests/lab99_ex1_easy.expected_output b/unit_tests/lab99_ex1_easy.expected_output new file mode 100644 index 0000000..a45fd52 --- /dev/null +++ b/unit_tests/lab99_ex1_easy.expected_output @@ -0,0 +1 @@ +24 diff --git a/unit_tests/lab99_ex1_hard.expected_input b/unit_tests/lab99_ex1_hard.expected_input new file mode 100644 index 0000000..77ac6c0 --- /dev/null +++ b/unit_tests/lab99_ex1_hard.expected_input @@ -0,0 +1 @@ +4129088901429017650 diff --git a/unit_tests/lab99_ex1_hard.expected_output b/unit_tests/lab99_ex1_hard.expected_output new file mode 100644 index 0000000..c430968 --- /dev/null +++ b/unit_tests/lab99_ex1_hard.expected_output @@ -0,0 +1 @@ +8258177802858035300 diff --git a/unit_tests/lab99_ex1_odd.expected_input b/unit_tests/lab99_ex1_odd.expected_input new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/unit_tests/lab99_ex1_odd.expected_input @@ -0,0 +1 @@ +1 diff --git a/unit_tests/lab99_ex1_odd.expected_output b/unit_tests/lab99_ex1_odd.expected_output new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/unit_tests/lab99_ex1_odd.expected_output @@ -0,0 +1 @@ +2 diff --git a/unit_tests/lab99_tests.py b/unit_tests/lab99_tests.py new file mode 100644 index 0000000..4c8e83f --- /dev/null +++ b/unit_tests/lab99_tests.py @@ -0,0 +1,46 @@ +""" +This file is basic example of a unit test for the labs. +The TestClass keeps a list of filenames which refer to sample input and +corresponding output files. + +The test_ex1 function tries to pass the contents of each lab99_ex1_*.in to the +function in the "doubler.py" file; output should match that in lab99_ex1_.*out. + +Use ``python3 lab99_tests.py -v`` to run tests. +""" + + +# ideas: +# tarball the tests, have students decompress the files +# make sure students know how to run the tests +# have .tex read test cases and insert them into the exercises +# change exercises to require filename as argument to main (use default args) +# and have strict return type requirements +# can also redirect stdout; or offer students the option + +# have the TestCase class make sure filenames are valid + +import unittest +import doubler + +def get_output(fname): + return fname + ".expected_output" + +def get_input(fname): + return fname + ".expected_input" + +class TestLab99(unittest.TestCase): + ex1_test_fnames = ["lab99_ex1_easy", "lab99_ex1_hard", "lab99_ex1_odd"] + + def test_ex1(self): + # run subtests, one for each test input + for test in TestLab99.ex1_test_fnames: + # know which test file caused the code for the exercise to fail + with self.subTest(i=test): + with open(get_output(test)) as f: + # test the function! + self.assertEqual(doubler.d(get_input(test)), + f.read().split()[0]) + +if __name__ == '__main__': + unittest.main() diff --git a/unit_tests/lab9_tests.py b/unit_tests/lab9_tests.py new file mode 100644 index 0000000..6dd7a78 --- /dev/null +++ b/unit_tests/lab9_tests.py @@ -0,0 +1,20 @@ +import unittest +import luhns + +class TestLab9(unittest.TestCase): + tests = [{"i": "38520000023237", "o": True}, + {"i": "49927398716", "o": True}, + {"i": "49927398717", "o": False}, + {"i": "1234567812345670", "o": False}, + {"i": "1234567812345678", "o": True}] + + def test_ex1(self): + # run subtests, one for each test input + for test in TestLab9.tests: + # know which test file caused the code for the exercise to fail + with self.subTest(i=test): + # test the function! + self.assertEqual(luhns.validate(test["i"]), test["o"]) + +if __name__ == '__main__': + unittest.main() diff --git a/unit_tests/luhns.py b/unit_tests/luhns.py new file mode 100644 index 0000000..0b865b4 --- /dev/null +++ b/unit_tests/luhns.py @@ -0,0 +1,6 @@ +def validate(card): + return {"38520000023237": True, + "49927398716": False, #;True, + "49927398717": False, + "1234567812345670": False, + "1234567812345678": True}[card]