adding proper unit testing
Some checks failed
Build / build (push) Successful in 1m26s
Test / build (push) Failing after 5m16s

This commit is contained in:
2026-01-13 00:06:49 +01:00
parent 14e802709c
commit dacd67b858
6 changed files with 198 additions and 32 deletions

29
test/basics.zig git.filemode.normal_file
View File

@@ -0,0 +1,29 @@
const std = @import("std");
const root = @import("root.zig");
const compileNzsl = root.compileNzsl;
const case = root.case;
test "FMul vec4[f32]" {
const allocator = std.testing.allocator;
const shader =
\\ [nzsl_version("1.1")]
\\ module;
\\
\\ struct FragOut
\\ {
\\ [location(0)] color: vec4[f32]
\\ }
\\
\\ [entry(frag)]
\\ fn main() -> FragOut
\\ {
\\ let output: FragOut;
\\ output.color = vec4[f32](4.0, 3.0, 2.0, 1.0);
\\ return output;
\\ }
;
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &.{ 4, 3, 2, 1 });
}

58
test/maths.zig git.filemode.normal_file
View File

@@ -0,0 +1,58 @@
const std = @import("std");
const root = @import("root.zig");
const compileNzsl = root.compileNzsl;
const case = root.case;
test "FMul vec4[f32]" {
const allocator = std.testing.allocator;
const shader =
\\ [nzsl_version("1.1")]
\\ module;
\\
\\ struct FragOut
\\ {
\\ [location(0)] color: vec4[f32]
\\ }
\\
\\ [entry(frag)]
\\ fn main() -> FragOut
\\ {
\\ let ratio = vec4[f32](2.0, 2.0, 8.0, 0.25);
\\
\\ let output: FragOut;
\\ output.color = vec4[f32](4.0, 3.0, 2.0, 1.0) * ratio;
\\ return output;
\\ }
;
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &.{ 8, 6, 16, 0.25 });
}
test "IMul vec4[i32]" {
const allocator = std.testing.allocator;
const shader =
\\ [nzsl_version("1.1")]
\\ module;
\\
\\ struct FragOut
\\ {
\\ [location(0)] color: vec4[i32]
\\ }
\\
\\ [entry(frag)]
\\ fn main() -> FragOut
\\ {
\\ let ratio = vec4[i32](2, 2, 8, 25);
\\
\\ let output: FragOut;
\\ output.color = vec4[i32](4, 3, 2, 1) * ratio;
\\ return output;
\\ }
;
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(i32, code, "color", &.{ 8, 6, 16, 25 });
}

43
test/root.zig git.filemode.normal_file
View File

@@ -0,0 +1,43 @@
const std = @import("std");
const spv = @import("spv");
const nzsl = @import("nzsl");
pub fn compileNzsl(allocator: std.mem.Allocator, source: []const u8) ![]const u32 {
const module = try nzsl.parser.parseSource(source);
defer module.deinit();
const params = try nzsl.BackendParameters.init();
defer params.deinit();
params.setDebugLevel(.full);
const writer = try nzsl.SpirvWriter.init();
defer writer.deinit();
const output = try writer.generate(module, params);
defer output.deinit();
return allocator.dupe(u32, output.getCode());
}
pub const case = struct {
pub fn expectOutput(comptime T: type, source: []const u32, output_name: []const u8, comptime expected: []const T) !void {
const allocator = std.testing.allocator;
var module = try spv.Module.init(allocator, source);
defer module.deinit(allocator);
var rt = try spv.Runtime.init(allocator, &module);
defer rt.deinit(allocator);
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
var output: [expected.len]T = undefined;
try rt.readOutput(T, output[0..output.len], try rt.getResultByName(output_name));
try std.testing.expectEqualSlices(T, expected, &output);
}
};
test {
std.testing.refAllDecls(@import("basics.zig"));
std.testing.refAllDecls(@import("maths.zig"));
}