From 5fc95c2a538b10ad346273112b1d4ed5371f8135 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 22 Dec 2016 00:55:21 -0500 Subject: [PATCH] IR: port some tests --- src/ir.cpp | 2 +- test/cases3/array.zig | 31 +++++++++++ test/cases3/fn.zig | 30 +++++++++++ test/cases3/math.zig | 19 +++++++ test/cases3/struct.zig | 41 +++++++++++++++ test/self_hosted.zig | 115 ----------------------------------------- test/self_hosted3.zig | 1 + 7 files changed, 123 insertions(+), 116 deletions(-) create mode 100644 test/cases3/array.zig diff --git a/src/ir.cpp b/src/ir.cpp index 79f561b7c7..8a041aa4ad 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5462,7 +5462,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst ConstExprValue *val = ir_resolve_const(ira, value, UndefBad); if (!val) return ira->codegen->builtin_types.entry_invalid; - bool ptr_is_const = true; + bool ptr_is_const = false; return ir_analyze_const_ptr(ira, source_instruction, val, value->type_entry, false, ConstPtrSpecialNone, ptr_is_const); } diff --git a/test/cases3/array.zig b/test/cases3/array.zig new file mode 100644 index 0000000000..94f453c100 --- /dev/null +++ b/test/cases3/array.zig @@ -0,0 +1,31 @@ +fn arrays() { + @setFnTest(this); + + var array : [5]u32 = undefined; + + var i : u32 = 0; + while (i < 5) { + array[i] = i + 1; + i = array[i]; + } + + i = 0; + var accumulator = u32(0); + while (i < 5) { + accumulator += array[i]; + + i += 1; + } + + assert(accumulator == 15); + assert(getArrayLen(array) == 5); +} +fn getArrayLen(a: []u32) -> usize { + a.len +} + +// TODO const assert = @import("std").debug.assert; +fn assert(ok: bool) { + if (!ok) + @unreachable(); +} diff --git a/test/cases3/fn.zig b/test/cases3/fn.zig index 2fe1db4e45..d2bc592655 100644 --- a/test/cases3/fn.zig +++ b/test/cases3/fn.zig @@ -32,6 +32,36 @@ fn voidFun(a: i32, b: void, c: i32, d: void) { } +fn mutableLocalVariables() { + @setFnTest(this); + + var zero : i32 = 0; + assert(zero == 0); + + var i = i32(0); + while (i != 3) { + i += 1; + } + assert(i == 3); +} + +fn separateBlockScopes() { + @setFnTest(this); + + { + const no_conflict : i32 = 5; + assert(no_conflict == 5); + } + + const c = { + const no_conflict = i32(10); + no_conflict + }; + assert(c == 10); +} + + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/math.zig b/test/cases3/math.zig index e6100f4b45..8eda479c89 100644 --- a/test/cases3/math.zig +++ b/test/cases3/math.zig @@ -50,6 +50,25 @@ fn countTrailingZeroes() { assert(@ctz(u8(0b00000000)) == 8); } +fn modifyOperators() { + @setFnTest(this); + + var i : i32 = 0; + i += 5; assert(i == 5); + i -= 2; assert(i == 3); + i *= 20; assert(i == 60); + i /= 3; assert(i == 20); + i %= 11; assert(i == 9); + i <<= 1; assert(i == 18); + i >>= 2; assert(i == 4); + i = 6; + i &= 5; assert(i == 4); + i ^= 6; assert(i == 2); + i = 6; + i |= 3; assert(i == 7); +} + + // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { if (!ok) diff --git a/test/cases3/struct.zig b/test/cases3/struct.zig index de703f0b40..45328a554f 100644 --- a/test/cases3/struct.zig +++ b/test/cases3/struct.zig @@ -21,6 +21,47 @@ fn invokeStaticMethodInGlobalScope() { assert(should_be_11 == 11); } +fn voidStructFields() { + @setFnTest(this); + + const foo = VoidStructFieldsFoo { + .a = void{}, + .b = 1, + .c = void{}, + }; + assert(foo.b == 1); + assert(@sizeOf(VoidStructFieldsFoo) == 4); +} +const VoidStructFieldsFoo = struct { + a : void, + b : i32, + c : void, +}; + + +pub fn structs() { + @setFnTest(this); + + var foo: StructFoo = undefined; + @memset((&u8)(&foo), 0, @sizeOf(StructFoo)); + foo.a += 1; + foo.b = foo.a == 1; + testFoo(foo); + testMutation(&foo); + assert(foo.c == 100); +} +const StructFoo = struct { + a : i32, + b : bool, + c : f32, +}; +fn testFoo(foo : StructFoo) { + assert(foo.b); +} +fn testMutation(foo : &StructFoo) { + foo.c = 100; +} + // TODO const assert = @import("std").debug.assert; diff --git a/test/self_hosted.zig b/test/self_hosted.zig index e1a68eda5e..f67a3c81d5 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -17,121 +17,6 @@ const test_this = @import("cases/this.zig"); -fn mutableLocalVariables() { - @setFnTest(this, true); - - var zero : i32 = 0; - assert(zero == 0); - - var i = i32(0); - while (i != 3) { - i += 1; - } - assert(i == 3); -} - -fn arrays() { - @setFnTest(this, true); - - var array : [5]u32 = undefined; - - var i : u32 = 0; - while (i < 5) { - array[i] = i + 1; - i = array[i]; - } - - i = 0; - var accumulator = u32(0); - while (i < 5) { - accumulator += array[i]; - - i += 1; - } - - assert(accumulator == 15); - assert(getArrayLen(array) == 5); -} -fn getArrayLen(a: []u32) -> usize { - a.len -} - -fn modifyOperators() { - @setFnTest(this, true); - - var i : i32 = 0; - i += 5; assert(i == 5); - i -= 2; assert(i == 3); - i *= 20; assert(i == 60); - i /= 3; assert(i == 20); - i %= 11; assert(i == 9); - i <<= 1; assert(i == 18); - i >>= 2; assert(i == 4); - i = 6; - i &= 5; assert(i == 4); - i ^= 6; assert(i == 2); - i = 6; - i |= 3; assert(i == 7); -} - - -fn separateBlockScopes() { - @setFnTest(this, true); - - { - const no_conflict : i32 = 5; - assert(no_conflict == 5); - } - - const c = { - const no_conflict = i32(10); - no_conflict - }; - assert(c == 10); -} - - -fn voidStructFields() { - @setFnTest(this, true); - - const foo = VoidStructFieldsFoo { - .a = void{}, - .b = 1, - .c = void{}, - }; - assert(foo.b == 1); - assert(@sizeOf(VoidStructFieldsFoo) == 4); -} -struct VoidStructFieldsFoo { - a : void, - b : i32, - c : void, -} - - - -pub fn structs() { - @setFnTest(this, true); - - var foo : StructFoo = undefined; - @memset(&foo, 0, @sizeOf(StructFoo)); - foo.a += 1; - foo.b = foo.a == 1; - testFoo(foo); - testMutation(&foo); - assert(foo.c == 100); -} -struct StructFoo { - a : i32, - b : bool, - c : f32, -} -fn testFoo(foo : StructFoo) { - assert(foo.b); -} -fn testMutation(foo : &StructFoo) { - foo.c = 100; -} struct Node { val: Val, next: &Node, diff --git a/test/self_hosted3.zig b/test/self_hosted3.zig index c2bf9ca8b6..c32926a5a0 100644 --- a/test/self_hosted3.zig +++ b/test/self_hosted3.zig @@ -1,4 +1,5 @@ // TODO '_' identifier for unused variable bindings +const test_array = @import("cases3/array.zig"); const test_atomics = @import("cases3/atomics.zig"); const test_defer = @import("cases3/defer.zig"); const test_enum = @import("cases3/enum.zig");