-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
168 lines (125 loc) · 4.91 KB
/
test.py
File metadata and controls
168 lines (125 loc) · 4.91 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import unittest
from stream import Stream
class TestStream(unittest.TestCase):
def test_empty(self):
s = Stream.empty()
self.assertEqual(s.count(), 0)
def test_of(self):
self.assertEqual(Stream.of(1, 2, 3).count(), 3)
self.assertTrue(Stream.of(1, 2, 3).findAny().isPresent())
self.assertEqual(Stream.of(1, 2, 3).findAny().get(), 1)
self.assertEqual(Stream.ofNullable(None), Stream.empty())
self.assertEqual(Stream.of(), Stream.empty())
def test_iterate(self):
index = 0
s = Stream.iterate(index, lambda i: i + 1)
for elem in s:
self.assertEqual(elem, index)
index += 1
if index == 10:
break
def test_generate(self):
index = 0
s = Stream.generate(lambda: 1)
for elem in s:
self.assertEqual(elem, 1)
index += 1
if index == 10:
break
def test_concat(self):
s = Stream.concat(Stream.of(1, 2, 3), Stream.of(
4, 5, 6)).toList()
self.assertEqual(s, [1, 2, 3, 4, 5, 6])
def test_filter(self):
s = Stream.of(1, 2, 3, 4).filter(lambda x: x % 2 == 0).toList()
self.assertEqual(s, [2, 4])
def test_map(self):
s = Stream.of(1, 2, 3).map(lambda x: x**2).toList()
self.assertEqual(s, [1, 4, 9])
def test_flatMap(self):
s = Stream.of(1, 2, 3).flatMap(lambda x: Stream.of(x, x)).toList()
self.assertEqual(s, [1, 1, 2, 2, 3, 3])
def test_distinct(self):
s = Stream.of(1, 1, 2, 2, 3, 4)
self.assertEqual(s.distinct().toList(), [1, 2, 3, 4])
def test_limit(self):
s = Stream.iterate(0, lambda i: i + 1).limit(5).toList()
self.assertEqual(s, [0, 1, 2, 3, 4])
def test_skip(self):
s = Stream.of(1, 2, 3, 4, 5, 6).skip(3).toList()
self.assertEqual(s, [4, 5, 6])
def test_takeWhile(self):
s = Stream.of(1, 2, 3, 4, 5, 6).takeWhile(lambda x: x != 4).toList()
self.assertEqual(s, [1, 2, 3])
def test_dropWhile(self):
s = Stream.of(1, 2, 3, 4, 5, 6).dropWhile(lambda x: x != 4).toList()
self.assertEqual(s, [4, 5, 6])
def test_sorted(self):
s = Stream.of(1, 2, 5, 4, 3).sorted().toList()
s2 = Stream.of(1, 2, 3, 4, 5).sorted(lambda x, y: y-x).toList()
self.assertEqual(s, [1, 2, 3, 4, 5])
self.assertEqual(
s2, [5, 4, 3, 2, 1])
def test_peek(self):
self.count = 0
def inc(*args):
self.count += 1
s = Stream.of(1, 2, 3).peek(inc)
self.assertEqual(self.count, 0)
s = s.toList()
self.assertEqual(self.count, 3)
def test_forEach(self):
self.count = 0
def inc(*args):
self.count += 1
s = Stream.of(1, 2, 3).forEach(inc)
self.assertEqual(self.count, 3)
def test_anyMatch(self):
self.assertTrue(Stream.of(1, 2, 3).anyMatch(lambda x: x % 2 == 0))
self.assertFalse(
Stream.of(1, 3, 5, 7).anyMatch(lambda x: x % 2 == 0))
def test_allMatch(self):
self.assertTrue(Stream.of(2, 4, 6, 8).allMatch(lambda x: x % 2 == 0))
self.assertFalse(Stream.of(1, 2, 3, 4, 5).allMatch(lambda x: x < 5))
def test_noneMatch(self):
self.assertTrue(Stream.of(1, 2, 3, 4).noneMatch(lambda x: x > 4))
self.assertFalse(Stream.of(1, 2, 3, 4, 5).noneMatch(lambda x: x > 4))
def test_findFirst(self):
elem = Stream.of(1, 2, 3, 4, 5).findFirst()
self.assertTrue(elem.isPresent())
self.assertEqual(elem.get(), 1)
def test_findAny(self):
elem = Stream.of(1, 2, 3, 4, 5).findAny()
self.assertTrue(elem.isPresent())
self.assertIn(elem.get(), [1, 2, 3, 4, 5])
def test_reduce(self):
elem = Stream.of(1, 2, 3).reduce(lambda x, y: x - y)
self.assertTrue(elem.isPresent())
self.assertEqual(elem.get(), -4)
elem = Stream.of(1, 2, 3).reduce(lambda x, y: x - y, 0)
self.assertTrue(elem.isPresent())
self.assertEqual(elem.get(), -6)
def test_min(self):
s = Stream.of(1, 2, 5, 4, 3).min()
self.assertTrue(s.isPresent())
self.assertEqual(s.get(), 1)
def test_max(self):
s = Stream.of(1, 2, 5, 4, 3).max()
self.assertTrue(s.isPresent())
self.assertEqual(s.get(), 5)
def test_sum(self):
self.assertTrue(Stream.of(1, 2, 3, 4).sum().isPresent())
self.assertEqual(Stream.of(1, 2, 3, 4).sum().get(), 10)
def test_count(self):
self.assertEqual(Stream.of(1, 2, 3, 4).count(), 4)
self.assertEqual(Stream.empty().count(), 0)
self.assertEqual(Stream.generate(lambda: 1).limit(10).count(), 10)
def test_iter(self):
index = 1
s = Stream.of(1, 2, 3)
for elem in s:
self.assertEqual(elem, index)
index += 1
if __name__ == '__main__':
unittest.main()
iterable