blob: c1f7aac75db48428ee2bb20267f58c24c9db563a [file] [log] [blame]
<!DOCTYPE html>
<html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>Test override subscripts.</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.overrideSubscriptStruct = async () =>
{
let program = `
struct Foo {
int x;
int y;
}
thread int* operator&[](thread Foo* foo, uint index)
{
if (index == 0)
return &foo->x;
if (index == 1)
return &foo->y;
return null;
}
int foo()
{
Foo foo;
foo.x = 498;
foo.y = 19;
return foo[0] + foo[1] * 3;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 498 + 19 * 3);
}
whlslTests.overrideSubscriptStructAndDoStores = async () =>
{
let program = `
struct Foo {
int x;
int y;
}
thread int* operator&[](thread Foo* foo, uint index)
{
if (index == 0)
return &foo->x;
if (index == 1)
return &foo->y;
return null;
}
int foo()
{
Foo foo;
foo[0] = 498;
foo[1] = 19;
return foo.x + foo.y;
}
`;
assert_equals(await callIntFunction(program, "foo", []), 498 + 19);
}
whlslTests.overrideSubscriptStructAndUsePointers = async () =>
{
let program = `
struct Foo {
int x;
int y;
}
thread int* operator&[](thread Foo* foo, uint index)
{
if (index == 0)
return &foo->x;
if (index == 1)
return &foo->y;
return null;
}
int bar(thread Foo* foo)
{
return (*foo)[0] + (*foo)[1];
}
int foo()
{
Foo foo;
foo.x = 498;
foo.y = 19;
return bar(&foo);
}
`;
assert_equals(await callIntFunction(program, "foo", []), 498 + 19);
}
whlslTests.overrideSubscriptStructAndUsePointersIncorrectly = async () =>
{
checkFail(
`
struct Foo {
int x;
int y;
}
thread int* operator&[](thread Foo* foo, uint index)
{
if (index == 0)
return &foo->x;
if (index == 1)
return &foo->y;
return null;
}
int bar(thread Foo* foo)
{
return foo[0] + foo[1];
}
int foo()
{
Foo foo;
foo.x = 498;
foo.y = 19;
return bar(&foo);
}
`);
}
runTests(whlslTests);
</script>
</html>