-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomap_test.go
1704 lines (1417 loc) · 53.9 KB
/
gomap_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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gomap_test
import (
"math"
"reflect"
"sort"
"strconv"
"strings"
"testing"
"github.com/lindsaygelle/gomap"
"github.com/lindsaygelle/slice"
)
// TestHasnewMapable tests Map.
func TestHasnewMapable(t *testing.T) {
table := gomap.Map[string, string]{
"hello": "world"}
t.Log(table)
}
// TestAdd tests Map.Add.
func TestAdd(t *testing.T) {
// Test case 1: Add key-value pairs to an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 3)
// Verify the values using Get method.
value, ok := newMap.Get("apple")
if !ok || value != 5 {
t.Errorf("Expected key 'apple' to have value 5, but got %v", value)
}
value, ok = newMap.Get("banana")
if !ok || value != 3 {
t.Errorf("Expected key 'banana' to have value 3, but got %v", value)
}
// Test case 2: Replace existing key-value pair.
newMap.Add("banana", 10) // Updates the value for the key "banana" to 10.
value, ok = newMap.Get("banana")
if !ok || value != 10 {
t.Errorf("Expected key 'banana' to have updated value 10, but got %v", value)
}
// Test case 3: Add a new key-value pair.
newMap.Add("cherry", 8)
value, ok = newMap.Get("cherry")
if !ok || value != 8 {
t.Errorf("Expected key 'cherry' to have value 8, but got %v", value)
}
// Verify that other keys are not affected.
value, ok = newMap.Get("grape")
if ok {
t.Errorf("Expected key 'grape' to be absent, but it was found with value %v", value)
}
}
// TestAddLength tests Map.AddLength.
func TestAddLength(t *testing.T) {
// Test case 1: Add key-value pairs to an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
length := newMap.AddLength("apple", 5)
if length != 1 {
t.Errorf("Expected length of gomap after adding 'apple' with value 5 to be 1, but got %v", length)
}
// Test case 2: Replace an existing key-value pair.
length = newMap.AddLength("apple", 10)
if length != 1 {
t.Errorf("Expected length of gomap after updating 'apple' with value 10 to be 1, but got %v", length)
}
// Test case 3: Add a new key-value pair.
length = newMap.AddLength("banana", 3)
if length != 2 {
t.Errorf("Expected length of gomap after adding 'banana' with value 3 to be 2, but got %v", length)
}
// Test case 4: Add another new key-value pair.
length = newMap.AddLength("cherry", 8)
if length != 3 {
t.Errorf("Expected length of gomap after adding 'cherry' with value 8 to be 3, but got %v", length)
}
// Verify that other keys are not affected.
value, ok := newMap.Get("grape")
if ok {
t.Errorf("Expected key 'grape' to be absent, but it was found with value %v", value)
}
}
// TestAddMany tests Map.AddMany.
func TestAddMany(t *testing.T) {
// Test case 1: Add key-value pairs to an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
newMap.AddMany(map[string]int{"orange": 7, "grape": 4}, map[string]int{"kiwi": 6, "pear": 9})
// Verify the added key-value pairs.
expected := map[string]int{"orange": 7, "grape": 4, "kiwi": 6, "pear": 9}
for key, expectedValue := range expected {
value, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be present, but it was not found", key)
}
if value != expectedValue {
t.Errorf("Expected value for key '%s' to be %d, but got %d", key, expectedValue, value)
}
}
// Test case 2: Replace existing key-value pairs and add new ones.
newMap.AddMany(map[string]int{"orange": 10, "grape": 3}, map[string]int{"apple": 5, "banana": 8})
// Verify the updated and added key-value pairs.
expected = map[string]int{"orange": 10, "grape": 3, "kiwi": 6, "pear": 9, "apple": 5, "banana": 8}
for key, expectedValue := range expected {
value, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be present, but it was not found", key)
}
if value != expectedValue {
t.Errorf("Expected value for key '%s' to be %d, but got %d", key, expectedValue, value)
}
}
// Test case 3: Add new key-value pairs.
newMap.AddMany(map[string]int{"watermelon": 12, "cherry": 7})
// Verify the added key-value pairs.
expected = map[string]int{"orange": 10, "grape": 3, "kiwi": 6, "pear": 9, "apple": 5, "banana": 8, "watermelon": 12, "cherry": 7}
for key, expectedValue := range expected {
value, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be present, but it was not found", key)
}
if value != expectedValue {
t.Errorf("Expected value for key '%s' to be %d, but got %d", key, expectedValue, value)
}
}
// Verify that other keys are not affected.
_, ok := newMap.Get("pineapple")
if ok {
t.Errorf("Expected key 'pineapple' to be absent, but it was found")
}
}
// TestAddManyFunc tests Map.AddManyFunc.
func TestAddManyFunc(t *testing.T) {
// Test case 1: Add key-value pairs with values greater than 0 to the gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
values := []map[string]int{{"apple": 5, "orange": -3, "banana": 10}}
condition := func(i int, key string, value int) bool {
return value > 0
}
newMap.AddManyFunc(values, condition)
// Verify that only the key-value pairs with values greater than 0 are added to the gomap.
expected := &gomap.Map[string, int]{"apple": 5, "banana": 10}
if !reflect.DeepEqual(newMap, expected) {
t.Errorf("Expected %v, but got %v", expected, newMap)
}
// Test case 2: Add all key-value pairs to the gomap.
newMap = &gomap.Map[string, int]{} // Create an empty gomap.
values = []map[string]int{{"apple": 5, "orange": -3, "banana": 10}}
condition = func(i int, key string, value int) bool {
return true // Add all key-value pairs
}
newMap.AddManyFunc(values, condition)
// Verify that all key-value pairs are added to the gomap.
expected = &gomap.Map[string, int]{"apple": 5, "orange": -3, "banana": 10}
if !reflect.DeepEqual(newMap, expected) {
t.Errorf("Expected %v, but got %v", expected, newMap)
}
}
// TestAddManyOK tests Map.AddManyOK.
func TestAddManyOK(t *testing.T) {
// Test case 1: Add key-value pairs to an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
results := newMap.AddManyOK(
map[string]int{"apple": 5},
map[string]int{"banana": 3},
map[string]int{"banana": 10},
map[string]int{"cherry": 8},
)
// Verify the success status of insertions.
expectedResults := &slice.Slice[bool]{true, true, false, true}
if !reflect.DeepEqual(results, expectedResults) {
t.Errorf("Expected insertion results %v, but got %v", expectedResults, *results)
}
// Verify the added and updated key-value pairs.
expected := map[string]int{"apple": 5, "cherry": 8}
for key, expectedValue := range expected {
value, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be present, but it was not found", key)
}
if value != expectedValue {
t.Errorf("Expected value for key '%s' to be %d, but got %d", key, expectedValue, value)
}
}
// Test case 2: Add new key-value pairs.
results = newMap.AddManyOK(
map[string]int{"watermelon": 12, "pineapple": 7},
)
// Verify the success status of insertions.
expectedResults = &slice.Slice[bool]{true, true}
if !reflect.DeepEqual(results, expectedResults) {
t.Errorf("Expected insertion results %v, but got %v", expectedResults, *results)
}
// Verify the added key-value pairs.
expected = map[string]int{"apple": 5, "cherry": 8, "watermelon": 12, "pineapple": 7}
for key, expectedValue := range expected {
value, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be present, but it was not found", key)
}
if value != expectedValue {
t.Errorf("Expected value for key '%s' to be %d, but got %d", key, expectedValue, value)
}
}
}
// TestAddOK tests Map.AddOK.
func TestAddOK(t *testing.T) {
newMap := make(gomap.Map[string, int])
// Test adding a new key-value pair.
added := newMap.AddOK("apple", 5)
if !added {
t.Error("Expected 'apple' to be added, but it was not.")
}
// Check if the key-value pair is added correctly.
value, exists := newMap["apple"]
if !exists || value != 5 {
t.Fatalf("Expected key 'apple' with value '5', but got value '%d'", value)
}
// Test adding an existing key.
reAdded := newMap.AddOK("apple", 10)
if reAdded {
t.Error("Expected 'apple' to not be re-added, but it was.")
}
// Check if the value for 'apple' remains unchanged.
value, exists = newMap["apple"]
if !exists || value != 5 {
t.Fatalf("Expected key 'apple' to have value '5' after re-adding attempt, but got value '%d'", value)
}
// Test adding another new key-value pair.
addedNew := newMap.AddOK("banana", 3)
if !addedNew {
t.Error("Expected 'banana' to be added, but it was not.")
}
// Check if the key-value pair for 'banana' is added correctly.
value, exists = newMap["banana"]
if !exists || value != 3 {
t.Fatalf("Expected key 'banana' with value '3', but got value '%d'", value)
}
}
// TestAddValueFunc tests Map.AddValueFunc.
func TestAddValueFunc(t *testing.T) {
// Create a new Map instance.
newMap := &gomap.Map[string, int]{} // Change the data types accordingly.
// Function to calculate key from value using string conversion.
keyCalculationFunc := func(value int) string {
return strconv.Itoa(value)
}
// Test case: Add a key-value pair using AddValueFunc.
valueToAdd := 5
newMap.AddValueFunc(valueToAdd, keyCalculationFunc)
// Verify that the key-value pair is added correctly.
expectedKey := "5"
expectedValue := 5
retrievedValue, ok := newMap.Get(expectedKey)
if !ok || retrievedValue != expectedValue {
t.Errorf("Expected key '%s' with value %d, but got key '%s' with value %d", expectedKey, expectedValue, expectedKey, retrievedValue)
}
}
// TestAddValuesFunc tests the AddValuesFunc method of the Map.
func TestAddValuesFunc(t *testing.T) {
// Test case 1: Adding multiple key-value pairs to the map using a slice of values and a key calculation function.
newMap := &gomap.Map[string, int]{} // Create an empty map.
values := []int{5, 10, 15}
keyCalculationFunc := func(i int, value int) string {
return strconv.Itoa(value) + strconv.Itoa(i)
}
newMap.AddValuesFunc(values, keyCalculationFunc) // Adds keys "50", "101", and "152" with values 5, 10, and 15 to the map.
// Verify that the key-value pairs have been added correctly.
expectedKeyValues := map[string]int{
"50": 5,
"101": 10,
"152": 15,
}
for key, expectedValue := range expectedKeyValues {
actualValue, ok := newMap.Get(key)
if !ok {
t.Errorf("Expected key '%s' to be in the map, but it was not found.", key)
} else if actualValue != expectedValue {
t.Errorf("Expected value %d for key '%s', but got %d", expectedValue, key, actualValue)
}
}
}
// TestContains tests Map.Contains.
func TestContains(t *testing.T) {
// Test case 1: Check for a value in an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
key, found := newMap.Contains(5) // Check if value 5 is in the gomap.
// Since the gomap is empty, the result should be ("", false).
if key != "" || found {
t.Errorf("Expected ('', false), but got (%s, %t)", key, found)
}
// Test case 2: Check for a value in a non-empty gomap.
newMap = &gomap.Map[string, int]{} // Create a new gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 10)
newMap.Add("orange", 5)
// Check if value 5 is in the gomap.
key, found = newMap.Contains(5)
// Since value 5 exists in the gomap, the result should be ("apple", true).
if !found {
t.Errorf("Expected ('%s', true), but got (%s, %t)", key, key, found)
}
// Test case 3: Check for a value that doesn't exist in the gomap.
key, found = newMap.Contains(15) // Check if value 15 is in the gomap.
// Since value 15 doesn't exist in the gomap, the result should be ("", false).
if key != "" || found {
t.Errorf("Expected ('', false), but got (%s, %t)", key, found)
}
}
// TestDelete tests Map.Delete.
func TestDelete(t *testing.T) {
newMap := make(gomap.Map[string, int])
newMap["apple"] = 5
newMap["banana"] = 3
// Test case 1: Delete an existing key.
newMap.Delete("apple")
if _, ok := newMap["apple"]; ok {
t.Fatalf("Expected key 'apple' to be deleted, but it still exists in the gomap")
}
// Test case 2: Delete a non-existing key.
newMap.Delete("nonexistent")
if _, ok := newMap["nonexistent"]; ok {
t.Fatalf("Expected key 'nonexistent' to not exist, but it was found in the gomap")
}
// Test case 3: Delete a key after adding it again.
newMap["apple"] = 10
newMap.Delete("apple")
if _, ok := newMap["apple"]; ok {
t.Fatalf("Expected key 'apple' to be deleted, but it still exists in the gomap")
}
}
// TestDeleteLength tests Map.DeleteLength.
func TestDeleteLength(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap and get the initial length.
newMap["apple"] = 5
newMap["banana"] = 3
initialLength := len(newMap)
// Delete an existing key from the gomap and get the updated length.
lengthAfterDelete := newMap.DeleteLength("apple")
// Expected length after deleting "apple": initial length - 1.
expectedLength := initialLength - 1
// Verify that the obtained length matches the expected length.
if lengthAfterDelete != expectedLength {
t.Fatalf("Expected length: %d, but got: %d", expectedLength, lengthAfterDelete)
}
// Attempt to delete a non-existing key from the gomap.
lengthAfterNonExistingDelete := newMap.DeleteLength("grape")
// Length should remain the same after attempting to delete a non-existing key.
// Expected length: initial length.
expectedLengthNonExisting := len(newMap)
// Verify that the obtained length matches the expected length.
if lengthAfterNonExistingDelete != expectedLengthNonExisting {
t.Fatalf("Expected length: %d, but got: %d", expectedLengthNonExisting, lengthAfterNonExistingDelete)
}
}
// TestDeleteMany tests Map.DeleteMany.
func TestDeleteMany(t *testing.T) {
// Test case 1: Delete keys from an empty gomap.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
newMap.DeleteMany("apple", "banana") // Attempt to delete keys "apple" and "banana".
// The gomap should remain empty.
if !newMap.IsEmpty() {
t.Errorf("Expected gomap to be empty, but got non-empty gomap: %v", newMap)
}
// Test case 2: Delete keys from a non-empty gomap.
newMap = &gomap.Map[string, int]{} // Create a new gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 10)
newMap.Add("orange", 3)
// Delete keys "apple" and "banana".
newMap.DeleteMany("apple", "banana")
// Verify that "apple" and "banana" are deleted, and "orange" remains in the gomap.
expected := &slice.Slice[string]{"orange"}
result := newMap.Keys()
if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected keys %v after deleting 'apple' and 'banana', but got keys %v", expected, result)
}
}
// TestDeleteManyOK tests Map.DeleteManyOK.
func TestDeleteManyOK(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 3)
// Specify keys to delete.
keysToDelete := []string{"apple", "grape"}
// Attempt to delete keys and check if deletion is successful.
results := newMap.DeleteManyOK(keysToDelete...)
expectedResults := []bool{true, true} // Expected results for "apple" (exists) and "grape" (does not exist).
// Check if results match the expected results.
for i, result := range *results {
if result != expectedResults[i] {
t.Fatalf("Expected deletion of key %s to be %v but got %v", keysToDelete[i], expectedResults[i], result)
}
}
}
// TestDeleteManyValues tests Map.DeleteManyValues.
func TestDeleteManyValues(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
newMap.Add("apple", 5)
newMap.Add("banana", 3)
newMap.Add("cherry", 8)
// Delete key-value pairs where the value is 3 or 8.
newMap.DeleteManyValues(3, 8)
// Verify that the gomap only contains the expected key-value pair.
expected := gomap.Map[string, int]{"apple": 5}
if !reflect.DeepEqual(newMap, expected) {
t.Fatalf("Expected gomap: %v, but got: %v", expected, newMap)
}
}
// TestDeleteOK tests Map.DeleteOK.
func TestDeleteOK(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 3)
// Delete keys and check if deletion is successful.
deleted := newMap.DeleteOK("apple")
if !deleted {
t.Fatalf("Expected deletion of 'apple' to be successful")
}
// Attempt to delete a key that does not exist.
notDeleted := newMap.DeleteOK("grape")
if !notDeleted {
t.Fatalf("Expected deletion of 'grape' to be successful because the key does not exist")
}
// Attempt to delete a key that has already been deleted.
alreadyDeleted := newMap.DeleteOK("apple")
if !alreadyDeleted {
t.Fatalf("Expected deletion of 'apple' to be successful even though it was already deleted")
}
}
// TestEach tests Map.Each.
func TestEach(t *testing.T) {
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap["apple"] = 5
newMap["banana"] = 3
newMap["cherry"] = 8
// Define a map to store the expected output.
expectedOutput := map[string]int{
"apple": 5,
"banana": 3,
"cherry": 8,
}
// Define a function to compare the actual output with the expected output.
printKeyValue := func(key string, value int) {
if expected, ok := expectedOutput[key]; ok {
if value != expected {
t.Fatalf("Expected %s: %d, but got %d", key, expected, value)
}
delete(expectedOutput, key)
} else {
t.Fatalf("Unexpected key: %s", key)
}
}
// Call the Each function with the printKeyValue function.
newMap.Each(printKeyValue)
// Check if all expected keys have been processed.
if len(expectedOutput) > 0 {
t.Fatalf("Not all keys were processed: %v", expectedOutput)
}
}
// TestEachBreak tests Map.EachBreak.
func TestEachBreak(t *testing.T) {
var stopPrinting string
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 3)
newMap.Add("cherry", 8)
// Define a function to stop iteration when key is "banana".
newMap.EachBreak(func(key string, value int) bool {
t.Logf("%s %d", key, value)
stopPrinting = key
return key != "banana"
})
// Ensure that iteration stopped at "banana".
if stopPrinting != "banana" {
t.Fatalf("Iteration did not stop at 'banana'.")
}
}
// TestEachKey tests Map.EachKey.
func TestEachKey(t *testing.T) {
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap["apple"] = 5
newMap["banana"] = 3
newMap["cherry"] = 8
// Define a function to print each key.
var printedKeys []string
printKey := func(key string) {
printedKeys = append(printedKeys, key)
}
// Iterate over the keys and print each key.
newMap.EachKey(printKey)
// Sort the printed values for consistent comparison.
sort.Strings(printedKeys)
// Expected output: "apple", "banana", "cherry".
expectedKeys := []string{"apple", "banana", "cherry"}
for i, key := range printedKeys {
if key != expectedKeys[i] {
t.Fatalf("Expected key %s at index %d, but got %s", expectedKeys[i], i, key)
}
}
}
// TestEachKeyBreak tests Map.EachKeyBreak.
func TestEachKeyBreak(t *testing.T) {
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap["apple"] = 5
newMap["banana"] = 3
newMap["cherry"] = 8
var keyToBreak string
newMap.EachBreak(func(key string, value int) bool {
keyToBreak = key
return key != "banana"
})
if keyToBreak != "banana" {
t.Fatalf("Expect keyToBreak to equal 'banana', but got %s", keyToBreak)
}
}
// TestEachValue tests Map.EachValue.
func TestEachValue(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap["apple"] = 5
newMap["banana"] = 3
newMap["cherry"] = 8
// Define a function to print each value.
var printedValues []int
printValue := func(value int) {
printedValues = append(printedValues, value)
}
// Iterate over the gomap values and print them.
newMap.EachValue(printValue)
// Sort the printed values for consistent comparison.
sort.Ints(printedValues)
// Expected output: 3, 5, 8.
expectedValues := []int{3, 5, 8}
if len(printedValues) != len(expectedValues) {
t.Fatalf("Expected %d values, but got %d", len(expectedValues), len(printedValues))
return
}
for i, value := range printedValues {
if value != expectedValues[i] {
t.Fatalf("Expected value %d at index %d, but got %d", expectedValues[i], i, value)
}
}
}
// TestEachValueBreak tests Map.EachValueBreak.
func TestEachValueBreak(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
// Add key-value pairs to the gomap.
newMap.Add("apple", 5)
newMap.Add("banana", 3)
newMap.Add("cherry", 8)
keys := make([]string, 0, len(newMap))
for key := range newMap {
keys = append(keys, key)
}
// Sort the keys for consistent iteration order.
sort.Strings(keys)
// Define a function to process each value. It returns false to break the iteration if the value is 3.
var processedValues []int
processValue := func(value int) bool {
processedValues = append(processedValues, value)
return value != 3
}
// Iterate over the gomap values and process them until the value is 3.
for _, key := range keys {
value, _ := newMap.Get(key)
if !processValue(value) {
break
}
}
// Expected output: 5, 3.
expectedValues := []int{5, 3}
for i, value := range processedValues {
if value != expectedValues[i] {
t.Fatalf("Expected value %d at index %d, but got %d", expectedValues[i], i, value)
}
}
}
// TestEmptyInto tests Map.EmptyInto.
func TestEmptyInto(t *testing.T) {
// Test case 1: Transfer from an empty gomap to another empty gomap.
newMap1 := &gomap.Map[string, int]{} // Create an empty source gomap.
newMap2 := &gomap.Map[string, int]{} // Create an empty destination gomap.
newMap1.EmptyInto(newMap2) // Transfer from newMap1 to newMap2.
// Verify that newMap1 is empty.
if newMap1.Length() != 0 {
t.Errorf("Expected source gomap to be empty after transfer, but it has %d items", newMap1.Length())
}
// Verify that newMap2 is still empty.
if newMap2.Length() != 0 {
t.Errorf("Expected destination gomap to be empty after transfer, but it has %d items", newMap2.Length())
}
// Test case 2: Transfer from a non-empty gomap to an empty gomap.
newMap1 = &gomap.Map[string, int]{} // Create an empty source gomap.
newMap1.Add("apple", 5)
newMap2 = &gomap.Map[string, int]{} // Create an empty destination gomap.
newMap1.EmptyInto(newMap2) // Transfer from newMap1 to newMap2.
// Verify that newMap1 is empty.
if newMap1.Length() != 0 {
t.Errorf("Expected source gomap to be empty after transfer, but it has %d items", newMap1.Length())
}
// Verify that newMap2 contains the transferred key-value pair.
expectedValue := 5
transferredValue, ok := newMap2.Get("apple")
if !ok || transferredValue != expectedValue {
t.Errorf("Expected destination gomap to contain 'apple': %d after transfer, but it contains '%d'", expectedValue, transferredValue)
}
}
// TestEqual tests Map.Equal.
func TestEqual(t *testing.T) {
// Test case 1: Compare equal hasnewMapables.
newMap1 := &gomap.Map[string, int]{} // Create a new gomap.
newMap1.Add("apple", 5)
newMap1.Add("orange", 10)
newMap2 := &gomap.Map[string, int]{} // Create another gomap with similar values.
newMap2.Add("apple", 5)
newMap2.Add("orange", 10)
// Check if the two hasnewMapables are equal.
equal := newMap1.Equal(newMap2)
// Since newMap1 and newMap2 have the same key-value pairs, they are considered equal.
if !equal {
t.Errorf("Expected true, but got false")
}
// Test case 2: Compare unequal hasnewMapables.
newMap3 := &gomap.Map[string, int]{} // Create a new gomap.
newMap3.Add("apple", 5)
newMap3.Add("orange", 10)
newMap4 := &gomap.Map[string, int]{} // Create another gomap with different values for "orange".
newMap4.Add("apple", 5)
newMap4.Add("orange", 12)
// Check if the two hasnewMapables are equal.
equal = newMap3.Equal(newMap4)
// Since newMap3 and newMap4 have different values for "orange", they are not considered equal.
if equal {
t.Errorf("Expected false, but got true")
}
}
// TestEqualFunc tests Map.EqualFunc.
func TestEqualFunc(t *testing.T) {
// Custom comparison function to check if two integers are equal when their difference is less than or equal to 1
compareFunc := func(a, b int) bool {
return math.Abs(float64(a-b)) <= 1
}
// Test case 1: Compare equal hasnewMapables based on the custom comparison function.
newMap1 := &gomap.Map[string, int]{} // Create a new gomap.
newMap1.Add("apple", 5)
newMap1.Add("orange", 10)
newMap2 := &gomap.Map[string, int]{} // Create another gomap with similar values.
newMap2.Add("apple", 5)
newMap2.Add("orange", 11) // The difference between 10 and 11 is within the allowed range according to compareFunc.
// Check if the two hasnewMapables are equal based on the custom comparison function.
equal := newMap1.EqualFunc(newMap2, compareFunc)
// Since the values for "orange" (10 and 11) have a difference of 1, within the allowed range,
// the hasnewMapables are considered equal according to the custom comparison function.
if !equal {
t.Errorf("Expected true, but got false")
}
// Test case 2: Compare unequal hasnewMapables based on the custom comparison function.
newMap3 := &gomap.Map[string, int]{} // Create a new gomap.
newMap3.Add("apple", 5)
newMap3.Add("orange", 10)
newMap4 := &gomap.Map[string, int]{} // Create another gomap with different values for "orange".
newMap4.Add("apple", 5)
newMap4.Add("orange", 12) // The difference between 10 and 12 is greater than the allowed range according to compareFunc.
// Check if the two hasnewMapables are equal based on the custom comparison function.
equal = newMap3.EqualFunc(newMap4, compareFunc)
// Since the difference between 10 and 12 is greater than the allowed range according to compareFunc,
// the hasnewMapables are not considered equal based on the custom comparison function.
if equal {
t.Errorf("Expected false, but got true")
}
}
// TestEqualLength tests Map.EqualLength.
func TestEqualLength(t *testing.T) {
// Test case 1: Compare hasnewMapables with equal length.
newMap1 := &gomap.Map[string, int]{} // Create a new gomap.
newMap1.Add("apple", 5)
newMap1.Add("orange", 10)
newMap2 := &gomap.Map[string, int]{} // Create another gomap with the same number of key-value pairs.
newMap2.Add("apple", 5)
newMap2.Add("orange", 7)
// Check if the two hasnewMapables have equal length.
equalLength := newMap1.EqualLength(newMap2)
// Since newMap1 and newMap2 have the same number of key-value pairs, they are considered equal in length.
if !equalLength {
t.Errorf("Expected true, but got false")
}
// Test case 2: Compare hasnewMapables with unequal length.
newMap3 := &gomap.Map[string, int]{} // Create a new gomap.
newMap3.Add("apple", 5)
newMap4 := &gomap.Map[string, int]{} // Create another gomap with a different number of key-value pairs.
newMap4.Add("apple", 5)
newMap4.Add("orange", 7)
// Check if the two hasnewMapables have equal length.
equalLength = newMap3.EqualLength(newMap4)
// Since newMap3 and newMap4 have different numbers of key-value pairs, they are not considered equal in length.
if equalLength {
t.Errorf("Expected false, but got true")
}
}
// TestFetch tests Map.Fetch.
func TestFetch(t *testing.T) {
// Test case 1: Fetch value for an existing key.
newMap := &gomap.Map[string, int]{} // Create a new gomap.
newMap.Add("apple", 5)
newMap.Add("orange", 10)
// Fetch the value associated with the key "apple".
fetchedValue := newMap.Fetch("apple")
// Since "apple" is in the gomap, the fetched value should be 5.
expectedValue := 5
if fetchedValue != expectedValue {
t.Errorf("Expected %d, but got %d", expectedValue, fetchedValue)
}
// Test case 2: Fetch value for a non-existing key.
// Fetch the value associated with the key "banana", which is not in the gomap.
fetchedValue = newMap.Fetch("banana")
// Since "banana" is not in the gomap, the fetched value should be the zero value for int, which is 0.
expectedValue = 0
if fetchedValue != expectedValue {
t.Errorf("Expected %d, but got %d", expectedValue, fetchedValue)
}
}
// TestFilter tests Map.Filter.
func TestFilter(t *testing.T) {
// Test case 1: Filter with an empty gomap and a function that never selects any pairs.
newMap := &gomap.Map[string, int]{} // Create an empty gomap.
filterFunc := func(key string, value int) bool {
return false // Never select any values
}
filtered := newMap.Filter(filterFunc)
expected := &gomap.Map[string, int]{} // Expected empty gomap.
if !reflect.DeepEqual(filtered, expected) {
t.Errorf("Expected %v, but got %v", expected, filtered)
}
// Test case 2: Filter with a non-empty gomap and a function that never selects any pairs.
newMap = &gomap.Map[string, int]{} // Create an empty gomap.
newMap.Add("apple", 5)
newMap.Add("orange", 10)
filterFunc = func(key string, value int) bool {
return false // Never select any values
}
filtered = newMap.Filter(filterFunc)
expected = &gomap.Map[string, int]{} // Expected empty gomap.
if !reflect.DeepEqual(filtered, expected) {
t.Errorf("Expected %v, but got %v", expected, filtered)
}
// Test case 3: Filter with a non-empty gomap and a function that selects certain pairs.
newMap = &gomap.Map[string, int]{} // Create an empty gomap.
newMap.Add("apple", 5)
newMap.Add("orange", 10)
newMap.Add("banana", 3)
filterFunc = func(key string, value int) bool {
return value > 4 // Select pairs where value is greater than 4
}
filtered = newMap.Filter(filterFunc)
expected = &gomap.Map[string, int]{"apple": 5, "orange": 10} // Expected filtered gomap.
if !reflect.DeepEqual(filtered, expected) {
t.Errorf("Expected %v, but got %v", expected, filtered)
}
}
// TestGet tests Map.Get.
func TestGet(t *testing.T) {
newMap := make(gomap.Map[string, int])
newMap["apple"] = 5
newMap["banana"] = 3
// Test case 1: Get an existing key.
value, exists := newMap.Get("apple")
if !exists {
t.Fatalf("Expected key 'apple' to exist, but it was not found in the gomap")
}
if value != 5 {
t.Fatalf("Expected value for key 'apple' to be 5, but got %d", value)
}
// Test case 2: Get a non-existing key.
value, exists = newMap.Get("orange")
if exists {
t.Fatalf("Expected key 'orange' to not exist, but it was found in the gomap with value %d", value)
}
if value != 0 {
t.Fatalf("Expected default value for non-existing key 'orange' to be 0, but got %d", value)
}
}
// TestGetMany tests Map.GetMany.
func TestGetMany(t *testing.T) {
// Create a new gomap.
newMap := make(gomap.Map[string, int])
newMap["apple"] = 5
newMap["banana"] = 3
newMap["cherry"] = 8
// Get values for specific keys.
values := newMap.GetMany("apple", "banana", "orange")
// Sort the keys for consistent iteration order.
sort.Ints(*values)
// The expected values slice: {5, 3}.
expectedValues := &slice.Slice[int]{5, 3}
// Sort the keys for consistent iteration order.
sort.Ints(*expectedValues)
// Verify that the obtained values match the expected values.
if values == expectedValues {
t.Fatalf("Expected values: %v, but got: %v", expectedValues, values)
}
}
// TestHas tests Map.Has.
func TestHas(t *testing.T) {
newMap := make(gomap.Map[string, int])
newMap["apple"] = 5
newMap["banana"] = 3
// Test case 1: Key exists in the gomap.
if !newMap.Has("apple") {
t.Fatalf("Expected key 'apple' to exist, but it was not found in the gomap")
}
// Test case 2: Key does not exist in the gomap.
if newMap.Has("orange") {
t.Fatalf("Expected key 'orange' to not exist, but it was found in the gomap")
}
}