-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_lazy_test.go
88 lines (72 loc) · 2.32 KB
/
stream_lazy_test.go
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
package gostream_test
//go:generate genny -in=stream.go -out=intstream.go gen "GenericStreamEntity=int"
import (
"testing"
"github.com/Bios-Marcel/gostream"
)
func TestStreamLazyCollect(t *testing.T) {
testData := []int{1, 2, 3}
resultData := gostream.
StreamIntLazy(testData).
Filter(func(value int) bool { return value != 2 }).
Map(func(value int) int { return value * 4 }).
Collect()
valueAtIndexZero := resultData[0]
if valueAtIndexZero != 4 {
t.Errorf("Error, value at index 0 was %d, but should have been 4.", valueAtIndexZero)
}
valueAtIndexOne := resultData[1]
if valueAtIndexOne != 12 {
t.Errorf("Error, value at index 1 was %d, but should have been 12.", valueAtIndexOne)
}
}
func TestStreamLazyFindFirst(t *testing.T) {
testData := []int{1, 2, 3}
firstValueValid := gostream.
StreamIntLazy(testData).
Filter(func(value int) bool { return value%2 == 1 }).
Map(func(value int) int { return value * 4 }).
FindFirst()
if *firstValueValid != 4 {
t.Errorf("Error, first value was %d, but should have been 4.", firstValueValid)
}
firstValueInvalid := gostream.
StreamIntLazy(testData).
Filter(func(value int) bool { return value == -1 }).
FindFirst()
if firstValueInvalid != nil {
t.Errorf("Error, first value was %d, but should have been nil.", firstValueInvalid)
}
}
func TestStreamLazyReduce(t *testing.T) {
testData := []int{1, 2, 3}
reducedValue := gostream.
StreamIntLazy(testData).
Filter(func(value int) bool { return value != 1 }).
Map(func(value int) int { return value * 4 }).
Reduce(func(one, two int) int { return one + two })
if *reducedValue != 20 {
t.Errorf("Error, first value was %d, but should have been 20.", reducedValue)
}
reducedValue = gostream.
StreamIntLazy(testData).
Filter(func(value int) bool { return value == -1 }).
Reduce(func(one, two int) int { return one + two })
if reducedValue != nil {
t.Errorf("Error, first value was %d, but should have been nil.", reducedValue)
}
}
func TestStreamLaziness(t *testing.T) {
testData := []int{1, 2, 3}
mapIterationCounter := 0
gostream.
StreamIntLazy(testData).
Map(func(value int) int {
mapIterationCounter++
return value * 2
}).
FindFirst()
if mapIterationCounter != 1 {
t.Errorf("Due to laziness, there should have only been one iteration, but there have been %d", mapIterationCounter)
}
}