adding loops tests
This commit is contained in:
96
test/loops.zig
git.filemode.normal_file
96
test/loops.zig
git.filemode.normal_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 });
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user