-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_test.go
135 lines (102 loc) · 3.07 KB
/
set_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gg_test
import (
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
func TestSetOf(t *testing.T) {
defer gtest.Catch(t)
gtest.Equal(gg.SetOf[int](), IntSet{})
gtest.Equal(gg.SetOf(10), IntSet{10: void})
gtest.Equal(gg.SetOf(10, 20), IntSet{10: void, 20: void})
gtest.Equal(gg.SetOf(10, 20, 30), IntSet{10: void, 20: void, 30: void})
gtest.Equal(gg.SetOf(10, 20, 30, 10, 20), IntSet{10: void, 20: void, 30: void})
}
func BenchmarkSetOf_empty(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.SetOf[int]())
}
}
func BenchmarkSetOf_non_empty(b *testing.B) {
for ind := 0; ind < b.N; ind++ {
gg.Nop1(gg.SetOf(10, 20, 30, 40, 50, 60, 70, 80, 90))
}
}
func TestSet(t *testing.T) {
defer gtest.Catch(t)
t.Run(`SetOf`, func(t *testing.T) {
defer gtest.Catch(t)
gtest.Equal(gg.SetOf[int](), IntSet{})
gtest.Equal(gg.SetOf[int](10), IntSet{10: void})
gtest.Equal(gg.SetOf[int](10, 20), IntSet{10: void, 20: void})
})
t.Run(`Add`, func(t *testing.T) {
defer gtest.Catch(t)
set := IntSet{}
gtest.Equal(set, IntSet{})
set.Add(10)
gtest.Equal(set, IntSet{10: void})
set.Add(20, 30)
gtest.Equal(set, IntSet{10: void, 20: void, 30: void})
})
t.Run(`Clear`, func(t *testing.T) {
defer gtest.Catch(t)
gtest.Equal(IntSet{10: void, 20: void}.Clear(), IntSet{})
})
t.Run(`Reset`, func(t *testing.T) {
defer gtest.Catch(t)
set := IntSet{}
gtest.Equal(set, IntSet{})
set.Add(10)
gtest.Equal(set, IntSet{10: void})
set.Reset(20, 30)
gtest.Equal(set, IntSet{20: void, 30: void})
})
// TODO test multiple values (issue: ordering).
t.Run(`Slice`, func(t *testing.T) {
defer gtest.Catch(t)
gtest.Zero(IntSet(nil).Slice())
gtest.Equal(IntSet{}.Slice(), []int{})
gtest.Equal(IntSet{10: void}.Slice(), []int{10})
})
// TODO test multiple values (issue: ordering).
t.Run(`Filter`, func(t *testing.T) {
defer gtest.Catch(t)
gtest.Zero(IntSet(nil).Filter(gg.IsPos[int]))
gtest.Zero(IntSet{}.Filter(gg.IsPos[int]))
gtest.Zero(IntSet{-10: void}.Filter(gg.IsPos[int]))
gtest.Equal(IntSet{10: void}.Filter(gg.IsPos[int]), []int{10})
})
// TODO test multiple values (issue: ordering).
t.Run(`MarshalJSON`, func(t *testing.T) {
defer gtest.Catch(t)
test := func(set IntSet, exp string) {
gtest.Equal(gg.JsonString(set), exp)
}
test(IntSet(nil), `null`)
test(IntSet{}, `[]`)
test(IntSet{}.Add(10), `[10]`)
})
// TODO test multiple values (issue: ordering).
t.Run(`UnmarshalJSON`, func(t *testing.T) {
defer gtest.Catch(t)
test := func(src string, exp IntSet) {
gtest.Equal(gg.JsonDecodeTo[IntSet](src), exp)
}
test(`[]`, IntSet{})
test(`[10]`, IntSet{}.Add(10))
})
// TODO test multiple values (issue: ordering).
t.Run(`GoString`, func(t *testing.T) {
defer gtest.Catch(t)
gtest.Eq(gg.GoString(IntSet(nil)), `gg.Set[int](nil)`)
gtest.Eq(gg.GoString(IntSet{}), `gg.Set[int]{}`)
gtest.Eq(gg.GoString(IntSet{}.Add(10)), `gg.Set[int]{}.Add(10)`)
})
}
func Benchmark_Set_GoString(b *testing.B) {
val := gg.SetOf(10, 20, 30)
for ind := 0; ind < b.N; ind++ {
gg.Nop1(val.GoString())
}
}