1
1
package sampler
2
2
3
3
import (
4
+ "math"
4
5
"reflect"
5
6
"testing"
6
7
)
@@ -17,7 +18,6 @@ func TestTransform(t *testing.T) {
17
18
return v [0 ]
18
19
}
19
20
20
- //Test basic transform
21
21
in = []int {1 , 2 , 3 }
22
22
s , err := Compress (in , 2 , fn )
23
23
if err != nil {
@@ -29,15 +29,80 @@ func TestTransform(t *testing.T) {
29
29
if ! reflect .DeepEqual (in , acc ) {
30
30
t .Fatalf ("Compress was not called with all the integers in the given slice expected: %v, got: %v" , in , acc )
31
31
}
32
- acc = acc [:0 ]
32
+ }
33
+
34
+ func TestTransformBounds (t * testing.T ) {
35
+ var acc []int
36
+ var in []int
37
+
38
+ fn := func (v []int ) int {
39
+ acc = append (acc , v ... )
40
+ sum := 0
41
+ for _ , i := range v {
42
+ sum += i
43
+ }
44
+ return sum
45
+ }
46
+
47
+ in = []int {1 , 2 , 3 , 4 }
48
+ s , err := Compress (in , 2 , fn )
49
+ if err != nil {
50
+ t .Fatal ("Compress was expected to succeed" )
51
+ }
52
+ expected := []int {3 , 7 }
53
+ if ! reflect .DeepEqual (expected , s ) {
54
+ t .Fatalf ("Compress was expected to return %v but returned %v" , expected , s )
55
+ }
56
+ if ! reflect .DeepEqual (in , acc ) {
57
+ t .Fatalf ("Compress was not called with all the integers in the given slice expected: %v, got: %v" , in , acc )
58
+ }
59
+ }
60
+
61
+ func TestTransformBounds2 (t * testing.T ) {
62
+ var acc []int
63
+ var in []int
64
+
65
+ fn := func (v []int ) int {
66
+ if len (v ) == 0 {
67
+ return math .MaxUint8
68
+ }
69
+ acc = append (acc , v ... )
70
+ sum := 0
71
+ for _ , i := range v {
72
+ sum += i
73
+ }
74
+ return sum
75
+ }
76
+
77
+ in = []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }
78
+ s , err := Compress (in , 6 , fn )
79
+ if err != nil {
80
+ t .Fatal ("Compress was expected to succeed" )
81
+ }
82
+ expected := []int {3 , 7 , 11 , 15 , 255 , 255 }
83
+ if ! reflect .DeepEqual (expected , s ) {
84
+ t .Fatalf ("Compress was expected to return %v but returned %v" , expected , s )
85
+ }
86
+ if ! reflect .DeepEqual (in , acc ) {
87
+ t .Fatalf ("Compress was not called with all the integers in the given slice expected: %v, got: %v" , in , acc )
88
+ }
89
+ }
90
+
91
+ func TestTransformError (t * testing.T ) {
92
+ var in []int
93
+
94
+ fn := func (v []int ) int {
95
+ t .Fatal ("sampler was expected te be called" )
96
+ return 0
97
+ }
33
98
34
99
//Test transform with count larger than number of elements
35
- s , err = Compress (in , len (in )+ 1 , fn )
100
+ _ , err : = Compress (in , len (in )+ 1 , fn )
36
101
if err == nil {
37
102
t .Fatal ("Compress was expected to fail" )
38
103
}
39
- }
40
104
105
+ }
41
106
func TestWindowSize (t * testing.T ) {
42
107
if windowSize (1 , 3 ) != 1 {
43
108
t .Error ("window size was expected to return 1" )
0 commit comments