-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
68 lines (61 loc) · 1.89 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
package set
import (
"testing"
)
type Person struct {
FirstName string
LastName string
}
func TestSet(t *testing.T) {
// Test with int
intSet := make(Set[int])
intSet.Add(1)
intSet.Add(2)
if !intSet.Contains(1) || !intSet.Contains(2) {
t.Errorf("Expected set to contain 1 and 2")
}
intSet.Remove(1)
if intSet.Contains(1) {
t.Errorf("Expected set to not contain 1")
}
intSet.Clear()
if intSet.Size() != 0 {
t.Errorf("Expected set to be empty")
}
// Test with string
stringSet := make(Set[string])
stringSet.Add("hello")
stringSet.Add("world")
if !stringSet.Contains("hello") || !stringSet.Contains("world") {
t.Errorf("Expected set to contain 'hello' and 'world'")
}
stringSet.Remove("hello")
if stringSet.Contains("hello") {
t.Errorf("Expected set to not contain 'hello'")
}
stringSet.Clear()
if stringSet.Size() != 0 {
t.Errorf("Expected set to be empty")
}
// Test with custom type
personSet := make(Set[Person])
personSet.Add(Person{FirstName: "John", LastName: "Doe"})
personSet.Add(Person{FirstName: "Jane", LastName: "Smith"})
if !personSet.Contains(Person{FirstName: "John", LastName: "Doe"}) ||
!personSet.Contains(Person{FirstName: "Jane", LastName: "Smith"}) {
t.Errorf("Expected set to contain John Doe and Jane Smith")
}
personSet.Remove(Person{FirstName: "John", LastName: "Doe"})
if personSet.Contains(Person{FirstName: "John", LastName: "Doe"}) {
t.Errorf("Expected set to not contain John Doe")
}
personSet.Clear()
if personSet.Size() != 0 {
t.Errorf("Expected set to be empty")
}
// Test creating a new set with values
testingSet := NewSet("Testing", "Set", "with", "values")
if testingSet.Size() != 4 {
t.Errorf("Expected set to contain 4 values")
}
}