adding loops tests
All checks were successful
Build / build (push) Successful in 2m2s
Test / build (push) Successful in 5m10s

This commit is contained in:
2026-01-18 18:18:21 +01:00
parent 04092e25c1
commit 9868b34f92
6 changed files with 158 additions and 49 deletions

96
test/loops.zig git.filemode.normal_file
View File

@@ -0,0 +1,96 @@
const std = @import("std");
const root = @import("root.zig");
const compileNzsl = root.compileNzsl;
const case = root.case;
test "Simple for loop" {
const allocator = std.testing.allocator;
const base = @mod(case.random(f32), 5.0);
const iterations = 5;
var expected = base;
for (1..iterations) |i| {
expected *= @floatFromInt(i);
}
const shader = try std.fmt.allocPrint(
allocator,
\\ [nzsl_version("1.1")]
\\ [feature(float64)]
\\ module;
\\
\\ struct FragOut
\\ {{
\\ [location(0)] color: vec4[f32]
\\ }}
\\
\\ [entry(frag)]
\\ fn main() -> FragOut
\\ {{
\\ let value = f32({d});
\\ for i in 1 -> {d}
\\ {{
\\ value *= f32(i);
\\ }}
\\ let output: FragOut;
\\ output.color = vec4[f32](value, value, value, value);
\\ return output;
\\ }}
,
.{
base,
iterations,
},
);
defer allocator.free(shader);
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, 4, code, "color", &.{ expected, expected, expected, expected });
}
test "Simple while loop" {
const allocator = std.testing.allocator;
const base = @mod(case.random(f32), 5.0);
const iterations = 5;
var expected = base;
for (1..iterations) |i| {
expected *= @floatFromInt(i);
}
const shader = try std.fmt.allocPrint(
allocator,
\\ [nzsl_version("1.1")]
\\ [feature(float64)]
\\ module;
\\
\\ struct FragOut
\\ {{
\\ [location(0)] color: vec4[f32]
\\ }}
\\
\\ [entry(frag)]
\\ fn main() -> FragOut
\\ {{
\\ let value = f32({d});
\\ let i = 1;
\\ while (i < {d})
\\ {{
\\ value *= f32(i);
\\ i += 1;
\\ }}
\\ let output: FragOut;
\\ output.color = vec4[f32](value, value, value, value);
\\ return output;
\\ }}
,
.{
base,
iterations,
},
);
defer allocator.free(shader);
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, 4, code, "color", &.{ expected, expected, expected, expected });
}

View File

@@ -60,5 +60,6 @@ test {
std.testing.refAllDecls(@import("branching.zig"));
std.testing.refAllDecls(@import("casts.zig"));
std.testing.refAllDecls(@import("functions.zig"));
std.testing.refAllDecls(@import("loops.zig"));
std.testing.refAllDecls(@import("maths.zig"));
}