Repository navigation
Expand file tree
/
Copy pathtest_list1.py
More file actions
137 lines (104 loc) · 4.16 KB
/
Copy pathtest_list1.py
File metadata and controls
137 lines (104 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Tests for the module list1.
# Author: 292680
import unittest
import list1
class TestDivisible(unittest.TestCase):
def test_typical(self):
# basic test
result = list1.divisible([12, 7, 15, 22, 30, 41, 18], 3)
self.assertEqual(result, [12, 15, 30, 18])
def test_repeated_numbers(self):
# 8 twice
self.assertEqual(list1.divisible([8, 3, 14, 8, 11, 6], 2),[8, 14, 8, 6])
def test_negative_numbers(self):
# negatives
self.assertEqual(list1.divisible([-12, -5, -21, 13], 3), [-12, -21])
def test_negative_divisor(self):
self.assertEqual(list1.divisible([10, 7, 25, -15, 4], -5),[10, 25, -15])
def test_zero_in_list(self):
# 0 is divisible by everything
self.assertEqual(list1.divisible([0, 3, 11], 11), [0, 11])
def test_empty_list(self):
# empty
self.assertEqual(list1.divisible([], 4), [])
def test_one_element(self):
self.assertEqual(list1.divisible([9], 3), [9])
self.assertEqual(list1.divisible([10], 3), [])
def test_divisor_one(self):
# all divisible by 1
self.assertEqual(list1.divisible([-8, 0, 13, 27], 1), [-8, 0, 13, 27])
def test_nothing_divisible(self):
self.assertEqual(list1.divisible([1, 5, 13, 22], 4), [])
def test_list_not_changed(self):
# check if original list is the same
numbers = [5, 8, 12]
list1.divisible(numbers, 4)
self.assertEqual(numbers, [5, 8, 12])
# errors
def test_divisor_zero(self):
with self.assertRaises(ValueError):
list1.divisible([4, 8, 15], 0)
def test_numbers_not_list(self):
# not a list
with self.assertRaises(TypeError):
list1.divisible((3, 6, 9), 3)
with self.assertRaises(TypeError):
list1.divisible("369", 3)
def test_element_not_int(self):
with self.assertRaises(TypeError):
list1.divisible([4, 7.5, 9], 3)
with self.assertRaises(TypeError):
list1.divisible([4, "7", 9], 3)
def test_divisor_not_int(self):
# 4.0 is float so error
with self.assertRaises(TypeError):
list1.divisible([4, 8, 12], 4.0)
with self.assertRaises(TypeError):
list1.divisible([4, 8, 12], "4")
# ex 2
class TestCommon(unittest.TestCase):
def test_typical(self):
# 2 should be twice
self.assertEqual(list1.common([2, 2, 5, 8, 3], [2, 2, 2, 5, 7]),[2, 2, 5])
def test_order_like_in_x(self):
# order from x
self.assertEqual(list1.common([8, 4, 6], [6, 8, 1]), [8, 6])
def test_same_elements_many_times(self):
# min count
self.assertEqual(list1.common([7, 7, 7], [7, 7, 7, 7]), [7, 7, 7])
self.assertEqual(list1.common([7, 7, 7, 7], [7, 7, 7]), [7, 7, 7])
def test_no_common_elements(self):
self.assertEqual(list1.common([1, 3, 5], [2, 4, 6, 8]), [])
def test_empty_lists(self):
# empty
self.assertEqual(list1.common([], []), [])
self.assertEqual(list1.common([], [3, 8]), [])
self.assertEqual(list1.common([3, 8], []), [])
def test_one_element(self):
self.assertEqual(list1.common([4], [4]), [4])
self.assertEqual(list1.common([4], [9]), [])
def test_same_lists(self):
self.assertEqual(list1.common([5, 2, 5, 9], [5, 2, 5, 9]),[5, 2, 5, 9])
def test_strings(self):
# strings
self.assertEqual(list1.common(["ala", "ma", "kota", "ma"],["ma", "ala", "psa", "ma"]),["ala", "ma", "ma"])
def test_lists_not_changed(self):
# x and y shouldnt change
x = [3, 3, 8]
y = [3, 8, 8]
list1.common(x, y)
self.assertEqual(x, [3, 3, 8])
self.assertEqual(y, [3, 8, 8])
# errors
def test_x_not_list(self):
with self.assertRaises(TypeError):
list1.common((2, 5), [2, 5])
with self.assertRaises(TypeError):
list1.common("25", [2, 5])
def test_y_not_list(self):
with self.assertRaises(TypeError):
list1.common([2, 5], (2, 5))
with self.assertRaises(TypeError):
list1.common([2, 5], None)
if __name__ == "__main__":
unittest.main()