Skip to content

Commit

Permalink
Added cts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger committed Dec 23, 2024
1 parent 0651f41 commit aa8e87b
Show file tree
Hide file tree
Showing 9 changed files with 1,590 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tests/cts"]
path = tests/cts
url = https://github.com/gpuweb/cts.git
54 changes: 54 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
"type": "module",
"scripts": {
"build": "cd src && tsc",
"test": "npm run test:generate-spec-list && npm run test:rollup && npm run test:jco && echo 'Test wasm built but not run temp/tests.wasm'",
"test:generate-spec-list": "node tests/generate-spec-list.js",
"test:rollup": "rollup --config './tests/rollup.config.js'",
"test:jco": "jco componentize ./temp/tests.js --wit ./wit -n imports -o out/tests.wasm --disable 'http'",
"example": "npm run example:rollup && npm run example:jco",
"example:rollup": "rollup --config './examples/rollup.config.js' --configExample=$npm_config_example",
"example:jco": "mkdir -p out && jco componentize temp/$npm_config_example.js --wit ./wit -n imports -o out/$npm_config_example.wasm --disable 'http'"
},
"devDependencies": {
"@bytecodealliance/jco": "^1.8.1",
"@rollup/plugin-typescript": "^12.1.1",
"@types/dom-mediacapture-transform": "^0.1.10",
"@types/node": "^22.10.2",
"@types/w3c-image-capture": "^1.0.10",
"@webgpu/types": "^0.1.52",
"rollup": "^4.12.0",
"tslib": "^2.8.1",
Expand Down
38 changes: 38 additions & 0 deletions tests/generate-spec-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from "fs";
import path from "path";

main();

async function main() {
const testFiles = await getTestFiles('./tests/cts/src/webgpu');
const result = generateTS(testFiles);
await fs.promises.writeFile('./tests/spec-list.ts', result);
}

async function getTestFiles(dir, out = []) {
for await (const entry of await fs.promises.opendir(dir)) {
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await getTestFiles(entryPath, out);
} else if (entry.isFile() && entry.name.endsWith(".spec.ts")) {
out.push(entryPath.replace("tests/", ""));
}
}
return out;
}

function generateTS(testFiles) {
let imports = "";
let exports = [];
for (const file of testFiles) {
if (file.endsWith("worker.spec.ts")) {
// requires dynamic imports which are not yet supported in ComponentizeJS
continue;
}
const name = file.replace("cts/src/webgpu/", "").replace(".spec.ts", "").replaceAll("/", "_");
imports += `import { g as ${name} } from './${file.replace(".ts", "")}';\n`;
exports.push(name);
}
const result = imports + "\n\n" + "export const specs = {\n " + exports.join(",\n ") + "\n};\n";
return result;
}
55 changes: 55 additions & 0 deletions tests/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getGpu, GPU, GPUAdapter, GPUDevice, GPUQueue, GPUBuffer, GPUBufferUsage, GPUMapMode, GPUTextureUsage, GPUTexture, GPUTextureView, GPUSampler, GPUBindGroupLayout, GPUPipelineLayout, GPUBindGroup, GPUShaderModule, GPUShaderStage, GPUComputePipeline, GPURenderPipeline, GPUColorWrite, GPUCommandEncoder, GPURenderPassEncoder, GPUComputePassEncoder, GPUCommandBuffer, GPUQuerySet } from "../src/index";

class Navigator implements Pick<globalThis.Navigator, "gpu"> {
gpu: GPU;
constructor() {
this.gpu = getGpu();
}
}

function defineGlobal<T>(name: string, value: T) {
Object.defineProperty(globalThis, name, {
value,
writable: true,
enumerable: true,
configurable: true,
});
}

export function declareGlobals() {
defineGlobal("window", globalThis);
defineGlobal("navigator", new Navigator());
defineGlobal("GPU", GPU);
defineGlobal("GPUAdapter", GPUAdapter);
// defineGlobal("GPUAdapterInfo", GPUAdapterInfo);
// defineGlobal("GPUSupportedLimits", GPUSupportedLimits);
// defineGlobal("GPUSupportedFeatures", GPUSupportedFeatures);
// defineGlobal("GPUDeviceLostInfo", GPUDeviceLostInfo);
defineGlobal("GPUDevice", GPUDevice);
defineGlobal("GPUQueue", GPUQueue);
defineGlobal("GPUBuffer", GPUBuffer);
defineGlobal("GPUBufferUsage", GPUBufferUsage);
defineGlobal("GPUMapMode", GPUMapMode);
defineGlobal("GPUTextureUsage", GPUTextureUsage);
defineGlobal("GPUTexture", GPUTexture);
defineGlobal("GPUTextureView", GPUTextureView);
defineGlobal("GPUSampler", GPUSampler);
defineGlobal("GPUBindGroupLayout", GPUBindGroupLayout);
defineGlobal("GPUPipelineLayout", GPUPipelineLayout);
defineGlobal("GPUBindGroup", GPUBindGroup);
defineGlobal("GPUShaderModule", GPUShaderModule);
defineGlobal("GPUShaderStage", GPUShaderStage);
defineGlobal("GPUComputePipeline", GPUComputePipeline);
defineGlobal("GPURenderPipeline", GPURenderPipeline);
defineGlobal("GPUColorWrite", GPUColorWrite);
defineGlobal("GPUCommandEncoder", GPUCommandEncoder);
defineGlobal("GPURenderPassEncoder", GPURenderPassEncoder);
defineGlobal("GPUComputePassEncoder", GPUComputePassEncoder);
defineGlobal("GPUCommandBuffer", GPUCommandBuffer);
// defineGlobal("GPURenderBundleEncoder", GPURenderBundleEncoder);
// defineGlobal("GPURenderBundle", GPURenderBundle);
defineGlobal("GPUQuerySet", GPUQuerySet);
// defineGlobal("GPUError", GPUError);
// defineGlobal("GPUValidationError", GPUValidationError);
// defineGlobal("GPUOutOfMemoryError", GPUOutOfMemoryError);
}
21 changes: 21 additions & 0 deletions tests/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import typescript from '@rollup/plugin-typescript';

export default function() {
return {
input: `./tests/tests.ts`,
external: [
'wasi:webgpu/webgpu',
'wasi:webgpu/surface',
'wasi:webgpu/graphics-context',
],
output: {
file: `temp/tests.js`,
format: 'esm'
},
plugins: [
typescript({
tsconfig: './tests/tsconfig.json',
}),
]
};
};
Loading

0 comments on commit aa8e87b

Please sign in to comment.