improving tests
This commit is contained in:
@@ -26,5 +26,10 @@ test "Simple array" {
|
|||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
|
|
||||||
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]f32{ 4, 3, 2, 1 }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,5 +25,10 @@ test "Simple fragment shader" {
|
|||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
|
|
||||||
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]f32{ 4, 3, 2, 1 }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,12 @@ test "Bitwise primitives" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +147,12 @@ test "Bitwise vectors" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, L, code, "color", &@as([L]T, expected));
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&@as([L]T, expected)),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,12 @@ test "Simple branching" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,12 @@ test "Primitives casts" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T[1], 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T[1]{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +108,11 @@ test "Primitives bitcasts" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T[1], 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T[1]{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,12 @@ test "Simple function calls" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T{ n, n, n, n }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +100,11 @@ test "Nested function calls" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, 4, code, "color", &.{ n, n, n, n });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T{ n, n, n, n }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,15 @@ test "Inputs" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutputWithInput(T, L, code, "color", &@as([L]T, input.val), "pos", &@as([L]T, input.val));
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.inputs = &.{
|
||||||
|
std.mem.asBytes(&@as([L]T, input.val)),
|
||||||
|
},
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&@as([L]T, input.val)),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,5 +47,10 @@ test "Simple while loop" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(f32, 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]f32{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,12 @@ test "Maths primitives" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, 4, code, "color", &.{ expected, expected, expected, expected });
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&[_]T{ expected, expected, expected, expected }),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,7 +144,12 @@ test "Maths vectors" {
|
|||||||
defer allocator.free(shader);
|
defer allocator.free(shader);
|
||||||
const code = try compileNzsl(allocator, shader);
|
const code = try compileNzsl(allocator, shader);
|
||||||
defer allocator.free(code);
|
defer allocator.free(code);
|
||||||
try case.expectOutput(T, L, code, "color", &@as([L]T, expected));
|
try case.expect(.{
|
||||||
|
.source = code,
|
||||||
|
.expected_outputs = &.{
|
||||||
|
std.mem.asBytes(&@as([L]T, expected)),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,34 +20,13 @@ pub fn compileNzsl(allocator: std.mem.Allocator, source: []const u8) ![]const u3
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub const case = struct {
|
pub const case = struct {
|
||||||
pub fn expectOutput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T) !void {
|
pub const Config = struct {
|
||||||
const allocator = std.testing.allocator;
|
source: []const u32,
|
||||||
|
inputs: []const []const u8 = &.{},
|
||||||
const module_options = [_]spv.Module.ModuleOptions{
|
expected_outputs: []const []const u8,
|
||||||
.{
|
|
||||||
.use_simd_vectors_specializations = true,
|
|
||||||
},
|
|
||||||
.{
|
|
||||||
.use_simd_vectors_specializations = false,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (module_options) |opt| {
|
pub fn expect(config: Config) !void {
|
||||||
var module = try spv.Module.init(allocator, source, opt);
|
|
||||||
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: [len]T = undefined;
|
|
||||||
try rt.readOutput(std.mem.sliceAsBytes(output[0..]), try rt.getResultByName(output_name));
|
|
||||||
|
|
||||||
try std.testing.expectEqualSlices(T, expected, &output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expectOutputWithInput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T, input_name: []const u8, input: []const T) !void {
|
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
// To test with all important module options
|
// To test with all important module options
|
||||||
@@ -61,19 +40,25 @@ pub const case = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (module_options) |opt| {
|
for (module_options) |opt| {
|
||||||
var module = try spv.Module.init(allocator, source, opt);
|
var module = try spv.Module.init(allocator, config.source, opt);
|
||||||
defer module.deinit(allocator);
|
defer module.deinit(allocator);
|
||||||
|
|
||||||
var rt = try spv.Runtime.init(allocator, &module);
|
var rt = try spv.Runtime.init(allocator, &module);
|
||||||
defer rt.deinit(allocator);
|
defer rt.deinit(allocator);
|
||||||
|
|
||||||
try rt.writeInput(std.mem.sliceAsBytes(input[0..len]), try rt.getResultByName(input_name));
|
for (config.inputs, 0..) |input, n| {
|
||||||
|
try rt.writeInput(input[0..], module.input_locations[n]);
|
||||||
|
}
|
||||||
|
|
||||||
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
|
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
|
||||||
var output: [len]T = undefined;
|
|
||||||
try rt.readOutput(std.mem.sliceAsBytes(output[0..]), try rt.getResultByName(output_name));
|
|
||||||
|
|
||||||
try std.testing.expectEqualSlices(T, expected, &output);
|
for (config.expected_outputs, 0..) |expected, n| {
|
||||||
|
const output = try allocator.alloc(u8, expected.len);
|
||||||
|
defer allocator.free(output);
|
||||||
|
|
||||||
|
try rt.readOutput(output[0..], module.output_locations[n]);
|
||||||
|
try std.testing.expectEqualSlices(u8, expected, output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user