|
1 | | -import { describe, expect, it } from 'vitest'; |
2 | | -import { vec2b, vec3b, vec4b } from '../../../src/data/index.ts'; |
| 1 | +import { describe, expect } from 'vitest'; |
| 2 | +import { it } from 'typegpu-testing-utility'; |
| 3 | +import { vec2b, vec2f, vec3b, vec3i, vec4b, vec4h, vec4u } from '../../../src/data/index.ts'; |
3 | 4 | import { not } from '../../../src/std/boolean.ts'; |
| 5 | +import tgpu, { d } from '../../../src/index.js'; |
4 | 6 |
|
5 | | -describe('neg', () => { |
6 | | - it('negates', () => { |
| 7 | +describe('not', () => { |
| 8 | + it('negates booleans', () => { |
| 9 | + expect(not(true)).toBe(false); |
| 10 | + expect(not(false)).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it('converts numbers to booleans and negates', () => { |
| 14 | + expect(not(0)).toBe(true); |
| 15 | + expect(not(-1)).toBe(false); |
| 16 | + expect(not(42)).toBe(false); |
| 17 | + }); |
| 18 | + |
| 19 | + it('negates boolean vectors', () => { |
7 | 20 | expect(not(vec2b(true, false))).toStrictEqual(vec2b(false, true)); |
8 | 21 | expect(not(vec3b(false, false, true))).toStrictEqual(vec3b(true, true, false)); |
9 | 22 | expect(not(vec4b(true, true, false, false))).toStrictEqual(vec4b(false, false, true, true)); |
10 | 23 | }); |
| 24 | + |
| 25 | + it('converts numeric vectors to booleans vectors and negates component-wise', () => { |
| 26 | + expect(not(vec2f(0.0, 1.0))).toStrictEqual(vec2b(true, false)); |
| 27 | + expect(not(vec3i(0, 5, -1))).toStrictEqual(vec3b(true, false, false)); |
| 28 | + expect(not(vec4u(0, 0, 1, 0))).toStrictEqual(vec4b(true, true, false, true)); |
| 29 | + expect(not(vec4h(0, 3.14, 0, -2.5))).toStrictEqual(vec4b(true, false, true, false)); |
| 30 | + }); |
| 31 | + |
| 32 | + it('negates truthiness check', () => { |
| 33 | + const s = {}; |
| 34 | + expect(not(null)).toBe(true); |
| 35 | + expect(not(undefined)).toBe(true); |
| 36 | + expect(not(s)).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('generates correct WGSL on a boolean runtime-known argument', () => { |
| 40 | + const testFn = tgpu.fn( |
| 41 | + [d.bool], |
| 42 | + d.bool, |
| 43 | + )((v) => { |
| 44 | + return not(v); |
| 45 | + }); |
| 46 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 47 | + "fn testFn(v: bool) -> bool { |
| 48 | + return !v; |
| 49 | + }" |
| 50 | + `); |
| 51 | + }); |
| 52 | + |
| 53 | + it('generates correct WGSL on a numeric runtime-known argument', () => { |
| 54 | + const testFn = tgpu.fn( |
| 55 | + [d.i32], |
| 56 | + d.bool, |
| 57 | + )((v) => { |
| 58 | + return not(v); |
| 59 | + }); |
| 60 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 61 | + "fn testFn(v: i32) -> bool { |
| 62 | + return !bool(v); |
| 63 | + }" |
| 64 | + `); |
| 65 | + }); |
| 66 | + |
| 67 | + it('generates correct WGSL on a boolean vector runtime-known argument', () => { |
| 68 | + const testFn = tgpu.fn( |
| 69 | + [d.vec3b], |
| 70 | + d.vec3b, |
| 71 | + )((v) => { |
| 72 | + return not(v); |
| 73 | + }); |
| 74 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 75 | + "fn testFn(v: vec3<bool>) -> vec3<bool> { |
| 76 | + return !(v); |
| 77 | + }" |
| 78 | + `); |
| 79 | + }); |
| 80 | + |
| 81 | + it('generates correct WGSL on a numeric vector runtime-known argument', () => { |
| 82 | + const testFn = tgpu.fn( |
| 83 | + [d.vec3f], |
| 84 | + d.vec3b, |
| 85 | + )((v) => { |
| 86 | + return not(v); |
| 87 | + }); |
| 88 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 89 | + "fn testFn(v: vec3f) -> vec3<bool> { |
| 90 | + return !vec3<bool>(v); |
| 91 | + }" |
| 92 | + `); |
| 93 | + }); |
| 94 | + |
| 95 | + it('evaluates at compile time for comptime-known arguments', () => { |
| 96 | + const getN = tgpu.comptime(() => 42); |
| 97 | + const slot = tgpu.slot<{ a?: number }>({}); |
| 98 | + |
| 99 | + const f = () => { |
| 100 | + 'use gpu'; |
| 101 | + if (not(getN()) && not(slot.$.a) && not(d.vec4f(1, 8, 8, 2)).x) { |
| 102 | + return 1; |
| 103 | + } |
| 104 | + return -1; |
| 105 | + }; |
| 106 | + |
| 107 | + expect(tgpu.resolve([f])).toMatchInlineSnapshot(` |
| 108 | + "fn f() -> i32 { |
| 109 | + if (((false && true) && false)) { |
| 110 | + return 1; |
| 111 | + } |
| 112 | + return -1; |
| 113 | + }" |
| 114 | + `); |
| 115 | + }); |
| 116 | + |
| 117 | + it('throws when cannot determine truthiness of argument at compile time', ({ root }) => { |
| 118 | + const buffer = root.createUniform(d.mat4x4f); |
| 119 | + const testFn = tgpu.fn( |
| 120 | + [], |
| 121 | + d.bool, |
| 122 | + )(() => { |
| 123 | + return not(buffer.$); |
| 124 | + }); |
| 125 | + |
| 126 | + expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(` |
| 127 | + [Error: Resolution of the following tree failed: |
| 128 | + - <root> |
| 129 | + - fn:testFn |
| 130 | + - fn:not: \`std.not\` cannot determine truthiness for runtime value of type: mat4x4f.] |
| 131 | + `); |
| 132 | + }); |
11 | 133 | }); |
0 commit comments