Debian Codegraph @

maps/maps_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package maps
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/jesseduffield/generics/internal/testutils"
8 )
9
10 func TestKeys(t *testing.T) {
11 tests := []struct {
12 hashMap map[string]int
13 expected []string
14 }{
15 {map[string]int{}, []string{}},
16 {map[string]int{"a": 1}, []string{"a"}},
17 }
18 for _, test := range tests {
19 testutils.ExpectSlice(t, test.expected, Keys(test.hashMap))
20 }
21 }
22
23 func TestValues(t *testing.T) {
24 tests := []struct {
25 hashMap map[string]int
26 expected []int
27 }{
28 {map[string]int{}, []int{}},
29 {map[string]int{"a": 1}, []int{1}},
30 }
31 for _, test := range tests {
32 testutils.ExpectSlice(t, test.expected, Values(test.hashMap))
33 }
34 }
35
36 func TestTransformKeys(t *testing.T) {
37 tests := []struct {
38 hashMap map[int]string
39 transform func(int) int64
40 expected map[int64]string
41 }{
42 {
43 hashMap: map[int]string{},
44 transform: func(i int) int64 { return (int64)(i) },
45 expected: map[int64]string{},
46 },
47 {
48 hashMap: map[int]string{1: "a"},
49 transform: func(i int) int64 { return 2 * (int64)(i) },
50 expected: map[int64]string{2: "a"},
51 },
52 }
53 for _, test := range tests {
54 testutils.ExpectMap(t, test.expected, TransformKeys(test.hashMap, test.transform))
55 }
56 }
57
58 func TestTransformValues(t *testing.T) {
59 tests := []struct {
60 hashMap map[string]int
61 transform func(int) int64
62 expected map[string]int64
63 }{
64 {
65 hashMap: map[string]int{},
66 transform: func(i int) int64 { return (int64)(i) },
67 expected: map[string]int64{},
68 },
69 {
70 hashMap: map[string]int{"a": 1},
71 transform: func(i int) int64 { return 2 * (int64)(i) },
72 expected: map[string]int64{"a": 2},
73 },
74 }
75 for _, test := range tests {
76 testutils.ExpectMap(t, test.expected, TransformValues(test.hashMap, test.transform))
77 }
78 }
79
80 func TestMapToSlice(t *testing.T) {
81 tests := []struct {
82 hashMap map[int64]int
83 f func(int64, int) string
84 expected []string
85 }{
86 {
87 hashMap: map[int64]int{},
88 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
89 expected: []string{},
90 },
91 {
92 hashMap: map[int64]int{2: 5},
93 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
94 expected: []string{"2:5"},
95 },
96 {
97 hashMap: map[int64]int{2: 5, 3: 4},
98 f: func(k int64, v int) string { return fmt.Sprintf("%d:%d", k, v) },
99 expected: []string{"2:5", "3:4"},
100 },
101 }
102 for _, test := range tests {
103 testutils.ExpectSlice(t, test.expected, MapToSlice(test.hashMap, test.f))
104 }
105 }
106
107 func TestFilter(t *testing.T) {
108 tests := []struct {
109 hashMap map[int64]int
110 f func(int64, int) bool
111 expected map[int64]int
112 }{
113 {
114 hashMap: map[int64]int{},
115 f: func(k int64, v int) bool { return int(k)+v > 0 },
116 expected: map[int64]int{},
117 },
118 {
119 hashMap: map[int64]int{2: 5},
120 f: func(k int64, v int) bool { return int(k)+v > 0 },
121 expected: map[int64]int{2: 5},
122 },
123 {
124 hashMap: map[int64]int{2: 5, 3: -4},
125 f: func(k int64, v int) bool { return int(k)+v > 0 },
126 expected: map[int64]int{2: 5},
127 },
128 }
129 for _, test := range tests {
130 testutils.ExpectMap(t, test.expected, Filter(test.hashMap, test.f))
131 }
132 }