v2 / vlib / v / tests / nested_map_test.v
46 lines · 41 sloc · 881 bytes · 19f080ffb8f8f01976692f6b79d9f857c685e109
Raw
1fn test_map_of_map() {
2 mut x := map[string]map[string]int{}
3 x['a'] = map[string]int{}
4 assert x['a']['b'] == 0
5 x['a']['b'] = 5
6 assert x['a']['b'] == 5
7 x['a']['b'] = 7
8 assert x['a']['b'] == 7
9}
10
11fn test_map_of_map_of_map() {
12 mut y := map[string]map[string]map[string]int{}
13 y['a'] = map[string]map[string]int{}
14 y['a']['b'] = map[string]int{}
15 assert y['a']['b']['c'] == 0
16 y['a']['b']['c'] = 5
17 assert y['a']['b']['c'] == 5
18 y['a']['b']['c'] = 7
19 assert y['a']['b']['c'] == 7
20}
21
22struct Foo {
23mut:
24 name string
25}
26
27fn test_map_of_map_to_struct() {
28 mut foos := map[string]map[string]Foo{}
29 foos['zza']['zzb'] = Foo{'bar'}
30 assert foos['zza']['zzb'].name == 'bar'
31
32 foos['zza']['zzb'].name = 'baz'
33 assert foos['zza']['zzb'].name == 'baz'
34}
35
36fn test_map_of_map_key_plus_assign() {
37 m := {
38 'a': 'x'
39 }
40 mut n := {
41 'x': 1
42 }
43 n[m['a']] += 2
44 println(n)
45 assert n['x'] == 3
46}
47