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 | d4c4dde | 2020-12-08 10:54:28 -0800 | [diff] [blame] | 7 | import { params, pbool, poptions } from '../common/framework/params_builder.js'; |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 8 | import { makeTestGroup } from '../common/framework/test_group.js'; |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 9 | |
| 10 | import { GPUTest } from './gpu_test.js'; |
| 11 | |
| 12 | // 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] | 13 | // - http://localhost:8080/?runnow=1&q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 14 | // 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] | 15 | // - (wpt server url)/webgpu/cts.html?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 16 | // |
| 17 | // Tests here can be run individually or in groups: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 18 | // - ?q=webgpu:examples:basic/async= |
| 19 | // - ?q=webgpu:examples:basic/ |
| 20 | // - ?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 21 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 22 | export const g = makeTestGroup(GPUTest); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 23 | |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 24 | // Note: spaces in test names are replaced with underscores: webgpu:examples:test_name= |
Austin Eng | 639527e | 2020-04-14 16:20:36 -0700 | [diff] [blame] | 25 | /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 26 | g.test('test_name').fn(t => {}); |
Kai Ninomiya | 04ec6b6 | 2019-10-25 00:35:32 -0700 | [diff] [blame] | 27 | |
Kai Ninomiya | fba2630 | 2020-11-04 13:38:05 -0800 | [diff] [blame] | 28 | g.test('not_implemented_yet,without_plan').unimplemented(); |
| 29 | g.test('not_implemented_yet,with_plan') |
| 30 | .desc( |
| 31 | ` |
| 32 | Plan for this test. What it tests. Summary of how it tests that functionality. |
| 33 | - Description of cases, by describing parameters {a, b, c} |
| 34 | - x= more parameters {x, y, z} |
| 35 | ` |
| 36 | ) |
| 37 | .unimplemented(); |
| 38 | |
Kai Ninomiya | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 39 | g.test('basic').fn(t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 40 | t.expect(true); |
| 41 | t.expect(true, 'true should be true'); |
| 42 | |
| 43 | t.shouldThrow( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 44 | // The expected '.name' of the thrown error. |
| 45 | 'TypeError', |
| 46 | // This function is run inline inside shouldThrow, and is expected to throw. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 47 | () => { |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 48 | throw new TypeError(); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 49 | }, |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 50 | // Log message. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 51 | 'function should throw Error' |
| 52 | ); |
| 53 | }); |
| 54 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 55 | g.test('basic,async').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 56 | // 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] | 57 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 58 | // The expected '.name' of the thrown error. |
| 59 | 'TypeError', |
| 60 | // Promise expected to reject. |
| 61 | Promise.reject(new TypeError()), |
| 62 | // Log message. |
| 63 | 'Promise.reject should reject' |
| 64 | ); |
| 65 | |
| 66 | // Promise can also be an IIFE. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 67 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 68 | 'TypeError', |
| 69 | (async () => { |
| 70 | throw new TypeError(); |
| 71 | })(), |
| 72 | 'Promise.reject should reject' |
| 73 | ); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 74 | }); |
| 75 | |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 76 | // A test can be parameterized with a simple array of objects using .cases(). |
| 77 | // Each such instance of the test is a "case". |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 78 | // |
| 79 | // Parameters can be public (x, y) which means they're part of the case name. |
| 80 | // They can also be private by starting with an underscore (_result), which passes |
| 81 | // them into the test but does not make them part of the case name: |
| 82 | // |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 83 | // In this example, the following cases are generated (identified by their "query string"): |
| 84 | // - webgpu:examples:basic,cases:x=2;y=4 runs once, with t.params set to: |
| 85 | // - { x: 2, y: 4, _result: 6 } |
| 86 | // - webgpu:examples:basic,cases:x=-10;y=18 runs once, with t.params set to: |
| 87 | // - { x: -10, y: 18, _result: 8 } |
| 88 | |
| 89 | g.test('basic,cases') |
| 90 | .cases([ |
Kai Ninomiya | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 91 | { x: 2, y: 4, _result: 6 }, // |
| 92 | { x: -10, y: 18, _result: 8 }, |
| 93 | ]) |
| 94 | .fn(t => { |
| 95 | t.expect(t.params.x + t.params.y === t.params._result); |
| 96 | }); |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 97 | // (note the blank comment above to enforce newlines on autoformat) |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 98 | |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 99 | // Each case can be further parameterized using .subcases(). |
| 100 | // Each such instance of the test is a "subcase", which cannot be run independently of other |
| 101 | // subcases. It is analogous to wrapping the entire fn body in a for-loop. |
| 102 | // |
| 103 | // In this example, the following cases are generated (identified by their "query string"): |
| 104 | // - webgpu:examples:basic,cases:x=1 runs twice, with t.params set to each of: |
| 105 | // - { x: 1, a: 2 } |
| 106 | // - { x: 1, b: 2 } |
| 107 | // - webgpu:examples:basic,cases:x=2 runs twice, with t.params set to each of: |
| 108 | // - { x: 2, a: 3 } |
| 109 | // - { x: 2, b: 2 } |
| 110 | |
| 111 | g.test('basic,subcases') |
| 112 | .cases([{ x: 1 }, { x: 2 }]) |
| 113 | .subcases(p => [{ a: p.x + 1 }, { b: 2 }]) |
| 114 | .fn(t => { |
| 115 | t.expect( |
| 116 | ((t.params.a === 2 || t.params.a === 3) && t.params.b === undefined) || |
| 117 | (t.params.a === undefined && t.params.b === 2) |
| 118 | ); |
| 119 | }); |
| 120 | |
Kai Ninomiya | d4c4dde | 2020-12-08 10:54:28 -0800 | [diff] [blame] | 121 | // Runs the following cases: |
| 122 | // { x: 2, y: 2 } |
| 123 | // { x: 2, z: 3 } |
| 124 | // { x: 3, y: 2 } |
| 125 | // { x: 3, z: 3 } |
| 126 | g.test('basic,params_builder') |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 127 | .cases( |
Kai Ninomiya | d4c4dde | 2020-12-08 10:54:28 -0800 | [diff] [blame] | 128 | params() |
| 129 | .combine(poptions('x', [2, 3])) |
| 130 | .combine([{ y: 2 }, { z: 3 }]) |
| 131 | ) |
| 132 | .fn(() => {}); |
| 133 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 134 | g.test('gpu,async').fn(async t => { |
Kai Ninomiya | 2f242cf | 2021-04-14 14:12:03 -0700 | [diff] [blame] | 135 | const x = await t.queue.onSubmittedWorkDone(); |
| 136 | t.expect(x === undefined); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 137 | }); |
| 138 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 139 | g.test('gpu,buffers').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 140 | const data = new Uint32Array([0, 1234, 0]); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 141 | const src = t.device.createBuffer({ |
| 142 | mappedAtCreation: true, |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 143 | size: 12, |
| 144 | usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, |
| 145 | }); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 146 | new Uint32Array(src.getMappedRange()).set(data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 147 | src.unmap(); |
| 148 | |
| 149 | // Use the expectContents helper to check the actual contents of a GPUBuffer. |
| 150 | // Like shouldReject, it must be awaited. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 151 | t.expectContents(src, data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 152 | }); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 153 | |
| 154 | // One of the following two tests should be skipped on most platforms. |
| 155 | |
| 156 | g.test('gpu,with_texture_compression,bc') |
Kai Ninomiya | feb406c | 2020-11-19 15:11:13 -0800 | [diff] [blame] | 157 | .desc( |
| 158 | `Example of a test using a device descriptor. |
| 159 | Tests that a BC format passes validation iff the feature is enabled.` |
| 160 | ) |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 161 | .cases(pbool('textureCompressionBC')) |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 162 | .fn(async t => { |
| 163 | const { textureCompressionBC } = t.params; |
| 164 | |
| 165 | if (textureCompressionBC) { |
Kai Ninomiya | 0d21f75 | 2021-04-14 14:41:48 -0700 | [diff] [blame^] | 166 | await t.selectDeviceOrSkipTestCase('texture-compression-bc'); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const shouldError = !textureCompressionBC; |
| 170 | t.expectGPUError( |
| 171 | 'validation', |
| 172 | () => { |
| 173 | t.device.createTexture({ |
| 174 | format: 'bc1-rgba-unorm', |
| 175 | size: [4, 4, 1], |
| 176 | usage: GPUTextureUsage.SAMPLED, |
| 177 | }); |
| 178 | }, |
| 179 | shouldError |
| 180 | ); |
| 181 | }); |
| 182 | |
| 183 | g.test('gpu,with_texture_compression,etc') |
Kai Ninomiya | feb406c | 2020-11-19 15:11:13 -0800 | [diff] [blame] | 184 | .desc( |
| 185 | `Example of a test using a device descriptor. |
| 186 | |
| 187 | TODO: Test that an ETC format passes validation iff the feature is enabled.` |
| 188 | ) |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 189 | .cases(pbool('textureCompressionETC')) |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 190 | .fn(async t => { |
| 191 | const { textureCompressionETC } = t.params; |
| 192 | |
| 193 | if (textureCompressionETC) { |
Kai Ninomiya | 2f242cf | 2021-04-14 14:12:03 -0700 | [diff] [blame] | 194 | await t.selectDeviceOrSkipTestCase('texture-compression-etc' as GPUFeatureName); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 197 | // TODO: Should actually test createTexture with an ETC format here. |
| 198 | }); |