@@ -9,14 +9,134 @@ def test_empty(self):
99 self .assertEqual (s .count (), 0 )
1010
1111 def test_of (self ):
12+
1213 s = Stream .of (1 , 2 , 3 )
14+
1315 self .assertEqual (s .count (), 3 )
1416 self .assertTrue (s .findAny ().isPresent ())
1517 self .assertEqual (s .findAny ().get (), 1 )
1618
17- def test_sum (self ):
18- self .assertTrue (Stream .of (1 , 2 , 3 , 4 ).sum ().isPresent ())
19- self .assertEqual (Stream .of (1 , 2 , 3 , 4 ).sum ().get (), 10 )
19+ self .assertEqual (Stream .ofNullable (None ), Stream .empty ())
20+
21+ self .assertEqual (Stream .of (), Stream .empty ())
22+
23+ def test_iterate (self ):
24+ index = 0
25+ s = Stream .iterate (index , lambda i : i + 1 ).iterator ()
26+
27+ for elem in s :
28+ self .assertEqual (elem , index )
29+ index += 1
30+ if index == 10 :
31+ break
32+
33+ def test_generate (self ):
34+ index = 0
35+ s = Stream .generate (lambda : 1 ).iterator ()
36+
37+ for elem in s :
38+ self .assertEqual (elem , 1 )
39+ index += 1
40+ if index == 10 :
41+ break
42+
43+ def test_concat (self ):
44+ s = Stream .concat (Stream .of (1 , 2 , 3 ), Stream .of (
45+ 4 , 5 , 6 )).toList ()
46+ self .assertEqual (s , [1 , 2 , 3 , 4 , 5 , 6 ])
47+
48+ def test_filter (self ):
49+ s = Stream .of (1 , 2 , 3 , 4 ).filter (lambda x : x % 2 == 0 ).toList ()
50+ self .assertEqual (s , [2 , 4 ])
51+
52+ def test_map (self ):
53+ s = Stream .of (1 , 2 , 3 ).map (lambda x : x ** 2 ).toList ()
54+ self .assertEqual (s , [1 , 4 , 9 ])
55+
56+ def test_flatMap (self ):
57+ s = Stream .of (1 , 2 , 3 ).flatMap (lambda x : Stream .of (x , x )).toList ()
58+ self .assertEqual (s , [1 , 1 , 2 , 2 , 3 , 3 ])
59+
60+ def test_distinct (self ):
61+ s = Stream .of (1 , 1 , 2 , 2 , 3 , 4 )
62+ self .assertEqual (s .distinct ().toList (), [1 , 2 , 3 , 4 ])
63+
64+ def test_limit (self ):
65+ s = Stream .iterate (0 , lambda i : i + 1 ).limit (5 ).toList ()
66+ self .assertEqual (s , [0 , 1 , 2 , 3 , 4 ])
67+
68+ def test_skip (self ):
69+ s = Stream .of (1 , 2 , 3 , 4 , 5 , 6 ).skip (3 ).toList ()
70+ self .assertEqual (s , [4 , 5 , 6 ])
71+
72+ def test_takeWhile (self ):
73+ s = Stream .of (1 , 2 , 3 , 4 , 5 , 6 ).takeWhile (lambda x : x != 4 ).toList ()
74+ self .assertEqual (s , [1 , 2 , 3 ])
75+
76+ def test_dropWhile (self ):
77+ s = Stream .of (1 , 2 , 3 , 4 , 5 , 6 ).dropWhile (lambda x : x != 4 ).toList ()
78+ self .assertEqual (s , [4 , 5 , 6 ])
79+
80+ def test_sorted (self ):
81+ s = Stream .of (1 , 2 , 5 , 4 , 3 ).sorted ().toList ()
82+ s2 = Stream .of (1 , 2 , 3 , 4 , 5 ).sorted (lambda x , y : y - x ).toList ()
83+
84+ self .assertEqual (s , [1 , 2 , 3 , 4 , 5 ])
85+ self .assertEqual (
86+ s2 , [5 , 4 , 3 , 2 , 1 ])
87+
88+ def test_peek (self ):
89+ self .count = 0
90+
91+ def inc (* args ):
92+ self .count += 1
93+
94+ s = Stream .of (1 , 2 , 3 ).peek (inc )
95+ self .assertEqual (self .count , 0 )
96+
97+ s = s .toList ()
98+ self .assertEqual (self .count , 3 )
99+
100+ def test_forEach (self ):
101+ self .count = 0
102+
103+ def inc (* args ):
104+ self .count += 1
105+
106+ s = Stream .of (1 , 2 , 3 ).forEach (inc )
107+ self .assertEqual (self .count , 3 )
108+
109+ def test_anyMatch (self ):
110+ self .assertTrue (Stream .of (1 , 2 , 3 ).anyMatch (lambda x : x % 2 == 0 ))
111+ self .assertFalse (
112+ Stream .of (1 , 3 , 5 , 7 ).anyMatch (lambda x : x % 2 == 0 ))
113+
114+ def test_allMatch (self ):
115+ self .assertTrue (Stream .of (2 , 4 , 6 , 8 ).allMatch (lambda x : x % 2 == 0 ))
116+ self .assertFalse (Stream .of (1 , 2 , 3 , 4 , 5 ).allMatch (lambda x : x < 5 ))
117+
118+ def test_noneMatch (self ):
119+ self .assertTrue (Stream .of (1 , 2 , 3 , 4 ).noneMatch (lambda x : x > 4 ))
120+ self .assertFalse (Stream .of (1 , 2 , 3 , 4 , 5 ).noneMatch (lambda x : x > 4 ))
121+
122+ def test_findFirst (self ):
123+ elem = Stream .of (1 , 2 , 3 , 4 , 5 ).findFirst ()
124+ self .assertTrue (elem .isPresent ())
125+ self .assertEqual (elem .get (), 1 )
126+
127+ def test_findAny (self ):
128+ elem = Stream .of (1 , 2 , 3 , 4 , 5 ).findAny ()
129+ self .assertTrue (elem .isPresent ())
130+ self .assertIn (elem .get (), [1 , 2 , 3 , 4 , 5 ])
131+
132+ def test_reduce (self ):
133+ elem = Stream .of (1 , 2 , 3 ).reduce (lambda x , y : x - y )
134+ self .assertTrue (elem .isPresent ())
135+ self .assertEqual (elem .get (), - 4 )
136+
137+ elem = Stream .of (1 , 2 , 3 ).reduce (lambda x , y : x - y , 0 )
138+ self .assertTrue (elem .isPresent ())
139+ self .assertEqual (elem .get (), - 6 )
20140
21141 def test_min (self ):
22142 s = Stream .of (1 , 2 , 5 , 4 , 3 )
@@ -28,15 +148,14 @@ def test_max(self):
28148 self .assertTrue (s .max ().isPresent ())
29149 self .assertEqual (s .max ().get (), 5 )
30150
31- def test_sorted (self ):
32- s = Stream .of (1 , 2 , 5 , 4 , 3 )
33- self .assertEqual (s .sorted ().toList (), [1 , 2 , 3 , 4 , 5 ])
34- self .assertEqual (
35- s .sorted (lambda x , y : y - x ).toList (), [5 , 4 , 3 , 2 , 1 ])
151+ def test_sum (self ):
152+ self .assertTrue (Stream .of (1 , 2 , 3 , 4 ).sum ().isPresent ())
153+ self .assertEqual (Stream .of (1 , 2 , 3 , 4 ).sum ().get (), 10 )
36154
37- def test_distinct (self ):
38- s = Stream .of (1 , 1 , 2 , 2 , 3 , 4 )
39- self .assertEqual (s .distinct ().toList (), [1 , 2 , 3 , 4 ])
155+ def test_count (self ):
156+ self .assertEqual (Stream .of (1 , 2 , 3 , 4 ).count (), 4 )
157+ self .assertEqual (Stream .empty ().count (), 0 )
158+ self .assertEqual (Stream .generate (lambda : 1 ).limit (10 ).count (), 10 )
40159
41160
42161if __name__ == '__main__' :
0 commit comments