-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_test.go2
113 lines (83 loc) · 2.06 KB
/
set_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
// 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 addsCount = 3000
func TestNewSet(t *testing.T) {
s := NewSet[int]()
checkSet[int](s, 0, true, t)
}
func TestAdd(t *testing.T) {
s := NewSet[int]()
checkSet[int](s, 0, true, t)
for i := 0; i < addsCount; i++ {
s.Add(i)
checkSet[int](s, i+1, false, t)
}
}
func TestIsEmpty(t *testing.T) {
s := NewSet[int]()
checkIsEmpty[int](s, true, t)
s.Add(1)
s.Add(42)
checkIsEmpty[int](s, false, t)
}
func TestNotEmpty(t *testing.T) {
s := NewSet[int]()
checkNotEmpty[int](s, false, t)
s.Add(0)
s.Add(35)
checkNotEmpty[int](s, true, t)
}
func TestContains(t *testing.T) {
s := NewSet[int]()
checkSet[int](s, 0, true, t)
for i := 0; i < addsCount/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 TestDelete(t *testing.T) {
s := NewSet[int]()
for i := 0; i < addsCount; i++ {
s.Add(i)
}
checkSet[int](s, addsCount, false, t)
for i := 0; i < addsCount; i++ {
checkSet[int](s, addsCount-i, false, t)
s.Delete(i)
}
checkSet[int](s, 0, true, t)
for i := 0; i < addsCount; i++ {
checkSet[int](s, 0, true, t)
s.Delete(i)
}
}
func checkSet[T comparable](s *Set[T], size int, isEmpty bool, t *testing.T) {
checkSize(s, size, t)
checkIsEmpty(s, isEmpty, t)
}
func checkSize[T comparable](s *Set[T], size int, t *testing.T) {
if size != s.Len() {
t.Errorf("Expected set size %d, got %d", size, s.Len())
}
}
func checkIsEmpty[T comparable](s *Set[T], isEmpty bool, t *testing.T) {
if isEmpty != s.IsEmpty() {
t.Errorf("Expected IsEmpty to be %v, got %v", isEmpty, s.IsEmpty())
}
}
func checkNotEmpty[T comparable](s *Set[T], notEmpty bool, t *testing.T) {
if notEmpty != s.NotEmpty() {
t.Errorf("Expected NotEmpty to be %v, got %v", notEmpty, s.NotEmpty())
}
}