import unittest from comp_ex import * from copy import deepcopy class TestEx1(unittest.TestCase): """ tests function only_positive """ def test_positive_all_positive(self): self.assertEqual(only_positive([1, 2, 3, 4]), [1, 2, 3, 4]) def test_positive_all_negative(self): self.assertEqual(only_positive([-1, -2, -3, -4]), []) def test_positive_mixed(self): self.assertEqual(only_positive([-1, 2, -3, 4]), [2, 4]) def test_positive_mixed_float(self): self.assertEqual(only_positive([-1, 2, -3, 4, -5.0, 6.0]), [2, 4, 6.0]) class TestEx2(unittest.TestCase): """ tests function print_public_methods """ def test_for_list(self): methods = ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] self.assertEqual(print_public_methods(list), methods) def test_for_tuple(self): methods = ['count', 'index'] self.assertEqual(print_public_methods(tuple), methods) def test_for_dict(self): methods = ['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] self.assertEqual(print_public_methods(dict), methods) class TestEx3(unittest.TestCase): """ tests function flatten """ def test_empty(self): self.assertEqual(flatten([]), []) def test_with_two_lists(self): self.assertEqual(flatten([[1, 2, 3], [4, 5, 6]]), [1, 2, 3, 4, 5, 6]) def test_with_three_lists(self): self.assertEqual(flatten([[1, 2, 3], [4, 5, 6], [7]]), [1, 2, 3, 4, 5, 6, 7]) class TestEx4(unittest.TestCase): """ tests function is_prime """ def test_17(self): self.assertEqual(is_prime(17), True) def test_25(self): self.assertEqual(is_prime(25), False) def test_2(self): self.assertEqual(is_prime(2), True) class TestEx5(unittest.TestCase): """ tests function matrix_addition """ def test_two_matrixes(self): m_1 = [[1, 1], [2, 2]] m_2 = [[5, 5], [1, 1]] result = [[6, 6], [3, 3]] self.assertEqual(matrix_addition(m_1, m_2), result) def test_with_one_item(self): m_1 = [[1], [2]] m_2 = [[5], [1]] result = [[6], [3]] self.assertEqual(matrix_addition(m_1, m_2), result) def test_new_result(self): m1 = [[6, 6], [3, 1]] m2 = [[1, 2], [3, 4]] m1_copy = deepcopy(m1) m2_copy = deepcopy(m2) matrix_addition(m1, m2) self.assertEqual(m1, m1_copy) self.assertEqual(m2, m2_copy) if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestEx1)) # TODO uncomment the following test if you proceed # tests for exercise 2 # suite.addTest(unittest.makeSuite(TestEx2)) # tests for exercise 3 # suite.addTest(unittest.makeSuite(TestEx3)) # tests for exercise 4 # suite.addTest(unittest.makeSuite(TestEx4)) # tests for exercise 5 # suite.addTest(unittest.makeSuite(TestEx5)) unittest.TextTestRunner(verbosity=3).run(suite)