Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 1 | export const description = ` |
| 2 | Examples of writing CTS tests with various features. |
Kai Ninomiya | cb13fba | 2019-10-24 16:59:36 -0700 | [diff] [blame] | 3 | |
| 4 | Start here when looking for examples of basic framework usage. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 5 | `; |
| 6 | |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 7 | import { TestGroup } from '../common/framework/index.js'; |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 8 | |
| 9 | import { GPUTest } from './gpu_test.js'; |
| 10 | |
| 11 | // To run these tests in the standalone runner, run `grunt build` or `grunt pre` then open: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 12 | // - http://localhost:8080/?runnow=1&q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 13 | // To run in WPT, copy/symlink the out-wpt/ directory as the webgpu/ directory in WPT, then open: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 14 | // - (wpt server url)/webgpu/cts.html?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 15 | // |
| 16 | // Tests here can be run individually or in groups: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 17 | // - ?q=webgpu:examples:basic/async= |
| 18 | // - ?q=webgpu:examples:basic/ |
| 19 | // - ?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 20 | |
| 21 | export const g = new TestGroup(GPUTest); |
| 22 | |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 23 | // Note: spaces in test names are replaced with underscores: webgpu:examples:test_name= |
Kai Ninomiya | 04ec6b6 | 2019-10-25 00:35:32 -0700 | [diff] [blame] | 24 | g.test('test name', t => {}); |
| 25 | |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 26 | g.test('basic', t => { |
| 27 | t.expect(true); |
| 28 | t.expect(true, 'true should be true'); |
| 29 | |
| 30 | t.shouldThrow( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 31 | // The expected '.name' of the thrown error. |
| 32 | 'TypeError', |
| 33 | // This function is run inline inside shouldThrow, and is expected to throw. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 34 | () => { |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 35 | throw new TypeError(); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 36 | }, |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 37 | // Log message. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 38 | 'function should throw Error' |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | g.test('basic/async', async t => { |
| 43 | // shouldReject must be awaited to ensure it can wait for the promise before the test ends. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 44 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 45 | // The expected '.name' of the thrown error. |
| 46 | 'TypeError', |
| 47 | // Promise expected to reject. |
| 48 | Promise.reject(new TypeError()), |
| 49 | // Log message. |
| 50 | 'Promise.reject should reject' |
| 51 | ); |
| 52 | |
| 53 | // Promise can also be an IIFE. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 54 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 55 | 'TypeError', |
| 56 | (async () => { |
| 57 | throw new TypeError(); |
| 58 | })(), |
| 59 | 'Promise.reject should reject' |
| 60 | ); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 61 | }); |
| 62 | |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 63 | // A test can be parameterized with a simple array of objects. |
| 64 | // |
| 65 | // Parameters can be public (x, y) which means they're part of the case name. |
| 66 | // They can also be private by starting with an underscore (_result), which passes |
| 67 | // them into the test but does not make them part of the case name: |
| 68 | // |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame^] | 69 | // - webgpu:examples:basic/params={"x":2,"y":4} runs with t.params = {x: 2, y: 5, _result: 6}. |
| 70 | // - webgpu:examples:basic/params={"x":-10,"y":18} runs with t.params = {x: -10, y: 18, _result: 8}. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 71 | g.test('basic/params', t => { |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 72 | t.expect(t.params.x + t.params.y === t.params._result); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 73 | }).params([ |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 74 | { x: 2, y: 4, _result: 6 }, // |
| 75 | { x: -10, y: 18, _result: 8 }, |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 76 | ]); |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 77 | // (note the blank comment above to enforce newlines on autoformat) |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 78 | |
| 79 | g.test('gpu/async', async t => { |
| 80 | const fence = t.queue.createFence(); |
| 81 | t.queue.signal(fence, 2); |
| 82 | await fence.onCompletion(1); |
| 83 | t.expect(fence.getCompletedValue() === 2); |
| 84 | }); |
| 85 | |
| 86 | g.test('gpu/buffers', async t => { |
| 87 | const data = new Uint32Array([0, 1234, 0]); |
| 88 | const [src, map] = t.device.createBufferMapped({ |
| 89 | size: 12, |
| 90 | usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, |
| 91 | }); |
| 92 | new Uint32Array(map).set(data); |
| 93 | src.unmap(); |
| 94 | |
| 95 | // Use the expectContents helper to check the actual contents of a GPUBuffer. |
| 96 | // Like shouldReject, it must be awaited. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 97 | t.expectContents(src, data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 98 | }); |