blob: 646b80af5859d1e223315dc2467de0d8cd171c14 [file] [log] [blame]
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07001export const description = `
2Examples of writing CTS tests with various features.
Kai Ninomiyacb13fba2019-10-24 16:59:36 -07003
4Start here when looking for examples of basic framework usage.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07005`;
6
Kai Ninomiya9cbb8002020-05-18 15:33:41 -07007import { makeTestGroup } from '../common/framework/test_group.js';
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07008
9import { GPUTest } from './gpu_test.js';
10
11// To run these tests in the standalone runner, run `grunt build` or `grunt pre` then open:
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070012// - http://localhost:8080/?runnow=1&q=webgpu:examples:
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070013// To run in WPT, copy/symlink the out-wpt/ directory as the webgpu/ directory in WPT, then open:
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070014// - (wpt server url)/webgpu/cts.html?q=webgpu:examples:
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070015//
16// Tests here can be run individually or in groups:
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070017// - ?q=webgpu:examples:basic/async=
18// - ?q=webgpu:examples:basic/
19// - ?q=webgpu:examples:
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070020
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070021export const g = makeTestGroup(GPUTest);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070022
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070023// Note: spaces in test names are replaced with underscores: webgpu:examples:test_name=
Austin Eng639527e2020-04-14 16:20:36 -070024/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070025g.test('test_name').fn(t => {});
Kai Ninomiya04ec6b62019-10-25 00:35:32 -070026
Kai Ninomiyafb584a52020-04-16 16:43:24 -070027g.test('basic').fn(t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070028 t.expect(true);
29 t.expect(true, 'true should be true');
30
31 t.shouldThrow(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070032 // The expected '.name' of the thrown error.
33 'TypeError',
34 // This function is run inline inside shouldThrow, and is expected to throw.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070035 () => {
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070036 throw new TypeError();
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070037 },
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070038 // Log message.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070039 'function should throw Error'
40 );
41});
42
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070043g.test('basic,async').fn(async t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070044 // shouldReject must be awaited to ensure it can wait for the promise before the test ends.
Kai Ninomiyac42e2982019-10-23 15:44:28 -070045 t.shouldReject(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070046 // The expected '.name' of the thrown error.
47 'TypeError',
48 // Promise expected to reject.
49 Promise.reject(new TypeError()),
50 // Log message.
51 'Promise.reject should reject'
52 );
53
54 // Promise can also be an IIFE.
Kai Ninomiyac42e2982019-10-23 15:44:28 -070055 t.shouldReject(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070056 'TypeError',
57 (async () => {
58 throw new TypeError();
59 })(),
60 'Promise.reject should reject'
61 );
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070062});
63
Kai Ninomiya13820262019-10-17 15:30:56 -070064// A test can be parameterized with a simple array of objects.
65//
66// Parameters can be public (x, y) which means they're part of the case name.
67// They can also be private by starting with an underscore (_result), which passes
68// them into the test but does not make them part of the case name:
69//
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070070// - webgpu:examples:basic/params={"x":2,"y":4} runs with t.params = {x: 2, y: 5, _result: 6}.
71// - webgpu:examples:basic/params={"x":-10,"y":18} runs with t.params = {x: -10, y: 18, _result: 8}.
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070072g.test('basic,params')
Kai Ninomiyafb584a52020-04-16 16:43:24 -070073 .params([
74 { x: 2, y: 4, _result: 6 }, //
75 { x: -10, y: 18, _result: 8 },
76 ])
77 .fn(t => {
78 t.expect(t.params.x + t.params.y === t.params._result);
79 });
Kai Ninomiya13820262019-10-17 15:30:56 -070080// (note the blank comment above to enforce newlines on autoformat)
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070081
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070082g.test('gpu,async').fn(async t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070083 const fence = t.queue.createFence();
84 t.queue.signal(fence, 2);
85 await fence.onCompletion(1);
86 t.expect(fence.getCompletedValue() === 2);
87});
88
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070089g.test('gpu,buffers').fn(async t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070090 const data = new Uint32Array([0, 1234, 0]);
Kai Ninomiyabe420af2020-07-24 18:32:40 -070091 const src = t.device.createBuffer({
92 mappedAtCreation: true,
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070093 size: 12,
94 usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
95 });
Kai Ninomiyabe420af2020-07-24 18:32:40 -070096 new Uint32Array(src.getMappedRange()).set(data);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070097 src.unmap();
98
99 // Use the expectContents helper to check the actual contents of a GPUBuffer.
100 // Like shouldReject, it must be awaited.
Kai Ninomiyac42e2982019-10-23 15:44:28 -0700101 t.expectContents(src, data);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700102});