Commit 56cc2e2b24
Changed files (9)
test/cases/namespace_fn_call.zig → test/cases3/import/a_namespace.zig
File renamed without changes
test/cases3/error.zig
@@ -18,8 +18,34 @@ fn errorWrapping() {
assert(%%baz() == 15);
}
+error ItBroke;
+fn gimmeItBroke() -> []const u8 {
+ @errorName(error.ItBroke)
+}
+
+fn errorName() {
+ @setFnTest(this);
+ assert(memeql(@errorName(error.ItBroke), "ItBroke"));
+}
+
+
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)
@unreachable();
}
+
+// TODO import from std.str
+pub fn memeql(a: []const u8, b: []const u8) -> bool {
+ sliceEql(u8, a, b)
+}
+
+// TODO import from std.str
+pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
+ if (a.len != b.len) return false;
+ for (a) |item, index| {
+ if (b[index] != item) return false;
+ }
+ return true;
+}
+
test/cases3/eval.zig
@@ -0,0 +1,49 @@
+fn fib(x: i32) -> i32 {
+ if (x < 2) x else fib(x - 1) + fib(x - 2)
+}
+const fib_7 = fib(7);
+fn compileTimeFib() {
+ @setFnTest(this);
+ assert(fib_7 == 13);
+}
+
+
+fn unwrapAndAddOne(blah: ?i32) -> i32 {
+ return ??blah + 1;
+}
+const should_be_1235 = unwrapAndAddOne(1234);
+fn testStaticAddOne() {
+ @setFnTest(this);
+ assert(should_be_1235 == 1235);
+}
+
+fn inlinedLoop() {
+ @setFnTest(this);
+
+ inline var i = 0;
+ inline var sum = 0;
+ inline while (i <= 5; i += 1)
+ sum += i;
+ assert(sum == 15);
+}
+
+fn gimme1or2(inline a: bool) -> i32 {
+ const x: i32 = 1;
+ const y: i32 = 2;
+ inline var z: i32 = if (a) x else y;
+ return z;
+}
+fn inlineVariableGetsResultOfConstIf() {
+ @setFnTest(this);
+ assert(gimme1or2(true) == 1);
+ assert(gimme1or2(false) == 2);
+}
+
+
+
+// TODO const assert = @import("std").debug.assert;
+fn assert(ok: bool) {
+ if (!ok)
+ @unreachable();
+}
+
test/cases3/generics.zig
@@ -40,17 +40,6 @@ fn fnWithInlineArgs() {
assert(sameButWithFloats(0.43, 0.49) == 0.49);
}
-fn inlinedLoop() {
- @setFnTest(this);
-
- inline var i = 0;
- inline var sum = 0;
- inline while (i <= 5; i += 1)
- sum += i;
- assert(sum == 15);
-}
-
-
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)
test/cases3/import.zig
@@ -0,0 +1,13 @@
+const a_namespace = @import("cases3/import/a_namespace.zig");
+
+fn callFnViaNamespaceLookup() {
+ @setFnTest(this);
+
+ assert(a_namespace.foo() == 1234);
+}
+
+// TODO const assert = @import("std").debug.assert;
+fn assert(ok: bool) {
+ if (!ok)
+ @unreachable();
+}
test/cases3/misc.zig
@@ -120,6 +120,30 @@ fn assignToIfVarPtr() {
assert(??maybe_bool == false);
}
+fn first4KeysOfHomeRow() -> []const u8 {
+ "aoeu"
+}
+
+fn ReturnStringFromFunction() {
+ @setFnTest(this);
+
+ assert(memeql(first4KeysOfHomeRow(), "aoeu"));
+}
+
+// TODO import from std.str
+pub fn memeql(a: []const u8, b: []const u8) -> bool {
+ sliceEql(u8, a, b)
+}
+
+// TODO import from std.str
+pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
+ if (a.len != b.len) return false;
+ for (a) |item, index| {
+ if (b[index] != item) return false;
+ }
+ return true;
+}
+
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
test/cases3/struct.zig
@@ -0,0 +1,30 @@
+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);
+}
+
+
+
+// TODO const assert = @import("std").debug.assert;
+fn assert(ok: bool) {
+ if (!ok)
+ @unreachable();
+}
test/self_hosted2.zig
@@ -1,99 +0,0 @@
-const case_namespace_fn_call = @import("cases/namespace_fn_call.zig");
-
-
-fn testNamespaceFnCall() {
- assert(case_namespace_fn_call.foo() == 1234);
-}
-
-
-const FooA = struct {
- fn add(a: i32, b: i32) -> i32 { a + b }
-};
-const foo_a = FooA {};
-
-fn testStructStatic() {
- const result = FooA.add(3, 4);
- assert(result == 7);
-}
-
-const should_be_11 = FooA.add(5, 6);
-fn testStaticFnEval() {
- assert(should_be_11 == 11);
-}
-
-fn fib(x: i32) -> i32 {
- if (x < 2) x else fib(x - 1) + fib(x - 2)
-}
-
-const fib_7 = fib(7);
-
-fn testCompileTimeFib() {
- assert(fib_7 == 13);
-}
-
-fn unwrapAndAddOne(blah: ?i32) -> i32 {
- return ??blah + 1;
-}
-
-const should_be_1235 = unwrapAndAddOne(1234);
-
-fn testStaticAddOne() {
- assert(should_be_1235 == 1235);
-}
-
-fn gimme1or2(inline a: bool) -> i32 {
- const x: i32 = 1;
- const y: i32 = 2;
- inline var z: i32 = inline if (a) x else y;
- return z;
-}
-
-fn testInlineVarsAgain() {
- assert(gimme1or2(true) == 1);
- assert(gimme1or2(false) == 2);
-}
-
-fn first4KeysOfHomeRow() -> []const u8 {
- "aoeu"
-}
-
-fn testReturnStringFromFunction() {
- assert(memeql(first4KeysOfHomeRow(), "aoeu"));
-}
-
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
- sliceEql(u8, a, b)
-}
-
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
- if (a.len != b.len) return false;
- for (a) |item, index| {
- if (b[index] != item) return false;
- }
- return true;
-}
-
-error ItBroke;
-fn gimmeItBroke() -> []const u8 {
- @errorName(error.ItBroke)
-}
-
-fn testErrorName() {
- assert(memeql(@errorName(error.ItBroke), "ItBroke"));
-}
-
-fn runAllTests() {
- testNamespaceFnCall();
- testStructStatic();
- testStaticFnEval();
- testCompileTimeFib();
- testCompileTimeGenericEval();
- testFnWithInlineArgs();
- testContinueInForLoop();
- shortCircuit();
- testStaticAddOne();
- testInlineVarsAgain();
- testMinValueAndMaxValue();
- testReturnStringFromFunction();
- testErrorName();
-}
test/self_hosted3.zig
@@ -1,11 +1,14 @@
// TODO '_' identifier for unused variable bindings
-const test_misc = @import("cases3/misc.zig");
-const test_switch = @import("cases3/switch.zig");
-const test_error = @import("cases3/error.zig");
-const test_goto = @import("cases3/goto.zig");
const test_atomics = @import("cases3/atomics.zig");
-const test_for = @import("cases3/for.zig");
-const test_math = @import("cases3/math.zig");
-const test_generics = @import("cases3/generics.zig");
const test_defer = @import("cases3/defer.zig");
const test_enum = @import("cases3/enum.zig");
+const test_error = @import("cases3/error.zig");
+const test_eval = @import("cases3/eval.zig");
+const test_for = @import("cases3/for.zig");
+const test_generics = @import("cases3/generics.zig");
+const test_goto = @import("cases3/goto.zig");
+const test_import = @import("cases3/import.zig");
+const test_math = @import("cases3/math.zig");
+const test_misc = @import("cases3/misc.zig");
+const test_struct = @import("cases3/struct.zig");
+const test_switch = @import("cases3/switch.zig");