-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_collections.py
157 lines (119 loc) · 3.84 KB
/
test_collections.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
__author__ = 'andi'
from functional_collections import *
from six import string_types
def test_listmap():
assert([1,2,3].map(lambda x:x*2) == [2,4,6])
def test_listfilter():
assert([1,2,3].filter(lambda x:x%2!=0) == [1,3])
def test_listfilterNot():
assert([1,2,3].filterNot(lambda x:x%2==0) == [1,3])
def test_listreduce():
assert([1,2,3].reduce(lambda x,y: x+y) == 6)
assert(["a", "b", "c"].reduce(lambda x,y: x+y) == "abc")
def test_listzip():
assert([1,3,5].zip([2,4,6]) == [(1,2),(3,4),(5,6)])
def test_listflatten():
assert([(1,2),(3,4),(5,6)].flatten() == [1,2,3,4,5,6])
def test_listjoin():
assert([1,2,3].join(", ") == "1, 2, 3")
assert([1,2,3].mkString(", ") == "1, 2, 3")
def test_listtoDict():
assert([(1,2),(3,4)].toDict() == {1:2, 3:4})
def test_listlen():
assert([1,2,3].len() == 3)
assert([1,2,3,4].length() == 4)
assert([1,2,3,4,5].size() == 5)
def test_listenumerate():
assert(list(["a", "b", "c"].enumerate()) == [(0, "a"), (1, "b"), (2, "c")])
def test_listiter():
i = [1,2,3].iter()
assert(next(i) == 1)
assert(next(i) == 2)
assert(next(i) == 3)
i = [1,2,3].iterate()
assert(next(i) == 1)
assert(next(i) == 2)
assert(next(i) == 3)
def test_listmax():
assert([1,7,2].max() == 7)
def test_listmin():
assert([1,7,2].min() == 1)
def test_listtoSet():
assert([1,1,2].toSet() == set([1,2]))
def test_listsorted():
assert([2,1,7,5].sorted() == [1,2,5,7])
def test_listsim():
assert([1,18,2].sum() == 21)
def test_listtotuple():
assert([1,7,5].toTuple() == (1,7,5))
def test_listdistinct():
assert([1,1,2,1].distinct() == [1,2])
def test_listfind():
assert([1,2,3,4].find(lambda x:x==3) == 3)
assert([1,2,3,4].find(lambda x:x==42) == None)
assert([1,2,3,4].find(lambda x:x=="fish") == None)
def test_listexists():
assert([1,2,3,"cat"].exists(lambda x:x=="cat") == True)
assert([1,2,3,"cat"].exists(lambda x:x=="dog") == False)
def test_list_flatMap():
assert([(1,2),(3,4)].flatMap(lambda x:x*2) == [2,4,6,8])
def test_listfold():
assert([1,2,3,4].fold(0)(lambda a,i: a+i) == 10)
assert([1,2,3,4].foldRight([])(lambda a,i: a + [i, ""]) == [1,"",2,"",3,"",4, ""])
def test_listfoldleft():
assert([1,2,3,4].foldLeft(0)(lambda a,i: a+i) == 10)
assert([1,2,3,4].foldLeft([])(lambda a,i: a + [i, ""]) == [4,"",3,"",2,"",1, ""])
def test_list_forall():
assert([1,2,3,4].forall(lambda x:x%2==0) == False)
assert([2,4].forall(lambda x:x%2==0) == True)
def test_list_groupBy():
"TODO work out what this does"
assert([1,2,3].groupBy())
def test_list_grouped():
assert(list([1,2,3,4,5,6].grouped(3)) == [[1,2,3],[4,5,6]])
def test_list_intersect():
assert([1,2,3].intersect([2,3,4]) == set([2,3]))
def test_list_empty():
assert([].empty() == True)
assert([1].empty() == False)
def test_list_nonEmpty():
assert([].nonEmpty() == False)
assert([1].nonEmpty() == True)
def test_list_combinations():
assert(list([1,2,3].combinations(2)) == [(1, 2), (1, 3), (2, 3)])
def test_list_permutations():
assert(list([1,2,3].permutations()) == [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)])
def test_list_reversed():
assert([1,2,3].reversed() == [3,2,1])
def test_list_shuffle():
# TODO come up with a good way to test shuffle
assert([1,2,3].shuffle())
def test_list_collect():
assert([1,2,3].collect(lambda x:x%2) == {0: [2], 1: [1, 3]})
def test_list_match():
# types
r = []
i = []
["foo", 42, 57.1].match({int: i.append, string_types[0]: r.append})
assert(r == ["foo"])
assert(i == [42])
# classes
class F:
pass
r = []
f = F()
["foo", 42, 57.1, f].match({F: r.append})
assert(r == [f])
# functions
r = []
def isthree(n):
return n==3
[1,2,3,4,5].match({isthree: r.append})
assert(r == [3])
# exhaustive warnings
r = []
try:
[1,3].match({isthree: r.append}, exhaustive=True)
assert False, "Didn't throw exhaustive runtime error"
except ValueError as e:
assert(e.args[0] == "Unable to match item 1")