| 1 | const builtin = @import("builtin"); |
| 2 | const expect = @import("std").testing.expect; |
| 3 | |
| 4 | const Node = struct { |
| 5 | payload: i32, |
| 6 | children: []Node, |
| 7 | }; |
| 8 | |
| 9 | const NodeAligned = struct { |
| 10 | payload: i32, |
| 11 | children: []align(1) NodeAligned, |
| 12 | }; |
| 13 | |
| 14 | test "struct contains slice of itself" { |
| 15 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 16 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 17 | |
| 18 | var other_nodes = [_]Node{ |
| 19 | Node{ |
| 20 | .payload = 31, |
| 21 | .children = &[_]Node{}, |
| 22 | }, |
| 23 | Node{ |
| 24 | .payload = 32, |
| 25 | .children = &[_]Node{}, |
| 26 | }, |
| 27 | }; |
| 28 | var nodes = [_]Node{ |
| 29 | Node{ |
| 30 | .payload = 1, |
| 31 | .children = &[_]Node{}, |
| 32 | }, |
| 33 | Node{ |
| 34 | .payload = 2, |
| 35 | .children = &[_]Node{}, |
| 36 | }, |
| 37 | Node{ |
| 38 | .payload = 3, |
| 39 | .children = other_nodes[0..], |
| 40 | }, |
| 41 | }; |
| 42 | const root = Node{ |
| 43 | .payload = 1234, |
| 44 | .children = nodes[0..], |
| 45 | }; |
| 46 | try expect(root.payload == 1234); |
| 47 | try expect(root.children[0].payload == 1); |
| 48 | try expect(root.children[1].payload == 2); |
| 49 | try expect(root.children[2].payload == 3); |
| 50 | try expect(root.children[2].children[0].payload == 31); |
| 51 | try expect(root.children[2].children[1].payload == 32); |
| 52 | } |
| 53 | |
| 54 | test "struct contains aligned slice of itself" { |
| 55 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 56 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 57 | |
| 58 | var other_nodes = [_]NodeAligned{ |
| 59 | NodeAligned{ |
| 60 | .payload = 31, |
| 61 | .children = &[_]NodeAligned{}, |
| 62 | }, |
| 63 | NodeAligned{ |
| 64 | .payload = 32, |
| 65 | .children = &[_]NodeAligned{}, |
| 66 | }, |
| 67 | }; |
| 68 | var nodes = [_]NodeAligned{ |
| 69 | NodeAligned{ |
| 70 | .payload = 1, |
| 71 | .children = &[_]NodeAligned{}, |
| 72 | }, |
| 73 | NodeAligned{ |
| 74 | .payload = 2, |
| 75 | .children = &[_]NodeAligned{}, |
| 76 | }, |
| 77 | NodeAligned{ |
| 78 | .payload = 3, |
| 79 | .children = other_nodes[0..], |
| 80 | }, |
| 81 | }; |
| 82 | const root = NodeAligned{ |
| 83 | .payload = 1234, |
| 84 | .children = nodes[0..], |
| 85 | }; |
| 86 | try expect(root.payload == 1234); |
| 87 | try expect(root.children[0].payload == 1); |
| 88 | try expect(root.children[1].payload == 2); |
| 89 | try expect(root.children[2].payload == 3); |
| 90 | try expect(root.children[2].children[0].payload == 31); |
| 91 | try expect(root.children[2].children[1].payload == 32); |
| 92 | } |