-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_test.go
149 lines (121 loc) · 3.18 KB
/
dictionary_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
package toolbox
import (
"testing"
)
func TestDictionary(t *testing.T) {
// Test Has method
t.Run("Has", func(t *testing.T) {
dict := Dictionary[string]{
{Key: "name", Value: "Alice"},
{Key: "city", Value: "Wonderland"},
}
if !dict.Has("name") {
t.Error("Expected to have key 'name'")
}
if dict.Has("age") {
t.Error("Expected not to have key 'age'")
}
})
// Test ContainsValue method
t.Run("ContainsValue", func(t *testing.T) {
dict := Dictionary[string]{
{Key: "name", Value: "Alice"},
{Key: "city", Value: "Wonderland"},
}
found, kv := dict.ContainsValue("Alice")
if !found || kv.Key != "name" {
t.Error("Expected to contain value 'Alice' with key 'name'")
}
found, _ = dict.ContainsValue("Bob")
if found {
t.Error("Expected not to contain value 'Bob'")
}
})
// Test Set method
t.Run("Set", func(t *testing.T) {
dict := Dictionary[string]{}
dict.Set("name", "Alice")
if len(dict) != 1 {
t.Errorf("Expected dictionary length 1, got %d", len(dict))
}
if dict[0].Key != "name" || dict[0].Value != "Alice" {
t.Error("Expected key-value pair 'name'-'Alice'")
}
dict.Set("name", "Bob")
if dict[0].Value != "Bob" {
t.Error("Expected key 'name' to be updated to value 'Bob'")
}
})
// Test Delete method
t.Run("Delete", func(t *testing.T) {
dict := Dictionary[string]{
{Key: "name", Value: "Alice"},
{Key: "city", Value: "Wonderland"},
}
err := dict.Delete("name")
if err != nil {
t.Error("Expected to delete key 'name'")
}
if len(dict) != 1 {
t.Errorf("Expected dictionary length 1, got %d", len(dict))
}
if dict.Has("name") {
t.Error("Expected key 'name' to be deleted")
}
err = dict.Delete("non-existing")
if err == nil {
t.Error("Expected error when deleting non-existing key")
}
})
// Test ContainsValue with non-string type
t.Run("ContainsValue with int", func(t *testing.T) {
dict := Dictionary[int]{
{Key: "age", Value: 30},
{Key: "score", Value: 100},
}
found, kv := dict.ContainsValue(30)
if !found || kv.Key != "age" {
t.Error("Expected to contain value 30 with key 'age'")
}
found, _ = dict.ContainsValue(50)
if found {
t.Error("Expected not to contain value 50")
}
})
// Test Set with non-string type
t.Run("Set with int", func(t *testing.T) {
dict := Dictionary[int]{}
dict.Set("age", 30)
if len(dict) != 1 {
t.Errorf("Expected dictionary length 1, got %d", len(dict))
}
if dict[0].Key != "age" || dict[0].Value != 30 {
t.Error("Expected key-value pair 'age'-30")
}
dict.Set("age", 40)
if dict[0].Value != 40 {
t.Error("Expected key 'age' to be updated to value 40")
}
})
// Test Delete with non-string type
t.Run("Delete with int", func(t *testing.T) {
dict := Dictionary[int]{
{Key: "age", Value: 30},
{Key: "score", Value: 100},
}
err := dict.Delete("age")
if err != nil {
t.Error("Expected to delete key 'age'")
}
if len(dict) != 1 {
t.Errorf("Expected dictionary length 1, got %d", len(dict))
}
if dict.Has("age") {
t.Error("Expected key 'age' to be deleted")
}
err = dict.Delete("non-existing")
if err == nil {
t.Error("Expected error when deleting non-existing key")
}
})
}