-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_obj_test.go
189 lines (173 loc) · 3.75 KB
/
sorted_obj_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2018-2023 Jean-Francois SMIGIELSKI
// 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 http://mozilla.org/MPL/2.0/.
package bags
import (
"sort"
"testing"
)
type Obj struct {
pk int64
}
func (o *Obj) PK() int64 { return o.pk }
func TestObj_RemoveSorted(T *testing.T) {
bag := SortedObj[int64, *Obj]{&Obj{0}, &Obj{1}, &Obj{2}, &Obj{3}}
for _, v := range []int64{-1, 4} {
if bag.Has(v) {
T.Fatal()
}
bag.Remove(v)
if bag.Has(v) {
T.Fatal()
}
bag.Assert()
}
for _, v := range []int64{0, 1, 2, 3} {
bag.Remove(v)
if bag.Has(v) {
T.Fatal()
}
bag.Assert()
}
}
func TestObj_RemoveUnsorted(T *testing.T) {
bag := SortedObj[int64, *Obj]{&Obj{0}, &Obj{1}, &Obj{2}, &Obj{3}}
for _, v := range []int64{-1, 4} {
if bag.Has(v) {
T.Fatal()
}
bag.Remove(v)
if bag.Has(v) {
T.Fatal()
}
bag.Assert()
}
for _, v := range []int64{3, 2, 1, 0} {
bag.Remove(v)
if bag.Has(v) {
T.Fatal()
}
bag.Assert()
}
}
func TestObj_Lookup(T *testing.T) {
bag := SortedObj[int64, *Obj]{&Obj{0}, &Obj{1}, &Obj{2}, &Obj{3}}
for idx, v := range []int64{0, 1, 2, 3} {
if !bag.Has(v) {
T.Fatal()
}
if x, ok := bag.Get(v); !ok {
T.Fatal()
} else if x.PK() != v {
T.Fatal()
}
if idx != bag.GetIndex(v) {
T.Fatal()
}
}
for _, v := range []int64{-1, -2, 5, 6} {
if bag.Has(v) {
T.Fatal()
}
if _, ok := bag.Get(v); ok {
T.Fatal()
}
if -1 != bag.GetIndex(v) {
T.Fatal()
}
}
}
func TestObj_Slice(T *testing.T) {
bag := SortedObj[int64, *Obj]{&Obj{0}, &Obj{1}, &Obj{2}, &Obj{3}}
testSlice := func(marker int64, max uint32, expectations ...*Obj) {
slice := bag.Slice(marker, max)
if len(slice) != len(expectations) {
T.Fatal()
}
for i, v := range slice {
if v.PK() != expectations[i].PK() {
T.Fatal()
}
}
}
testSlice(bag[0].PK(), 2, bag[1], bag[2])
testSlice(bag[0].PK()-1, 2, bag[0], bag[1])
testSlice(bag[len(bag)-1].PK(), 1)
testSlice(bag[0].PK()-1, MinSliceSize-1, bag[:MinSliceSize]...)
testSlice(bag[0].PK()-1, MaxSliceSize+1, bag...)
}
func TestObj_SliceLarge(T *testing.T) {
const total = 3 * MaxSliceSize
var bag SortedObj[int64, *Obj]
for i := int64(0); i < total; i++ {
bag.Add(&Obj{i})
}
bag.Assert()
slice := bag.Slice(bag[0].PK(), total)
if len(slice) < MinSliceSize || len(slice) > MaxSliceSize {
T.Fatal()
}
}
func TestObj_AppendUnsorted(T *testing.T) {
var bag SortedObj[int64, *Obj]
bag.Append(&Obj{3}, &Obj{1}, &Obj{0}, &Obj{2})
bag.Assert()
if bag.Len() != 4 {
T.Fatalf("invalid length")
}
}
func TestObj_AppendSorted(T *testing.T) {
var bag SortedObj[int64, *Obj]
bag.Append(&Obj{0}, &Obj{1}, &Obj{2}, &Obj{3})
bag.Assert()
if bag.Len() != 4 {
T.Fatal()
}
}
func TestObj_AddUnsorted(T *testing.T) {
var bag SortedObj[int64, *Obj]
bag.Add(&Obj{3})
bag.Add(&Obj{1})
bag.Add(&Obj{0})
bag.Add(&Obj{2})
bag.Assert()
}
func TestObj_AddSorted(T *testing.T) {
var bag SortedObj[int64, *Obj]
bag.Add(&Obj{0})
bag.Add(&Obj{1})
bag.Add(&Obj{2})
bag.Add(&Obj{3})
bag.Assert()
}
// Assert panics if Check returns an error
func (s SortedObj[int64, T]) Assert() {
if err := s.Check(); err != nil {
panic(err)
}
}
// Check validates the ordering and the unicity of the elements in the array
func (s SortedObj[int64, T]) Check() error {
if !sort.IsSorted(s) {
return errUnsorted
}
if !s.areItemsUnique() {
return errDuplicates
}
return nil
}
// areItemsUnique validates the unicity of the elements in the array
func (s SortedObj[int64, T]) areItemsUnique() bool {
if s.Len() < 2 {
return true
}
lastId := s[0].PK()
for _, a := range s[1:] {
if lastId == a.PK() {
return false
}
lastId = a.PK()
}
return true
}