13
13
import com .jnape .palatable .traitor .runners .Traits ;
14
14
import org .junit .Test ;
15
15
import org .junit .runner .RunWith ;
16
- import testsupport .traits .*;
16
+ import testsupport .traits .ApplicativeLaws ;
17
+ import testsupport .traits .Equivalence ;
18
+ import testsupport .traits .FunctorLaws ;
19
+ import testsupport .traits .MonadLaws ;
20
+ import testsupport .traits .MonadRecLaws ;
17
21
18
22
import java .util .ArrayList ;
19
23
import java .util .Collection ;
30
34
import static com .jnape .palatable .lambda .functions .recursion .RecursiveResult .terminate ;
31
35
import static com .jnape .palatable .lambda .functor .builtin .Identity .pureIdentity ;
32
36
import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
33
- import static com .jnape .palatable .lambda .functor .builtin .Writer .*;
37
+ import static com .jnape .palatable .lambda .functor .builtin .Writer .listen ;
38
+ import static com .jnape .palatable .lambda .functor .builtin .Writer .pureWriter ;
39
+ import static com .jnape .palatable .lambda .functor .builtin .Writer .tell ;
40
+ import static com .jnape .palatable .lambda .functor .builtin .Writer .writer ;
34
41
import static com .jnape .palatable .lambda .io .IO .io ;
35
- import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .*;
42
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .empty ;
43
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .liftIterateT ;
44
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .pureIterateT ;
45
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .singleton ;
46
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .IterateT .unfold ;
36
47
import static com .jnape .palatable .lambda .monoid .builtin .AddAll .addAll ;
37
48
import static com .jnape .palatable .lambda .monoid .builtin .Join .join ;
38
49
import static com .jnape .palatable .traitor .framework .Subjects .subjects ;
44
55
import static org .junit .Assert .assertThat ;
45
56
import static testsupport .Constants .STACK_EXPLODING_NUMBER ;
46
57
import static testsupport .matchers .IOMatcher .yieldsValue ;
47
- import static testsupport .matchers .IterateTMatcher .*;
58
+ import static testsupport .matchers .IterateTMatcher .isEmpty ;
59
+ import static testsupport .matchers .IterateTMatcher .iterates ;
60
+ import static testsupport .matchers .IterateTMatcher .iteratesAll ;
48
61
import static testsupport .traits .Equivalence .equivalence ;
49
62
50
63
@ RunWith (Traits .class )
@@ -219,32 +232,59 @@ public void stackSafetyForStrictMonads() {
219
232
@ Test
220
233
public void stackSafetyForNonStrictMonads () {
221
234
IterateT <Lazy <?>, Integer > hugeNonStrictIterateT =
222
- unfold (x -> lazy (() -> x <= 50_000 ? just (tuple (x , x + 1 )) : nothing ()), lazy (0 ));
235
+ unfold (x -> lazy (() -> x <= STACK_EXPLODING_NUMBER ? just (tuple (x , x + 1 )) : nothing ()), lazy (0 ));
223
236
Lazy <Integer > fold = hugeNonStrictIterateT .fold ((x , y ) -> lazy (() -> x + y ), lazy (0 ));
224
237
assertEquals ((Integer ) 1250025000 , fold .value ());
225
238
}
226
239
227
240
@ Test
228
241
public void concatIsStackSafe () {
229
- IterateT <Identity <?>, Integer > bigIterateT = times (10_000 , xs -> xs .concat (singleton (new Identity <>(1 ))),
242
+ IterateT <Identity <?>, Integer > bigIterateT = times (STACK_EXPLODING_NUMBER ,
243
+ xs -> xs .concat (singleton (new Identity <>(1 ))),
230
244
singleton (new Identity <>(0 )));
231
- assertEquals (new Identity <>(10_000 ),
245
+ assertEquals (new Identity <>(STACK_EXPLODING_NUMBER ),
232
246
bigIterateT .fold ((x , y ) -> new Identity <>(x + y ), new Identity <>(0 )));
233
247
}
234
248
249
+ @ Test
250
+ public void iterateT () {
251
+ assertEquals (IterateT .<Identity <?>, Integer >empty (pureIdentity ()).cons (new Identity <>(1 )),
252
+ IterateT .iterateT (new Identity <>(just (tuple (1 , empty (pureIdentity ()))))));
253
+ }
254
+
255
+ @ Test
256
+ public void testToString () {
257
+ assertEquals ("IterateT{ Identity{a=1} :# Identity{a=2} :# Identity{a=3} }" ,
258
+ IterateT .of (new Identity <>(1 ), new Identity <>(2 ), new Identity <>(3 )).toString ());
259
+
260
+ assertEquals ("IterateT{ ... }" ,
261
+ IterateT .iterateT (new Identity <>(just (tuple (1 , empty (pureIdentity ()))))).toString ());
262
+
263
+ assertEquals ("IterateT{ Identity{a=1} :# ... :# Identity{a=3} }" ,
264
+ IterateT .iterateT (new Identity <>(just (tuple (2 , empty (pureIdentity ())))))
265
+ .cons (new Identity <>(1 ))
266
+ .snoc (new Identity <>(3 ))
267
+ .toString ());
268
+
269
+ assertEquals ("IterateT{ ... }" ,
270
+ IterateT .of (new Identity <>(1 ))
271
+ .concat (IterateT .of (new Identity <>(2 )))
272
+ .toString ());
273
+ }
274
+
235
275
@ Test
236
276
public void staticPure () {
237
277
assertEquals (new Identity <>(singletonList (1 )),
238
278
pureIterateT (pureIdentity ())
239
- .<Integer , IterateT <Identity <?>, Integer >>apply (1 )
240
- .<List <Integer >, Identity <List <Integer >>>toCollection (ArrayList ::new ));
279
+ .<Integer , IterateT <Identity <?>, Integer >>apply (1 )
280
+ .<List <Integer >, Identity <List <Integer >>>toCollection (ArrayList ::new ));
241
281
}
242
282
243
283
@ Test
244
284
public void staticLift () {
245
285
assertEquals (new Identity <>(singletonList (1 )),
246
286
liftIterateT ()
247
- .<Integer , Identity <?>, IterateT <Identity <?>, Integer >>apply (new Identity <>(1 ))
248
- .<List <Integer >, Identity <List <Integer >>>toCollection (ArrayList ::new ));
287
+ .<Integer , Identity <?>, IterateT <Identity <?>, Integer >>apply (new Identity <>(1 ))
288
+ .<List <Integer >, Identity <List <Integer >>>toCollection (ArrayList ::new ));
249
289
}
250
290
}
0 commit comments