From bb40e5b33f6b361c6271dbeb9c6363af98bf4b11 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sun, 18 Jan 2026 02:16:35 +0100 Subject: [PATCH] adding unit tests; ci skip --- example/main.zig | 4 +- example/shader.nzsl | 9 ++- example/shader.spv | Bin 936 -> 1168 bytes example/shader.spv.txt | 121 +++++++++++++++++------------- test/functions.zig | 162 +++++++++++++++++++++++++++++++++++++++++ test/root.zig | 1 + 6 files changed, 239 insertions(+), 58 deletions(-) create mode 100644 test/functions.zig diff --git a/example/main.zig b/example/main.zig index 169a6cc..7f778e5 100644 --- a/example/main.zig +++ b/example/main.zig @@ -17,8 +17,8 @@ pub fn main() !void { defer rt.deinit(allocator); try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main")); - var output: [4]f32 = undefined; - try rt.readOutput(f32, output[0..output.len], try rt.getResultByName("color")); + var output: [4]i32 = undefined; + try rt.readOutput(i32, output[0..output.len], try rt.getResultByName("color")); std.log.info("Output: Vec4{any}", .{output}); } std.log.info("Successfully executed", .{}); diff --git a/example/shader.nzsl b/example/shader.nzsl index ec65fc3..0058b30 100644 --- a/example/shader.nzsl +++ b/example/shader.nzsl @@ -4,18 +4,19 @@ module; struct FragOut { - [location(0)] color: vec4[f32] + [location(0)] color: vec4[i32] } -fn computeColor(val: f32) -> f32 +fn fibonacci(n: i32) -> i32 { - return 2.0 * val; + if (n <= i32(1)) return n; + return fibonacci(n - i32(1)) + fibonacci(n - i32(2)); } [entry(frag)] fn main() -> FragOut { let output: FragOut; - output.color = vec4[f32](computeColor(1.0), computeColor(2.0), computeColor(3.0), computeColor(4.0)); + output.color = vec4[i32](fibonacci(2), fibonacci(2), fibonacci(2), fibonacci(2)); return output; } diff --git a/example/shader.spv b/example/shader.spv index afda241dd18c7ecce07cf702cbdd38dcaaeb6c24..bc61751b4aa39d96b9ed1e735253978c58f5e3a0 100644 GIT binary patch literal 1168 zcmZ9KTTc{05QR&YLA>E|6R$Yya#28#7ZnIbU(`4B#rJiiW)s;>7I-K8C4Zb~BJulX zdctHosi~@Ss!w;-bYrZ2HR4K~HCJOh$~7HhAQAfO_^D_VgxKwvh;e)qJM8rO5;uyx zQDBHs-H2-v&9~HM@#Wj_s5j{E=G&Xa5gF)B==a0UkAstA6tg0COLy>dFvMG-<{c#pOM#&aWq4{1aXH);2KDRong6ykic& l$LE;+`cBm|x88r@JKn!t$DN|8@%o174YRa^4xVr8$EiwGQFflXh#3ivg1$AZR<%z3O zncL?@!c)T3fv+iIkHl!RE`}=$&sjdb&wJo`8OOh0u;}B`~WatG4+{~x3Du| zdSNH@bEuB#lU;J)j`Yi{CE2&4CHZsRBd_fEFL$pZ%r4-6l?y&rCUw~b--5$#s8yAx z2WDQD`JQ-hWel&5;k7Zm?(l{B {s} + \\ {{ + \\ return {d}; + \\ }} + \\ + \\ [entry(frag)] + \\ fn main() -> FragOut + \\ {{ + \\ let output: FragOut; + \\ output.color = vec4[{s}](value(), value(), value(), value()); + \\ return output; + \\ }} + , + .{ + @typeName(T), + @typeName(T), + n, + @typeName(T), + }, + ); + defer allocator.free(shader); + const code = try compileNzsl(allocator, shader); + defer allocator.free(code); + try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n }); + } +} + +test "Nested function calls" { + const allocator = std.testing.allocator; + const types = [_]type{ i32, u32, f32, f64 }; + + inline for (types) |T| { + const n = case.random(T); + + const shader = try std.fmt.allocPrint( + allocator, + \\ [nzsl_version("1.1")] + \\ [feature(float64)] + \\ module; + \\ + \\ struct FragOut + \\ {{ + \\ [location(0)] color: vec4[{s}] + \\ }} + \\ + \\ fn deepValue() -> {s} + \\ {{ + \\ return {d}; + \\ }} + \\ + \\ fn value() -> {s} + \\ {{ + \\ return deepValue(); + \\ }} + \\ + \\ [entry(frag)] + \\ fn main() -> FragOut + \\ {{ + \\ let output: FragOut; + \\ output.color = vec4[{s}](value(), value(), value(), value()); + \\ return output; + \\ }} + , + .{ + @typeName(T), + @typeName(T), + n, + @typeName(T), + @typeName(T), + }, + ); + defer allocator.free(shader); + const code = try compileNzsl(allocator, shader); + defer allocator.free(code); + try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n }); + } +} + +test "Recursive function calls" { + const allocator = std.testing.allocator; + const types = [_]type{ i32, u32, f32, f64 }; + + inline for (types) |T| { + const iterations = 10; + + const fib = struct { + fn onacci(n: T) T { + if (n <= 0) return n; + return onacci(n - 1) + onacci(n - 2); + } + }; + const expected = fib.onacci(iterations); + + const shader = try std.fmt.allocPrint( + allocator, + \\ [nzsl_version("1.1")] + \\ [feature(float64)] + \\ module; + \\ + \\ struct FragOut + \\ {{ + \\ [location(0)] color: vec4[{s}] + \\ }} + \\ + \\ fn fibonacci(n: {s}) -> {s} + \\ {{ + \\ if (n <= {s}(1)) return n; + \\ return fibonacci(n - {s}(1)) + fibonacci(n - {s}(2)); + \\ }} + \\ + \\ [entry(frag)] + \\ fn main() -> FragOut + \\ {{ + \\ let output: FragOut; + \\ output.color = vec4[{s}](fibonacci({d}), fibonacci({d}), fibonacci({d}), fibonacci({d})); + \\ return output; + \\ }} + , + .{ + @typeName(T), + @typeName(T), + @typeName(T), + @typeName(T), + @typeName(T), + @typeName(T), + @typeName(T), + iterations, + iterations, + iterations, + iterations, + }, + ); + defer allocator.free(shader); + std.debug.print("{s}\n\n", .{shader}); + const code = try compileNzsl(allocator, shader); + defer allocator.free(code); + try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected }); + } +} diff --git a/test/root.zig b/test/root.zig index 6c7ec9c..becad8d 100644 --- a/test/root.zig +++ b/test/root.zig @@ -59,5 +59,6 @@ test { std.testing.refAllDecls(@import("basics.zig")); std.testing.refAllDecls(@import("branching.zig")); std.testing.refAllDecls(@import("casts.zig")); + std.testing.refAllDecls(@import("functions.zig")); std.testing.refAllDecls(@import("maths.zig")); }