-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
123 lines (91 loc) · 3.06 KB
/
set_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
package sets_test
import (
"testing"
"github.com/kolah/sets"
"github.com/stretchr/testify/require"
)
func TestSet_Add(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3)
set.Add(4)
require.True(t, set.Contains(4), "Add method did not add the element to the set")
}
func TestSet_Contains(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3)
require.True(t, set.Contains(3), "Contains method did not find the existing element in the set")
require.False(t, set.Contains(4), "Contains method incorrectly found a non-existing element in the set")
}
func TestSet_Items(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3, 4)
expectedItems := []int{1, 2, 3, 4}
require.ElementsMatch(t, expectedItems, set.Items(), "Items method did not return the expected items")
}
func TestSet_Intersection(t *testing.T) {
t.Parallel()
set1 := sets.New(1, 2, 3, 4)
set2 := sets.New(3, 4, 5)
intersection := set1.Intersection(set2)
expectedIntersection := sets.New(3, 4)
require.Equal(
t,
expectedIntersection,
intersection,
"Intersection method did not return the expected intersection set",
)
}
func TestSet_Intersects(t *testing.T) {
t.Parallel()
set1 := sets.New(1, 2, 3)
set2 := sets.New(3, 4, 5)
require.True(t, set1.Intersects(set2), "Intersects method did not detect intersection between sets")
require.False(
t,
set1.Intersects(sets.New(6, 7, 8)),
"Intersects method incorrectly detected intersection between sets with no common elements",
)
}
func TestSet_Remove(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3, 4)
require.True(t, set.Contains(2), "Initial set should contain element 2")
set.Remove(2)
require.False(t, set.Contains(2), "Remove method did not remove the element from the set")
set.Remove(5)
require.False(t, set.Contains(5), "Remove method incorrectly removed a non-existing element")
expectedItems := []int{1, 3, 4}
require.ElementsMatch(t, expectedItems, set.Items(), "Remove method altered the set incorrectly")
}
func TestSet_Clone(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3, 4)
clonedSet := set.Clone()
require.Equal(t, set, clonedSet, "Clone method did not produce an equal cloned set")
clonedSet.Add(5)
require.False(t, set.Contains(5), "Modifying cloned set should not affect the original set")
}
func TestSet_Count(t *testing.T) {
t.Parallel()
emptySet := sets.New[int]()
setWithValues := sets.New(1, 2, 3, 4)
require.Equal(t, 0, emptySet.Count(), "Count method on empty set did not return 0")
require.Equal(t, 4, setWithValues.Count(), "Count method on non-empty set did not return the correct count")
}
func TestSet_Diff(t *testing.T) {
t.Parallel()
set1 := sets.New(1, 2, 3, 4)
set2 := sets.New(3, 4, 5)
diff := set1.Diff(set2)
expectedDiff := sets.New(1, 2)
require.Equal(t, expectedDiff, diff, "Diff method did not return the expected diff set")
}
func TestSet_Flush(t *testing.T) {
t.Parallel()
set := sets.New(1, 2, 3, 4)
set.Add(5)
set.Remove(2)
set.Flush()
expectedItems := make([]int, 0)
require.ElementsMatch(t, expectedItems, set.Items(), "Flush method did not flush the set")
}