-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtesting_utils_test.go
109 lines (94 loc) · 2.12 KB
/
testing_utils_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
package sortedmap
import (
"fmt"
mrand "math/rand"
"time"
"github.com/umpc/go-sortedmap/asc"
)
func init() {
mrand.Seed(time.Now().UnixNano())
}
func randStr(n int) string {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~[]{}|:;<>,./?"
result := make([]byte, n)
for i := 0; i < n; i++ {
result[i] = chars[mrand.Intn(len(chars))]
}
return string(result)
}
func randRecord() Record {
year := mrand.Intn(2129)
if year < 1 {
year++
}
mth := time.Month(mrand.Intn(12))
if mth < 1 {
mth++
}
day := mrand.Intn(28)
if day < 1 {
day++
}
return Record{
Key: randStr(42),
Val: time.Date(year, mth, day, 0, 0, 0, 0, time.UTC),
}
}
func randRecords(n int) []Record {
records := make([]Record, n)
for i := range records {
records[i] = randRecord()
}
return records
}
func verifyRecords(ch <-chan Record, reverse bool) error {
previousRec := Record{}
if ch != nil {
for rec := range ch {
if previousRec.Key != nil {
switch reverse {
case false:
if previousRec.Val.(time.Time).After(rec.Val.(time.Time)) {
return fmt.Errorf("%v %v",
unsortedErr,
fmt.Sprintf("prev: %+v, current: %+v.", previousRec, rec),
)
}
case true:
if previousRec.Val.(time.Time).Before(rec.Val.(time.Time)) {
return fmt.Errorf("%v %v",
unsortedErr,
fmt.Sprintf("prev: %+v, current: %+v.", previousRec, rec),
)
}
}
}
previousRec = rec
}
} else {
return fmt.Errorf("Channel was nil.")
}
return nil
}
func newSortedMapFromRandRecords(n int) (*SortedMap, []Record, error) {
records := randRecords(n)
sm := New(0, asc.Time)
sm.BatchReplace(records)
iterCh, err := sm.IterCh()
if err != nil {
return sm, records, err
}
defer iterCh.Close()
return sm, records, verifyRecords(iterCh.Records(), false)
}
func newRandSortedMapWithKeys(n int) (*SortedMap, []Record, []interface{}, error) {
sm, records, err := newSortedMapFromRandRecords(n)
if err != nil {
return nil, nil, nil, err
}
keys := make([]interface{}, n)
for n, rec := range records {
keys[n] = rec.Key
}
return sm, records, keys, err
}