Commit c1a98cd65d

Jakub Konka <kubkon@jakubkonka.com>
2022-04-27 12:41:05
test: set case name from initial filename for a sequence
Port more incremental tests.
1 parent 46db5e2
src/test.zig
@@ -233,7 +233,7 @@ const TestManifest = struct {
 
         fn next(self: *TrailingIterator) ?[]const u8 {
             const next_inner = self.inner.next() orelse return null;
-            return std.mem.trim(u8, next_inner, " \t");
+            return std.mem.trim(u8, next_inner[2..], " \t");
         }
     };
 
@@ -1033,31 +1033,25 @@ pub const TestContext = struct {
         for (filenames.items) |filename| {
             current_file.* = filename;
 
-            { // First, check if this file is part of an incremental update sequence
-
-                // Split filename into "<base_name>.<index>.<file_ext>"
-                const prev_parts = getTestFileNameParts(prev_filename);
-                const new_parts = getTestFileNameParts(filename);
-
-                // If base_name and file_ext match, these files are in the same test sequence
-                // and the new one should be the incremented version of the previous test
-                if (std.mem.eql(u8, prev_parts.base_name, new_parts.base_name) and
-                    std.mem.eql(u8, prev_parts.file_ext, new_parts.file_ext))
-                {
-
-                    // This is "foo.X.zig" followed by "foo.Y.zig". Make sure that X = Y + 1
-                    if (prev_parts.test_index == null) return error.InvalidIncrementalTestIndex;
-                    if (new_parts.test_index == null) return error.InvalidIncrementalTestIndex;
-                    if (new_parts.test_index.? != prev_parts.test_index.? + 1) return error.InvalidIncrementalTestIndex;
-                } else {
-
-                    // This is not the same test sequence, so the new file must be the first file
-                    // in a new sequence ("*.0.zig") or an independent test file ("*.zig")
-                    if (new_parts.test_index != null and new_parts.test_index.? != 0) return error.InvalidIncrementalTestIndex;
-
-                    if (strategy == .independent)
-                        cases.clearRetainingCapacity(); // Generate a new independent test case for this update
-                }
+            // First, check if this file is part of an incremental update sequence
+            // Split filename into "<base_name>.<index>.<file_ext>"
+            const prev_parts = getTestFileNameParts(prev_filename);
+            const new_parts = getTestFileNameParts(filename);
+
+            // If base_name and file_ext match, these files are in the same test sequence
+            // and the new one should be the incremented version of the previous test
+            if (std.mem.eql(u8, prev_parts.base_name, new_parts.base_name) and
+                std.mem.eql(u8, prev_parts.file_ext, new_parts.file_ext))
+            {
+                // This is "foo.X.zig" followed by "foo.Y.zig". Make sure that X = Y + 1
+                if (prev_parts.test_index == null) return error.InvalidIncrementalTestIndex;
+                if (new_parts.test_index == null) return error.InvalidIncrementalTestIndex;
+                if (new_parts.test_index.? != prev_parts.test_index.? + 1) return error.InvalidIncrementalTestIndex;
+            } else {
+                // This is not the same test sequence, so the new file must be the first file
+                // in a new sequence ("*.0.zig") or an independent test file ("*.zig")
+                if (new_parts.test_index != null and new_parts.test_index.? != 0) return error.InvalidIncrementalTestIndex;
+                cases.clearRetainingCapacity();
             }
             prev_filename = filename;
 
@@ -1073,12 +1067,23 @@ pub const TestContext = struct {
                 const is_test = manifest.getConfigForKeyAssertSingle("is_test", bool);
                 const output_mode = manifest.getConfigForKeyAssertSingle("output_mode", std.builtin.OutputMode);
 
+                const name_prefix = blk: {
+                    const ext_index = std.mem.lastIndexOfScalar(u8, current_file.*, '.') orelse
+                        return error.InvalidFilename;
+                    const index = std.mem.lastIndexOfScalar(u8, current_file.*[0..ext_index], '.') orelse ext_index;
+                    break :blk current_file.*[0..index];
+                };
+
                 // Cross-product to get all possible test combinations
                 while (backends.next()) |backend| {
                     while (targets.next()) |target| {
+                        const name = try std.fmt.allocPrint(ctx.arena, "{s} ({s})", .{
+                            name_prefix,
+                            try target.zigTriple(ctx.arena),
+                        });
                         const case = try ctx.cases.addOne();
                         case.* = .{
-                            .name = "none",
+                            .name = name,
                             .target = target,
                             .backend = backend,
                             .updates = std.ArrayList(TestContext.Update).init(ctx.cases.allocator),
@@ -1109,6 +1114,10 @@ pub const TestContext = struct {
                         var trailing_it = manifest.trailing();
                         while (trailing_it.next()) |line| {
                             try output.appendSlice(line);
+                            try output.append('\n');
+                        }
+                        if (output.items.len > 0) {
+                            try output.resize(output.items.len - 1);
                         }
                         case.addCompareOutput(src, output.toOwnedSlice());
                     },
test/incremental/add.0.zig → test/incremental/adding_numbers_at_runtime_and_comptime.0.zig
File renamed without changes
test/incremental/add.1.zig → test/incremental/adding_numbers_at_runtime_and_comptime.1.zig
File renamed without changes
test/incremental/add.2.zig → test/incremental/adding_numbers_at_runtime_and_comptime.2.zig
File renamed without changes
test/incremental/assert_function.0.zig
@@ -0,0 +1,15 @@
+pub fn main() void {
+    add(3, 4);
+}
+
+fn add(a: u32, b: u32) void {
+    assert(a + b == 7);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+// target=x86_64-linux,x86_64-macos
+//
test/incremental/assert_function.1.zig
@@ -0,0 +1,17 @@
+pub fn main() void {
+    add(3, 4);
+}
+
+fn add(a: u32, b: u32) void {
+    const c = a + b; // 7
+    const d = a + c; // 10
+    const e = d + b; // 14
+    assert(e == 14);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.10.zig
@@ -0,0 +1,27 @@
+pub fn main() void {
+    assert(add(3, 4) == 116);
+}
+
+fn add(a: u32, b: u32) u32 {
+    const x: u32 = blk: {
+        const c = a + b; // 7
+        const d = a + c; // 10
+        const e = d + b; // 14
+        const f = d + e; // 24
+        const g = e + f; // 38
+        const h = f + g; // 62
+        const i = g + h; // 100
+        const j = i + d; // 110
+        break :blk j;
+    };
+    const y = x + a; // 113
+    const z = y + a; // 116
+    return z;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.11.zig
@@ -0,0 +1,66 @@
+pub fn main() void {
+    assert(add(3, 4) == 1221);
+    assert(mul(3, 4) == 21609);
+}
+
+fn add(a: u32, b: u32) u32 {
+    const x: u32 = blk: {
+        const c = a + b; // 7
+        const d = a + c; // 10
+        const e = d + b; // 14
+        const f = d + e; // 24
+        const g = e + f; // 38
+        const h = f + g; // 62
+        const i = g + h; // 100
+        const j = i + d; // 110
+        const k = i + j; // 210
+        const l = j + k; // 320
+        const m = l + c; // 327
+        const n = m + d; // 337
+        const o = n + e; // 351
+        const p = o + f; // 375
+        const q = p + g; // 413
+        const r = q + h; // 475
+        const s = r + i; // 575
+        const t = s + j; // 685
+        const u = t + k; // 895
+        const v = u + l; // 1215
+        break :blk v;
+    };
+    const y = x + a; // 1218
+    const z = y + a; // 1221
+    return z;
+}
+
+fn mul(a: u32, b: u32) u32 {
+    const x: u32 = blk: {
+        const c = a * a * a * a; // 81
+        const d = a * a * a * b; // 108
+        const e = a * a * b * a; // 108
+        const f = a * a * b * b; // 144
+        const g = a * b * a * a; // 108
+        const h = a * b * a * b; // 144
+        const i = a * b * b * a; // 144
+        const j = a * b * b * b; // 192
+        const k = b * a * a * a; // 108
+        const l = b * a * a * b; // 144
+        const m = b * a * b * a; // 144
+        const n = b * a * b * b; // 192
+        const o = b * b * a * a; // 144
+        const p = b * b * a * b; // 192
+        const q = b * b * b * a; // 192
+        const r = b * b * b * b; // 256
+        const s = c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r; // 2401
+        break :blk s;
+    };
+    const y = x * a; // 7203
+    const z = y * a; // 21609
+    return z;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.12.zig
@@ -0,0 +1,47 @@
+pub fn main() void {
+    assert(add(3, 4) == 791);
+    assert(add(4, 3) == 79);
+}
+
+fn add(a: u32, b: u32) u32 {
+    const x: u32 = if (a < b) blk: {
+        const c = a + b; // 7
+        const d = a + c; // 10
+        const e = d + b; // 14
+        const f = d + e; // 24
+        const g = e + f; // 38
+        const h = f + g; // 62
+        const i = g + h; // 100
+        const j = i + d; // 110
+        const k = i + j; // 210
+        const l = k + c; // 217
+        const m = l + d; // 227
+        const n = m + e; // 241
+        const o = n + f; // 265
+        const p = o + g; // 303
+        const q = p + h; // 365
+        const r = q + i; // 465
+        const s = r + j; // 575
+        const t = s + k; // 785
+        break :blk t;
+    } else blk: {
+        const t = b + b + a; // 10
+        const c = a + t; // 14
+        const d = c + t; // 24
+        const e = d + t; // 34
+        const f = e + t; // 44
+        const g = f + t; // 54
+        const h = c + g; // 68
+        break :blk h + b; // 71
+    };
+    const y = x + a; // 788, 75
+    const z = y + a; // 791, 79
+    return z;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.13.zig
@@ -0,0 +1,19 @@
+pub fn main() void {
+    const ignore =
+        \\ cool thx
+        \\
+    ;
+    _ = ignore;
+    add('ぁ', '\x03');
+}
+
+fn add(a: u32, b: u32) void {
+    assert(a + b == 12356);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.14.zig
@@ -0,0 +1,17 @@
+pub fn main() void {
+    add(aa, bb);
+}
+
+const aa = 'ぁ';
+const bb = '\x03';
+
+fn add(a: u32, b: u32) void {
+    assert(a + b == 12356);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.15.zig
@@ -0,0 +1,10 @@
+pub fn main() void {
+    assert("hello"[0] == 'h');
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.16.zig
@@ -0,0 +1,11 @@
+const hello = "hello".*;
+pub fn main() void {
+    assert(hello[1] == 'e');
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.17.zig
@@ -0,0 +1,11 @@
+pub fn main() void {
+    var i: u64 = 0xFFEEDDCCBBAA9988;
+    assert(i == 0xFFEEDDCCBBAA9988);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.18.zig
@@ -0,0 +1,35 @@
+const builtin = @import("builtin");
+
+extern "c" fn write(usize, usize, usize) usize;
+
+pub fn main() void {
+    for ("hello") |_| print();
+}
+
+fn print() void {
+    switch (builtin.os.tag) {
+        .linux => {
+            asm volatile ("syscall"
+                :
+                : [number] "{rax}" (1),
+                  [arg1] "{rdi}" (1),
+                  [arg2] "{rsi}" (@ptrToInt("hello\n")),
+                  [arg3] "{rdx}" (6),
+                : "rcx", "r11", "memory"
+            );
+        },
+        .macos => {
+            _ = write(1, @ptrToInt("hello\n"), 6);
+        },
+        else => unreachable,
+    }
+}
+
+// run
+//
+// hello
+// hello
+// hello
+// hello
+// hello
+//
test/incremental/assert_function.2.zig
@@ -0,0 +1,21 @@
+pub fn main() void {
+    add(3, 4);
+}
+
+fn add(a: u32, b: u32) void {
+    const c = a + b; // 7
+    const d = a + c; // 10
+    const e = d + b; // 14
+    const f = d + e; // 24
+    const g = e + f; // 38
+    const h = f + g; // 62
+    const i = g + h; // 100
+    assert(i == 100);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.3.zig
@@ -0,0 +1,22 @@
+pub fn main() void {
+    add(3, 4);
+}
+
+fn add(a: u32, b: u32) void {
+    const c = a + b; // 7
+    const d = a + c; // 10
+    const e = d + b; // 14
+    const f = d + e; // 24
+    const g = e + f; // 38
+    const h = f + g; // 62
+    const i = g + h; // 100
+    const j = i + d; // 110
+    assert(j == 110);
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.4.zig
@@ -0,0 +1,15 @@
+pub fn main() void {
+    assert(add(3, 4) == 7);
+    assert(add(20, 10) == 30);
+}
+
+fn add(a: u32, b: u32) u32 {
+    return a + b;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.5.zig
@@ -0,0 +1,19 @@
+pub fn main() void {
+    assert(add(3, 4) == 7);
+    assert(add(20, 10) == 30);
+}
+
+fn add(a: u32, b: u32) u32 {
+    var x: u32 = undefined;
+    x = 0;
+    x += a;
+    x += b;
+    return x;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/assert_function.6.zig
@@ -0,0 +1,9 @@
+pub fn main() void {
+    const a: u32 = 2;
+    const b: ?u32 = a;
+    const c = b.?;
+    if (c != 2) unreachable;
+}
+
+// run
+//
test/incremental/assert_function.7.zig
@@ -0,0 +1,40 @@
+const builtin = @import("builtin");
+
+extern "c" fn write(usize, usize, usize) usize;
+
+pub fn main() void {
+    var i: u32 = 0;
+    while (i < 4) : (i += 1) print();
+    assert(i == 4);
+}
+
+fn print() void {
+    switch (builtin.os.tag) {
+        .linux => {
+            asm volatile ("syscall"
+                :
+                : [number] "{rax}" (1),
+                  [arg1] "{rdi}" (1),
+                  [arg2] "{rsi}" (@ptrToInt("hello\n")),
+                  [arg3] "{rdx}" (6),
+                : "rcx", "r11", "memory"
+            );
+        },
+        .macos => {
+            _ = write(1, @ptrToInt("hello\n"), 6);
+        },
+        else => unreachable,
+    }
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
+// hello
+// hello
+// hello
+// hello
+//
test/incremental/assert_function.8.zig
@@ -0,0 +1,36 @@
+const builtin = @import("builtin");
+
+extern "c" fn write(usize, usize, usize) usize;
+
+pub fn main() void {
+    var i: u32 = 0;
+    inline while (i < 4) : (i += 1) print();
+    assert(i == 4);
+}
+
+fn print() void {
+    switch (builtin.os.tag) {
+        .linux => {
+            asm volatile ("syscall"
+                :
+                : [number] "{rax}" (1),
+                  [arg1] "{rdi}" (1),
+                  [arg2] "{rsi}" (@ptrToInt("hello\n")),
+                  [arg3] "{rdx}" (6),
+                : "rcx", "r11", "memory"
+            );
+        },
+        .macos => {
+            _ = write(1, @ptrToInt("hello\n"), 6);
+        },
+        else => unreachable,
+    }
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// error
+//
+// :3:21: error: unable to resolve comptime value
test/incremental/assert_function.9.zig
@@ -0,0 +1,22 @@
+pub fn main() void {
+    assert(add(3, 4) == 20);
+}
+
+fn add(a: u32, b: u32) u32 {
+    const x: u32 = blk: {
+        const c = a + b; // 7
+        const d = a + c; // 10
+        const e = d + b; // 14
+        break :blk e;
+    };
+    const y = x + a; // 17
+    const z = y + a; // 20
+    return z;
+}
+
+pub fn assert(ok: bool) void {
+    if (!ok) unreachable; // assertion failure
+}
+
+// run
+//
test/incremental/multiplying_numbers_at_runtime_and_comptime.0.zig
@@ -0,0 +1,11 @@
+pub fn main() void {
+    mul(3, 4);
+}
+
+fn mul(a: u32, b: u32) void {
+    if (a * b != 12) unreachable;
+}
+
+// run
+// target=x86_64-linux,x86_64-macos
+//
test/incremental/multiplying_numbers_at_runtime_and_comptime.1.zig
@@ -0,0 +1,12 @@
+pub fn main() void {
+    if (x - 12 != 0) unreachable;
+}
+
+fn mul(a: u32, b: u32) u32 {
+    return a * b;
+}
+
+const x = mul(3, 4);
+
+// run
+//
test/incremental/multiplying_numbers_at_runtime_and_comptime.2.zig
@@ -0,0 +1,12 @@
+pub fn main() void {
+    var x: usize = 5;
+    const y = mul(2, 3, x);
+    if (y - 30 != 0) unreachable;
+}
+
+inline fn mul(a: usize, b: usize, c: usize) usize {
+    return a * b * c;
+}
+
+// run
+//
test/incremental/subtracting_numbers_at_runtime.zig
@@ -0,0 +1,10 @@
+pub fn main() void {
+    sub(7, 4);
+}
+
+fn sub(a: u32, b: u32) void {
+    if (a - b != 3) unreachable;
+}
+
+// run
+//
test/incremental/unused_vars.zig
@@ -0,0 +1,7 @@
+pub fn main() void {
+    const x = 1;
+}
+
+// error
+//
+// :2:11: error: unused local constant