mirror of
https://github.com/ziglang/zig.git
synced 2026-01-03 03:53:20 +00:00
parent
39567e8b50
commit
fb81b1978b
@ -398,3 +398,14 @@ test "std.ArrayList.insertSlice" {
|
||||
assert(list.len == 6);
|
||||
assert(list.items[0] == 1);
|
||||
}
|
||||
|
||||
const Item = struct {
|
||||
integer: i32,
|
||||
sub_items: ArrayList(Item),
|
||||
};
|
||||
|
||||
test "std.ArrayList: ArrayList(T) of struct T" {
|
||||
var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) };
|
||||
try root.sub_items.append( Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) } );
|
||||
assert(root.sub_items.items[0].integer == 42);
|
||||
}
|
||||
|
||||
@ -5,6 +5,11 @@ const Node = struct {
|
||||
children: []Node,
|
||||
};
|
||||
|
||||
const NodeAligned = struct {
|
||||
payload: i32,
|
||||
children: []align(@alignOf(NodeAligned)) NodeAligned,
|
||||
};
|
||||
|
||||
test "struct contains slice of itself" {
|
||||
var other_nodes = []Node{
|
||||
Node{
|
||||
@ -41,3 +46,40 @@ test "struct contains slice of itself" {
|
||||
assert(root.children[2].children[0].payload == 31);
|
||||
assert(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..],
|
||||
};
|
||||
assert(root.payload == 1234);
|
||||
assert(root.children[0].payload == 1);
|
||||
assert(root.children[1].payload == 2);
|
||||
assert(root.children[2].payload == 3);
|
||||
assert(root.children[2].children[0].payload == 31);
|
||||
assert(root.children[2].children[1].payload == 32);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user