| 1 | module maps |
| 2 | |
| 3 | interface TestValue { |
| 4 | value() string |
| 5 | } |
| 6 | |
| 7 | struct TestValueImpl { |
| 8 | raw string |
| 9 | } |
| 10 | |
| 11 | fn (v TestValueImpl) value() string { |
| 12 | return v.raw |
| 13 | } |
| 14 | |
| 15 | fn test_filter() { |
| 16 | m1 := { |
| 17 | 0: 'ab' |
| 18 | 1: 'bc' |
| 19 | 2: 'cd' |
| 20 | 3: 'de' |
| 21 | 4: 'ef' |
| 22 | 5: 'fg' |
| 23 | } |
| 24 | assert filter(m1, fn (k int, v string) bool { |
| 25 | return k % 2 == 0 |
| 26 | }) == { |
| 27 | 0: 'ab' |
| 28 | 2: 'cd' |
| 29 | 4: 'ef' |
| 30 | } |
| 31 | assert filter(m1, fn (k int, v string) bool { |
| 32 | return v.contains('b') || v.contains('c') |
| 33 | }) == { |
| 34 | 0: 'ab' |
| 35 | 1: 'bc' |
| 36 | 2: 'cd' |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn test_to_array() { |
| 41 | m1 := { |
| 42 | `a`: 'bc' |
| 43 | `d`: 'ef' |
| 44 | `g`: 'hi' |
| 45 | } |
| 46 | assert to_array(m1, fn (k rune, v string) string { |
| 47 | return '${k}${v}' |
| 48 | }) == ['abc', 'def', 'ghi'] |
| 49 | } |
| 50 | |
| 51 | fn test_helpers_with_interface_map_values() { |
| 52 | m1 := { |
| 53 | 'a': TestValue(TestValueImpl{'bc'}) |
| 54 | 'd': TestValue(TestValueImpl{'ef'}) |
| 55 | 'g': TestValue(TestValueImpl{'hi'}) |
| 56 | } |
| 57 | filtered := filter(m1, fn (k string, v TestValue) bool { |
| 58 | return k == 'a' || v.value() == 'ef' |
| 59 | }) |
| 60 | assert filtered.len == 2 |
| 61 | assert filtered['a'].value() == 'bc' |
| 62 | assert filtered['d'].value() == 'ef' |
| 63 | assert to_array(m1, fn (k string, v TestValue) string { |
| 64 | return '${k}${v.value()}' |
| 65 | }) == ['abc', 'def', 'ghi'] |
| 66 | assert flat_map[string, TestValue, string](m1, fn (k string, v TestValue) []string { |
| 67 | return [k, v.value()] |
| 68 | }) == ['a', 'bc', 'd', 'ef', 'g', 'hi'] |
| 69 | assert to_map[string, TestValue, string, string](m1, fn (k string, v TestValue) (string, string) { |
| 70 | return k, v.value() |
| 71 | }) == { |
| 72 | 'a': 'bc' |
| 73 | 'd': 'ef' |
| 74 | 'g': 'hi' |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn test_flat_map() { |
| 79 | m1 := { |
| 80 | 1: [2, 3] |
| 81 | 4: [5, 6] |
| 82 | 7: [8, 9] |
| 83 | } |
| 84 | assert flat_map[int, []int, int](m1, fn (k int, v []int) []int { |
| 85 | mut a := [k] |
| 86 | a << v |
| 87 | return a |
| 88 | }) == [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 89 | } |
| 90 | |
| 91 | fn test_to_map() { |
| 92 | m1 := { |
| 93 | 0: '0' |
| 94 | 1: '1' |
| 95 | 2: '2' |
| 96 | 3: '3' |
| 97 | 4: '4' |
| 98 | 5: '5' |
| 99 | } |
| 100 | assert to_map[int, string, string, int](m1, fn (k int, v string) (string, int) { |
| 101 | return v, k |
| 102 | }) == { |
| 103 | '0': 0 |
| 104 | '1': 1 |
| 105 | '2': 2 |
| 106 | '3': 3 |
| 107 | '4': 4 |
| 108 | '5': 5 |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fn test_invert() { |
| 113 | m1 := { |
| 114 | 0: '0' |
| 115 | 1: '1' |
| 116 | 2: '2' |
| 117 | 3: '3' |
| 118 | 4: '4' |
| 119 | 5: '5' |
| 120 | } |
| 121 | assert invert(m1) == { |
| 122 | '0': 0 |
| 123 | '1': 1 |
| 124 | '2': 2 |
| 125 | '3': 3 |
| 126 | '4': 4 |
| 127 | '5': 5 |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn test_from_array() { |
| 132 | a1 := [ |
| 133 | 'a', |
| 134 | 'b', |
| 135 | 'c', |
| 136 | 'd', |
| 137 | 'e', |
| 138 | 'f', |
| 139 | ] |
| 140 | assert from_array(a1) == { |
| 141 | 0: 'a' |
| 142 | 1: 'b' |
| 143 | 2: 'c' |
| 144 | 3: 'd' |
| 145 | 4: 'e' |
| 146 | 5: 'f' |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn test_merge_in_place() { |
| 151 | mut m1 := { |
| 152 | 'abc': 'def' |
| 153 | 'aa': 'bb' |
| 154 | } |
| 155 | m2 := { |
| 156 | 'xyz': 'zyx' |
| 157 | 'aa': 'dd' |
| 158 | } |
| 159 | merge_in_place(mut m1, m2) |
| 160 | assert m1 == { |
| 161 | 'abc': 'def' |
| 162 | 'aa': 'dd' |
| 163 | 'xyz': 'zyx' |
| 164 | } |
| 165 | assert m2 == { |
| 166 | 'xyz': 'zyx' |
| 167 | 'aa': 'dd' |
| 168 | } |
| 169 | |
| 170 | mut im1 := { |
| 171 | 11: 22 |
| 172 | 33: 44 |
| 173 | } |
| 174 | im2 := { |
| 175 | 55: 66 |
| 176 | 33: 999 |
| 177 | } |
| 178 | merge_in_place(mut im1, im2) |
| 179 | assert im1 == { |
| 180 | 11: 22 |
| 181 | 33: 999 |
| 182 | 55: 66 |
| 183 | } |
| 184 | assert im2 == { |
| 185 | 55: 66 |
| 186 | 33: 999 |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | fn test_merge() { |
| 191 | m1 := { |
| 192 | 'abc': 'def' |
| 193 | 'aa': 'bb' |
| 194 | } |
| 195 | m2 := { |
| 196 | 'xyz': 'zyx' |
| 197 | 'aa': 'dd' |
| 198 | } |
| 199 | res := merge(m1, m2) |
| 200 | assert res == { |
| 201 | 'abc': 'def' |
| 202 | 'aa': 'dd' |
| 203 | 'xyz': 'zyx' |
| 204 | } |
| 205 | assert m1 == { |
| 206 | 'abc': 'def' |
| 207 | 'aa': 'bb' |
| 208 | } |
| 209 | assert m2 == { |
| 210 | 'xyz': 'zyx' |
| 211 | 'aa': 'dd' |
| 212 | } |
| 213 | |
| 214 | mut im1 := { |
| 215 | 11: 22 |
| 216 | 33: 44 |
| 217 | } |
| 218 | im2 := { |
| 219 | 55: 66 |
| 220 | 33: 999 |
| 221 | } |
| 222 | ires := merge(im1, im2) |
| 223 | assert im1 == { |
| 224 | 11: 22 |
| 225 | 33: 44 |
| 226 | } |
| 227 | assert im2 == { |
| 228 | 55: 66 |
| 229 | 33: 999 |
| 230 | } |
| 231 | assert ires == { |
| 232 | 11: 22 |
| 233 | 33: 999 |
| 234 | 55: 66 |
| 235 | } |
| 236 | } |
| 237 | |