-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcollections_external_test.go
142 lines (120 loc) · 3.26 KB
/
collections_external_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
136
137
138
139
140
141
142
// Copyright 2019 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"reflect"
"strconv"
"testing"
. "pgregory.net/rapid"
)
func TestSliceOf(t *testing.T) {
t.Parallel()
gens := []*Generator[any]{
SliceOf(Bool()).AsAny(),
SliceOf(Byte()).AsAny(),
SliceOf(Int()).AsAny(),
SliceOf(Uint()).AsAny(),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) {
v := g.Draw(t, "v")
if rv(v).Kind() != reflect.Slice {
t.Fatalf("got not a slice")
}
if rv(v).Len() == 0 {
t.Skip("empty")
}
}))
}
}
func TestSliceOfDistinct(t *testing.T) {
t.Parallel()
g := SliceOfDistinct(Int(), ID[int])
Check(t, func(t *T) {
s := g.Draw(t, "s")
m := map[int]struct{}{}
for _, i := range s {
m[i] = struct{}{}
}
if len(m) != len(s) {
t.Fatalf("%v unique out of %v", len(m), len(s))
}
})
}
func TestSliceOfDistinctBy(t *testing.T) {
t.Parallel()
g := SliceOfDistinct(Int(), func(i int) string { return strconv.Itoa(i % 5) })
Check(t, func(t *T) {
s := g.Draw(t, "s")
m := map[int]struct{}{}
for _, i := range s {
m[i%5] = struct{}{}
}
if len(m) != len(s) {
t.Fatalf("%v unique out of %v", len(m), len(s))
}
})
}
func TestMapOf(t *testing.T) {
t.Parallel()
gens := []*Generator[any]{
MapOf(Bool(), Int()).AsAny(),
MapOf(Int(), Uint()).AsAny(),
MapOf(Uint(), SliceOf(Bool())).AsAny(),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) {
v := g.Draw(t, "v")
if rv(v).Kind() != reflect.Map {
t.Fatalf("got not a map")
}
if rv(v).Len() == 0 {
t.Skip("empty")
}
}))
}
}
func TestMapOfValues(t *testing.T) {
t.Parallel()
g := MapOfValues(Custom(genStruct), func(s testStruct) int { return s.x })
Check(t, func(t *T) {
m := g.Draw(t, "m")
for k, v := range m {
if k != v.x {
t.Fatalf("got key %v with value %v", k, v)
}
}
})
}
func TestCollectionLenLimits(t *testing.T) {
t.Parallel()
genFuncs := []func(i, j int) *Generator[any]{
func(i, j int) *Generator[any] { return StringOfN(Int32Range('A', 'Z'), i, j, -1).AsAny() },
func(i, j int) *Generator[any] { return SliceOfN(Byte(), i, j).AsAny() },
func(i, j int) *Generator[any] { return SliceOfNDistinct(Byte(), i, j, ID[byte]).AsAny() },
func(i, j int) *Generator[any] {
return SliceOfNDistinct(Int(), i, j, func(n int) int { return n % j }).AsAny()
},
func(i, j int) *Generator[any] { return MapOfN(Int(), Int(), i, j).AsAny() },
func(i, j int) *Generator[any] { return MapOfNValues(Int(), i, j, ID[int]).AsAny() },
func(i, j int) *Generator[any] {
return MapOfNValues(Int(), i, j, func(n int) int { return n % j }).AsAny()
},
}
for i, gf := range genFuncs {
t.Run(strconv.Itoa(i), MakeCheck(func(t *T) {
minLen := IntRange(0, 256).Draw(t, "minLen")
maxLen := IntMin(minLen).Draw(t, "maxLen")
s := rv(gf(minLen, maxLen).Draw(t, "s"))
if s.Len() < minLen {
t.Fatalf("got collection of length %v with minLen %v", s.Len(), minLen)
}
if s.Len() > maxLen {
t.Fatalf("got collection of length %v with maxLen %v", s.Len(), maxLen)
}
}))
}
}