1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
const assert = @import("std").debug.assert;
const StructWithNoFields = struct {
fn add(a: i32, b: i32) -> i32 { a + b }
};
const empty_global_instance = StructWithNoFields {};
fn callStructStaticMethod() {
@setFnTest(this);
const result = StructWithNoFields.add(3, 4);
assert(result == 7);
}
fn returnEmptyStructInstance() -> StructWithNoFields {
@setFnTest(this);
return empty_global_instance;
}
const should_be_11 = StructWithNoFields.add(5, 6);
fn invokeStaticMethodInGlobalScope() {
@setFnTest(this);
assert(should_be_11 == 11);
}
fn voidStructFields() {
@setFnTest(this);
const foo = VoidStructFieldsFoo {
.a = void{},
.b = 1,
.c = void{},
};
assert(foo.b == 1);
assert(@sizeOf(VoidStructFieldsFoo) == 4);
}
const VoidStructFieldsFoo = struct {
a : void,
b : i32,
c : void,
};
pub fn structs() {
@setFnTest(this);
var foo: StructFoo = undefined;
@memset((&u8)(&foo), 0, @sizeOf(StructFoo));
foo.a += 1;
foo.b = foo.a == 1;
testFoo(foo);
testMutation(&foo);
assert(foo.c == 100);
}
const StructFoo = struct {
a : i32,
b : bool,
c : f32,
};
fn testFoo(foo : StructFoo) {
assert(foo.b);
}
fn testMutation(foo : &StructFoo) {
foo.c = 100;
}
const Node = struct {
val: Val,
next: &Node,
};
const Val = struct {
x: i32,
};
fn structPointToSelf() {
@setFnTest(this);
var root : Node = undefined;
root.val.x = 1;
var node : Node = undefined;
node.next = &root;
node.val.x = 2;
root.next = &node;
assert(node.next.next.next.val.x == 1);
}
fn structByvalAssign() {
@setFnTest(this);
var foo1 : StructFoo = undefined;
var foo2 : StructFoo = undefined;
foo1.a = 1234;
foo2.a = 0;
assert(foo2.a == 0);
foo2 = foo1;
assert(foo2.a == 1234);
}
fn structInitializer() {
const val = Val { .x = 42 };
assert(val.x == 42);
}
fn fnCallOfStructField() {
@setFnTest(this);
assert(callStructField(Foo {.ptr = aFunc,}) == 13);
}
const Foo = struct {
ptr: fn() -> i32,
};
fn aFunc() -> i32 { 13 }
fn callStructField(foo: Foo) -> i32 {
return foo.ptr();
}
fn storeMemberFunctionInVariable() {
@setFnTest(this);
const instance = MemberFnTestFoo { .x = 1234, };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
assert(result == 1234);
}
const MemberFnTestFoo = struct {
x: i32,
fn member(foo: MemberFnTestFoo) -> i32 { foo.x }
};
fn callMemberFunctionDirectly() {
@setFnTest(this);
const instance = MemberFnTestFoo { .x = 1234, };
const result = MemberFnTestFoo.member(instance);
assert(result == 1234);
}
fn memberFunctions() {
@setFnTest(this);
const r = MemberFnRand {.seed = 1234};
assert(r.getSeed() == 1234);
}
const MemberFnRand = struct {
seed: u32,
pub fn getSeed(r: &const MemberFnRand) -> u32 {
r.seed
}
};
fn returnStructByvalFromFunction() {
@setFnTest(this);
const bar = makeBar(1234, 5678);
assert(bar.y == 5678);
}
const Bar = struct {
x: i32,
y: i32,
};
fn makeBar(x: i32, y: i32) -> Bar {
Bar {
.x = x,
.y = y,
}
}
fn emptyStructMethodCall() {
@setFnTest(this);
const es = EmptyStruct{};
assert(es.method() == 1234);
}
const EmptyStruct = struct {
fn method(es: &const EmptyStruct) -> i32 {
1234
}
};
fn returnEmptyStructFromFn() {
@setFnTest(this);
testReturnEmptyStructFromFn();
}
const EmptyStruct2 = struct {};
fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
EmptyStruct2 {}
}
fn passSliceOfEmptyStructToFn() {
@setFnTest(this);
assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
}
fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
slice.len
}
|