check_contents.spec.ts (2834B)
1 export const description = `Unit tests for check_contents`; 2 3 import { Fixture } from '../common/framework/fixture.js'; 4 import { makeTestGroup } from '../common/internal/test_group.js'; 5 import { ErrorWithExtra } from '../common/util/util.js'; 6 import { checkElementsEqual } from '../webgpu/util/check_contents.js'; 7 8 class F extends Fixture { 9 test(substr: undefined | string, result: undefined | ErrorWithExtra) { 10 if (substr === undefined) { 11 this.expect(result === undefined, result?.message); 12 } else { 13 this.expect(result !== undefined && result.message.indexOf(substr) !== -1, result?.message); 14 } 15 } 16 } 17 18 export const g = makeTestGroup(F); 19 20 g.test('checkElementsEqual').fn(t => { 21 t.shouldThrow('Error', () => checkElementsEqual(new Uint8Array(), new Uint16Array())); 22 t.shouldThrow('Error', () => checkElementsEqual(new Uint32Array(), new Float32Array())); 23 t.shouldThrow('Error', () => checkElementsEqual(new Uint8Array([]), new Uint8Array([0]))); 24 t.shouldThrow('Error', () => checkElementsEqual(new Uint8Array([0]), new Uint8Array([]))); 25 { 26 t.test(undefined, checkElementsEqual(new Uint8Array([]), new Uint8Array([]))); 27 t.test(undefined, checkElementsEqual(new Uint8Array([0]), new Uint8Array([0]))); 28 t.test(undefined, checkElementsEqual(new Uint8Array([1]), new Uint8Array([1]))); 29 t.test( 30 ` 31 Starting at index 0: 32 actual == 0x: 00 33 failed -> xx 34 expected == 01`, 35 checkElementsEqual(new Uint8Array([0]), new Uint8Array([1])) 36 ); 37 t.test( 38 'expected == 01 02 01', 39 checkElementsEqual(new Uint8Array([1, 1, 1]), new Uint8Array([1, 2, 1])) 40 ); 41 } 42 { 43 const actual = new Uint8Array(280); 44 const exp = new Uint8Array(280); 45 for (let i = 2; i < 20; ++i) actual[i] = i - 4; 46 t.test( 47 '00 fe ff 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 00', 48 checkElementsEqual(actual, exp) 49 ); 50 for (let i = 2; i < 280; ++i) actual[i] = i - 4; 51 t.test('Starting at index 1:', checkElementsEqual(actual, exp)); 52 for (let i = 0; i < 2; ++i) actual[i] = i - 4; 53 t.test('Starting at index 0:', checkElementsEqual(actual, exp)); 54 } 55 { 56 const actual = new Int32Array(30); 57 const exp = new Int32Array(30); 58 for (let i = 2; i < 7; ++i) actual[i] = i - 3; 59 t.test('00000002 00000003 00000000\n', checkElementsEqual(actual, exp)); 60 for (let i = 2; i < 30; ++i) actual[i] = i - 3; 61 t.test('00000000 00000000 ...', checkElementsEqual(actual, exp)); 62 } 63 { 64 const actual = new Float64Array(30); 65 const exp = new Float64Array(30); 66 for (let i = 2; i < 7; ++i) actual[i] = (i - 4) * 1e100; 67 t.test('2.000e+100 0.000\n', checkElementsEqual(actual, exp)); 68 for (let i = 2; i < 280; ++i) actual[i] = (i - 4) * 1e100; 69 t.test('6.000e+100 7.000e+100 ...', checkElementsEqual(actual, exp)); 70 } 71 });