| author | |
| committer | |
| log | c1a98cd65d48693c9cfce5fbc5e5f5a1dbf4c1b7 |
| tree | 198ff979342674e87ae14d1e2bf67cb833c42f33 |
| parent | 46db5e2a44b38c92e97a438c4b6a67627cc12d16 |
Port more incremental tests.31 files changed, 581 insertions(+), 61 deletions(-)
src/test.zig+36-27| ... | ... | @@ -233,7 +233,7 @@ const TestManifest = struct { |
| 233 | 233 | |
| 234 | 234 | fn next(self: *TrailingIterator) ?[]const u8 { |
| 235 | 235 | const next_inner = self.inner.next() orelse return null; |
| 236 | return std.mem.trim(u8, next_inner, " \t"); | |
| 236 | return std.mem.trim(u8, next_inner[2..], " \t"); | |
| 237 | 237 | } |
| 238 | 238 | }; |
| 239 | 239 | |
| ... | ... | @@ -1033,31 +1033,25 @@ pub const TestContext = struct { |
| 1033 | 1033 | for (filenames.items) |filename| { |
| 1034 | 1034 | current_file.* = filename; |
| 1035 | 1035 | |
| 1036 | { // First, check if this file is part of an incremental update sequence | |
| 1037 | ||
| 1038 | // Split filename into "<base_name>.<index>.<file_ext>" | |
| 1039 | const prev_parts = getTestFileNameParts(prev_filename); | |
| 1040 | const new_parts = getTestFileNameParts(filename); | |
| 1041 | ||
| 1042 | // If base_name and file_ext match, these files are in the same test sequence | |
| 1043 | // and the new one should be the incremented version of the previous test | |
| 1044 | if (std.mem.eql(u8, prev_parts.base_name, new_parts.base_name) and | |
| 1045 | std.mem.eql(u8, prev_parts.file_ext, new_parts.file_ext)) | |
| 1046 | { | |
| 1047 | ||
| 1048 | // This is "foo.X.zig" followed by "foo.Y.zig". Make sure that X = Y + 1 | |
| 1049 | if (prev_parts.test_index == null) return error.InvalidIncrementalTestIndex; | |
| 1050 | if (new_parts.test_index == null) return error.InvalidIncrementalTestIndex; | |
| 1051 | if (new_parts.test_index.? != prev_parts.test_index.? + 1) return error.InvalidIncrementalTestIndex; | |
| 1052 | } else { | |
| 1053 | ||
| 1054 | // This is not the same test sequence, so the new file must be the first file | |
| 1055 | // in a new sequence ("*.0.zig") or an independent test file ("*.zig") | |
| 1056 | if (new_parts.test_index != null and new_parts.test_index.? != 0) return error.InvalidIncrementalTestIndex; | |
| 1057 | ||
| 1058 | if (strategy == .independent) | |
| 1059 | cases.clearRetainingCapacity(); // Generate a new independent test case for this update | |
| 1060 | } | |
| 1036 | // First, check if this file is part of an incremental update sequence | |
| 1037 | // Split filename into "<base_name>.<index>.<file_ext>" | |
| 1038 | const prev_parts = getTestFileNameParts(prev_filename); | |
| 1039 | const new_parts = getTestFileNameParts(filename); | |
| 1040 | ||
| 1041 | // If base_name and file_ext match, these files are in the same test sequence | |
| 1042 | // and the new one should be the incremented version of the previous test | |
| 1043 | if (std.mem.eql(u8, prev_parts.base_name, new_parts.base_name) and | |
| 1044 | std.mem.eql(u8, prev_parts.file_ext, new_parts.file_ext)) | |
| 1045 | { | |
| 1046 | // This is "foo.X.zig" followed by "foo.Y.zig". Make sure that X = Y + 1 | |
| 1047 | if (prev_parts.test_index == null) return error.InvalidIncrementalTestIndex; | |
| 1048 | if (new_parts.test_index == null) return error.InvalidIncrementalTestIndex; | |
| 1049 | if (new_parts.test_index.? != prev_parts.test_index.? + 1) return error.InvalidIncrementalTestIndex; | |
| 1050 | } else { | |
| 1051 | // This is not the same test sequence, so the new file must be the first file | |
| 1052 | // in a new sequence ("*.0.zig") or an independent test file ("*.zig") | |
| 1053 | if (new_parts.test_index != null and new_parts.test_index.? != 0) return error.InvalidIncrementalTestIndex; | |
| 1054 | cases.clearRetainingCapacity(); | |
| 1061 | 1055 | } |
| 1062 | 1056 | prev_filename = filename; |
| 1063 | 1057 | |
| ... | ... | @@ -1073,12 +1067,23 @@ pub const TestContext = struct { |
| 1073 | 1067 | const is_test = manifest.getConfigForKeyAssertSingle("is_test", bool); |
| 1074 | 1068 | const output_mode = manifest.getConfigForKeyAssertSingle("output_mode", std.builtin.OutputMode); |
| 1075 | 1069 | |
| 1070 | const name_prefix = blk: { | |
| 1071 | const ext_index = std.mem.lastIndexOfScalar(u8, current_file.*, '.') orelse | |
| 1072 | return error.InvalidFilename; | |
| 1073 | const index = std.mem.lastIndexOfScalar(u8, current_file.*[0..ext_index], '.') orelse ext_index; | |
| 1074 | break :blk current_file.*[0..index]; | |
| 1075 | }; | |
| 1076 | ||
| 1076 | 1077 | // Cross-product to get all possible test combinations |
| 1077 | 1078 | while (backends.next()) |backend| { |
| 1078 | 1079 | while (targets.next()) |target| { |
| 1080 | const name = try std.fmt.allocPrint(ctx.arena, "{s} ({s})", .{ | |
| 1081 | name_prefix, | |
| 1082 | try target.zigTriple(ctx.arena), | |
| 1083 | }); | |
| 1079 | 1084 | const case = try ctx.cases.addOne(); |
| 1080 | 1085 | case.* = .{ |
| 1081 | .name = "none", | |
| 1086 | .name = name, | |
| 1082 | 1087 | .target = target, |
| 1083 | 1088 | .backend = backend, |
| 1084 | 1089 | .updates = std.ArrayList(TestContext.Update).init(ctx.cases.allocator), |
| ... | ... | @@ -1109,6 +1114,10 @@ pub const TestContext = struct { |
| 1109 | 1114 | var trailing_it = manifest.trailing(); |
| 1110 | 1115 | while (trailing_it.next()) |line| { |
| 1111 | 1116 | try output.appendSlice(line); |
| 1117 | try output.append('\n'); | |
| 1118 | } | |
| 1119 | if (output.items.len > 0) { | |
| 1120 | try output.resize(output.items.len - 1); | |
| 1112 | 1121 | } |
| 1113 | 1122 | case.addCompareOutput(src, output.toOwnedSlice()); |
| 1114 | 1123 | }, |
test/incremental/add.0.zig deleted-10| ... | ... | @@ -1,10 +0,0 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | if (a + b != 7) unreachable; | |
| 7 | } | |
| 8 | ||
| 9 | // run | |
| 10 | // |
test/incremental/add.1.zig deleted-12| ... | ... | @@ -1,12 +0,0 @@ |
| 1 | pub fn main() void { | |
| 2 | if (x - 7 != 0) unreachable; | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) u32 { | |
| 6 | return a + b; | |
| 7 | } | |
| 8 | ||
| 9 | const x = add(3, 4); | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/add.2.zig deleted-12| ... | ... | @@ -1,12 +0,0 @@ |
| 1 | pub fn main() void { | |
| 2 | var x: usize = 3; | |
| 3 | const y = add(1, 2, x); | |
| 4 | if (y - 6 != 0) unreachable; | |
| 5 | } | |
| 6 | ||
| 7 | inline fn add(a: usize, b: usize, c: usize) usize { | |
| 8 | return a + b + c; | |
| 9 | } | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/adding_numbers_at_runtime_and_comptime.0.zig created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | if (a + b != 7) unreachable; | |
| 7 | } | |
| 8 | ||
| 9 | // run | |
| 10 | // |
test/incremental/adding_numbers_at_runtime_and_comptime.1.zig created+12| ... | ... | @@ -0,0 +1,12 @@ |
| 1 | pub fn main() void { | |
| 2 | if (x - 7 != 0) unreachable; | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) u32 { | |
| 6 | return a + b; | |
| 7 | } | |
| 8 | ||
| 9 | const x = add(3, 4); | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/adding_numbers_at_runtime_and_comptime.2.zig created+12| ... | ... | @@ -0,0 +1,12 @@ |
| 1 | pub fn main() void { | |
| 2 | var x: usize = 3; | |
| 3 | const y = add(1, 2, x); | |
| 4 | if (y - 6 != 0) unreachable; | |
| 5 | } | |
| 6 | ||
| 7 | inline fn add(a: usize, b: usize, c: usize) usize { | |
| 8 | return a + b + c; | |
| 9 | } | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/assert_function.0.zig created+15| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | assert(a + b == 7); | |
| 7 | } | |
| 8 | ||
| 9 | pub fn assert(ok: bool) void { | |
| 10 | if (!ok) unreachable; // assertion failure | |
| 11 | } | |
| 12 | ||
| 13 | // run | |
| 14 | // target=x86_64-linux,x86_64-macos | |
| 15 | // |
test/incremental/assert_function.1.zig created+17| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | const c = a + b; // 7 | |
| 7 | const d = a + c; // 10 | |
| 8 | const e = d + b; // 14 | |
| 9 | assert(e == 14); | |
| 10 | } | |
| 11 | ||
| 12 | pub fn assert(ok: bool) void { | |
| 13 | if (!ok) unreachable; // assertion failure | |
| 14 | } | |
| 15 | ||
| 16 | // run | |
| 17 | // |
test/incremental/assert_function.10.zig created+27| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 116); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) u32 { | |
| 6 | const x: u32 = blk: { | |
| 7 | const c = a + b; // 7 | |
| 8 | const d = a + c; // 10 | |
| 9 | const e = d + b; // 14 | |
| 10 | const f = d + e; // 24 | |
| 11 | const g = e + f; // 38 | |
| 12 | const h = f + g; // 62 | |
| 13 | const i = g + h; // 100 | |
| 14 | const j = i + d; // 110 | |
| 15 | break :blk j; | |
| 16 | }; | |
| 17 | const y = x + a; // 113 | |
| 18 | const z = y + a; // 116 | |
| 19 | return z; | |
| 20 | } | |
| 21 | ||
| 22 | pub fn assert(ok: bool) void { | |
| 23 | if (!ok) unreachable; // assertion failure | |
| 24 | } | |
| 25 | ||
| 26 | // run | |
| 27 | // |
test/incremental/assert_function.11.zig created+66| ... | ... | @@ -0,0 +1,66 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 1221); | |
| 3 | assert(mul(3, 4) == 21609); | |
| 4 | } | |
| 5 | ||
| 6 | fn add(a: u32, b: u32) u32 { | |
| 7 | const x: u32 = blk: { | |
| 8 | const c = a + b; // 7 | |
| 9 | const d = a + c; // 10 | |
| 10 | const e = d + b; // 14 | |
| 11 | const f = d + e; // 24 | |
| 12 | const g = e + f; // 38 | |
| 13 | const h = f + g; // 62 | |
| 14 | const i = g + h; // 100 | |
| 15 | const j = i + d; // 110 | |
| 16 | const k = i + j; // 210 | |
| 17 | const l = j + k; // 320 | |
| 18 | const m = l + c; // 327 | |
| 19 | const n = m + d; // 337 | |
| 20 | const o = n + e; // 351 | |
| 21 | const p = o + f; // 375 | |
| 22 | const q = p + g; // 413 | |
| 23 | const r = q + h; // 475 | |
| 24 | const s = r + i; // 575 | |
| 25 | const t = s + j; // 685 | |
| 26 | const u = t + k; // 895 | |
| 27 | const v = u + l; // 1215 | |
| 28 | break :blk v; | |
| 29 | }; | |
| 30 | const y = x + a; // 1218 | |
| 31 | const z = y + a; // 1221 | |
| 32 | return z; | |
| 33 | } | |
| 34 | ||
| 35 | fn mul(a: u32, b: u32) u32 { | |
| 36 | const x: u32 = blk: { | |
| 37 | const c = a * a * a * a; // 81 | |
| 38 | const d = a * a * a * b; // 108 | |
| 39 | const e = a * a * b * a; // 108 | |
| 40 | const f = a * a * b * b; // 144 | |
| 41 | const g = a * b * a * a; // 108 | |
| 42 | const h = a * b * a * b; // 144 | |
| 43 | const i = a * b * b * a; // 144 | |
| 44 | const j = a * b * b * b; // 192 | |
| 45 | const k = b * a * a * a; // 108 | |
| 46 | const l = b * a * a * b; // 144 | |
| 47 | const m = b * a * b * a; // 144 | |
| 48 | const n = b * a * b * b; // 192 | |
| 49 | const o = b * b * a * a; // 144 | |
| 50 | const p = b * b * a * b; // 192 | |
| 51 | const q = b * b * b * a; // 192 | |
| 52 | const r = b * b * b * b; // 256 | |
| 53 | const s = c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r; // 2401 | |
| 54 | break :blk s; | |
| 55 | }; | |
| 56 | const y = x * a; // 7203 | |
| 57 | const z = y * a; // 21609 | |
| 58 | return z; | |
| 59 | } | |
| 60 | ||
| 61 | pub fn assert(ok: bool) void { | |
| 62 | if (!ok) unreachable; // assertion failure | |
| 63 | } | |
| 64 | ||
| 65 | // run | |
| 66 | // |
test/incremental/assert_function.12.zig created+47| ... | ... | @@ -0,0 +1,47 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 791); | |
| 3 | assert(add(4, 3) == 79); | |
| 4 | } | |
| 5 | ||
| 6 | fn add(a: u32, b: u32) u32 { | |
| 7 | const x: u32 = if (a < b) blk: { | |
| 8 | const c = a + b; // 7 | |
| 9 | const d = a + c; // 10 | |
| 10 | const e = d + b; // 14 | |
| 11 | const f = d + e; // 24 | |
| 12 | const g = e + f; // 38 | |
| 13 | const h = f + g; // 62 | |
| 14 | const i = g + h; // 100 | |
| 15 | const j = i + d; // 110 | |
| 16 | const k = i + j; // 210 | |
| 17 | const l = k + c; // 217 | |
| 18 | const m = l + d; // 227 | |
| 19 | const n = m + e; // 241 | |
| 20 | const o = n + f; // 265 | |
| 21 | const p = o + g; // 303 | |
| 22 | const q = p + h; // 365 | |
| 23 | const r = q + i; // 465 | |
| 24 | const s = r + j; // 575 | |
| 25 | const t = s + k; // 785 | |
| 26 | break :blk t; | |
| 27 | } else blk: { | |
| 28 | const t = b + b + a; // 10 | |
| 29 | const c = a + t; // 14 | |
| 30 | const d = c + t; // 24 | |
| 31 | const e = d + t; // 34 | |
| 32 | const f = e + t; // 44 | |
| 33 | const g = f + t; // 54 | |
| 34 | const h = c + g; // 68 | |
| 35 | break :blk h + b; // 71 | |
| 36 | }; | |
| 37 | const y = x + a; // 788, 75 | |
| 38 | const z = y + a; // 791, 79 | |
| 39 | return z; | |
| 40 | } | |
| 41 | ||
| 42 | pub fn assert(ok: bool) void { | |
| 43 | if (!ok) unreachable; // assertion failure | |
| 44 | } | |
| 45 | ||
| 46 | // run | |
| 47 | // |
test/incremental/assert_function.13.zig created+19| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | pub fn main() void { | |
| 2 | const ignore = | |
| 3 | \\ cool thx | |
| 4 | \\ | |
| 5 | ; | |
| 6 | _ = ignore; | |
| 7 | add('ぁ', '\x03'); | |
| 8 | } | |
| 9 | ||
| 10 | fn add(a: u32, b: u32) void { | |
| 11 | assert(a + b == 12356); | |
| 12 | } | |
| 13 | ||
| 14 | pub fn assert(ok: bool) void { | |
| 15 | if (!ok) unreachable; // assertion failure | |
| 16 | } | |
| 17 | ||
| 18 | // run | |
| 19 | // |
test/incremental/assert_function.14.zig created+17| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | pub fn main() void { | |
| 2 | add(aa, bb); | |
| 3 | } | |
| 4 | ||
| 5 | const aa = 'ぁ'; | |
| 6 | const bb = '\x03'; | |
| 7 | ||
| 8 | fn add(a: u32, b: u32) void { | |
| 9 | assert(a + b == 12356); | |
| 10 | } | |
| 11 | ||
| 12 | pub fn assert(ok: bool) void { | |
| 13 | if (!ok) unreachable; // assertion failure | |
| 14 | } | |
| 15 | ||
| 16 | // run | |
| 17 | // |
test/incremental/assert_function.15.zig created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | pub fn main() void { | |
| 2 | assert("hello"[0] == 'h'); | |
| 3 | } | |
| 4 | ||
| 5 | pub fn assert(ok: bool) void { | |
| 6 | if (!ok) unreachable; // assertion failure | |
| 7 | } | |
| 8 | ||
| 9 | // run | |
| 10 | // |
test/incremental/assert_function.16.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | const hello = "hello".*; | |
| 2 | pub fn main() void { | |
| 3 | assert(hello[1] == 'e'); | |
| 4 | } | |
| 5 | ||
| 6 | pub fn assert(ok: bool) void { | |
| 7 | if (!ok) unreachable; // assertion failure | |
| 8 | } | |
| 9 | ||
| 10 | // run | |
| 11 | // |
test/incremental/assert_function.17.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | pub fn main() void { | |
| 2 | var i: u64 = 0xFFEEDDCCBBAA9988; | |
| 3 | assert(i == 0xFFEEDDCCBBAA9988); | |
| 4 | } | |
| 5 | ||
| 6 | pub fn assert(ok: bool) void { | |
| 7 | if (!ok) unreachable; // assertion failure | |
| 8 | } | |
| 9 | ||
| 10 | // run | |
| 11 | // |
test/incremental/assert_function.18.zig created+35| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | const builtin = @import("builtin"); | |
| 2 | ||
| 3 | extern "c" fn write(usize, usize, usize) usize; | |
| 4 | ||
| 5 | pub fn main() void { | |
| 6 | for ("hello") |_| print(); | |
| 7 | } | |
| 8 | ||
| 9 | fn print() void { | |
| 10 | switch (builtin.os.tag) { | |
| 11 | .linux => { | |
| 12 | asm volatile ("syscall" | |
| 13 | : | |
| 14 | : [number] "{rax}" (1), | |
| 15 | [arg1] "{rdi}" (1), | |
| 16 | [arg2] "{rsi}" (@ptrToInt("hello\n")), | |
| 17 | [arg3] "{rdx}" (6), | |
| 18 | : "rcx", "r11", "memory" | |
| 19 | ); | |
| 20 | }, | |
| 21 | .macos => { | |
| 22 | _ = write(1, @ptrToInt("hello\n"), 6); | |
| 23 | }, | |
| 24 | else => unreachable, | |
| 25 | } | |
| 26 | } | |
| 27 | ||
| 28 | // run | |
| 29 | // | |
| 30 | // hello | |
| 31 | // hello | |
| 32 | // hello | |
| 33 | // hello | |
| 34 | // hello | |
| 35 | // |
test/incremental/assert_function.2.zig created+21| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | const c = a + b; // 7 | |
| 7 | const d = a + c; // 10 | |
| 8 | const e = d + b; // 14 | |
| 9 | const f = d + e; // 24 | |
| 10 | const g = e + f; // 38 | |
| 11 | const h = f + g; // 62 | |
| 12 | const i = g + h; // 100 | |
| 13 | assert(i == 100); | |
| 14 | } | |
| 15 | ||
| 16 | pub fn assert(ok: bool) void { | |
| 17 | if (!ok) unreachable; // assertion failure | |
| 18 | } | |
| 19 | ||
| 20 | // run | |
| 21 | // |
test/incremental/assert_function.3.zig created+22| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | pub fn main() void { | |
| 2 | add(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) void { | |
| 6 | const c = a + b; // 7 | |
| 7 | const d = a + c; // 10 | |
| 8 | const e = d + b; // 14 | |
| 9 | const f = d + e; // 24 | |
| 10 | const g = e + f; // 38 | |
| 11 | const h = f + g; // 62 | |
| 12 | const i = g + h; // 100 | |
| 13 | const j = i + d; // 110 | |
| 14 | assert(j == 110); | |
| 15 | } | |
| 16 | ||
| 17 | pub fn assert(ok: bool) void { | |
| 18 | if (!ok) unreachable; // assertion failure | |
| 19 | } | |
| 20 | ||
| 21 | // run | |
| 22 | // |
test/incremental/assert_function.4.zig created+15| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 7); | |
| 3 | assert(add(20, 10) == 30); | |
| 4 | } | |
| 5 | ||
| 6 | fn add(a: u32, b: u32) u32 { | |
| 7 | return a + b; | |
| 8 | } | |
| 9 | ||
| 10 | pub fn assert(ok: bool) void { | |
| 11 | if (!ok) unreachable; // assertion failure | |
| 12 | } | |
| 13 | ||
| 14 | // run | |
| 15 | // |
test/incremental/assert_function.5.zig created+19| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 7); | |
| 3 | assert(add(20, 10) == 30); | |
| 4 | } | |
| 5 | ||
| 6 | fn add(a: u32, b: u32) u32 { | |
| 7 | var x: u32 = undefined; | |
| 8 | x = 0; | |
| 9 | x += a; | |
| 10 | x += b; | |
| 11 | return x; | |
| 12 | } | |
| 13 | ||
| 14 | pub fn assert(ok: bool) void { | |
| 15 | if (!ok) unreachable; // assertion failure | |
| 16 | } | |
| 17 | ||
| 18 | // run | |
| 19 | // |
test/incremental/assert_function.6.zig created+9| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | pub fn main() void { | |
| 2 | const a: u32 = 2; | |
| 3 | const b: ?u32 = a; | |
| 4 | const c = b.?; | |
| 5 | if (c != 2) unreachable; | |
| 6 | } | |
| 7 | ||
| 8 | // run | |
| 9 | // |
test/incremental/assert_function.7.zig created+40| ... | ... | @@ -0,0 +1,40 @@ |
| 1 | const builtin = @import("builtin"); | |
| 2 | ||
| 3 | extern "c" fn write(usize, usize, usize) usize; | |
| 4 | ||
| 5 | pub fn main() void { | |
| 6 | var i: u32 = 0; | |
| 7 | while (i < 4) : (i += 1) print(); | |
| 8 | assert(i == 4); | |
| 9 | } | |
| 10 | ||
| 11 | fn print() void { | |
| 12 | switch (builtin.os.tag) { | |
| 13 | .linux => { | |
| 14 | asm volatile ("syscall" | |
| 15 | : | |
| 16 | : [number] "{rax}" (1), | |
| 17 | [arg1] "{rdi}" (1), | |
| 18 | [arg2] "{rsi}" (@ptrToInt("hello\n")), | |
| 19 | [arg3] "{rdx}" (6), | |
| 20 | : "rcx", "r11", "memory" | |
| 21 | ); | |
| 22 | }, | |
| 23 | .macos => { | |
| 24 | _ = write(1, @ptrToInt("hello\n"), 6); | |
| 25 | }, | |
| 26 | else => unreachable, | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | pub fn assert(ok: bool) void { | |
| 31 | if (!ok) unreachable; // assertion failure | |
| 32 | } | |
| 33 | ||
| 34 | // run | |
| 35 | // | |
| 36 | // hello | |
| 37 | // hello | |
| 38 | // hello | |
| 39 | // hello | |
| 40 | // |
test/incremental/assert_function.8.zig created+36| ... | ... | @@ -0,0 +1,36 @@ |
| 1 | const builtin = @import("builtin"); | |
| 2 | ||
| 3 | extern "c" fn write(usize, usize, usize) usize; | |
| 4 | ||
| 5 | pub fn main() void { | |
| 6 | var i: u32 = 0; | |
| 7 | inline while (i < 4) : (i += 1) print(); | |
| 8 | assert(i == 4); | |
| 9 | } | |
| 10 | ||
| 11 | fn print() void { | |
| 12 | switch (builtin.os.tag) { | |
| 13 | .linux => { | |
| 14 | asm volatile ("syscall" | |
| 15 | : | |
| 16 | : [number] "{rax}" (1), | |
| 17 | [arg1] "{rdi}" (1), | |
| 18 | [arg2] "{rsi}" (@ptrToInt("hello\n")), | |
| 19 | [arg3] "{rdx}" (6), | |
| 20 | : "rcx", "r11", "memory" | |
| 21 | ); | |
| 22 | }, | |
| 23 | .macos => { | |
| 24 | _ = write(1, @ptrToInt("hello\n"), 6); | |
| 25 | }, | |
| 26 | else => unreachable, | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | pub fn assert(ok: bool) void { | |
| 31 | if (!ok) unreachable; // assertion failure | |
| 32 | } | |
| 33 | ||
| 34 | // error | |
| 35 | // | |
| 36 | // :3:21: error: unable to resolve comptime value |
test/incremental/assert_function.9.zig created+22| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | pub fn main() void { | |
| 2 | assert(add(3, 4) == 20); | |
| 3 | } | |
| 4 | ||
| 5 | fn add(a: u32, b: u32) u32 { | |
| 6 | const x: u32 = blk: { | |
| 7 | const c = a + b; // 7 | |
| 8 | const d = a + c; // 10 | |
| 9 | const e = d + b; // 14 | |
| 10 | break :blk e; | |
| 11 | }; | |
| 12 | const y = x + a; // 17 | |
| 13 | const z = y + a; // 20 | |
| 14 | return z; | |
| 15 | } | |
| 16 | ||
| 17 | pub fn assert(ok: bool) void { | |
| 18 | if (!ok) unreachable; // assertion failure | |
| 19 | } | |
| 20 | ||
| 21 | // run | |
| 22 | // |
test/incremental/multiplying_numbers_at_runtime_and_comptime.0.zig created+11| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | pub fn main() void { | |
| 2 | mul(3, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn mul(a: u32, b: u32) void { | |
| 6 | if (a * b != 12) unreachable; | |
| 7 | } | |
| 8 | ||
| 9 | // run | |
| 10 | // target=x86_64-linux,x86_64-macos | |
| 11 | // |
test/incremental/multiplying_numbers_at_runtime_and_comptime.1.zig created+12| ... | ... | @@ -0,0 +1,12 @@ |
| 1 | pub fn main() void { | |
| 2 | if (x - 12 != 0) unreachable; | |
| 3 | } | |
| 4 | ||
| 5 | fn mul(a: u32, b: u32) u32 { | |
| 6 | return a * b; | |
| 7 | } | |
| 8 | ||
| 9 | const x = mul(3, 4); | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/multiplying_numbers_at_runtime_and_comptime.2.zig created+12| ... | ... | @@ -0,0 +1,12 @@ |
| 1 | pub fn main() void { | |
| 2 | var x: usize = 5; | |
| 3 | const y = mul(2, 3, x); | |
| 4 | if (y - 30 != 0) unreachable; | |
| 5 | } | |
| 6 | ||
| 7 | inline fn mul(a: usize, b: usize, c: usize) usize { | |
| 8 | return a * b * c; | |
| 9 | } | |
| 10 | ||
| 11 | // run | |
| 12 | // |
test/incremental/subtracting_numbers_at_runtime.zig created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | pub fn main() void { | |
| 2 | sub(7, 4); | |
| 3 | } | |
| 4 | ||
| 5 | fn sub(a: u32, b: u32) void { | |
| 6 | if (a - b != 3) unreachable; | |
| 7 | } | |
| 8 | ||
| 9 | // run | |
| 10 | // |
test/incremental/unused_vars.zig created+7| ... | ... | @@ -0,0 +1,7 @@ |
| 1 | pub fn main() void { | |
| 2 | const x = 1; | |
| 3 | } | |
| 4 | ||
| 5 | // error | |
| 6 | // | |
| 7 | // :2:11: error: unused local constant |