v2 / vlib / v / tests / enums / enum_bitfield_test.v
116 lines · 100 sloc · 2.74 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1@[flag]
2enum BfPermission {
3 read
4 write
5 execute
6 other
7}
8
9struct BfFile {
10mut:
11 perm BfPermission
12}
13
14fn test_enum_bitfield() {
15 mut a := BfFile{}
16 assert 1 == int(BfPermission.read)
17 assert 2 == int(BfPermission.write)
18 assert 4 == int(BfPermission.execute)
19 assert 8 == int(BfPermission.other)
20 a.perm.set(.read)
21 a.perm.set(.write)
22 a.perm.toggle(.execute)
23 a.perm.clear(.write)
24 // a.perm.set(.other)
25 assert a.perm.has(.read)
26 assert a.perm.has(.execute)
27 assert !a.perm.has(.write)
28 assert !a.perm.has(.other)
29}
30
31fn test_enum_bitfield_operators() {
32 mut b := BfPermission.read | BfPermission.execute
33 assert int(b) == 1 + 0 + 4 + 0
34 assert b.has(.read)
35 assert b.has(.execute)
36 b.set(.write)
37 assert int(b) == 1 + 2 + 4 + 0
38 b.set(.other)
39 assert int(b) == 1 + 2 + 4 + 8
40 assert b.has(.write)
41 assert b.has(.other)
42 b.toggle(.read)
43 assert int(b) == 0 + 2 + 4 + 8
44 b.toggle(.execute)
45 assert int(b) == 0 + 2 + 0 + 8
46 assert !b.has(.read)
47 assert !b.has(.execute)
48}
49
50fn test_enum_bitfield_has_vs_all_methods_with_combined_flags() {
51 mut c := BfPermission.read
52 c.set(.write | .execute)
53 assert c.has(.read | .write | .execute)
54 assert !c.has(.other)
55 assert c.has(.read | .write | .execute | .other)
56 // .all() tests if *ALL* of the given flags are set, i.e. not just any one of them.
57 // .has() tests if *ANY* of the given flags is set, even though some of the others may not be.
58 assert c.all(.read | .write | .execute)
59 assert !c.all(.read | .write | .execute | .other)
60}
61
62fn test_enum_bitfield_has_vs_all_methods_with_combined_flags_2() {
63 mut c := BfPermission.read | .execute | .other
64
65 assert c.has(.read | .execute | .other | .write)
66 assert c.has(.read | .write)
67 assert !c.all(.read | .execute | .other | .write)
68 assert !c.all(.read | .write)
69
70 assert c.all(.read | .execute | .other)
71 assert c.all(.read | .execute)
72 assert c.all(.execute | .other)
73 assert c.all(.read | .other)
74
75 assert c.has(.read | .execute | .other)
76 assert c.has(.read | .execute)
77 assert c.has(.execute | .other)
78 assert c.has(.read | .other)
79}
80
81fn test_enum_bitfield_set_all() {
82 mut a := BfFile{}
83
84 a.perm.set_all()
85
86 assert a.perm.has(.read)
87 assert a.perm.has(.execute)
88 assert a.perm.has(.write)
89 assert a.perm.has(.other)
90
91 mut b := BfFile{}
92 b.perm.set(.read | .execute | .write | .other)
93 println(b.perm)
94 println(a.perm)
95 assert a.perm == b.perm, '.set_all() should be equivalent to using .set() with all the bit names'
96}
97
98fn test_enum_bitfield_clear_all() {
99 mut a := BfFile{}
100
101 a.perm.set(.read)
102 assert a.perm.has(.read)
103 a.perm.set(.write)
104 assert a.perm.has(.write)
105 a.perm.set(.execute)
106 assert a.perm.has(.execute)
107 a.perm.set(.other)
108 assert a.perm.has(.other)
109
110 a.perm.clear_all()
111
112 assert !a.perm.has(.read)
113 assert !a.perm.has(.execute)
114 assert !a.perm.has(.write)
115 assert !a.perm.has(.other)
116}
117