improving mul test
Some checks failed
Build / build (push) Successful in 55s
Test / build (push) Failing after 4m26s

This commit is contained in:
2026-01-14 20:14:23 +01:00
parent b8a564e135
commit 88e847e2d9
4 changed files with 103 additions and 85 deletions

View File

@@ -25,5 +25,5 @@ test "FMul vec4[f32]" {
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &.{ 4, 3, 2, 1 });
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
}

View File

@@ -5,25 +5,26 @@ const case = root.case;
test "Mul vec4" {
const allocator = std.testing.allocator;
const types = [_]type{ f32, i32 };
const types = [_]type{
f32,
//f64,
i32,
u32,
};
inline for (types) |T| {
const prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const base_color: [4]T = undefined;
std.Random.shuffle(prng, T, base_color);
const ratio: [4]T = undefined;
std.Random.shuffle(prng, T, ratio);
const expected = [4]T{
base_color[0] * ratio[0],
base_color[1] * ratio[1],
base_color[2] * ratio[2],
base_color[3] * ratio[3],
const base_color = case.random(@Vector(4, T));
const ratio = case.random(@Vector(4, T));
const expected = switch (@typeInfo(T)) {
.float => base_color * ratio,
.int => @mulWithOverflow(base_color, ratio)[0],
else => unreachable,
};
const shader = try std.fmt.allocPrint(
allocator,
\\ [nzsl_version("1.1")]
\\ [feature(float64)]
\\ module;
\\
\\ struct FragOut
@@ -41,21 +42,23 @@ test "Mul vec4" {
\\ return output;
\\ }}
,
@typeName(T),
@typeName(T),
ratio[0],
ratio[1],
ratio[2],
ratio[3],
@typeName(T),
base_color[0],
base_color[1],
base_color[2],
base_color[3],
.{
@typeName(T),
@typeName(T),
ratio[0],
ratio[1],
ratio[2],
ratio[3],
@typeName(T),
base_color[0],
base_color[1],
base_color[2],
base_color[3],
},
);
defer allocator.free(shader);
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &expected);
try case.expectOutput(T, 4, code, "color", &@as([4]T, expected));
}
}

View File

@@ -20,7 +20,7 @@ pub fn compileNzsl(allocator: std.mem.Allocator, source: []const u8) ![]const u3
}
pub const case = struct {
pub fn expectOutput(comptime T: type, source: []const u32, output_name: []const u8, comptime expected: []const T) !void {
pub fn expectOutput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T) !void {
const allocator = std.testing.allocator;
var module = try spv.Module.init(allocator, source);
@@ -30,11 +30,29 @@ pub const case = struct {
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));
var output: [len]T = undefined;
try rt.readOutput(T, output[0..len], try rt.getResultByName(output_name));
try std.testing.expectEqualSlices(T, expected, &output);
}
pub fn random(comptime T: type) T {
var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const rand = prng.random();
return switch (@typeInfo(T)) {
.int => rand.int(T),
.float => rand.float(T),
.vector => |v| blk: {
var vec: @Vector(v.len, v.child) = undefined;
for (0..v.len) |i| {
vec[i] = random(v.child);
}
break :blk vec;
},
inline else => unreachable,
};
}
};
test {