1+ type T0 = int | string
2+ type T1 = T0 | rune
3+
14struct Point {
25 x f64
36 y f64
47}
58
9+ enum Colors {
10+ red = 1
11+ green
12+ blue
13+ }
14+
615fn generic_map [T](items map [string ]T) []T {
716 return items.values ()
817}
@@ -226,17 +235,111 @@ fn test_map_len() {
226235 assert items_2 .len == 2
227236}
228237
229- fn test_rune_keys () {
230- mut m := {
238+ fn test_map_with_different_key_types () {
239+ // map[int]string
240+ mut items_1 := {
241+ 1 : 'one'
242+ 2 : 'two'
243+ 3 : 'three'
244+ }
245+ assert typeof (items_1 ).name == 'map[int]string'
246+ assert items_1 [2 ] == 'two'
247+ items_1 [4 ] = 'four'
248+ assert items_1 [4 ].len == 4
249+ keys_1 := items_1 .keys ()
250+ assert keys_1 .contains (1 )
251+ assert keys_1 .contains (2 )
252+ assert keys_1 .contains (3 )
253+ assert keys_1 .contains (4 )
254+ assert '${items_1 }' == "{1: 'one', 2: 'two', 3: 'three', 4: 'four'}"
255+
256+ // map[string]int
257+ mut items_2 := {
258+ 'one' : 1
259+ 'two' : 2
260+ 'three' : 3
261+ }
262+ assert typeof (items_2 ).name == 'map[string]int'
263+ assert items_2 ['two' ] == 2
264+ items_2 ['four' ] = 4
265+ assert items_2 ['four' ] == 4
266+ keys_2 := items_2 .keys ()
267+ assert keys_2 .contains ('one' )
268+ assert keys_2 .contains ('two' )
269+ assert keys_2 .contains ('three' )
270+ assert keys_2 .contains ('four' )
271+ assert '${items_2 }' == "{'one': 1, 'two': 2, 'three': 3, 'four': 4}"
272+
273+ // map[f64]string
274+ mut items_3 := {
275+ 1.1 : 'one dot one'
276+ 2.2 : 'two dot two'
277+ 3.3 : 'three dot three'
278+ }
279+ assert typeof (items_3 ).name == 'map[f64]string'
280+ assert items_3 [2.2 ] == 'two dot two'
281+ items_3 [4.4 ] = 'four dot four'
282+ assert items_3 [4.4 ].len == 13
283+ keys_3 := items_3 .keys ()
284+ assert keys_3 .contains (1.1 )
285+ assert keys_3 .contains (2.2 )
286+ assert keys_3 .contains (3.3 )
287+ assert keys_3 .contains (4.4 )
288+ assert '${items_3 }' == "{1.1: 'one dot one', 2.2: 'two dot two', 3.3: 'three dot three', 4.4: 'four dot four'}"
289+
290+ // map[u8]string
291+ mut items_4 := {
292+ u8 (1 ): 'one'
293+ 2 : 'two'
294+ }
295+ assert typeof (items_4 ).name == 'map[u8]string'
296+ assert items_4 [2 ] == 'two'
297+ items_4 [3 ] = 'three'
298+ assert items_4 [3 ].len == 5
299+ keys_4 := items_4 .keys ()
300+ assert keys_4 .contains (1 )
301+ assert keys_4 .contains (2 )
302+ assert keys_4 .contains (3 )
303+ assert '${items_4 }' == "{49: 'one', 50: 'two', 51: 'three'}"
304+
305+ // map[rune]int
306+ mut items_5 := {
231307 `!` : 2
232308 `%` : 3
233309 }
234- assert typeof (m).name == 'map[rune]int'
235- assert m[`!` ] == 2
236- m[`@` ] = 7
237- assert m.len == 3
238- println (m)
239- assert '${m }' == '{`!`: 2, `%`: 3, `@`: 7}'
310+ assert typeof (items_5 ).name == 'map[rune]int'
311+ assert items_5 [`!` ] == 2
312+ items_5 [`@` ] = 7
313+ assert items_5 .len == 3
314+ keys_5 := items_5 .keys ()
315+ assert keys_5 .contains (`!` )
316+ assert keys_5 .contains (`%` )
317+ assert keys_5 .contains (`@` )
318+ assert '${items_5 }' == '{`!`: 2, `%`: 3, `@`: 7}'
319+
320+ // map[sum-type]string
321+ mut items_6 := {
322+ T1 (T0 (1 )): 'one'
323+ T0 ('2' ): 'two'
324+ }
325+ items_6 [`!` ] = 'exclamation'
326+ assert items_6 [`!` ].len == 11
327+ keys_6 := items_6 .keys ()
328+ assert keys_6 .contains (T0 (1 ))
329+ assert keys_6 .contains (T0 ('2' ))
330+ assert keys_6 .contains (`!` )
331+
332+ // map[enum-type]string
333+ mut items_7 := {
334+ Colors.red: 'red'
335+ .green: 'green'
336+ }
337+ items_7 [.blue] = 'blue'
338+ assert items_7 [.blue].len == 4
339+ keys_7 := items_7 .keys ()
340+ keys_7 .contains (.red)
341+ keys_7 .contains (.green)
342+ keys_7 .contains (.blue)
240343}
241344
242345fn main () {
@@ -246,5 +349,5 @@ fn main() {
246349 test_keys_method_with_generic_constraints ()
247350 test_direct_map_access ()
248351 test_map_len ()
249- test_rune_keys ()
352+ test_map_with_different_key_types ()
250353}
0 commit comments