From 8dc45bc50523d9fa58a937f525b9dc8f7569af0b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 22 Dec 2016 10:09:53 -0500 Subject: [PATCH] port more tests --- test/cases/maybe_return.zig | 17 ------ test/cases/return_type_type.zig | 22 -------- test/cases/var_params.zig | 35 ------------ test/cases3/eval.zig | 19 +++++++ test/cases3/fn.zig | 13 +++++ test/cases3/generics.zig | 24 ++++++++ test/cases3/misc.zig | 12 ++++ test/cases3/null.zig | 21 +++++++ test/{cases => cases3}/sizeof_and_typeof.zig | 10 +++- test/self_hosted.zig | 59 +------------------- test/self_hosted3.zig | 3 +- 11 files changed, 99 insertions(+), 136 deletions(-) delete mode 100644 test/cases/maybe_return.zig delete mode 100644 test/cases/return_type_type.zig delete mode 100644 test/cases/var_params.zig rename test/{cases => cases3}/sizeof_and_typeof.zig (51%) diff --git a/test/cases/maybe_return.zig b/test/cases/maybe_return.zig deleted file mode 100644 index 0caa5bdb0b..0000000000 --- a/test/cases/maybe_return.zig +++ /dev/null @@ -1,17 +0,0 @@ -const assert = @import("std").debug.assert; - -fn maybeReturn() { - @setFnTest(this, true); - - assert(??foo(1235)); - assert(if (const _ ?= foo(null)) false else true); - assert(!??foo(1234)); -} - -// TODO test static eval maybe return -fn foo(x: ?i32) -> ?bool { - @setFnStaticEval(this, false); - - const value = ?return x; - return value > 1234; -} diff --git a/test/cases/return_type_type.zig b/test/cases/return_type_type.zig deleted file mode 100644 index 24b7a06500..0000000000 --- a/test/cases/return_type_type.zig +++ /dev/null @@ -1,22 +0,0 @@ -const assert = @import("std").debug.assert; - -pub fn List(inline T: type) -> type { - SmallList(T, 8) -} - -pub struct SmallList(inline T: type, inline STATIC_SIZE: usize) { - items: []T, - length: usize, - prealloc_items: [STATIC_SIZE]T, -} - -fn functionWithReturnTypeType() { - @setFnTest(this, true); - - var list: List(i32) = undefined; - var list2: List(i32) = undefined; - list.length = 10; - list2.length = 10; - assert(list.prealloc_items.len == 8); - assert(list2.prealloc_items.len == 8); -} diff --git a/test/cases/var_params.zig b/test/cases/var_params.zig deleted file mode 100644 index 8db3b3e331..0000000000 --- a/test/cases/var_params.zig +++ /dev/null @@ -1,35 +0,0 @@ -const assert = @import("std").debug.assert; - -fn varParams() { - @setFnTest(this, true); - - assert(max_i32(12, 34) == 34); - assert(max_f64(1.2, 3.4) == 3.4); - - assert(max_i32_noeval(12, 34) == 34); - assert(max_f64_noeval(1.2, 3.4) == 3.4); -} - -fn max(a: var, b: var) -> @typeOf(a) { - if (a > b) a else b -} - -fn max_i32(a: i32, b: i32) -> i32 { - max(a, b) -} - -fn max_f64(a: f64, b: f64) -> f64 { - max(a, b) -} - -fn max_i32_noeval(a: i32, b: i32) -> i32 { - @setFnStaticEval(this, false); - - max(a, b) -} - -fn max_f64_noeval(a: f64, b: f64) -> f64 { - @setFnStaticEval(this, false); - - max(a, b) -} diff --git a/test/cases3/eval.zig b/test/cases3/eval.zig index df5213d9f2..fb38760ea6 100644 --- a/test/cases3/eval.zig +++ b/test/cases3/eval.zig @@ -52,6 +52,25 @@ const statically_added_number = staticAdd(1, 2); fn staticAdd(a: i32, b: i32) -> i32 { a + b } +fn constExprEvalOnSingleExprBlocks() { + @setFnTest(this); + + assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3); +} + +fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { + const literal = 3; + + const result = if (b) { + literal + } else { + x + }; + + return result; +} + + diff --git a/test/cases3/fn.zig b/test/cases3/fn.zig index 58c62cb99d..03243fe8af 100644 --- a/test/cases3/fn.zig +++ b/test/cases3/fn.zig @@ -73,6 +73,19 @@ fn @"weird function name"() { @setFnTest(this); } +fn implicitCastFnUnreachableReturn() { + @setFnTest(this); + + wantsFnWithVoid(fnWithUnreachable); +} + +fn wantsFnWithVoid(f: fn()) { } + +fn fnWithUnreachable() -> unreachable { + @unreachable() +} + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { diff --git a/test/cases3/generics.zig b/test/cases3/generics.zig index 79ead86184..643f679240 100644 --- a/test/cases3/generics.zig +++ b/test/cases3/generics.zig @@ -64,6 +64,30 @@ fn max_f64(a: f64, b: f64) -> f64 { max_var(a, b) } + +pub fn List(inline T: type) -> type { + SmallList(T, 8) +} + +pub fn SmallList(inline T: type, inline STATIC_SIZE: usize) -> type { + struct { + items: []T, + length: usize, + prealloc_items: [STATIC_SIZE]T, + } +} + +fn functionWithReturnTypeType() { + @setFnTest(this); + + var list: List(i32) = undefined; + var list2: List(i32) = undefined; + list.length = 10; + list2.length = 10; + assert(list.prealloc_items.len == 8); + assert(list2.prealloc_items.len == 8); +} + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/misc.zig b/test/cases3/misc.zig index f0914a06e6..8917813bed 100644 --- a/test/cases3/misc.zig +++ b/test/cases3/misc.zig @@ -230,6 +230,18 @@ fn stringEscapes() { assert(memeql("\u1234\u0069", "\xe1\x88\xb4\x69")); } +fn multilineString() { + @setFnTest(this); + + const s1 = + \\one + \\two) + \\three + ; + const s2 = "one\ntwo)\nthree"; + assert(memeql(s1, s2)); +} + // TODO import from std.str pub fn memeql(a: []const u8, b: []const u8) -> bool { diff --git a/test/cases3/null.zig b/test/cases3/null.zig index 6443b85d32..f236fcfc48 100644 --- a/test/cases3/null.zig +++ b/test/cases3/null.zig @@ -38,6 +38,27 @@ fn assignToIfVarPtr() { assert(??maybe_bool == false); } +fn rhsMaybeUnwrapReturn() { + @setFnTest(this); + + const x: ?bool = true; + const y = x ?? return; +} + + +fn maybeReturn() { + @setFnTest(this); + + assert(??foo(1235)); + assert(if (const _ ?= foo(null)) false else true); + assert(!??foo(1234)); +} + +// TODO test static eval maybe return +fn foo(x: ?i32) -> ?bool { + const value = ?return x; + return value > 1234; +} // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { diff --git a/test/cases/sizeof_and_typeof.zig b/test/cases3/sizeof_and_typeof.zig similarity index 51% rename from test/cases/sizeof_and_typeof.zig rename to test/cases3/sizeof_and_typeof.zig index 9711718e8b..0d65bfc8ca 100644 --- a/test/cases/sizeof_and_typeof.zig +++ b/test/cases3/sizeof_and_typeof.zig @@ -1,10 +1,14 @@ -const assert = @import("std").debug.assert; - fn sizeofAndTypeOf() { - @setFnTest(this, true); + @setFnTest(this); const y: @typeOf(x) = 120; assert(@sizeOf(@typeOf(y)) == 2); } const x: u16 = 13; const z: @typeOf(x) = 19; + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 0601cdc914..1b5f86bdad 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -2,36 +2,10 @@ const std = @import("std"); const assert = std.debug.assert; const str = std.str; const cstr = std.cstr; -const test_return_type_type = @import("cases/return_type_type.zig"); -const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); -const test_maybe_return = @import("cases/maybe_return.zig"); -const test_var_params = @import("cases/var_params.zig"); const test_const_slice_child = @import("cases/const_slice_child.zig"); const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig"); const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig"); const test_enum_with_members = @import("cases/enum_with_members.zig"); -const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); - - -fn rhsMaybeUnwrapReturn() { - @setFnTest(this, true); - - const x = ?true; - const y = x ?? return; -} - - -fn implicitCastFnUnreachableReturn() { - @setFnTest(this, true); - - wantsFnWithVoid(fnWithUnreachable); -} - -fn wantsFnWithVoid(f: fn()) { } - -fn fnWithUnreachable() -> unreachable { - @unreachable() -} fn explicitCastMaybePointers() { @@ -42,39 +16,8 @@ fn explicitCastMaybePointers() { } -fn constExprEvalOnSingleExprBlocks() { - @setFnTest(this, true); - - assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3); -} - -fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { - const literal = 3; - - const result = if (b) { - literal - } else { - x - }; - - return result; -} - - -fn multilineString() { - @setFnTest(this, true); - - const s1 = - \\one - \\two) - \\three - ; - const s2 = "one\ntwo)\nthree"; - assert(str.eql(s1, s2)); -} - fn multilineCString() { - @setFnTest(this, true); + @setFnTest(this); const s1 = c\\one diff --git a/test/self_hosted3.zig b/test/self_hosted3.zig index 448116890a..a7a5325e76 100644 --- a/test/self_hosted3.zig +++ b/test/self_hosted3.zig @@ -16,8 +16,9 @@ const test_import = @import("cases3/import.zig"); const test_math = @import("cases3/math.zig"); const test_misc = @import("cases3/misc.zig"); const test_null = @import("cases3/null.zig"); +const test_sizeof_and_typeof = @import("cases3/sizeof_and_typeof.zig"); const test_struct = @import("cases3/struct.zig"); +const test_struct_contains_slice_of_itself = @import("cases3/struct_contains_slice_of_itself.zig"); const test_switch = @import("cases3/switch.zig"); const test_this = @import("cases3/this.zig"); const test_while = @import("cases3/while.zig"); -const test_struct_contains_slice_of_itself = @import("cases3/struct_contains_slice_of_itself.zig");