v2 / vlib / v / tests / comptime / comptime_field_indirections_test.v
56 lines · 48 sloc · 1.04 KB · 7122be4dfe73c52c9f0b16e400500bbfa86acc1a
Raw
1struct Encoder {}
2
3struct Writer {}
4
5struct ComptimeTypeofIdxInner {
6 x int
7}
8
9struct ComptimeTypeofIdxOuter {
10mut:
11 inner &ComptimeTypeofIdxInner = unsafe { nil }
12}
13
14struct StructType[T] {
15mut:
16 val &T
17 val2 T
18}
19
20fn (e &Encoder) encode_struct[U](val U, mut wr Writer) ! {
21 $for field in U.fields {
22 if field.indirections == 1 {
23 assert field.indirections == 1
24 value := val.$(field.name)
25 $if field.indirections == 1 {
26 assert *value == 'ads'
27 } $else {
28 assert false
29 }
30 } else {
31 assert field.name == 'val2'
32 }
33 }
34}
35
36fn test_indirection_checking() {
37 e := Encoder{}
38 mut sb := Writer{}
39 mut string_pointer := 'ads'
40 e.encode_struct(StructType[string]{ val: &string_pointer }, mut sb)!
41}
42
43fn allocate_comptime_typeof_idx_ptrs[T](mut s T) {
44 $for f in T.fields {
45 $if f.indirections == 1 {
46 s.$(f.name) = &typeof(s.$(f.name)).idx{}
47 }
48 }
49}
50
51fn test_comptime_typeof_idx_struct_init() {
52 mut outer := ComptimeTypeofIdxOuter{}
53 allocate_comptime_typeof_idx_ptrs(mut outer)
54 assert outer.inner != unsafe { nil }
55 assert outer.inner.x == 0
56}
57