zig/test/behavior/struct_contains_slice_of_itself.zig
Andrew Kelley 5619ce2406 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * doc/langref.html.in
 * lib/std/enums.zig
 * lib/std/fmt.zig
 * lib/std/hash/auto_hash.zig
 * lib/std/math.zig
 * lib/std/mem.zig
 * lib/std/meta.zig
 * test/behavior/alignof.zig
 * test/behavior/bitcast.zig
 * test/behavior/bugs/1421.zig
 * test/behavior/cast.zig
 * test/behavior/ptrcast.zig
 * test/behavior/type_info.zig
 * test/behavior/vector.zig

Master branch added `try` to a bunch of testing function calls, and some
lines also had changed how to refer to the native architecture and other
`@import("builtin")` stuff.
2021-05-08 14:45:21 -07:00

86 lines
2.1 KiB
Zig

const expect = @import("std").testing.expect;
const Node = struct {
payload: i32,
children: []Node,
};
const NodeAligned = struct {
payload: i32,
children: []align(@alignOf(NodeAligned)) NodeAligned,
};
test "struct contains slice of itself" {
var other_nodes = [_]Node{
Node{
.payload = 31,
.children = &[_]Node{},
},
Node{
.payload = 32,
.children = &[_]Node{},
},
};
var nodes = [_]Node{
Node{
.payload = 1,
.children = &[_]Node{},
},
Node{
.payload = 2,
.children = &[_]Node{},
},
Node{
.payload = 3,
.children = other_nodes[0..],
},
};
const root = Node{
.payload = 1234,
.children = nodes[0..],
};
try expect(root.payload == 1234);
try expect(root.children[0].payload == 1);
try expect(root.children[1].payload == 2);
try expect(root.children[2].payload == 3);
try expect(root.children[2].children[0].payload == 31);
try expect(root.children[2].children[1].payload == 32);
}
test "struct contains aligned slice of itself" {
var other_nodes = [_]NodeAligned{
NodeAligned{
.payload = 31,
.children = &[_]NodeAligned{},
},
NodeAligned{
.payload = 32,
.children = &[_]NodeAligned{},
},
};
var nodes = [_]NodeAligned{
NodeAligned{
.payload = 1,
.children = &[_]NodeAligned{},
},
NodeAligned{
.payload = 2,
.children = &[_]NodeAligned{},
},
NodeAligned{
.payload = 3,
.children = other_nodes[0..],
},
};
const root = NodeAligned{
.payload = 1234,
.children = nodes[0..],
};
try expect(root.payload == 1234);
try expect(root.children[0].payload == 1);
try expect(root.children[1].payload == 2);
try expect(root.children[2].payload == 3);
try expect(root.children[2].children[0].payload == 31);
try expect(root.children[2].children[1].payload == 32);
}