v2 / vlib / v / tests / enums / maps_with_enum_flag_keys_test.v
47 lines · 45 sloc · 936 bytes · 013281451275121cbcd5780a402b475bf5c5b94e
Raw
1// vfmt off
2@[flag]
3enum Bits8 as u8 {
4 a1 b1 c1 d1 e1 f1 g1 h1
5}
6@[flag]
7enum Bits16 as u16 {
8 a1 b1 c1 d1 e1 f1 g1 h1
9 a2 b2 c2 d2 e2 f2 g2 h2
10}
11@[flag]
12enum Bits32 as u32 {
13 a1 b1 c1 d1 e1 f1 g1 h1
14 a2 b2 c2 d2 e2 f2 g2 h2
15 a3 b3 c3 d3 e3 f3 g3 h3
16 a4 b4 c4 d4 e4 f4 g4 h4
17}
18@[flag]
19enum Bits64 as u64 {
20 a1 b1 c1 d1 e1 f1 g1 h1
21 a2 b2 c2 d2 e2 f2 g2 h2
22 a3 b3 c3 d3 e3 f3 g3 h3
23 a4 b4 c4 d4 e4 f4 g4 h4
24 a5 b5 c5 d5 e5 f5 g5 h5
25 a6 b6 c6 d6 e6 f6 g6 h6
26 a7 b7 c7 d7 e7 f7 g7 h7
27 a8 b8 c8 d8 e8 f8 g8 h8
28}
29// vfmt on
30
31fn check_map[T](size int) {
32 println('>>> checking map of ${T.name:10} enum keys, size should be: ${size}')
33 mut m := map[T]u32{}
34 for i in 0 .. size {
35 n := u64(1) << i
36 m[unsafe { T(n) }] = i
37 // eprintln('>>> i: ${i:2} | n: ${n:20} | m.len: ${m.len}')
38 }
39 assert m.len == size
40}
41
42fn test_maps_with_enum_keys_work() {
43 check_map[Bits8](8)
44 check_map[Bits16](16)
45 check_map[Bits32](32)
46 check_map[Bits64](64)
47}
48