aboutsummaryrefslogtreecommitdiff
path: root/container/stack_test.go
blob: 711e6bb36f00c07259f77b00322d28ba7a2868ea (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package container_test

import (
	"fmt"
	"reflect"
	"testing"

	"go.neonxp.ru/pkg/container"
)

func TestGet(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)

	if val := m.Get("one"); val != 1 {
		t.Errorf("expected Get(\"one\") = 1, got %d", val)
	}

	if val := m.Get("two"); val != 2 {
		t.Errorf("expected Get(\"two\") = 2, got %d", val)
	}

	if val := m.Get("three"); val != 0 {
		t.Errorf("expected Get(\"three\") = 0 (zero value), got %d", val)
	}
}

func TestGet2(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)

	// Проверяем существующий ключ
	if val, ok := m.Get2("one"); !ok || val != 1 {
		t.Errorf("expected Get2(\"one\") = (1, true), got (%d, %v)", val, ok)
	}

	// Проверяем другой существующий ключ
	if val, ok := m.Get2("two"); !ok || val != 2 {
		t.Errorf("expected Get2(\"two\") = (2, true), got (%d, %v)", val, ok)
	}

	// Проверяем несуществующий ключ
	if val, ok := m.Get2("three"); ok || val != 0 {
		t.Errorf("expected Get2(\"three\") = (0, false), got (%d, %v)", val, ok)
	}

	// Проверяем пустую карту
	m2 := container.Make[string, int](0)
	if val, ok := m2.Get2("one"); ok || val != 0 {
		t.Errorf("expected Get2(\"one\") = (0, false) for empty map, got (%d, %v)", val, ok)
	}

	// Проверяем после удаления
	m.Delete("one")
	if val, ok := m.Get2("one"); ok || val != 0 {
		t.Errorf("expected Get2(\"one\") = (0, false) after deletion, got (%d, %v)", val, ok)
	}
}

func TestSet(t *testing.T) {
	m := container.Make[string, int](0)

	m.Set("one", 1)
	if val := m.Get("one"); val != 1 {
		t.Errorf("expected value 1 after Set, got %d", val)
	}

	m.Set("one", 2)
	if val := m.Get("one"); val != 2 {
		t.Errorf("expected value 2 after second Set, got %d", val)
	}

	keysCount := make(map[string]int)
	m.Keys(func(k string) bool {
		keysCount[k]++
		return true
	})

	if keysCount["one"] != 1 {
		t.Errorf("expected key \"one\" to appear once in keys, got %d times", keysCount["one"])
	}

	var keys []string
	m.Keys(func(k string) bool {
		keys = append(keys, k)
		return true
	})

	if len(keys) != 1 {
		t.Errorf("expected keys length 1, got %d", len(keys))
	}

	if keys[0] != "one" {
		t.Errorf("expected first key to be \"one\", got %s", keys[0])
	}
}

func TestDelete(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)
	m.Set("three", 3)

	// Проверяем, что ключ существует перед удалением
	if !m.Has("one") {
		t.Error("expected key \"one\" to exist before deletion")
	}

	// Удаляем ключ
	m.Delete("one")

	// Проверяем, что ключ больше не существует
	if m.Has("one") {
		t.Error("expected key \"one\" to be deleted")
	}

	// Проверяем, что значение по ключу - нулевое
	if val := m.Get("one"); val != 0 {
		t.Errorf("expected Get(\"one\") = 0 after deletion, got %d", val)
	}

	// Проверяем, что другие ключи остались
	if !m.Has("two") {
		t.Error("expected key \"two\" to still exist")
	}
	if !m.Has("three") {
		t.Error("expected key \"three\" to still exist")
	}

	// Проверяем порядок ключей
	var keys []string
	m.Keys(func(k string) bool {
		keys = append(keys, k)
		return true
	})

	expected := []string{"two", "three"}
	if !reflect.DeepEqual(keys, expected) {
		t.Errorf("expected keys %v after deletion, got %v", expected, keys)
	}

	// Проверяем удаление несуществующего ключа
	m.Delete("nonexistent")
	// Должно пройти без паники
}

func TestHas(t *testing.T) {
	m := container.Make[string, int](0)

	// Проверяем отсутствие ключа в пустой карте
	if m.Has("one") {
		t.Error("expected Has(\"one\") = false for empty map")
	}

	// Добавляем ключ
	m.Set("one", 1)
	if !m.Has("one") {
		t.Error("expected Has(\"one\") = true after Set")
	}

	// Проверяем другой ключ
	if m.Has("two") {
		t.Error("expected Has(\"two\") = false for non-existent key")
	}

	// Удаляем ключ
	m.Delete("one")
	if m.Has("one") {
		t.Error("expected Has(\"one\") = false after Delete")
	}
}

func TestKeys(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)
	m.Set("three", 3)

	var keys []string
	m.Keys(func(k string) bool {
		keys = append(keys, k)
		return true
	})

	expected := []string{"one", "two", "three"}
	if !reflect.DeepEqual(keys, expected) {
		t.Errorf("expected keys %v, got %v", expected, keys)
	}

	// Test early termination
	var partialKeys []string
	m.Keys(func(k string) bool {
		partialKeys = append(partialKeys, k)
		return len(partialKeys) < 2 // stop after 2 keys
	})

	if len(partialKeys) != 2 {
		t.Errorf("expected 2 keys with early termination, got %d", len(partialKeys))
	}

	if partialKeys[0] != "one" || partialKeys[1] != "two" {
		t.Errorf("expected first two keys to be \"one\", \"two\", got %v", partialKeys)
	}
}

func TestValues(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)
	m.Set("three", 3)

	var values []int
	m.Values(func(v int) bool {
		values = append(values, v)
		return true
	})

	expected := []int{1, 2, 3}
	if !reflect.DeepEqual(values, expected) {
		t.Errorf("expected values %v, got %v", expected, values)
	}

	// Test early termination
	var partialValues []int
	m.Values(func(v int) bool {
		partialValues = append(partialValues, v)
		return len(partialValues) < 2 // stop after 2 values
	})

	if len(partialValues) != 2 {
		t.Errorf("expected 2 values with early termination, got %d", len(partialValues))
	}
}

func TestEntries(t *testing.T) {
	m := container.Make[string, int](0)
	m.Set("one", 1)
	m.Set("two", 2)
	m.Set("three", 3)

	var entries []struct {
		k string
		v int
	}
	m.Entries(func(k string, v int) bool {
		entries = append(entries, struct {
			k string
			v int
		}{k, v})
		return true
	})

	expected := []struct {
		k string
		v int
	}{
		{"one", 1},
		{"two", 2},
		{"three", 3},
	}
	if !reflect.DeepEqual(entries, expected) {
		t.Errorf("expected entries %v, got %v", expected, entries)
	}

	// Test early termination
	var partialEntries []struct {
		k string
		v int
	}
	m.Entries(func(k string, v int) bool {
		partialEntries = append(partialEntries, struct {
			k string
			v int
		}{k, v})
		return len(partialEntries) < 2 // stop after 2 entries
	})

	if len(partialEntries) != 2 {
		t.Errorf("expected 2 entries with early termination, got %d", len(partialEntries))
	}
}

func ExampleMap() {
	// New map with capacity = 10
	m := container.Make[string, int](10)

	// Set example values
	m.Set("one", 1)
	m.Set("two", 2)
	m.Set("three", 3)

	// Get first value
	fmt.Println(`m.Get("one") =`, m.Get("one"))

	// Get value with existence check
	if val, ok := m.Get2("one"); ok {
		fmt.Println(`m.Get2("one") =`, val)
	} else {
		fmt.Println(`m.Get2("one") = key not found`)
	}

	// Get value with existence check
	if val, ok := m.Get2("four"); ok {
		fmt.Println(`m.Get2("four") =`, val)
	} else {
		fmt.Println(`m.Get2("four") = key not found`)
	}

	// Keys iteration
	for k := range m.Keys {
		fmt.Println("key iteration:", k)
	}

	// Values iteration
	for v := range m.Values {
		fmt.Println("value iteration:", v)
	}

	// Key:Value pairs iteration
	for k, v := range m.Entries {
		fmt.Println("pairs iteration:", k, v)
	}

	// Check if key exists
	fmt.Println("Has 'two':", m.Has("two"))

	// Delete a key
	m.Delete("two")

	// Check if key exists
	fmt.Println("Has 'two':", m.Has("two"))

	// Output:
	// m.Get("one") = 1
	// m.Get2("one") = 1
	// m.Get2("four") = key not found
	// key iteration: one
	// key iteration: two
	// key iteration: three
	// value iteration: 1
	// value iteration: 2
	// value iteration: 3
	// pairs iteration: one 1
	// pairs iteration: two 2
	// pairs iteration: three 3
	// Has 'two': true
	// Has 'two': false
}