summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/execution/expression/constructor/non_zero.spec.ts
blob: c28d3a8edb052375e3071bf5728caa9db3a04f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
export const description = `
Execution Tests for value constructors from components
`;

import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import { GPUTest } from '../../../../gpu_test.js';
import {
  ArrayValue,
  MatrixType,
  ScalarKind,
  Type,
  Value,
  VectorType,
  VectorValue,
  scalarTypeOf,
  vec2,
  vec3,
} from '../../../../util/conversion.js';
import { FP } from '../../../../util/floating_point.js';
import { allInputSources, basicExpressionBuilder, run } from '../expression.js';

export const g = makeTestGroup(GPUTest);

/** @returns true if 'v' is 'min' or 'max' */
function isMinOrMax(v: number | 'min' | 'max') {
  return v === 'min' || v === 'max';
}

/** A list of concrete types to test for the given abstract-numeric type */
const kConcreteTypesForAbstractType = {
  'abstract-float': ['f32', 'f16'] as const,
  'abstract-int': ['f32', 'f16', 'i32', 'u32'] as const,
  'vec3<abstract-int>': ['vec3f', 'vec3h', 'vec3i', 'vec3u'] as const,
  'vec4<abstract-float>': ['vec4f', 'vec4h'] as const,
  'mat2x3<abstract-float>': ['mat2x3f', 'mat2x3h'] as const,
};

/**
 * @returns the lowest finite value for 'kind' if 'v' is 'min',
 *          the highest finite value for 'kind' if 'v' is 'max',
 *          otherwise returns 'v'
 */
function valueFor(v: number | 'min' | 'max', kind: 'bool' | 'i32' | 'u32' | 'f32' | 'f16') {
  if (!isMinOrMax(v)) {
    return v as number;
  }
  switch (kind) {
    case 'bool':
      return v === 'min' ? 0 : 1;
    case 'i32':
      return v === 'min' ? -0x80000000 : 0x7fffffff;
    case 'u32':
      return v === 'min' ? 0 : 0xffffffff;
    case 'f32':
      return v === 'min' ? FP['f32'].constants().negative.min : FP['f32'].constants().positive.max;
    case 'f16':
      return v === 'min' ? FP['f16'].constants().negative.min : FP['f16'].constants().positive.max;
  }
}

g.test('scalar_identity')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a scalar constructed from a value of the same type produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16'] as const)
      .combine('value', ['min', 'max', 1, 2, 5, 100] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
    t.skipIf(t.params.type === 'bool' && !isMinOrMax(t.params.value));
  })
  .fn(async t => {
    const type = Type[t.params.type];
    const value = valueFor(t.params.value, t.params.type);
    await run(
      t,
      basicExpressionBuilder(ops => `${type}(${ops[0]})`),
      [type],
      type,
      t.params,
      [{ input: [type.create(value)], expected: type.create(value) }]
    );
  });

g.test('vector_identity')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a vector constructed from a value of the same type produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16'] as const)
      .combine('width', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const vectorType = Type.vec(t.params.width, elementType);
    const elements: number[] = [];
    const fn = t.params.infer_type ? `vec${t.params.width}` : `${vectorType}`;
    for (let i = 0; i < t.params.width; i++) {
      if (t.params.type === 'bool') {
        elements.push(i & 1);
      } else {
        elements.push((i + 1) * 10);
      }
    }

    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops[0]})`),
      [vectorType],
      vectorType,
      t.params,
      [
        {
          input: vectorType.create(elements),
          expected: vectorType.create(elements),
        },
      ]
    );
  });

g.test('concrete_vector_splat')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a vector constructed from a single concrete scalar produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16'] as const)
      .combine('value', ['min', 'max', 1, 2, 5, 100] as const)
      .combine('width', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
    t.skipIf(t.params.type === 'bool' && !isMinOrMax(t.params.value));
  })
  .fn(async t => {
    const value = valueFor(t.params.value, t.params.type);
    const elementType = Type[t.params.type];
    const vectorType = Type.vec(t.params.width, elementType);
    const fn = t.params.infer_type ? `vec${t.params.width}` : `${vectorType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops[0]})`),
      [elementType],
      vectorType,
      t.params,
      [{ input: [elementType.create(value)], expected: vectorType.create(value) }]
    );
  });

g.test('abstract_vector_splat')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a vector constructed from a single abstract scalar produces the expected value`)
  .params(u =>
    u
      .combine('abstract_type', ['abstract-int', 'abstract-float'] as const)
      .expand('concrete_type', t => kConcreteTypesForAbstractType[t.abstract_type])
      .combine('value', [1, 2, 5, 100] as const)
      .combine('width', [2, 3, 4] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.concrete_type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const suffix = t.params.abstract_type === 'abstract-float' ? '.0' : '';
    const concreteElementType = Type[t.params.concrete_type];
    const concreteVectorType = Type.vec(t.params.width, concreteElementType);
    const fn = `vec${t.params.width}`;
    await run(
      t,
      basicExpressionBuilder(_ => `${fn}(${t.params.value * 0x100000000}${suffix}) / 0x100000000`),
      [],
      concreteVectorType,
      { inputSource: 'const' },
      [{ input: [], expected: concreteVectorType.create(t.params.value) }]
    );
  });

g.test('concrete_vector_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a vector constructed from concrete element values produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16'] as const)
      .combine('width', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const vectorType = Type.vec(t.params.width, elementType);
    const elements: number[] = [];
    const fn = t.params.infer_type ? `vec${t.params.width}` : `${vectorType}`;
    for (let i = 0; i < t.params.width; i++) {
      if (t.params.type === 'bool') {
        elements.push(i & 1);
      } else {
        elements.push((i + 1) * 10);
      }
    }

    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops.join(', ')})`),
      elements.map(e => elementType),
      vectorType,
      t.params,
      [
        {
          input: elements.map(v => elementType.create(v)),
          expected: vectorType.create(elements),
        },
      ]
    );
  });

g.test('abstract_vector_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a vector constructed from abstract element values produces the expected value`)
  .params(u =>
    u
      .combine('abstract_type', ['abstract-int', 'abstract-float'] as const)
      .expand('concrete_type', t => kConcreteTypesForAbstractType[t.abstract_type])
      .combine('width', [2, 3, 4] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.concrete_type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const suffix = t.params.abstract_type === 'abstract-float' ? '.0' : '';
    const concreteElementType = Type[t.params.concrete_type];
    const concreteVectorType = Type.vec(t.params.width, concreteElementType);
    const fn = `vec${t.params.width}`;
    const elements: number[] = [];
    for (let i = 0; i < t.params.width; i++) {
      elements.push((i + 1) * 10);
    }
    await run(
      t,
      basicExpressionBuilder(
        _ => `${fn}(${elements.map(v => `${v * 0x100000000}${suffix}`).join(', ')}) / 0x100000000`
      ),
      [],
      concreteVectorType,
      { inputSource: 'const' },
      [{ input: [], expected: concreteVectorType.create(elements) }]
    );
  });

const kMixSignatures = [
  '2s', //   [vec2,   scalar]
  's2', //   [scalar, vec2]
  '2ss', //  [vec2,   scalar,   scalar]
  's2s', //  [scalar, vec2,     scalar]
  'ss2', //  [scalar, scalar,   vec2  ]
  '22', //   [vec2,   vec2]
  '3s', //   [vec3,   scalar]
  's3', //   [scalar, vec3]
] as const;

g.test('concrete_vector_mix')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(
    `Test that a vector constructed from a mix of concrete element values and sub-vectors produces the expected value`
  )
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16'] as const)
      .combine('signature', kMixSignatures)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    let width = 0;
    const elementValue = (i: number) => (t.params.type === 'bool' ? i & 1 : (i + 1) * 10);
    const elements: number[] = [];
    const nextValue = () => {
      const value = elementValue(width++);
      elements.push(value);
      return elementType.create(value);
    };
    const args: Value[] = [];
    for (const c of t.params.signature) {
      switch (c) {
        case '2':
          args.push(vec2(nextValue(), nextValue()));
          break;
        case '3':
          args.push(vec3(nextValue(), nextValue(), nextValue()));
          break;
        case 's':
          args.push(nextValue());
          break;
      }
    }
    const vectorType = Type.vec(width, elementType);
    const fn = t.params.infer_type ? `vec${width}` : `${vectorType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops.join(', ')})`),
      args.map(e => e.type),
      vectorType,
      t.params,
      [
        {
          input: args,
          expected: vectorType.create(elements),
        },
      ]
    );
  });

g.test('abstract_vector_mix')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(
    `Test that a vector constructed from a mix of abstract element values and sub-vectors produces the expected value`
  )
  .params(u =>
    u
      .combine('abstract_type', ['abstract-int', 'abstract-float'] as const)
      .expand('concrete_type', t => kConcreteTypesForAbstractType[t.abstract_type])
      .combine('signature', kMixSignatures)
  )
  .beforeAllSubcases(t => {
    if (t.params.concrete_type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    let width = 0;
    const suffix = t.params.abstract_type === 'abstract-float' ? '.0' : '';
    const concreteElementType = Type[t.params.concrete_type];
    const elementValue = (i: number) => (i + 1) * 10;
    const elements: number[] = [];
    const nextValue = () => {
      const value = elementValue(width++);
      elements.push(value);
      return `${value * 0x100000000}${suffix}`;
    };
    const args: string[] = [];
    for (const c of t.params.signature) {
      switch (c) {
        case '2':
          args.push(`vec2(${nextValue()}, ${nextValue()})`);
          break;
        case '3':
          args.push(`vec3(${nextValue()}, ${nextValue()}, ${nextValue()})`);
          break;
        case 's':
          args.push(`${nextValue()}`);
          break;
      }
    }
    const concreteVectorType = Type.vec(width, concreteElementType);
    const fn = `vec${width}`;
    await run(
      t,
      basicExpressionBuilder(_ => `${fn}(${args.join(', ')}) / 0x100000000`),
      [],
      concreteVectorType,
      { inputSource: 'const' },
      [
        {
          input: [],
          expected: concreteVectorType.create(elements),
        },
      ]
    );
  });

g.test('matrix_identity')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a matrix constructed from a value of the same type produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['f32', 'f16'] as const)
      .combine('columns', [2, 3, 4] as const)
      .combine('rows', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const matrixType = Type.mat(t.params.columns, t.params.rows, elementType);
    const elements: number[] = [];
    for (let column = 0; column < t.params.columns; column++) {
      for (let row = 0; row < t.params.rows; row++) {
        elements.push((column + 1) * 10 + (row + 1));
      }
    }
    const fn = t.params.infer_type ? `mat${t.params.columns}x${t.params.rows}` : `${matrixType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops[0]})`),
      [matrixType],
      matrixType,
      t.params,
      [
        {
          input: matrixType.create(elements),
          expected: matrixType.create(elements),
        },
      ]
    );
  });

g.test('concrete_matrix_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a matrix constructed from concrete element values produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['f32', 'f16'] as const)
      .combine('columns', [2, 3, 4] as const)
      .combine('rows', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const matrixType = Type.mat(t.params.columns, t.params.rows, elementType);
    const elements: number[] = [];
    for (let column = 0; column < t.params.columns; column++) {
      for (let row = 0; row < t.params.rows; row++) {
        elements.push((column + 1) * 10 + (row + 1));
      }
    }
    const fn = t.params.infer_type ? `mat${t.params.columns}x${t.params.rows}` : `${matrixType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops.join(', ')})`),
      elements.map(e => elementType),
      matrixType,
      t.params,
      [
        {
          input: elements.map(e => elementType.create(e)),
          expected: matrixType.create(elements),
        },
      ]
    );
  });

g.test('abstract_matrix_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a matrix constructed from concrete element values produces the expected value`)
  .params(u =>
    u
      .combine('concrete_type', ['f32', 'f16'] as const)
      .combine('columns', [2, 3, 4] as const)
      .combine('rows', [2, 3, 4] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.concrete_type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const concreteElementType = Type[t.params.concrete_type];
    const concreteMatrixType = Type.mat(t.params.columns, t.params.rows, concreteElementType);
    const elements: number[] = [];
    for (let column = 0; column < t.params.columns; column++) {
      for (let row = 0; row < t.params.rows; row++) {
        elements.push((column + 1) * 10 + (row + 1));
      }
    }
    const fn = `mat${t.params.columns}x${t.params.rows}`;
    await run(
      t,
      basicExpressionBuilder(
        _ => `${fn}(${elements.map(v => `${v * 0x100000000}.0`).join(', ')}) * (1.0 / 0x100000000)`
      ),
      [],
      concreteMatrixType,
      { inputSource: 'const' },
      [
        {
          input: [],
          expected: concreteMatrixType.create(elements),
        },
      ]
    );
  });

g.test('concrete_matrix_column_vectors')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a matrix constructed from concrete column vectors produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['f32', 'f16'] as const)
      .combine('columns', [2, 3, 4] as const)
      .combine('rows', [2, 3, 4] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const columnType = Type.vec(t.params.rows, elementType);
    const matrixType = Type.mat(t.params.columns, t.params.rows, elementType);
    const elements: number[] = [];
    const columnVectors: VectorValue[] = [];
    for (let column = 0; column < t.params.columns; column++) {
      const columnElements: number[] = [];
      for (let row = 0; row < t.params.rows; row++) {
        const v = (column + 1) * 10 + (row + 1);
        elements.push(v);
        columnElements.push(v);
      }
      columnVectors.push(columnType.create(columnElements));
    }
    const fn = t.params.infer_type ? `mat${t.params.columns}x${t.params.rows}` : `${matrixType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops.join(', ')})`),
      columnVectors.map(v => v.type),
      matrixType,
      t.params,
      [
        {
          input: columnVectors,
          expected: matrixType.create(elements),
        },
      ]
    );
  });

g.test('abstract_matrix_column_vectors')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that a matrix constructed from abstract column vectors produces the expected value`)
  .params(u =>
    u
      .combine('concrete_type', ['f32', 'f16'] as const)
      .combine('columns', [2, 3, 4] as const)
      .combine('rows', [2, 3, 4] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.concrete_type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const concreteElementType = Type[t.params.concrete_type];
    const concreteMatrixType = Type.mat(t.params.columns, t.params.rows, concreteElementType);
    const elements: number[] = [];
    const columnVectors: string[] = [];
    for (let column = 0; column < t.params.columns; column++) {
      const columnElements: string[] = [];
      for (let row = 0; row < t.params.rows; row++) {
        const v = (column + 1) * 10 + (row + 1);
        elements.push(v);
        columnElements.push(`${v * 0x100000000}`);
      }
      columnVectors.push(`vec${t.params.rows}(${columnElements.join(', ')})`);
    }
    const fn = `mat${t.params.columns}x${t.params.rows}`;
    await run(
      t,
      basicExpressionBuilder(_ => `${fn}(${columnVectors.join(', ')}) * (1.0 / 0x100000000)`),
      [],
      concreteMatrixType,
      { inputSource: 'const' },
      [
        {
          input: [],
          expected: concreteMatrixType.create(elements),
        },
      ]
    );
  });

g.test('concrete_array_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that an array constructed from concrete element values produces the expected value`)
  .params(u =>
    u
      .combine('inputSource', allInputSources)
      .combine('type', ['bool', 'i32', 'u32', 'f32', 'f16', 'vec3f', 'vec4i'] as const)
      .combine('length', [1, 5, 10] as const)
      .combine('infer_type', [false, true] as const)
  )
  .beforeAllSubcases(t => {
    if (t.params.type === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const elementType = Type[t.params.type];
    const arrayType = Type.array(t.params.length, elementType);
    const elements: number[] = [];
    for (let i = 0; i < t.params.length; i++) {
      elements.push((i + 1) * 10);
    }
    const fn = t.params.infer_type ? `array` : `${arrayType}`;
    await run(
      t,
      basicExpressionBuilder(ops => `${fn}(${ops.join(', ')})`),
      elements.map(e => elementType),
      arrayType,
      t.params,
      [
        {
          input: elements.map(e => elementType.create(e)),
          expected: arrayType.create(elements),
        },
      ]
    );
  });

g.test('abstract_array_elements')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that an array constructed from element values produces the expected value`)
  .params(u =>
    u
      .combine('abstract_type', [
        'abstract-int',
        'abstract-float',
        'vec3<abstract-int>',
        'vec4<abstract-float>',
        'mat2x3<abstract-float>',
      ] as const)
      .expand('concrete_type', t => kConcreteTypesForAbstractType[t.abstract_type])
      .combine('length', [1, 5, 10] as const)
  )
  .beforeAllSubcases(t => {
    if (scalarTypeOf(Type[t.params.concrete_type]).kind === 'f16') {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const count = t.params.length;
    const concreteElementType = Type[t.params.concrete_type];
    const concreteArrayType = Type.array(count, concreteElementType);
    const elements: { args: string; value: Value }[] = [];
    let i = 0;
    const nextValue = () => ++i * 10;
    for (let i = 0; i < count; i++) {
      switch (t.params.abstract_type) {
        case 'abstract-int': {
          const value = nextValue();
          elements.push({ args: `${value}`, value: concreteElementType.create(value) });
          break;
        }
        case 'abstract-float': {
          const value = nextValue();
          elements.push({ args: `${value}.0`, value: concreteElementType.create(value) });
          break;
        }
        case 'vec3<abstract-int>': {
          const x = nextValue();
          const y = nextValue();
          const z = nextValue();
          elements.push({
            args: `vec3(${x}, ${y}, ${z})`,
            value: (concreteElementType as VectorType).create([x, y, z]),
          });
          break;
        }
        case 'vec4<abstract-float>': {
          const x = nextValue();
          const y = nextValue();
          const z = nextValue();
          const w = nextValue();
          elements.push({
            args: `vec4(${x}.0, ${y}.0, ${z}.0, ${w}.0)`,
            value: (concreteElementType as VectorType).create([x, y, z, w]),
          });
          break;
        }
        case 'mat2x3<abstract-float>': {
          const e00 = nextValue();
          const e01 = nextValue();
          const e02 = nextValue();
          const e10 = nextValue();
          const e11 = nextValue();
          const e12 = nextValue();
          elements.push({
            args: `mat2x3(vec3(${e00}.0, ${e01}.0, ${e02}.0), vec3(${e10}.0, ${e11}.0, ${e12}.0))`,
            value: (concreteElementType as MatrixType).create([e00, e01, e02, e10, e11, e12]),
          });
          break;
        }
      }
    }
    const fn = `array`;
    await run(
      t,
      basicExpressionBuilder(_ => `${fn}(${elements.map(e => e.args).join(', ')})`),
      [],
      concreteArrayType,
      { inputSource: 'const' },
      [
        {
          input: [],
          expected: new ArrayValue(elements.map(e => e.value)),
        },
      ]
    );
  });

g.test('structure')
  .specURL('https://www.w3.org/TR/WGSL/#value-constructor-builtin-function')
  .desc(`Test that an structure constructed from element values produces the expected value`)
  .params(u =>
    u
      .combine('member_types', [
        ['bool'],
        ['u32'],
        ['vec3f'],
        ['i32', 'u32'],
        ['i32', 'f16', 'vec4i', 'mat3x2f'],
        ['bool', 'u32', 'f16', 'vec3f', 'vec2i'],
        ['i32', 'u32', 'f32', 'f16', 'vec3f', 'vec4i'],
      ] as readonly ScalarKind[][])
      .combine('nested', [false, true])
      .beginSubcases()
      .expand('member_index', t => t.member_types.map((_, i) => i))
  )
  .beforeAllSubcases(t => {
    if (t.params.member_types.includes('f16')) {
      t.selectDeviceOrSkipTestCase('shader-f16');
    }
  })
  .fn(async t => {
    const memberType = Type[t.params.member_types[t.params.member_index]];
    const values = t.params.member_types.map((ty, i) => Type[ty].create(i));

    const builder = basicExpressionBuilder(ops =>
      t.params.nested
        ? `OuterStruct(10, MyStruct(${ops.join(', ')}), 20).inner.member_${t.params.member_index}`
        : `MyStruct(${ops.join(', ')}).member_${t.params.member_index}`
    );
    await run(
      t,
      (parameterTypes, resultType, cases, inputSource) => {
        return `
${t.params.member_types.includes('f16') ? 'enable f16;' : ''}

${builder(parameterTypes, resultType, cases, inputSource)}

struct MyStruct {
${t.params.member_types.map((ty, i) => `  member_${i} : ${ty},`).join('\n')}
};
struct OuterStruct {
  pre : i32,
  inner : MyStruct,
  post : i32,
};
`;
      },
      t.params.member_types.map(ty => Type[ty]),
      memberType,
      { inputSource: 'const' },
      [{ input: values, expected: values[t.params.member_index] }]
    );
  });