Skip to content

Commit fb81b19

Browse files
author
kristopher tate
committed
tests: re: 79db394;
ref: #1832
1 parent 39567e8 commit fb81b19

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

std/array_list.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,14 @@ test "std.ArrayList.insertSlice" {
398398
assert(list.len == 6);
399399
assert(list.items[0] == 1);
400400
}
401+
402+
const Item = struct {
403+
integer: i32,
404+
sub_items: ArrayList(Item),
405+
};
406+
407+
test "std.ArrayList: ArrayList(T) of struct T" {
408+
var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) };
409+
try root.sub_items.append( Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) } );
410+
assert(root.sub_items.items[0].integer == 42);
411+
}

test/cases/struct_contains_slice_of_itself.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const Node = struct {
55
children: []Node,
66
};
77

8+
const NodeAligned = struct {
9+
payload: i32,
10+
children: []align(@alignOf(NodeAligned)) NodeAligned,
11+
};
12+
813
test "struct contains slice of itself" {
914
var other_nodes = []Node{
1015
Node{
@@ -41,3 +46,40 @@ test "struct contains slice of itself" {
4146
assert(root.children[2].children[0].payload == 31);
4247
assert(root.children[2].children[1].payload == 32);
4348
}
49+
50+
test "struct contains aligned slice of itself" {
51+
var other_nodes = []NodeAligned{
52+
NodeAligned{
53+
.payload = 31,
54+
.children = []NodeAligned{},
55+
},
56+
NodeAligned{
57+
.payload = 32,
58+
.children = []NodeAligned{},
59+
},
60+
};
61+
var nodes = []NodeAligned{
62+
NodeAligned{
63+
.payload = 1,
64+
.children = []NodeAligned{},
65+
},
66+
NodeAligned{
67+
.payload = 2,
68+
.children = []NodeAligned{},
69+
},
70+
NodeAligned{
71+
.payload = 3,
72+
.children = other_nodes[0..],
73+
},
74+
};
75+
const root = NodeAligned{
76+
.payload = 1234,
77+
.children = nodes[0..],
78+
};
79+
assert(root.payload == 1234);
80+
assert(root.children[0].payload == 1);
81+
assert(root.children[1].payload == 2);
82+
assert(root.children[2].payload == 3);
83+
assert(root.children[2].children[0].payload == 31);
84+
assert(root.children[2].children[1].payload == 32);
85+
}

0 commit comments

Comments
 (0)