forked from HU-CS201-master/chap01
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_bag.py
66 lines (58 loc) · 1.42 KB
/
test_bag.py
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
from bag import Bag
def test_find_all():
bag = Bag()
bag.add(1, 10)
bag.add(2, 20)
bag.add(1, 10)
bag.add(1, 20)
pairs = bag.find_all(1)
assert (1,10) in pairs
assert pairs.count((1,10)) == 2
assert bag.find_all(10) == []
assert bag.find_all(2) == [(2,20)]
def test_find():
bag = Bag()
bag.add(1, 10)
bag.add(2, 20)
bag.add(1, 10)
bag.add(1, 20)
assert bag.find(1) in bag.find_all(1)
assert bag.find(2) == (2, 20)
assert bag.find(10) == None
def test_remove():
bag = Bag()
bag.add(1, 10)
bag.add(2, 20)
bag.add(1, 10)
bag.add(1, 20)
pairs = bag.find_all(1)
assert bag.remove(1) in pairs
assert len(bag.find_all(1)) == len(pairs) - 1
assert bag.remove(2) == (2, 20)
assert bag.remove(2) == None
def test_size():
bag = Bag()
assert bag.size() == 0
bag.add(1, 10)
assert bag.size() == 1
bag.add(2, 20)
assert bag.size() == 2
bag.add(2, 20)
assert bag.size() == 3
bag.remove(10)
assert bag.size() == 3
bag.remove(1)
assert bag.size() == 2
def test_keys():
bag = Bag()
assert bag.keys() == []
bag.add(1, 10)
assert bag.keys() == [1]
bag.add(2, 20)
assert sorted(bag.keys()) == [1, 2]
bag.add(1, 10)
assert sorted(bag.keys()) == [1, 2]
bag.remove(1)
assert sorted(bag.keys()) == [1, 2]
bag.remove(2)
assert bag.keys() == [1]