authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 03:44:59-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 03:44:59-05:00
logaee7ad3de2e3c334ec5aac2d355e4eed086f1cdf
tree8577f02b3514539a2accc91e3ebbcb144d0f1208
parent73a751911e2c2be646287eb8c8d579dded25afe3

IR: port all passing tests over


12 files changed, 441 insertions(+), 457 deletions(-)

test/cases/array.zig+27
......@@ -55,6 +55,19 @@ const ArrayDotLenConstExpr = struct {
5555const some_array = []u8 {0, 1, 2, 3};
5656
5757
58fn nestedArrays() {
59 @setFnTest(this);
60
61 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
62 for (array_of_strings) |s, i| {
63 if (i == 0) assert(memeql(s, "hello"));
64 if (i == 1) assert(memeql(s, "this"));
65 if (i == 2) assert(memeql(s, "is"));
66 if (i == 3) assert(memeql(s, "my"));
67 if (i == 4) assert(memeql(s, "thing"));
68 }
69}
70
5871
5972
6073// TODO const assert = @import("std").debug.assert;
......@@ -62,3 +75,17 @@ fn assert(ok: bool) {
6275 if (!ok)
6376 @unreachable();
6477}
78
79// TODO import from std.str
80pub fn memeql(a: []const u8, b: []const u8) -> bool {
81 sliceEql(u8, a, b)
82}
83
84// TODO import from std.str
85pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
86 if (a.len != b.len) return false;
87 for (a) |item, index| {
88 if (b[index] != item) return false;
89 }
90 return true;
91}
test/cases/enum.zig+1
......@@ -95,6 +95,7 @@ fn shouldEqual(n: Number, expected: usize) {
9595 assert(usize(n) == expected);
9696}
9797
98
9899// TODO import from std
99100fn assert(ok: bool) {
100101 if (!ok)
test/cases/eval.zig+51
......@@ -110,6 +110,57 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
110110 }
111111}
112112
113
114fn constantExpressions() {
115 @setFnTest(this);
116
117 var array : [array_size]u8 = undefined;
118 assert(@sizeOf(@typeOf(array)) == 20);
119}
120const array_size : u8 = 20;
121
122
123fn constantStructWithNegation() {
124 @setFnTest(this);
125
126 assert(vertices[0].x == -0.6);
127}
128const Vertex = struct {
129 x: f32,
130 y: f32,
131 r: f32,
132 g: f32,
133 b: f32,
134};
135const vertices = []Vertex {
136 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
137 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
138 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
139};
140
141
142fn staticallyInitalizedStruct() {
143 @setFnTest(this);
144
145 st_init_str_foo.x += 1;
146 assert(st_init_str_foo.x == 14);
147}
148const StInitStrFoo = struct {
149 x: i32,
150 y: bool,
151};
152var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
153
154
155fn staticallyInitializedArrayLiteral() {
156 @setFnTest(this);
157
158 const y : [4]u8 = st_init_arr_lit_x;
159 assert(y[3] == 4);
160}
161const st_init_arr_lit_x = []u8{1,2,3,4};
162
163
113164// TODO const assert = @import("std").debug.assert;
114165fn assert(ok: bool) {
115166 if (!ok)
test/cases/fn.zig+14
......@@ -86,6 +86,20 @@ fn fnWithUnreachable() -> unreachable {
8686}
8787
8888
89fn functionPointers() {
90 @setFnTest(this);
91
92 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
93 for (fns) |f, i| {
94 assert(f() == u32(i) + 5);
95 }
96}
97fn fn1() -> u32 {5}
98fn fn2() -> u32 {6}
99fn fn3() -> u32 {7}
100fn fn4() -> u32 {8}
101
102
89103
90104// TODO const assert = @import("std").debug.assert;
91105fn assert(ok: bool) {
test/cases/for.zig+36
......@@ -12,3 +12,39 @@ fn continueInForLoop() {
1212 }
1313 if (sum != 6) @unreachable()
1414}
15
16fn forLoopWithPointerElemVar() {
17 @setFnTest(this);
18
19 const source = "abcdefg";
20 var target: [source.len]u8 = undefined;
21 @memcpy(&target[0], &source[0], source.len);
22 mangleString(target);
23 assert(memeql(target, "bcdefgh"));
24}
25fn mangleString(s: []u8) {
26 for (s) |*c| {
27 *c += 1;
28 }
29}
30
31
32// TODO import from std.str
33pub fn memeql(a: []const u8, b: []const u8) -> bool {
34 sliceEql(u8, a, b)
35}
36
37// TODO import from std.str
38pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
39 if (a.len != b.len) return false;
40 for (a) |item, index| {
41 if (b[index] != item) return false;
42 }
43 return true;
44}
45
46// TODO const assert = @import("std").debug.assert;
47fn assert(ok: bool) {
48 if (!ok)
49 @unreachable();
50}
test/cases/generics.zig+40
......@@ -88,6 +88,46 @@ fn functionWithReturnTypeType() {
8888 assert(list2.prealloc_items.len == 8);
8989}
9090
91
92fn genericStruct() {
93 @setFnTest(this);
94
95 var a1 = GenNode(i32) {.value = 13, .next = null,};
96 var b1 = GenNode(bool) {.value = true, .next = null,};
97 assert(a1.value == 13);
98 assert(a1.value == a1.getVal());
99 assert(b1.getVal());
100}
101fn GenNode(inline T: type) -> type {
102 struct {
103 value: T,
104 next: ?&GenNode(T),
105 fn getVal(n: &const GenNode(T)) -> T { n.value }
106 }
107}
108
109fn constDeclsInStruct() {
110 @setFnTest(this);
111
112 assert(GenericDataThing(3).count_plus_one == 4);
113}
114fn GenericDataThing(inline count: isize) -> type {
115 struct {
116 const count_plus_one = count + 1;
117 }
118}
119
120
121fn useGenericParamInGenericParam() {
122 @setFnTest(this);
123
124 assert(aGenericFn(i32, 3, 4) == 7);
125}
126fn aGenericFn(inline T: type, inline a: T, b: T) -> T {
127 return a + b;
128}
129
130
91131// TODO const assert = @import("std").debug.assert;
92132fn assert(ok: bool) {
93133 if (!ok)
test/cases/math.zig+64
......@@ -101,6 +101,70 @@ const ten = 10;
101101
102102
103103
104fn unsignedWrapping() {
105 @setFnTest(this);
106
107 testUnsignedWrappingEval(@maxValue(u32));
108}
109fn testUnsignedWrappingEval(x: u32) {
110 const zero = x +% 1;
111 assert(zero == 0);
112 const orig = zero -% 1;
113 assert(orig == @maxValue(u32));
114}
115
116fn signedWrapping() {
117 @setFnTest(this);
118
119 testSignedWrappingEval(@maxValue(i32));
120}
121fn testSignedWrappingEval(x: i32) {
122 const min_val = x +% 1;
123 assert(min_val == @minValue(i32));
124 const max_val = min_val -% 1;
125 assert(max_val == @maxValue(i32));
126}
127
128fn negationWrapping() {
129 @setFnTest(this);
130
131 testNegationWrappingEval(@minValue(i16));
132}
133fn testNegationWrappingEval(x: i16) {
134 assert(x == -32768);
135 const neg = -%x;
136 assert(neg == -32768);
137}
138
139fn shlWrapping() {
140 @setFnTest(this);
141
142 testShlWrappingEval(@maxValue(u16));
143}
144fn testShlWrappingEval(x: u16) {
145 const shifted = x <<% 1;
146 assert(shifted == 65534);
147}
148
149fn unsigned64BitDivision() {
150 @setFnTest(this);
151
152 const result = div(1152921504606846976, 34359738365);
153 assert(result.quotient == 33554432);
154 assert(result.remainder == 100663296);
155}
156fn div(a: u64, b: u64) -> DivResult {
157 DivResult {
158 .quotient = a / b,
159 .remainder = a % b,
160 }
161}
162const DivResult = struct {
163 quotient: u64,
164 remainder: u64,
165};
166
167
104168// TODO const assert = @import("std").debug.assert;
105169fn assert(ok: bool) {
106170 if (!ok)
test/cases/misc.zig+96
......@@ -322,6 +322,102 @@ fn outer() -> i64 {
322322}
323323
324324
325fn pointerDereferencing() {
326 @setFnTest(this);
327
328 var x = i32(3);
329 const y = &x;
330
331 *y += 1;
332
333 assert(x == 4);
334 assert(*y == 4);
335}
336
337fn callResultOfIfElseExpression() {
338 @setFnTest(this);
339
340 assert(memeql(f2(true), "a"));
341 assert(memeql(f2(false), "b"));
342}
343fn f2(x: bool) -> []u8 {
344 return (if (x) fA else fB)();
345}
346fn fA() -> []u8 { "a" }
347fn fB() -> []u8 { "b" }
348
349
350fn constExpressionEvalHandlingOfVariables() {
351 @setFnTest(this);
352
353 var x = true;
354 while (x) {
355 x = false;
356 }
357}
358
359
360
361fn constantEnumInitializationWithDifferingSizes() {
362 @setFnTest(this);
363
364 test3_1(test3_foo);
365 test3_2(test3_bar);
366}
367const Test3Foo = enum {
368 One,
369 Two: f32,
370 Three: Test3Point,
371};
372const Test3Point = struct {
373 x: i32,
374 y: i32,
375};
376const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}};
377const test3_bar = Test3Foo.Two{13};
378fn test3_1(f: Test3Foo) {
379 switch (f) {
380 Test3Foo.Three => |pt| {
381 assert(pt.x == 3);
382 assert(pt.y == 4);
383 },
384 else => @unreachable(),
385 }
386}
387fn test3_2(f: Test3Foo) {
388 switch (f) {
389 Test3Foo.Two => |x| {
390 assert(x == 13);
391 },
392 else => @unreachable(),
393 }
394}
395
396
397fn characterLiterals() {
398 @setFnTest(this);
399
400 assert('\'' == single_quote);
401}
402const single_quote = '\'';
403
404
405
406fn takeAddressOfParameter() {
407 @setFnTest(this);
408
409 testTakeAddressOfParameter(12.34);
410}
411fn testTakeAddressOfParameter(f: f32) {
412 const f_ptr = &f;
413 assert(*f_ptr == 12.34);
414}
415
416
417
418
419
420
325421// TODO import from std.str
326422pub fn memeql(a: []const u8, b: []const u8) -> bool {
327423 sliceEql(u8, a, b)
test/cases/null.zig+38
......@@ -54,6 +54,44 @@ fn maybeReturn() {
5454 assert(!??foo(1234));
5555}
5656
57
58fn ifVarMaybePointer() {
59 @setFnTest(this);
60
61 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
62}
63fn shouldBeAPlus1(p: Particle) -> u64 {
64 var maybe_particle: ?Particle = p;
65 if (const *particle ?= maybe_particle) {
66 particle.a += 1;
67 }
68 if (const particle ?= maybe_particle) {
69 return particle.a;
70 }
71 return 0;
72}
73const Particle = struct {
74 a: u64,
75 b: u64,
76 c: u64,
77 d: u64,
78};
79
80
81fn nullLiteralOutsideFunction() {
82 @setFnTest(this);
83
84 const is_null = if (const _ ?= here_is_a_null_literal.context) false else true;
85 assert(is_null);
86}
87const SillyStruct = struct {
88 context: ?i32,
89};
90const here_is_a_null_literal = SillyStruct {
91 .context = null,
92};
93
94
5795// TODO test static eval maybe return
5896fn foo(x: ?i32) -> ?bool {
5997 const value = ?return x;
test/cases/struct.zig+41
......@@ -158,6 +158,47 @@ const MemberFnRand = struct {
158158 }
159159};
160160
161fn returnStructByvalFromFunction() {
162 @setFnTest(this);
163
164 const bar = makeBar(1234, 5678);
165 assert(bar.y == 5678);
166}
167const Bar = struct {
168 x: i32,
169 y: i32,
170};
171fn makeBar(x: i32, y: i32) -> Bar {
172 Bar {
173 .x = x,
174 .y = y,
175 }
176}
177
178fn emptyStructMethodCall() {
179 @setFnTest(this);
180
181 const es = EmptyStruct{};
182 assert(es.method() == 1234);
183}
184const EmptyStruct = struct {
185 fn method(es: EmptyStruct) -> i32 {
186 1234
187 }
188};
189
190
191fn returnEmptyStructFromFn() {
192 @setFnTest(this);
193
194 testReturnEmptyStructFromFn();
195}
196const EmptyStruct2 = struct {};
197fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
198 EmptyStruct2 {}
199}
200
201
161202
162203// TODO const assert = @import("std").debug.assert;
163204fn assert(ok: bool) {
test/cases/switch.zig+13
......@@ -114,6 +114,19 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
114114}
115115
116116
117fn switchWithMultipleExpressions() {
118 @setFnTest(this);
119
120 const x = switch (returnsFive()) {
121 1, 2, 3 => 1,
122 4, 5, 6 => 2,
123 else => i32(3),
124 };
125 assert(x == 2);
126}
127fn returnsFive() -> i32 {
128 5
129}
117130
118131
119132// TODO const assert = @import("std").debug.assert;
test/self_hosted.zig+20-457
......@@ -1,12 +1,6 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const str = std.str;
4const cstr = std.cstr;
5
6
7
1// TODO not passing
82fn genericFnWithImplicitCast() {
9 @setFnTest(this, true);
3 @setFnTest(this);
104
115 assert(getFirstByte(u8, []u8 {13}) == 13);
126 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
......@@ -16,42 +10,10 @@ fn getFirstByte(inline T: type, mem: []T) -> u8 {
1610 getByte((&u8)(&mem[0]))
1711}
1812
19fn pointerDereferencing() {
20 @setFnTest(this, true);
21
22 var x = i32(3);
23 const y = &x;
24
25 *y += 1;
26
27 assert(x == 4);
28 assert(*y == 4);
29}
30
31fn constantExpressions() {
32 @setFnTest(this, true);
33
34 var array : [array_size]u8 = undefined;
35 assert(@sizeOf(@typeOf(array)) == 20);
36}
37const array_size : u8 = 20;
38
39
40fn nestedArrays() {
41 @setFnTest(this, true);
42
43 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
44 for (array_of_strings) |s, i| {
45 if (i == 0) assert(str.eql(s, "hello"));
46 if (i == 1) assert(str.eql(s, "this"));
47 if (i == 2) assert(str.eql(s, "is"));
48 if (i == 3) assert(str.eql(s, "my"));
49 if (i == 4) assert(str.eql(s, "thing"));
50 }
51}
5213
14// TODO not passing
5315fn intToPtrCast() {
54 @setFnTest(this, true);
16 @setFnTest(this);
5517
5618 const x = isize(13);
5719 const y = (&u8)(x);
......@@ -59,81 +21,9 @@ fn intToPtrCast() {
5921 assert(z == 13);
6022}
6123
62fn constantStructWithNegation() {
63 @setFnTest(this, true);
64
65 assert(vertices[0].x == -0.6);
66}
67struct Vertex {
68 x: f32,
69 y: f32,
70 r: f32,
71 g: f32,
72 b: f32,
73}
74const vertices = []Vertex {
75 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
76 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
77 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
78};
79
80
81fn returnStructByvalFromFunction() {
82 @setFnTest(this, true);
83
84 const bar = makeBar(1234, 5678);
85 assert(bar.y == 5678);
86}
87struct Bar {
88 x: i32,
89 y: i32,
90}
91fn makeBar(x: i32, y: i32) -> Bar {
92 Bar {
93 .x = x,
94 .y = y,
95 }
96}
97
98fn functionPointers() {
99 @setFnTest(this, true);
100
101 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
102 for (fns) |f, i| {
103 assert(f() == u32(i) + 5);
104 }
105}
106fn fn1() -> u32 {5}
107fn fn2() -> u32 {6}
108fn fn3() -> u32 {7}
109fn fn4() -> u32 {8}
110
111
112
113fn staticallyInitalizedStruct() {
114 @setFnTest(this, true);
115
116 st_init_str_foo.x += 1;
117 assert(st_init_str_foo.x == 14);
118}
119struct StInitStrFoo {
120 x: i32,
121 y: bool,
122}
123var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
124
125fn staticallyInitializedArrayLiteral() {
126 @setFnTest(this, true);
127
128 const y : [4]u8 = st_init_arr_lit_x;
129 assert(y[3] == 4);
130}
131const st_init_arr_lit_x = []u8{1,2,3,4};
132
133
134
24// TODO not passing
13525fn pointerToVoidReturnType() {
136 @setFnTest(this, true);
26 @setFnTest(this);
13727
13828 %%testPointerToVoidReturnType();
13929}
......@@ -142,129 +32,14 @@ fn testPointerToVoidReturnType() -> %void {
14232 return *a;
14333}
14434const test_pointer_to_void_return_type_x = void{};
145fn testPointerToVoidReturnType2() -> &void {
35fn testPointerToVoidReturnType2() -> &const void {
14636 return &test_pointer_to_void_return_type_x;
14737}
14838
14939
150fn callResultOfIfElseExpression() {
151 @setFnTest(this, true);
152
153 assert(str.eql(f2(true), "a"));
154 assert(str.eql(f2(false), "b"));
155}
156fn f2(x: bool) -> []u8 {
157 return (if (x) fA else fB)();
158}
159fn fA() -> []u8 { "a" }
160fn fB() -> []u8 { "b" }
161
162
163fn constExpressionEvalHandlingOfVariables() {
164 @setFnTest(this, true);
165
166 var x = true;
167 while (x) {
168 x = false;
169 }
170}
171
172
173
174fn constantEnumInitializationWithDifferingSizes() {
175 @setFnTest(this, true);
176
177 test3_1(test3_foo);
178 test3_2(test3_bar);
179}
180enum Test3Foo {
181 One,
182 Two: f32,
183 Three: Test3Point,
184}
185struct Test3Point {
186 x: i32,
187 y: i32,
188}
189const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}};
190const test3_bar = Test3Foo.Two{13};
191fn test3_1(f: Test3Foo) {
192 @setFnStaticEval(this, false);
193
194 switch (f) {
195 Three => |pt| {
196 assert(pt.x == 3);
197 assert(pt.y == 4);
198 },
199 else => @unreachable(),
200 }
201}
202fn test3_2(f: Test3Foo) {
203 @setFnStaticEval(this, false);
204
205 switch (f) {
206 Two => |x| {
207 assert(x == 13);
208 },
209 else => @unreachable(),
210 }
211}
212
213
214
215fn forLoopWithPointerElemVar() {
216 @setFnTest(this, true);
217
218 const source = "abcdefg";
219 var target: [source.len]u8 = undefined;
220 @memcpy(&target[0], &source[0], source.len);
221 mangleString(target);
222 assert(str.eql(target, "bcdefgh"));
223}
224fn mangleString(s: []u8) {
225 @setFnStaticEval(this, false);
226
227 for (s) |*c| {
228 *c += 1;
229 }
230}
231
232fn emptyStructMethodCall() {
233 @setFnTest(this, true);
234
235 const es = EmptyStruct{};
236 assert(es.method() == 1234);
237}
238struct EmptyStruct {
239 fn method(es: EmptyStruct) -> i32 {
240 @setFnStaticEval(this, false);
241 1234
242 }
243
244}
245
246
247
248
249
250fn returnEmptyStructFromFn() {
251 @setFnTest(this, true);
252
253 testReturnEmptyStructFromFn();
254 testReturnEmptyStructFromFnNoeval();
255}
256struct EmptyStruct2 {}
257fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
258 EmptyStruct2 {}
259}
260fn testReturnEmptyStructFromFnNoeval() -> EmptyStruct2 {
261 @setFnStaticEval(this, false);
262
263 EmptyStruct2 {}
264}
265
40// TODO not passing (goes in struct.zig)
26641fn passSliceOfEmptyStructToFn() {
267 @setFnTest(this, true);
42 @setFnTest(this);
26843
26944 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
27045}
......@@ -273,6 +48,7 @@ fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
27348}
27449
27550
51// TODO not passing
27652fn pointerComparison() {
27753 @setFnTest(this, true);
27854
......@@ -284,31 +60,8 @@ fn ptrEql(a: &[]u8, b: &[]u8) -> bool {
28460 a == b
28561}
28662
287fn characterLiterals() {
288 @setFnTest(this, true);
289
290 assert('\'' == single_quote);
291}
292const single_quote = '\'';
293
294
295fn switchWithMultipleExpressions() {
296 @setFnTest(this, true);
297
298 const x: i32 = switch (returnsFive()) {
299 1, 2, 3 => 1,
300 4, 5, 6 => 2,
301 else => 3,
302 };
303 assert(x == 2);
304}
305fn returnsFive() -> i32 {
306 @setFnStaticEval(this, false);
307 5
308}
309
310
311
63// TODO change this test to an issue
64// we're going to change how this works
31265fn switchOnErrorUnion() {
31366 @setFnTest(this, true);
31467
......@@ -327,133 +80,14 @@ fn returnsTen() -> %i32 {
32780 10
32881}
32982
330fn takeAddressOfParameter() {
331 @setFnTest(this, true);
332
333 testTakeAddressOfParameter(12.34);
334 testTakeAddressOfParameterNoeval(12.34);
335}
336fn testTakeAddressOfParameter(f: f32) {
337 const f_ptr = &f;
338 assert(*f_ptr == 12.34);
339}
340fn testTakeAddressOfParameterNoeval(f: f32) {
341 @setFnStaticEval(this, false);
342
343 const f_ptr = &f;
344 assert(*f_ptr == 12.34);
345}
346
347
348fn ifVarMaybePointer() {
349 @setFnTest(this, true);
350
351 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
352}
353fn shouldBeAPlus1(p: Particle) -> u64 {
354 @setFnStaticEval(this, false);
355
356 var maybe_particle: ?Particle = p;
357 if (const *particle ?= maybe_particle) {
358 particle.a += 1;
359 }
360 if (const particle ?= maybe_particle) {
361 return particle.a;
362 }
363 return 0;
364}
365struct Particle {
366 a: u64,
367 b: u64,
368 c: u64,
369 d: u64,
370}
371
372fn unsignedWrapping() {
373 @setFnTest(this, true);
374
375 testUnsignedWrappingEval(@maxValue(u32));
376 testUnsignedWrappingNoeval(@maxValue(u32));
377}
378fn testUnsignedWrappingEval(x: u32) {
379 const zero = x +% 1;
380 assert(zero == 0);
381 const orig = zero -% 1;
382 assert(orig == @maxValue(u32));
383}
384fn testUnsignedWrappingNoeval(x: u32) {
385 @setFnStaticEval(this, false);
386
387 const zero = x +% 1;
388 assert(zero == 0);
389 const orig = zero -% 1;
390 assert(orig == @maxValue(u32));
391}
392
393fn signedWrapping() {
394 @setFnTest(this, true);
395
396 testSignedWrappingEval(@maxValue(i32));
397 testSignedWrappingNoeval(@maxValue(i32));
398}
399fn testSignedWrappingEval(x: i32) {
400 const min_val = x +% 1;
401 assert(min_val == @minValue(i32));
402 const max_val = min_val -% 1;
403 assert(max_val == @maxValue(i32));
404}
405fn testSignedWrappingNoeval(x: i32) {
406 @setFnStaticEval(this, false);
407
408 const min_val = x +% 1;
409 assert(min_val == @minValue(i32));
410 const max_val = min_val -% 1;
411 assert(max_val == @maxValue(i32));
412}
413
414fn negationWrapping() {
415 @setFnTest(this, true);
416
417 testNegationWrappingEval(@minValue(i16));
418 testNegationWrappingNoeval(@minValue(i16));
419}
420fn testNegationWrappingEval(x: i16) {
421 assert(x == -32768);
422 const neg = -%x;
423 assert(neg == -32768);
424}
425fn testNegationWrappingNoeval(x: i16) {
426 @setFnStaticEval(this, false);
427
428 assert(x == -32768);
429 const neg = -%x;
430 assert(neg == -32768);
431}
432
433fn shlWrapping() {
434 @setFnTest(this, true);
435
436 testShlWrappingEval(@maxValue(u16));
437 testShlWrappingNoeval(@maxValue(u16));
438}
439fn testShlWrappingEval(x: u16) {
440 const shifted = x <<% 1;
441 assert(shifted == 65534);
442}
443fn testShlWrappingNoeval(x: u16) {
444 @setFnStaticEval(this, false);
445
446 const shifted = x <<% 1;
447 assert(shifted == 65534);
448}
449
83// TODO not passing
45084fn cStringConcatenation() {
45185 @setFnTest(this, true);
45286
45387 const a = c"OK" ++ c" IT " ++ c"WORKED";
45488 const b = c"OK IT WORKED";
45589
456 const len = cstr.len(b);
90 const len = cstrlen(b);
45791 const len_with_null = len + 1;
45892 {var i: u32 = 0; while (i < len_with_null; i += 1) {
45993 assert(a[i] == b[i]);
......@@ -462,23 +96,9 @@ fn cStringConcatenation() {
46296 assert(b[len] == 0);
46397}
46498
465fn genericStruct() {
466 @setFnTest(this, true);
467
468 var a1 = GenNode(i32) {.value = 13, .next = null,};
469 var b1 = GenNode(bool) {.value = true, .next = null,};
470 assert(a1.value == 13);
471 assert(a1.value == a1.getVal());
472 assert(b1.getVal());
473}
474struct GenNode(T: type) {
475 value: T,
476 next: ?&GenNode(T),
477 fn getVal(n: &const GenNode(T)) -> T { n.value }
478}
479
99// TODO not passing
480100fn castSliceToU8Slice() {
481 @setFnTest(this, true);
101 @setFnTest(this);
482102
483103 assert(@sizeOf(i32) == 4);
484104 var big_thing_array = []i32{1, 2, 3, 4};
......@@ -499,76 +119,19 @@ fn castSliceToU8Slice() {
499119 assert(bytes[11] == @maxValue(u8));
500120}
501121
502fn nullLiteralOutsideFunction() {
503 @setFnTest(this, true);
504
505 const is_null = if (const _ ?= here_is_a_null_literal.context) false else true;
506 assert(is_null);
507}
508struct SillyStruct {
509 context: ?i32,
510}
511const here_is_a_null_literal = SillyStruct {
512 .context = null,
513};
514
515fn constDeclsInStruct() {
516 @setFnTest(this, true);
517
518 assert(GenericDataThing(3).count_plus_one == 4);
519}
520struct GenericDataThing(count: isize) {
521 const count_plus_one = count + 1;
522}
523
524fn useGenericParamInGenericParam() {
525 @setFnTest(this, true);
526
527 assert(aGenericFn(i32, 3, 4) == 7);
528}
529fn aGenericFn(inline T: type, inline a: T, b: T) -> T {
530 return a + b;
531}
532
533
534fn unsigned64BitDivision() {
535 @setFnTest(this, true);
536
537 const result = div(1152921504606846976, 34359738365);
538 assert(result.quotient == 33554432);
539 assert(result.remainder == 100663296);
540}
541fn div(a: u64, b: u64) -> DivResult {
542 @setFnStaticEval(this, false);
543
544 DivResult {
545 .quotient = a / b,
546 .remainder = a % b,
547 }
548}
549struct DivResult {
550 quotient: u64,
551 remainder: u64,
552}
553
122// TODO not passing
554123fn intToEnum() {
555 @setFnTest(this, true);
124 @setFnTest(this);
556125
557126 testIntToEnumEval(3);
558 testIntToEnumNoeval(3);
559127}
560128fn testIntToEnumEval(x: i32) {
561129 assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
562130}
563fn testIntToEnumNoeval(x: i32) {
564 @setFnStaticEval(this, false);
565
566 assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
567}
568enum IntToEnumNumber {
131const IntToEnumNumber = enum {
569132 Zero,
570133 One,
571134 Two,
572135 Three,
573136 Four,
574}
137};