diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-05-25 22:23:32 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-05-25 22:33:48 -0700 |
| commit | 83f69af971723d3a0774deb9dfda4b3fcbf0006f (patch) | |
| tree | ba516b2bdd17b1492c6764dd1cc6bbd198cea853 /test/behavior | |
| parent | b82081e7092a6198e6f8c65524a5829e2e08527b (diff) | |
| download | zig-83f69af971723d3a0774deb9dfda4b3fcbf0006f.tar.gz zig-83f69af971723d3a0774deb9dfda4b3fcbf0006f.zip | |
stage2: implement runtime array multiplication
Additionally:
* Sema: fix array cat/mul not setting the sentinel value
- This required an LLVM backend enhancement to the handling of the
AIR instruction aggregate_init that likely needs to be
propagated to the other backends.
* Sema: report integer overflow of array concatenation in a proper
compile error instead of crashing.
* Sema: fix not using proper pointer address space for array cat/mul
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/eval.zig | 102 |
1 files changed, 88 insertions, 14 deletions
diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index cb0de8ed59..63eef12207 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -742,6 +742,23 @@ test "array concatenation of function calls" { try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 4 })); } +test "array multiplication of function calls" { + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var a = oneItem(3) ** scalar(2); + try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 })); +} + +fn oneItem(x: i32) [1]i32 { + return [_]i32{x}; +} + +fn scalar(x: u32) u32 { + return x; +} + test "array concatenation peer resolves element types - value" { if (builtin.zig_backend == .stage1) return error.SkipZigTest; if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; @@ -751,7 +768,7 @@ test "array concatenation peer resolves element types - value" { var a = [2]u3{ 1, 7 }; var b = [3]u8{ 200, 225, 255 }; var c = a ++ b; - try expect(@TypeOf(c) == [5]u8); + comptime assert(@TypeOf(c) == [5]u8); try expect(c[0] == 1); try expect(c[1] == 7); try expect(c[2] == 200); @@ -768,7 +785,7 @@ test "array concatenation peer resolves element types - pointer" { var a = [2]u3{ 1, 7 }; var b = [3]u8{ 200, 225, 255 }; var c = &a ++ &b; - try expect(@TypeOf(c) == *[5]u8); + comptime assert(@TypeOf(c) == *[5]u8); try expect(c[0] == 1); try expect(c[1] == 7); try expect(c[2] == 200); @@ -776,23 +793,80 @@ test "array concatenation peer resolves element types - pointer" { try expect(c[4] == 255); } -test "array multiplication forces comptime" { - if (builtin.zig_backend != .stage1) { - // note: our plan is to change the language to support runtime array - // multiplication instead of making this test pass. - return error.SkipZigTest; // TODO - } +test "array concatenation sets the sentinel - value" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - var a = oneItem(3) ** scalar(2); - try expect(std.mem.eql(i32, &a, &[_]i32{ 3, 3 })); + var a = [2]u3{ 1, 7 }; + var b = [3:69]u8{ 200, 225, 255 }; + var c = a ++ b; + comptime assert(@TypeOf(c) == [5:69]u8); + try expect(c[0] == 1); + try expect(c[1] == 7); + try expect(c[2] == 200); + try expect(c[3] == 225); + try expect(c[4] == 255); + var ptr: [*]const u8 = &c; + try expect(ptr[5] == 69); } -fn oneItem(x: i32) [1]i32 { - return [_]i32{x}; +test "array concatenation sets the sentinel - pointer" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var a = [2]u3{ 1, 7 }; + var b = [3:69]u8{ 200, 225, 255 }; + var c = &a ++ &b; + comptime assert(@TypeOf(c) == *[5:69]u8); + try expect(c[0] == 1); + try expect(c[1] == 7); + try expect(c[2] == 200); + try expect(c[3] == 225); + try expect(c[4] == 255); + var ptr: [*]const u8 = c; + try expect(ptr[5] == 69); } -fn scalar(x: u32) u32 { - return x; +test "array multiplication sets the sentinel - value" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var a = [2:7]u3{ 1, 6 }; + var b = a ** 2; + comptime assert(@TypeOf(b) == [4:7]u3); + try expect(b[0] == 1); + try expect(b[1] == 6); + try expect(b[2] == 1); + try expect(b[3] == 6); + var ptr: [*]const u3 = &b; + try expect(ptr[4] == 7); +} + +test "array multiplication sets the sentinel - pointer" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + var a = [2:7]u3{ 1, 6 }; + var b = &a ** 2; + comptime assert(@TypeOf(b) == *[4:7]u3); + try expect(b[0] == 1); + try expect(b[1] == 6); + try expect(b[2] == 1); + try expect(b[3] == 6); + var ptr: [*]const u3 = b; + try expect(ptr[4] == 7); } test "comptime assign int to optional int" { |
