-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortedset_test.go2
117 lines (86 loc) · 2.51 KB
/
sortedset_test.go2
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
// Copyright 2020. The GTL Authors. All rights reserved.
// https://github.com/modern-dev/gtl
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package gtl
import (
"testing"
)
const insertsCount = 3000
func sortedSetIntComparator(a, b int) int {
return a - b
}
func TestNewSortedSet(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
checkSortedSet[int](s, 0, true, t)
}
func TestSortedSetAdd(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
checkSortedSet[int](s, 0, true, t)
for i := 0; i < insertsCount; i++ {
s.Add(i)
checkSortedSet[int](s, i+1, false, t)
}
}
func TestSortedSetIsEmpty(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
checkSortedSetIsEmpty[int](s, true, t)
s.Add(1)
s.Add(42)
checkSortedSetIsEmpty[int](s, false, t)
}
func TestSortedSetNotEmpty(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
checkSortedSetNotEmpty[int](s, false, t)
s.Add(0)
s.Add(35)
checkSortedSetNotEmpty[int](s, true, t)
}
func TestSortedSetContains(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
checkSortedSet[int](s, 0, true, t)
for i := 0; i < insertsCount/2; i = i + 2 {
s.Add(i)
if s.Contains(i) != true {
t.Errorf("Expected set to contain %d", i)
}
if s.Contains(i+1) == true {
t.Errorf("Expected set to not contain %d", i+1)
}
}
}
func TestSortedSetDelete(t *testing.T) {
s := NewSortedSet[int](sortedSetIntComparator)
for i := 0; i < insertsCount; i++ {
s.Add(i)
}
checkSortedSet[int](s, insertsCount, false, t)
for i := 0; i < insertsCount; i++ {
checkSortedSet[int](s, insertsCount-i, false, t)
s.Delete(i)
}
checkSortedSet[int](s, 0, true, t)
for i := 0; i < insertsCount; i++ {
checkSortedSet[int](s, 0, true, t)
s.Delete(i)
}
}
func checkSortedSet[T comparable](s *SortedSet[T], size int, isEmpty bool, t *testing.T) {
checkSortedSetSize(s, size, t)
checkSortedSetIsEmpty(s, isEmpty, t)
}
func checkSortedSetSize[T comparable](s *SortedSet[T], size int, t *testing.T) {
if size != s.Len() {
t.Errorf("Expected set size %d, got %d", size, s.Len())
}
}
func checkSortedSetIsEmpty[T comparable](s *SortedSet[T], isEmpty bool, t *testing.T) {
if isEmpty != s.IsEmpty() {
t.Errorf("Expected IsEmpty to be %v, got %v", isEmpty, s.IsEmpty())
}
}
func checkSortedSetNotEmpty[T comparable](s *SortedSet[T], notEmpty bool, t *testing.T) {
if notEmpty != s.NotEmpty() {
t.Errorf("Expected NotEmpty to be %v, got %v", notEmpty, s.NotEmpty())
}
}