authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-19 00:41:37-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-19 00:41:37-05:00
log6b2d06710c52c348aa01b62007a2e990b47eee72
treefa9b66bc24fa7404250a7795e421be61807e72f6
parent09c34352f8a87ed9597b4af0de564aa7d831761a

IR: start a new passing self hosted test suite


16 files changed, 420 insertions(+), 611 deletions(-)

src/codegen.cpp+3-1
......@@ -2286,9 +2286,11 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
22862286 case IrInstructionIdEnumTag:
22872287 return ir_render_enum_tag(g, executable, (IrInstructionEnumTag *)instruction);
22882288 case IrInstructionIdSwitchVar:
2289 zig_panic("TODO render switch var instruction to LLVM");
22892290 case IrInstructionIdContainerInitList:
2291 zig_panic("TODO render container init list instruction to LLVM");
22902292 case IrInstructionIdStructInit:
2291 zig_panic("TODO render more IR instructions to LLVM");
2293 zig_panic("TODO render struct init to LLVM");
22922294 }
22932295 zig_unreachable();
22942296}
test/cases/err_wrapping.zig deleted-13
......@@ -1,13 +0,0 @@
1pub fn foo() -> %i32 {
2 const x = %return bar();
3 return x + 1
4}
5
6pub fn bar() -> %i32 {
7 return 13;
8}
9
10pub fn baz() -> %i32 {
11 const y = foo() %% 1234;
12 return y + 1;
13}
test/cases3/atomics.zig created+21
......@@ -0,0 +1,21 @@
1fn cmpxchg() {
2 @setFnTest(this);
3
4 var x: i32 = 1234;
5 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
6 assert(x == 5678);
7}
8
9fn fence() {
10 @setFnTest(this);
11
12 var x: i32 = 1234;
13 @fence(AtomicOrder.SeqCst);
14 x = 5678;
15}
16
17// TODO const assert = @import("std").debug.assert;
18fn assert(ok: bool) {
19 if (!ok)
20 @unreachable();
21}
test/cases3/disabled_export.zig created+8
......@@ -0,0 +1,8 @@
1export fn disabledExternFn() {
2 @setFnVisible(this, false);
3}
4
5fn callDisabledExternFn() {
6 @setFnTest(this);
7 disabledExternFn();
8}
test/cases3/empty_fn_with_comments.zig created+6
......@@ -0,0 +1,6 @@
1// normal comment
2/// this is a documentation comment
3/// doc comment line 2
4fn emptyFunctionWithComments() {
5 @setFnTest(this);
6}
test/cases3/error.zig created+25
......@@ -0,0 +1,25 @@
1pub fn foo() -> %i32 {
2 const x = %return bar();
3 return x + 1
4}
5
6pub fn bar() -> %i32 {
7 return 13;
8}
9
10pub fn baz() -> %i32 {
11 const y = foo() %% 1234;
12 return y + 1;
13}
14
15fn errorWrapping() {
16 @setFnTest(this);
17
18 assert(%%baz() == 15);
19}
20
21// TODO const assert = @import("std").debug.assert;
22fn assert(ok: bool) {
23 if (!ok)
24 @unreachable();
25}
test/cases3/for.zig created+14
......@@ -0,0 +1,14 @@
1fn continueInForLoop() {
2 @setFnTest(this);
3
4 const array = []i32 {1, 2, 3, 4, 5};
5 var sum : i32 = 0;
6 for (array) |x| {
7 sum += x;
8 if (x < 3) {
9 continue;
10 }
11 break;
12 }
13 if (sum != 6) @unreachable()
14}
test/cases3/generics.zig created+49
......@@ -0,0 +1,49 @@
1fn simpleGenericFn() {
2 @setFnTest(this);
3
4 assert(max(i32, 3, -1) == 3);
5 assert(max(f32, 0.123, 0.456) == 0.456);
6 assert(add(2, 3) == 5);
7}
8
9fn max(inline T: type, a: T, b: T) -> T {
10 return if (a > b) a else b;
11}
12
13fn add(inline a: i32, b: i32) -> i32 {
14 return @staticEval(a) + b;
15}
16
17const the_max = max(u32, 1234, 5678);
18fn compileTimeGenericEval() {
19 @setFnTest(this);
20 assert(the_max == 5678);
21}
22
23fn gimmeTheBigOne(a: u32, b: u32) -> u32 {
24 max(u32, a, b)
25}
26
27fn shouldCallSameInstance(a: u32, b: u32) -> u32 {
28 max(u32, a, b)
29}
30
31fn sameButWithFloats(a: f64, b: f64) -> f64 {
32 max(f64, a, b)
33}
34
35fn fnWithInlineArgs() {
36 @setFnTest(this);
37
38 assert(gimmeTheBigOne(1234, 5678) == 5678);
39 assert(shouldCallSameInstance(34, 12) == 34);
40 assert(sameButWithFloats(0.43, 0.49) == 0.49);
41}
42
43
44// TODO const assert = @import("std").debug.assert;
45fn assert(ok: bool) {
46 if (!ok)
47 @unreachable();
48}
49
test/cases3/goto.zig created+45
......@@ -0,0 +1,45 @@
1fn gotoAndLabels() {
2 @setFnTest(this);
3
4 gotoLoop();
5 assert(goto_counter == 10);
6}
7fn gotoLoop() {
8 var i: i32 = 0;
9 goto cond;
10loop:
11 i += 1;
12cond:
13 if (!(i < 10)) goto end;
14 goto_counter += 1;
15 goto loop;
16end:
17}
18var goto_counter: i32 = 0;
19
20
21
22fn gotoLeaveDeferScope() {
23 @setFnTest(this);
24
25 testGotoLeaveDeferScope(true);
26}
27fn testGotoLeaveDeferScope(b: bool) {
28 var it_worked = false;
29
30 goto entry;
31exit:
32 if (it_worked) {
33 return;
34 }
35 @unreachable();
36entry:
37 defer it_worked = true;
38 if (b) goto exit;
39}
40
41// TODO const assert = @import("std").debug.assert;
42fn assert(ok: bool) {
43 if (!ok)
44 @unreachable();
45}
test/cases3/inlined_loop.zig created+16
......@@ -0,0 +1,16 @@
1
2fn inlinedLoop() {
3 @setFnTest(this);
4
5 inline var i = 0;
6 inline var sum = 0;
7 inline while (i <= 5; i += 1)
8 sum += i;
9 assert(sum == 15);
10}
11
12// TODO const assert = @import("std").debug.assert;
13fn assert(ok: bool) {
14 if (!ok)
15 @unreachable();
16}
test/cases3/math.zig created+58
......@@ -0,0 +1,58 @@
1fn exactDivision() {
2 @setFnTest(this);
3
4 assert(divExact(55, 11) == 5);
5}
6fn divExact(a: u32, b: u32) -> u32 {
7 @divExact(a, b)
8}
9
10fn floatDivision() {
11 @setFnTest(this);
12
13 assert(fdiv32(12.0, 3.0) == 4.0);
14}
15fn fdiv32(a: f32, b: f32) -> f32 {
16 a / b
17}
18
19fn overflowIntrinsics() {
20 @setFnTest(this);
21
22 var result: u8 = undefined;
23 assert(@addWithOverflow(u8, 250, 100, &result));
24 assert(!@addWithOverflow(u8, 100, 150, &result));
25 assert(result == 250);
26}
27
28fn shlWithOverflow() {
29 @setFnTest(this);
30
31 var result: u16 = undefined;
32 assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
33 assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
34 assert(result == 0b1011111111111100);
35}
36
37fn countLeadingZeroes() {
38 @setFnTest(this);
39
40 assert(@clz(u8(0b00001010)) == 4);
41 assert(@clz(u8(0b10001010)) == 0);
42 assert(@clz(u8(0b00000000)) == 8);
43}
44
45fn countTrailingZeroes() {
46 @setFnTest(this);
47
48 assert(@ctz(u8(0b10100000)) == 5);
49 assert(@ctz(u8(0b10001010)) == 1);
50 assert(@ctz(u8(0b00000000)) == 8);
51}
52
53// TODO const assert = @import("std").debug.assert;
54fn assert(ok: bool) {
55 if (!ok)
56 @unreachable();
57}
58
test/cases3/misc.zig created+111
......@@ -0,0 +1,111 @@
1fn intTypeBuiltin() {
2 @setFnTest(this);
3
4 assert(@intType(true, 8) == i8);
5 assert(@intType(true, 16) == i16);
6 assert(@intType(true, 32) == i32);
7 assert(@intType(true, 64) == i64);
8
9 assert(@intType(false, 8) == u8);
10 assert(@intType(false, 16) == u16);
11 assert(@intType(false, 32) == u32);
12 assert(@intType(false, 64) == u64);
13
14 assert(i8.bit_count == 8);
15 assert(i16.bit_count == 16);
16 assert(i32.bit_count == 32);
17 assert(i64.bit_count == 64);
18
19 assert(i8.is_signed);
20 assert(i16.is_signed);
21 assert(i32.is_signed);
22 assert(i64.is_signed);
23 assert(isize.is_signed);
24
25 assert(!u8.is_signed);
26 assert(!u16.is_signed);
27 assert(!u32.is_signed);
28 assert(!u64.is_signed);
29 assert(!usize.is_signed);
30}
31
32fn minValueAndMaxValue() {
33 @setFnTest(this);
34
35 assert(@maxValue(u8) == 255);
36 assert(@maxValue(u16) == 65535);
37 assert(@maxValue(u32) == 4294967295);
38 assert(@maxValue(u64) == 18446744073709551615);
39
40 assert(@maxValue(i8) == 127);
41 assert(@maxValue(i16) == 32767);
42 assert(@maxValue(i32) == 2147483647);
43 assert(@maxValue(i64) == 9223372036854775807);
44
45 assert(@minValue(u8) == 0);
46 assert(@minValue(u16) == 0);
47 assert(@minValue(u32) == 0);
48 assert(@minValue(u64) == 0);
49
50 assert(@minValue(i8) == -128);
51 assert(@minValue(i16) == -32768);
52 assert(@minValue(i32) == -2147483648);
53 assert(@minValue(i64) == -9223372036854775808);
54}
55
56fn shortCircuit() {
57 @setFnTest(this);
58
59 var hit_1 = false;
60 var hit_2 = false;
61 var hit_3 = false;
62 var hit_4 = false;
63
64 if (true || {assert(false); false}) {
65 hit_1 = true;
66 }
67 if (false || { hit_2 = true; false }) {
68 assert(false);
69 }
70
71 if (true && { hit_3 = true; false }) {
72 assert(false);
73 }
74 if (false && {assert(false); false}) {
75 assert(false);
76 } else {
77 hit_4 = true;
78 }
79 assert(hit_1);
80 assert(hit_2);
81 assert(hit_3);
82 assert(hit_4);
83}
84
85fn truncate() {
86 @setFnTest(this);
87
88 assert(testTruncate(0x10fd) == 0xfd);
89}
90fn testTruncate(x: u32) -> u8 {
91 @truncate(u8, x)
92}
93
94fn assignToIfVarPtr() {
95 @setFnTest(this);
96
97 var maybe_bool: ?bool = true;
98
99 if (const *b ?= maybe_bool) {
100 *b = false;
101 }
102
103 assert(??maybe_bool == false);
104}
105
106
107// TODO const assert = @import("std").debug.assert;
108fn assert(ok: bool) {
109 if (!ok)
110 @unreachable();
111}
test/cases3/switch.zig created+52
......@@ -0,0 +1,52 @@
1fn switchWithNumbers() {
2 @setFnTest(this);
3
4 testSwitchWithNumbers(13);
5}
6
7fn testSwitchWithNumbers(x: u32) {
8 const result = switch (x) {
9 1, 2, 3, 4 ... 8 => false,
10 13 => true,
11 else => false,
12 };
13 assert(result);
14}
15
16fn switchWithAllRanges() {
17 @setFnTest(this);
18
19 assert(testSwitchWithAllRanges(50, 3) == 1);
20 assert(testSwitchWithAllRanges(101, 0) == 2);
21 assert(testSwitchWithAllRanges(300, 5) == 3);
22 assert(testSwitchWithAllRanges(301, 6) == 6);
23}
24
25fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
26 switch (x) {
27 0 ... 100 => 1,
28 101 ... 200 => 2,
29 201 ... 300 => 3,
30 else => y,
31 }
32}
33
34fn inlineSwitch() {
35 @setFnTest(this);
36
37 const x = 3 + 4;
38 const result = inline switch (x) {
39 3 => 10,
40 4 => 11,
41 5, 6 => 12,
42 7, 8 => 13,
43 else => 14,
44 };
45 assert(result + 1 == 14);
46}
47
48// TODO const assert = @import("std").debug.assert;
49fn assert(ok: bool) {
50 if (!ok)
51 @unreachable();
52}
test/self_hosted.zig-259
......@@ -2,7 +2,6 @@ const std = @import("std");
22const assert = std.debug.assert;
33const str = std.str;
44const cstr = std.cstr;
5// TODO '_' identifier for unused variable bindings
65const test_return_type_type = @import("cases/return_type_type.zig");
76const test_zeroes = @import("cases/zeroes.zig");
87const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig");
......@@ -16,12 +15,6 @@ const test_enum_with_members = @import("cases/enum_with_members.zig");
1615const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig");
1716const test_this = @import("cases/this.zig");
1817
19// normal comment
20/// this is a documentation comment
21/// doc comment line 2
22fn emptyFunctionWithComments() {
23 @setFnTest(this, true);
24}
2518
2619
2720
......@@ -129,41 +122,6 @@ fn getArrayLen(a: []u32) -> usize {
129122 a.len
130123}
131124
132fn shortCircuit() {
133 @setFnTest(this, true);
134
135 var hit_1 = false;
136 var hit_2 = false;
137 var hit_3 = false;
138 var hit_4 = false;
139
140 if (true || {assertRuntime(false); false}) {
141 hit_1 = true;
142 }
143 if (false || { hit_2 = true; false }) {
144 assertRuntime(false);
145 }
146
147 if (true && { hit_3 = true; false }) {
148 assertRuntime(false);
149 }
150 if (false && {assertRuntime(false); false}) {
151 assertRuntime(false);
152 } else {
153 hit_4 = true;
154 }
155 assert(hit_1);
156 assert(hit_2);
157 assert(hit_3);
158 assert(hit_4);
159}
160
161fn assertRuntime(b: bool) {
162 @setFnStaticEval(this, false);
163
164 if (!b) @unreachable()
165}
166
167125fn modifyOperators() {
168126 @setFnTest(this, true);
169127
......@@ -505,22 +463,6 @@ enum AnEnumWithPayload {
505463}
506464
507465
508fn continueInForLoop() {
509 @setFnTest(this, true);
510
511 const array = []i32 {1, 2, 3, 4, 5};
512 var sum : i32 = 0;
513 for (array) |x| {
514 sum += x;
515 if (x < 3) {
516 continue;
517 }
518 break;
519 }
520 if (sum != 6) @unreachable()
521}
522
523
524466fn castBoolToInt() {
525467 @setFnTest(this, true);
526468
......@@ -727,23 +669,6 @@ struct ArrayDotLenConstExpr {
727669const some_array = []u8 {0, 1, 2, 3};
728670
729671
730fn countLeadingZeroes() {
731 @setFnTest(this, true);
732
733 assert(@clz(u8, 0b00001010) == 4);
734 assert(@clz(u8, 0b10001010) == 0);
735 assert(@clz(u8, 0b00000000) == 8);
736}
737
738fn countTrailingZeroes() {
739 @setFnTest(this, true);
740
741 assert(@ctz(u8, 0b10100000) == 5);
742 assert(@ctz(u8, 0b10001010) == 1);
743 assert(@ctz(u8, 0b00000000) == 8);
744}
745
746
747672fn multilineString() {
748673 @setFnTest(this, true);
749674
......@@ -770,23 +695,6 @@ fn multilineCString() {
770695
771696
772697
773fn simpleGenericFn() {
774 @setFnTest(this, true);
775
776 assert(max(i32, 3, -1) == 3);
777 assert(max(f32, 0.123, 0.456) == 0.456);
778 assert(add(2, 3) == 5);
779}
780
781fn max(inline T: type, a: T, b: T) -> T {
782 return if (a > b) a else b;
783}
784
785fn add(inline a: i32, b: i32) -> i32 {
786 return @constEval(a) + b;
787}
788
789
790698fn constantEqualFunctionPointers() {
791699 @setFnTest(this, true);
792700
......@@ -838,49 +746,6 @@ fn errorNameString() {
838746}
839747
840748
841fn gotoAndLabels() {
842 @setFnTest(this, true);
843
844 gotoLoop();
845 assert(goto_counter == 10);
846}
847fn gotoLoop() {
848 var i: i32 = 0;
849 goto cond;
850loop:
851 i += 1;
852cond:
853 if (!(i < 10)) goto end;
854 goto_counter += 1;
855 goto loop;
856end:
857}
858var goto_counter: i32 = 0;
859
860
861
862fn gotoLeaveDeferScope() {
863 @setFnTest(this, true);
864
865 testGotoLeaveDeferScope(true);
866}
867fn testGotoLeaveDeferScope(b: bool) {
868 @setFnStaticEval(this, false);
869
870 var it_worked = false;
871
872 goto entry;
873exit:
874 if (it_worked) {
875 return;
876 }
877 @unreachable();
878entry:
879 defer it_worked = true;
880 if (b) goto exit;
881}
882
883
884749fn castUndefined() {
885750 @setFnTest(this, true);
886751
......@@ -1112,40 +977,6 @@ fn constantExpressions() {
1112977const array_size : u8 = 20;
1113978
1114979
1115fn minValueAndMaxValue() {
1116 @setFnTest(this, true);
1117
1118 assert(@maxValue(u8) == 255);
1119 assert(@maxValue(u16) == 65535);
1120 assert(@maxValue(u32) == 4294967295);
1121 assert(@maxValue(u64) == 18446744073709551615);
1122
1123 assert(@maxValue(i8) == 127);
1124 assert(@maxValue(i16) == 32767);
1125 assert(@maxValue(i32) == 2147483647);
1126 assert(@maxValue(i64) == 9223372036854775807);
1127
1128 assert(@minValue(u8) == 0);
1129 assert(@minValue(u16) == 0);
1130 assert(@minValue(u32) == 0);
1131 assert(@minValue(u64) == 0);
1132
1133 assert(@minValue(i8) == -128);
1134 assert(@minValue(i16) == -32768);
1135 assert(@minValue(i32) == -2147483648);
1136 assert(@minValue(i64) == -9223372036854775808);
1137}
1138
1139fn overflowIntrinsics() {
1140 @setFnTest(this, true);
1141
1142 var result: u8 = undefined;
1143 assert(@addWithOverflow(u8, 250, 100, &result));
1144 assert(!@addWithOverflow(u8, 100, 150, &result));
1145 assert(result == 250);
1146}
1147
1148
1149980fn nestedArrays() {
1150981 @setFnTest(this, true);
1151982
......@@ -1554,22 +1385,6 @@ fn assignToIfVarPtr() {
15541385 assert(??maybe_bool == false);
15551386}
15561387
1557fn cmpxchg() {
1558 @setFnTest(this, true);
1559
1560 var x: i32 = 1234;
1561 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
1562 assert(x == 5678);
1563}
1564
1565fn fence() {
1566 @setFnTest(this, true);
1567
1568 var x: i32 = 1234;
1569 @fence(AtomicOrder.SeqCst);
1570 x = 5678;
1571}
1572
15731388fn unsignedWrapping() {
15741389 @setFnTest(this, true);
15751390
......@@ -1648,15 +1463,6 @@ fn testShlWrappingNoeval(x: u16) {
16481463 assert(shifted == 65534);
16491464}
16501465
1651fn shlWithOverflow() {
1652 @setFnTest(this, true);
1653
1654 var result: u16 = undefined;
1655 assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
1656 assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
1657 assert(result == 0b1011111111111100);
1658}
1659
16601466fn cStringConcatenation() {
16611467 @setFnTest(this, true);
16621468
......@@ -1709,28 +1515,6 @@ fn castSliceToU8Slice() {
17091515 assert(bytes[11] == @maxValue(u8));
17101516}
17111517
1712fn floatDivision() {
1713 @setFnTest(this, true);
1714
1715 assert(fdiv32(12.0, 3.0) == 4.0);
1716}
1717fn fdiv32(a: f32, b: f32) -> f32 {
1718 @setFnStaticEval(this, false);
1719
1720 a / b
1721}
1722
1723fn exactDivision() {
1724 @setFnTest(this, true);
1725
1726 assert(divExact(55, 11) == 5);
1727}
1728fn divExact(a: u32, b: u32) -> u32 {
1729 @setFnStaticEval(this, false);
1730
1731 @divExact(a, b)
1732}
1733
17341518fn nullLiteralOutsideFunction() {
17351519 @setFnTest(this, true);
17361520
......@@ -1744,17 +1528,6 @@ const here_is_a_null_literal = SillyStruct {
17441528 .context = null,
17451529};
17461530
1747fn truncate() {
1748 @setFnTest(this, true);
1749
1750 assert(testTruncate(0x10fd) == 0xfd);
1751}
1752fn testTruncate(x: u32) -> u8 {
1753 @setFnStaticEval(this, false);
1754
1755 @truncate(u8, x)
1756}
1757
17581531fn constDeclsInStruct() {
17591532 @setFnTest(this, true);
17601533
......@@ -1794,38 +1567,6 @@ struct DivResult {
17941567 remainder: u64,
17951568}
17961569
1797fn intTypeBuiltin() {
1798 @setFnTest(this, true);
1799
1800 assert(@intType(true, 8) == i8);
1801 assert(@intType(true, 16) == i16);
1802 assert(@intType(true, 32) == i32);
1803 assert(@intType(true, 64) == i64);
1804
1805 assert(@intType(false, 8) == u8);
1806 assert(@intType(false, 16) == u16);
1807 assert(@intType(false, 32) == u32);
1808 assert(@intType(false, 64) == u64);
1809
1810 assert(i8.bit_count == 8);
1811 assert(i16.bit_count == 16);
1812 assert(i32.bit_count == 32);
1813 assert(i64.bit_count == 64);
1814
1815 assert(i8.is_signed);
1816 assert(i16.is_signed);
1817 assert(i32.is_signed);
1818 assert(i64.is_signed);
1819 assert(isize.is_signed);
1820
1821 assert(!u8.is_signed);
1822 assert(!u16.is_signed);
1823 assert(!u32.is_signed);
1824 assert(!u64.is_signed);
1825 assert(!usize.is_signed);
1826
1827}
1828
18291570fn intToEnum() {
18301571 @setFnTest(this, true);
18311572
test/self_hosted2.zig-338
......@@ -1,91 +1,10 @@
11const case_namespace_fn_call = @import("cases/namespace_fn_call.zig");
2const case_err_wrapping = @import("cases/err_wrapping.zig");
32
4pub const SYS_write = 1;
5pub const SYS_exit = 60;
6pub const stdout_fileno = 1;
7
8// normal comment
9/// this is a documentation comment
10/// doc comment line 2
11fn emptyFunctionWithComments() {
12}
13
14export fn disabledExternFn() {
15 @setFnVisible(this, false);
16}
17
18fn inlinedLoop() {
19 inline var i = 0;
20 inline var sum = 0;
21 inline while (i <= 5; i += 1)
22 sum += i;
23 assert(sum == 15);
24}
25
26fn switchWithNumbers() {
27 testSwitchWithNumbers(13);
28}
29
30fn testSwitchWithNumbers(x: u32) {
31 const result = switch (x) {
32 1, 2, 3, 4 ... 8 => false,
33 13 => true,
34 else => false,
35 };
36 assert(result);
37}
38
39fn switchWithAllRanges() {
40 assert(testSwitchWithAllRanges(50, 3) == 1);
41 assert(testSwitchWithAllRanges(101, 0) == 2);
42 assert(testSwitchWithAllRanges(300, 5) == 3);
43 assert(testSwitchWithAllRanges(301, 6) == 6);
44}
45
46fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
47 switch (x) {
48 0 ... 100 => 1,
49 101 ... 200 => 2,
50 201 ... 300 => 3,
51 else => y,
52 }
53}
54
55fn testInlineSwitch() {
56 const x = 3 + 4;
57 const result = inline switch (x) {
58 3 => 10,
59 4 => 11,
60 5, 6 => 12,
61 7, 8 => 13,
62 else => 14,
63 };
64 assert(result + 1 == 14);
65}
663
674fn testNamespaceFnCall() {
685 assert(case_namespace_fn_call.foo() == 1234);
696}
707
71fn gotoAndLabels() {
72 gotoLoop();
73 assert(goto_counter == 10);
74}
75fn gotoLoop() {
76 var i: i32 = 0;
77 goto cond;
78loop:
79 i += 1;
80cond:
81 if (!(i < 10)) goto end;
82 goto_counter += 1;
83 goto loop;
84end:
85}
86var goto_counter: i32 = 0;
87
88
898
909const FooA = struct {
9110 fn add(a: i32, b: i32) -> i32 { a + b }
......@@ -112,89 +31,6 @@ fn testCompileTimeFib() {
11231 assert(fib_7 == 13);
11332}
11433
115fn max(inline T: type, a: T, b: T) -> T {
116 if (a > b) a else b
117}
118const the_max = max(u32, 1234, 5678);
119
120fn testCompileTimeGenericEval() {
121 assert(the_max == 5678);
122}
123
124fn gimmeTheBigOne(a: u32, b: u32) -> u32 {
125 max(u32, a, b)
126}
127
128fn shouldCallSameInstance(a: u32, b: u32) -> u32 {
129 max(u32, a, b)
130}
131
132fn sameButWithFloats(a: f64, b: f64) -> f64 {
133 max(f64, a, b)
134}
135
136fn testFnWithInlineArgs() {
137 assert(gimmeTheBigOne(1234, 5678) == 5678);
138 assert(shouldCallSameInstance(34, 12) == 34);
139 assert(sameButWithFloats(0.43, 0.49) == 0.49);
140}
141
142
143fn testContinueInForLoop() {
144 const array = []i32 {1, 2, 3, 4, 5};
145 var sum : i32 = 0;
146 for (array) |x| {
147 sum += x;
148 if (x < 3) {
149 continue;
150 }
151 break;
152 }
153 assert(sum == 6);
154}
155
156fn shortCircuit() {
157 var hit_1 = false;
158 var hit_2 = false;
159 var hit_3 = false;
160 var hit_4 = false;
161
162 if (true || {assert(false); false}) {
163 hit_1 = true;
164 }
165 if (false || { hit_2 = true; false }) {
166 assert(false);
167 }
168
169 if (true && { hit_3 = true; false }) {
170 assert(false);
171 }
172 if (false && {assert(false); false}) {
173 assert(false);
174 } else {
175 hit_4 = true;
176 }
177 assert(hit_1);
178 assert(hit_2);
179 assert(hit_3);
180 assert(hit_4);
181}
182
183fn testGotoLeaveDeferScope(b: bool) {
184 var it_worked = false;
185
186 goto entry;
187exit:
188 if (it_worked) {
189 return;
190 }
191 @unreachable();
192entry:
193 defer it_worked = true;
194 if (it_worked) @unreachable();
195 if (b) goto exit;
196}
197
19834fn unwrapAndAddOne(blah: ?i32) -> i32 {
19935 return ??blah + 1;
20036}
......@@ -217,28 +53,6 @@ fn testInlineVarsAgain() {
21753 assert(gimme1or2(false) == 2);
21854}
21955
220fn testMinValueAndMaxValue() {
221 assert(@maxValue(u8) == 255);
222 assert(@maxValue(u16) == 65535);
223 assert(@maxValue(u32) == 4294967295);
224 assert(@maxValue(u64) == 18446744073709551615);
225
226 assert(@maxValue(i8) == 127);
227 assert(@maxValue(i16) == 32767);
228 assert(@maxValue(i32) == 2147483647);
229 assert(@maxValue(i64) == 9223372036854775807);
230
231 assert(@minValue(u8) == 0);
232 assert(@minValue(u16) == 0);
233 assert(@minValue(u32) == 0);
234 assert(@minValue(u64) == 0);
235
236 assert(@minValue(i8) == -128);
237 assert(@minValue(i16) == -32768);
238 assert(@minValue(i32) == -2147483648);
239 assert(@minValue(i64) == -9223372036854775808);
240}
241
24256fn first4KeysOfHomeRow() -> []const u8 {
24357 "aoeu"
24458}
......@@ -268,111 +82,8 @@ fn testErrorName() {
26882 assert(memeql(@errorName(error.ItBroke), "ItBroke"));
26983}
27084
271//error One;
272//fn getAnErrorValue (b: bool) -> %i32 {
273// const result = if (b) error.One else i32(1234);
274// return result;
275//}
276
277fn cmpxchg() {
278 var x: i32 = 1234;
279 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
280 assert(x == 5678);
281}
282
283fn fence() {
284 var x: i32 = 1234;
285 @fence(AtomicOrder.SeqCst);
286 x = 5678;
287}
288
289fn exactDivision() {
290 assert(divExact(55, 11) == 5);
291}
292fn divExact(a: u32, b: u32) -> u32 {
293 @divExact(a, b)
294}
295
296fn truncate() {
297 assert(testTruncate(0x10fd) == 0xfd);
298}
299fn testTruncate(x: u32) -> u8 {
300 @truncate(u8, x)
301}
302
303fn intTypeBuiltin() {
304 assert(@intType(true, 8) == i8);
305 assert(@intType(true, 16) == i16);
306 assert(@intType(true, 32) == i32);
307 assert(@intType(true, 64) == i64);
308
309 assert(@intType(false, 8) == u8);
310 assert(@intType(false, 16) == u16);
311 assert(@intType(false, 32) == u32);
312 assert(@intType(false, 64) == u64);
313
314 assert(i8.bit_count == 8);
315 assert(i16.bit_count == 16);
316 assert(i32.bit_count == 32);
317 assert(i64.bit_count == 64);
318
319 assert(i8.is_signed);
320 assert(i16.is_signed);
321 assert(i32.is_signed);
322 assert(i64.is_signed);
323 assert(isize.is_signed);
324
325 assert(!u8.is_signed);
326 assert(!u16.is_signed);
327 assert(!u32.is_signed);
328 assert(!u64.is_signed);
329 assert(!usize.is_signed);
330
331}
332
333fn overflowIntrinsics() {
334 var result: u8 = undefined;
335 assert(@addWithOverflow(u8, 250, 100, &result));
336 assert(!@addWithOverflow(u8, 100, 150, &result));
337 assert(result == 250);
338}
339
340fn shlWithOverflow() {
341 var result: u16 = undefined;
342 assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
343 assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
344 assert(result == 0b1011111111111100);
345}
346
347fn assignToIfVarPtr() {
348
349 var maybe_bool: ?bool = true;
350
351 if (const *b ?= maybe_bool) {
352 *b = false;
353 }
354
355 assert(??maybe_bool == false);
356}
357
358fn errorWrapping() {
359 assert(%%case_err_wrapping.baz() == 15);
360}
361
362fn assert(ok: bool) {
363 if (!ok)
364 @unreachable();
365}
366
36785fn runAllTests() {
368 emptyFunctionWithComments();
369 disabledExternFn();
370 inlinedLoop();
371 switchWithNumbers();
372 switchWithAllRanges();
373 testInlineSwitch();
37486 testNamespaceFnCall();
375 gotoAndLabels();
37687 testStructStatic();
37788 testStaticFnEval();
37889 testCompileTimeFib();
......@@ -380,58 +91,9 @@ fn runAllTests() {
38091 testFnWithInlineArgs();
38192 testContinueInForLoop();
38293 shortCircuit();
383 testGotoLeaveDeferScope(true);
38494 testStaticAddOne();
38595 testInlineVarsAgain();
38696 testMinValueAndMaxValue();
38797 testReturnStringFromFunction();
38898 testErrorName();
389 cmpxchg();
390 fence();
391 exactDivision();
392 truncate();
393 intTypeBuiltin();
394 overflowIntrinsics();
395 shlWithOverflow();
396 assignToIfVarPtr();
397 errorWrapping();
398}
399
400export nakedcc fn _start() -> unreachable {
401 myMain();
402}
403
404fn myMain() -> unreachable {
405 runAllTests();
406 const text = "OK\n";
407 write(stdout_fileno, &text[0], text.len);
408 exit(0);
409}
410
411pub inline fn syscall1(number: usize, arg1: usize) -> usize {
412 asm volatile ("syscall"
413 : [ret] "={rax}" (-> usize)
414 : [number] "{rax}" (number),
415 [arg1] "{rdi}" (arg1)
416 : "rcx", "r11")
417}
418
419pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
420 asm volatile ("syscall"
421 : [ret] "={rax}" (-> usize)
422 : [number] "{rax}" (number),
423 [arg1] "{rdi}" (arg1),
424 [arg2] "{rsi}" (arg2),
425 [arg3] "{rdx}" (arg3)
426 : "rcx", "r11")
427}
428
429pub fn write(fd: i32, buf: &const u8, count: usize) -> usize {
430 syscall3(SYS_write, usize(fd), usize(buf), count)
43199}
432
433pub fn exit(status: i32) -> unreachable {
434 syscall1(SYS_exit, usize(status));
435 @unreachable()
436}
437
test/self_hosted3.zig created+12
......@@ -0,0 +1,12 @@
1// TODO '_' identifier for unused variable bindings
2const test_empty_fn_with_comments = @import("cases3/empty_fn_with_comments.zig");
3const test_disabled_export = @import("cases3/disabled_export.zig");
4const test_inlined_loop = @import("cases3/inlined_loop.zig");
5const test_misc = @import("cases3/misc.zig");
6const test_switch = @import("cases3/switch.zig");
7const test_error = @import("cases3/error.zig");
8const test_goto = @import("cases3/goto.zig");
9const test_atomics = @import("cases3/atomics.zig");
10const test_for = @import("cases3/for.zig");
11const test_math = @import("cases3/math.zig");
12const test_generics = @import("cases3/generics.zig");