authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 00:12:27-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 00:12:27-05:00
log56cc2e2b24f50975591de02e8a556eee4ac45bf7
tree623cb79aafad44459e9d1ad590f9f218331864eb
parentd544672ed4cb8d8054665c9491d019dabac454e7

migrate all the temporary tests to new test file


10 files changed, 153 insertions(+), 118 deletions(-)

test/cases/namespace_fn_call.zig deleted-1
...@@ -1 +0,0 @@
1pub fn foo() -> i32 { 1234 }
test/cases3/error.zig+26
...@@ -18,8 +18,34 @@ fn errorWrapping() {...@@ -18,8 +18,34 @@ fn errorWrapping() {
18 assert(%%baz() == 15);18 assert(%%baz() == 15);
19}19}
2020
21error ItBroke;
22fn gimmeItBroke() -> []const u8 {
23 @errorName(error.ItBroke)
24}
25
26fn errorName() {
27 @setFnTest(this);
28 assert(memeql(@errorName(error.ItBroke), "ItBroke"));
29}
30
31
21// TODO const assert = @import("std").debug.assert;32// TODO const assert = @import("std").debug.assert;
22fn assert(ok: bool) {33fn assert(ok: bool) {
23 if (!ok)34 if (!ok)
24 @unreachable();35 @unreachable();
25}36}
37
38// TODO import from std.str
39pub fn memeql(a: []const u8, b: []const u8) -> bool {
40 sliceEql(u8, a, b)
41}
42
43// TODO import from std.str
44pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
45 if (a.len != b.len) return false;
46 for (a) |item, index| {
47 if (b[index] != item) return false;
48 }
49 return true;
50}
51
test/cases3/eval.zig created+49
...@@ -0,0 +1,49 @@
1fn fib(x: i32) -> i32 {
2 if (x < 2) x else fib(x - 1) + fib(x - 2)
3}
4const fib_7 = fib(7);
5fn compileTimeFib() {
6 @setFnTest(this);
7 assert(fib_7 == 13);
8}
9
10
11fn unwrapAndAddOne(blah: ?i32) -> i32 {
12 return ??blah + 1;
13}
14const should_be_1235 = unwrapAndAddOne(1234);
15fn testStaticAddOne() {
16 @setFnTest(this);
17 assert(should_be_1235 == 1235);
18}
19
20fn inlinedLoop() {
21 @setFnTest(this);
22
23 inline var i = 0;
24 inline var sum = 0;
25 inline while (i <= 5; i += 1)
26 sum += i;
27 assert(sum == 15);
28}
29
30fn gimme1or2(inline a: bool) -> i32 {
31 const x: i32 = 1;
32 const y: i32 = 2;
33 inline var z: i32 = if (a) x else y;
34 return z;
35}
36fn inlineVariableGetsResultOfConstIf() {
37 @setFnTest(this);
38 assert(gimme1or2(true) == 1);
39 assert(gimme1or2(false) == 2);
40}
41
42
43
44// TODO const assert = @import("std").debug.assert;
45fn assert(ok: bool) {
46 if (!ok)
47 @unreachable();
48}
49
test/cases3/generics.zig-11
...@@ -40,17 +40,6 @@ fn fnWithInlineArgs() {...@@ -40,17 +40,6 @@ fn fnWithInlineArgs() {
40 assert(sameButWithFloats(0.43, 0.49) == 0.49);40 assert(sameButWithFloats(0.43, 0.49) == 0.49);
41}41}
4242
43fn inlinedLoop() {
44 @setFnTest(this);
45
46 inline var i = 0;
47 inline var sum = 0;
48 inline while (i <= 5; i += 1)
49 sum += i;
50 assert(sum == 15);
51}
52
53
54// TODO const assert = @import("std").debug.assert;43// TODO const assert = @import("std").debug.assert;
55fn assert(ok: bool) {44fn assert(ok: bool) {
56 if (!ok)45 if (!ok)
test/cases3/import.zig created+13
...@@ -0,0 +1,13 @@
1const a_namespace = @import("cases3/import/a_namespace.zig");
2
3fn callFnViaNamespaceLookup() {
4 @setFnTest(this);
5
6 assert(a_namespace.foo() == 1234);
7}
8
9// TODO const assert = @import("std").debug.assert;
10fn assert(ok: bool) {
11 if (!ok)
12 @unreachable();
13}
test/cases3/import/a_namespace.zig created+1
...@@ -0,0 +1 @@
1pub fn foo() -> i32 { 1234 }
test/cases3/misc.zig+24
...@@ -120,6 +120,30 @@ fn assignToIfVarPtr() {...@@ -120,6 +120,30 @@ fn assignToIfVarPtr() {
120 assert(??maybe_bool == false);120 assert(??maybe_bool == false);
121}121}
122122
123fn first4KeysOfHomeRow() -> []const u8 {
124 "aoeu"
125}
126
127fn ReturnStringFromFunction() {
128 @setFnTest(this);
129
130 assert(memeql(first4KeysOfHomeRow(), "aoeu"));
131}
132
133// TODO import from std.str
134pub fn memeql(a: []const u8, b: []const u8) -> bool {
135 sliceEql(u8, a, b)
136}
137
138// TODO import from std.str
139pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
140 if (a.len != b.len) return false;
141 for (a) |item, index| {
142 if (b[index] != item) return false;
143 }
144 return true;
145}
146
123147
124// TODO const assert = @import("std").debug.assert;148// TODO const assert = @import("std").debug.assert;
125fn assert(ok: bool) {149fn assert(ok: bool) {
test/cases3/struct.zig created+30
...@@ -0,0 +1,30 @@
1const StructWithNoFields = struct {
2 fn add(a: i32, b: i32) -> i32 { a + b }
3};
4const empty_global_instance = StructWithNoFields {};
5
6fn callStructStaticMethod() {
7 @setFnTest(this);
8 const result = StructWithNoFields.add(3, 4);
9 assert(result == 7);
10}
11
12fn returnEmptyStructInstance() -> StructWithNoFields {
13 @setFnTest(this);
14 return empty_global_instance;
15}
16
17const should_be_11 = StructWithNoFields.add(5, 6);
18
19fn invokeStaticMethodInGlobalScope() {
20 @setFnTest(this);
21 assert(should_be_11 == 11);
22}
23
24
25
26// TODO const assert = @import("std").debug.assert;
27fn assert(ok: bool) {
28 if (!ok)
29 @unreachable();
30}
test/self_hosted2.zig deleted-99
...@@ -1,99 +0,0 @@
1const case_namespace_fn_call = @import("cases/namespace_fn_call.zig");
2
3
4fn testNamespaceFnCall() {
5 assert(case_namespace_fn_call.foo() == 1234);
6}
7
8
9const FooA = struct {
10 fn add(a: i32, b: i32) -> i32 { a + b }
11};
12const foo_a = FooA {};
13
14fn testStructStatic() {
15 const result = FooA.add(3, 4);
16 assert(result == 7);
17}
18
19const should_be_11 = FooA.add(5, 6);
20fn testStaticFnEval() {
21 assert(should_be_11 == 11);
22}
23
24fn fib(x: i32) -> i32 {
25 if (x < 2) x else fib(x - 1) + fib(x - 2)
26}
27
28const fib_7 = fib(7);
29
30fn testCompileTimeFib() {
31 assert(fib_7 == 13);
32}
33
34fn unwrapAndAddOne(blah: ?i32) -> i32 {
35 return ??blah + 1;
36}
37
38const should_be_1235 = unwrapAndAddOne(1234);
39
40fn testStaticAddOne() {
41 assert(should_be_1235 == 1235);
42}
43
44fn gimme1or2(inline a: bool) -> i32 {
45 const x: i32 = 1;
46 const y: i32 = 2;
47 inline var z: i32 = inline if (a) x else y;
48 return z;
49}
50
51fn testInlineVarsAgain() {
52 assert(gimme1or2(true) == 1);
53 assert(gimme1or2(false) == 2);
54}
55
56fn first4KeysOfHomeRow() -> []const u8 {
57 "aoeu"
58}
59
60fn testReturnStringFromFunction() {
61 assert(memeql(first4KeysOfHomeRow(), "aoeu"));
62}
63
64pub fn memeql(a: []const u8, b: []const u8) -> bool {
65 sliceEql(u8, a, b)
66}
67
68pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
69 if (a.len != b.len) return false;
70 for (a) |item, index| {
71 if (b[index] != item) return false;
72 }
73 return true;
74}
75
76error ItBroke;
77fn gimmeItBroke() -> []const u8 {
78 @errorName(error.ItBroke)
79}
80
81fn testErrorName() {
82 assert(memeql(@errorName(error.ItBroke), "ItBroke"));
83}
84
85fn runAllTests() {
86 testNamespaceFnCall();
87 testStructStatic();
88 testStaticFnEval();
89 testCompileTimeFib();
90 testCompileTimeGenericEval();
91 testFnWithInlineArgs();
92 testContinueInForLoop();
93 shortCircuit();
94 testStaticAddOne();
95 testInlineVarsAgain();
96 testMinValueAndMaxValue();
97 testReturnStringFromFunction();
98 testErrorName();
99}
test/self_hosted3.zig+10-7
...@@ -1,11 +1,14 @@...@@ -1,11 +1,14 @@
1// TODO '_' identifier for unused variable bindings1// TODO '_' identifier for unused variable bindings
2const test_misc = @import("cases3/misc.zig");
3const test_switch = @import("cases3/switch.zig");
4const test_error = @import("cases3/error.zig");
5const test_goto = @import("cases3/goto.zig");
6const test_atomics = @import("cases3/atomics.zig");2const test_atomics = @import("cases3/atomics.zig");
7const test_for = @import("cases3/for.zig");
8const test_math = @import("cases3/math.zig");
9const test_generics = @import("cases3/generics.zig");
10const test_defer = @import("cases3/defer.zig");3const test_defer = @import("cases3/defer.zig");
11const test_enum = @import("cases3/enum.zig");4const test_enum = @import("cases3/enum.zig");
5const test_error = @import("cases3/error.zig");
6const test_eval = @import("cases3/eval.zig");
7const test_for = @import("cases3/for.zig");
8const test_generics = @import("cases3/generics.zig");
9const test_goto = @import("cases3/goto.zig");
10const test_import = @import("cases3/import.zig");
11const test_math = @import("cases3/math.zig");
12const test_misc = @import("cases3/misc.zig");
13const test_struct = @import("cases3/struct.zig");
14const test_switch = @import("cases3/switch.zig");