1
1
package com .jnape .palatable .lambda .semigroup .builtin ;
2
2
3
+ import com .jnape .palatable .lambda .adt .Maybe ;
4
+ import com .jnape .palatable .lambda .adt .Unit ;
5
+ import com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly ;
3
6
import com .jnape .palatable .lambda .semigroup .Semigroup ;
4
7
import org .junit .Test ;
5
8
9
+ import java .util .Arrays ;
10
+
6
11
import static com .jnape .palatable .lambda .adt .Maybe .just ;
7
12
import static com .jnape .palatable .lambda .adt .Maybe .nothing ;
13
+ import static com .jnape .palatable .lambda .adt .Unit .UNIT ;
14
+ import static com .jnape .palatable .lambda .functions .builtin .fn1 .Repeat .repeat ;
8
15
import static com .jnape .palatable .lambda .semigroup .builtin .Absent .absent ;
9
16
import static org .junit .Assert .assertEquals ;
10
17
11
18
public class AbsentTest {
12
19
13
20
@ Test
14
21
public void semigroup () {
22
+ Semigroup <Integer > addition = Integer ::sum ;
23
+
24
+ assertEquals (just (3 ), absent (addition , just (1 ), just (2 )));
25
+ assertEquals (nothing (), absent (addition , nothing (), just (1 )));
26
+ assertEquals (nothing (), absent (addition , just (1 ), nothing ()));
27
+ assertEquals (nothing (), absent (addition , nothing (), nothing ()));
28
+ }
29
+
30
+ @ Test
31
+ public void foldRight () {
32
+ Absent <Integer > absent = absent ();
33
+ Semigroup <Integer > addition = Integer ::sum ;
34
+
35
+ assertEquals (just (3 ), absent .apply (addition ).foldRight (just (0 ), Arrays .asList (just (1 ), just (2 ))).value ());
36
+ assertEquals (nothing (), absent .apply (addition ).foldRight (just (0 ), Arrays .asList (nothing (), just (1 ))).value ());
37
+ assertEquals (nothing (), absent .apply (addition ).foldRight (just (0 ), Arrays .asList (just (1 ), nothing ())).value ());
38
+ assertEquals (nothing (), absent .apply (addition ).foldRight (just (0 ), Arrays .asList (nothing (), nothing ())).value ());
39
+ }
40
+
41
+ @ Test
42
+ public void foldLeft () {
15
43
Absent <Integer > absent = absent ();
16
44
Semigroup <Integer > addition = Integer ::sum ;
17
45
18
- assertEquals (just (3 ), absent .apply (addition , just (1 ), just (2 )));
19
- assertEquals (nothing (), absent .apply (addition , nothing (), just (1 )));
20
- assertEquals (nothing (), absent .apply (addition , just (1 ), nothing ()));
21
- assertEquals (nothing (), absent .apply (addition , nothing (), nothing ()));
46
+ assertEquals (just (3 ), absent .apply (addition ).foldLeft (just (0 ), Arrays .asList (just (1 ), just (2 ))));
47
+ assertEquals (nothing (), absent .apply (addition ).foldLeft (just (0 ), Arrays .asList (nothing (), just (1 ))));
48
+ assertEquals (nothing (), absent .apply (addition ).foldLeft (just (0 ), Arrays .asList (just (1 ), nothing ())));
49
+ assertEquals (nothing (), absent .apply (addition ).foldLeft (just (0 ), Arrays .asList (nothing (), nothing ())));
50
+ }
51
+
52
+ @ Test (timeout = 200 )
53
+ public void foldRightShortCircuit () {
54
+ Maybe <Unit > result = Absent .<Unit >absent (Constantly ::constantly )
55
+ .foldRight (just (UNIT ), repeat (nothing ())).value ();
56
+ assertEquals (nothing (), result );
57
+
58
+ result = Absent .<Unit >absent (Constantly ::constantly )
59
+ .foldRight (nothing (), repeat (just (UNIT ))).value ();
60
+ assertEquals (nothing (), result );
61
+ }
62
+
63
+ @ Test (timeout = 200 )
64
+ public void foldLeftShortCircuit () {
65
+ Maybe <Unit > result = Absent .<Unit >absent (Constantly ::constantly )
66
+ .foldLeft (just (UNIT ), repeat (nothing ()));
67
+ assertEquals (nothing (), result );
68
+
69
+ result = Absent .<Unit >absent (Constantly ::constantly )
70
+ .foldLeft (nothing (), repeat (just (UNIT )));
71
+ assertEquals (nothing (), result );
72
+ }
73
+
74
+ @ Test (timeout = 200 )
75
+ public void checkedApplyFoldRightShortCircuit () {
76
+ Maybe <Unit > result = Absent .<Unit >absent ().checkedApply (Constantly ::constantly )
77
+ .foldRight (just (UNIT ), repeat (nothing ())).value ();
78
+ assertEquals (nothing (), result );
79
+
80
+ result = Absent .<Unit >absent ().checkedApply (Constantly ::constantly )
81
+ .foldRight (nothing (), repeat (just (UNIT ))).value ();
82
+ assertEquals (nothing (), result );
83
+ }
84
+
85
+ @ Test (timeout = 200 )
86
+ public void checkedApplyFoldLeftShortCircuit () {
87
+ Maybe <Unit > result = Absent .<Unit >absent ().checkedApply (Constantly ::constantly )
88
+ .foldLeft (just (UNIT ), repeat (nothing ()));
89
+ assertEquals (nothing (), result );
90
+
91
+ result = Absent .<Unit >absent ().checkedApply (Constantly ::constantly )
92
+ .foldLeft (nothing (), repeat (just (UNIT )));
93
+ assertEquals (nothing (), result );
22
94
}
23
95
}
0 commit comments