blob: e12c309fc7a61dc143f00f98fe1e92016a6f42cb [file] [log] [blame]
<!DOCTYPE html>
<html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>Test the WHLSL test harness.</title>
<script src="js/whlsl-test-harness.js"></script>
<script src="js/webgpu-functions.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
const whlslTests = {};
whlslTests.returnIf = async () => {
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7) {
return y;
}
}
`);
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7) {
return y;
} else {
y = 8;
}
}
`);
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7) {
y = 8;
} else {
return y;
}
}
`);
let program = `
int foo(int x)
{
int y = 6;
if (x == 7) {
return 8;
} else {
return 10;
}
}
`;
assert_equals(await callIntFunction(program, "foo", [makeInt(3)]), 10);
assert_equals(await callIntFunction(program, "foo", [makeInt(4)]), 10);
assert_equals(await callIntFunction(program, "foo", [makeInt(5)]), 10);
assert_equals(await callIntFunction(program, "foo", [makeInt(6)]), 10);
assert_equals(await callIntFunction(program, "foo", [makeInt(7)]), 8);
assert_equals(await callIntFunction(program, "foo", [makeInt(8)]), 10);
assert_equals(await callIntFunction(program, "foo", [makeInt(9)]), 10);
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7) {
return 8;
} else if (x == 9) {
return 10;
}
}
`);
program = `
int foo(int x)
{
int y = 6;
if (x == 7) {
return 8;
} else {
y = 9;
}
return y;
}
`;
assert_equals(await callIntFunction(program, "foo", [makeInt(3)]), 9);
assert_equals(await callIntFunction(program, "foo", [makeInt(4)]), 9);
assert_equals(await callIntFunction(program, "foo", [makeInt(5)]), 9);
assert_equals(await callIntFunction(program, "foo", [makeInt(6)]), 9);
assert_equals(await callIntFunction(program, "foo", [makeInt(7)]), 8);
assert_equals(await callIntFunction(program, "foo", [makeInt(8)]), 9);
assert_equals(await callIntFunction(program, "foo", [makeInt(9)]), 9);
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7) {
return 8;
} else {
return 10;
}
return 11;
}
`);
await checkFail(
`
int foo(int x)
{
int y = 6;
if (x == 7)
int y = 8;
return y;
}
`);
};
whlslTests.returnReferenceToSameParameter = async () => {
let program = `
int foo()
{
return plus(bar(5), bar(7));
}
int plus(thread int* x, thread int* y)
{
return *x + *y;
}
thread int* bar(int x)
{
return &x;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 14);
};
whlslTests.returnReferenceToParameterWithDifferentFunctions = async () => {
let program = `
int foo()
{
return *bar(10) + *baz(20);
}
thread int* bar(int x)
{
return &x;
}
thread int* baz(int y)
{
return &y;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 30);
};
whlslTests.returnReferenceToParameter = async () => {
let program = `
int foo(bool condition)
{
return *bar(condition, 1, 2);
}
thread int* bar(bool condition, int a, int b)
{
return condition ? &a : &b;
}
`;
assert_equals(await callIntFunction(program, "foo", [makeBool(true)]), 1);
assert_equals(await callIntFunction(program, "foo", [makeBool(false)]), 2);
};
whlslTests.returnReferenceToLocalVariableWithNesting = async () => {
let program = `
int foo()
{
return *bar() + *baz();
}
thread int* bar()
{
int a = 20;
return &a;
}
thread int* baz()
{
int a = 22;
return &a;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 42);
};
whlslTests.returnReferenceToLocalVariable = async () => {
let program = `
int foo()
{
return *bar();
}
thread int* bar()
{
int a = 42;
return &a;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 42);
};
whlslTests.returnIntLiteralUint = async () => {
let program = "uint foo() { return 42; }";
assert_equals(await callUintFunction(program, "foo", []), 42);
};
whlslTests.returnIntLiteralFloat = async () => {
let program = "float foo() { return 42; }";
assert_equals(await callFloatFunction(program, "foo", []), 42);
};
runTests(whlslTests);
</script>
</html>