|
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 | 3 | import { not } from '../../../src/std/boolean.ts'; |
| 4 | +import tgpu, { d, std } from '../../../src/index.js'; |
4 | 5 |
|
5 | | -describe('neg', () => { |
6 | | - it('negates', () => { |
7 | | - expect(not(vec2b(true, false))).toStrictEqual(vec2b(false, true)); |
8 | | - expect(not(vec3b(false, false, true))).toStrictEqual(vec3b(true, true, false)); |
9 | | - expect(not(vec4b(true, true, false, false))).toStrictEqual(vec4b(false, false, true, true)); |
| 6 | +describe('not', () => { |
| 7 | + it('negates booleans', () => { |
| 8 | + expect(not(true)).toBe(false); |
| 9 | + expect(not(false)).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('converts numbers to booleans and negates', () => { |
| 13 | + expect(not(0)).toBe(true); |
| 14 | + expect(not(-1)).toBe(false); |
| 15 | + expect(not(42)).toBe(false); |
| 16 | + }); |
| 17 | + |
| 18 | + it('negates boolean vectors', () => { |
| 19 | + expect(not(d.vec2b(true, false))).toStrictEqual(d.vec2b(false, true)); |
| 20 | + expect(not(d.vec3b(false, false, true))).toStrictEqual(d.vec3b(true, true, false)); |
| 21 | + expect(not(d.vec4b(true, true, false, false))).toStrictEqual(d.vec4b(false, false, true, true)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('converts numeric vectors to booleans vectors and negates component-wise', () => { |
| 25 | + expect(not(d.vec2f(0.0, 1.0))).toStrictEqual(d.vec2b(true, false)); |
| 26 | + expect(not(d.vec3i(0, 5, -1))).toStrictEqual(d.vec3b(true, false, false)); |
| 27 | + expect(not(d.vec4u(0, 0, 1, 0))).toStrictEqual(d.vec4b(true, true, false, true)); |
| 28 | + expect(not(d.vec4h(0, 3.14, 0, -2.5))).toStrictEqual(d.vec4b(true, false, true, false)); |
| 29 | + }); |
| 30 | + |
| 31 | + it('negates truthiness check', () => { |
| 32 | + expect(not(null)).toBe(true); |
| 33 | + expect(not(undefined)).toBe(true); |
| 34 | + expect(not({})).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('mimics WGSL behavior on NaN', () => { |
| 38 | + expect(not(NaN)).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('generates correct WGSL on a boolean runtime-known argument', () => { |
| 42 | + const testFn = tgpu.fn( |
| 43 | + [d.bool], |
| 44 | + d.bool, |
| 45 | + )((v) => { |
| 46 | + return not(v); |
| 47 | + }); |
| 48 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 49 | + "fn testFn(v: bool) -> bool { |
| 50 | + return !v; |
| 51 | + }" |
| 52 | + `); |
| 53 | + }); |
| 54 | + |
| 55 | + it('generates correct WGSL on a numeric runtime-known argument', () => { |
| 56 | + const testFn = tgpu.fn( |
| 57 | + [d.i32], |
| 58 | + d.bool, |
| 59 | + )((v) => { |
| 60 | + return not(v); |
| 61 | + }); |
| 62 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 63 | + "fn testFn(v: i32) -> bool { |
| 64 | + return !bool(v); |
| 65 | + }" |
| 66 | + `); |
| 67 | + }); |
| 68 | + |
| 69 | + it('generates correct WGSL on a boolean vector runtime-known argument', () => { |
| 70 | + const testFn = tgpu.fn( |
| 71 | + [d.vec3b], |
| 72 | + d.vec3b, |
| 73 | + )((v) => { |
| 74 | + return not(v); |
| 75 | + }); |
| 76 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 77 | + "fn testFn(v: vec3<bool>) -> vec3<bool> { |
| 78 | + return !(v); |
| 79 | + }" |
| 80 | + `); |
| 81 | + }); |
| 82 | + |
| 83 | + it('generates correct WGSL on a numeric vector runtime-known argument', () => { |
| 84 | + const testFn = tgpu.fn( |
| 85 | + [d.vec3f], |
| 86 | + d.vec3b, |
| 87 | + )((v) => { |
| 88 | + return not(v); |
| 89 | + }); |
| 90 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 91 | + "fn testFn(v: vec3f) -> vec3<bool> { |
| 92 | + return !(vec3<bool>(v)); |
| 93 | + }" |
| 94 | + `); |
| 95 | + }); |
| 96 | + |
| 97 | + it('generates correct WGSL on a numeric vector comptime-known argument', () => { |
| 98 | + const f = () => { |
| 99 | + 'use gpu'; |
| 100 | + const v = not(d.vec4f(Infinity, -Infinity, 0, NaN)); |
| 101 | + }; |
| 102 | + |
| 103 | + expect(tgpu.resolve([f])).toMatchInlineSnapshot(` |
| 104 | + "fn f() { |
| 105 | + var v = vec4<bool>(false, false, true, false); |
| 106 | + }" |
| 107 | + `); |
| 108 | + }); |
| 109 | + |
| 110 | + it('evaluates at compile time for comptime-known arguments', () => { |
| 111 | + const getN = tgpu.comptime(() => 42); |
| 112 | + const slot = tgpu.slot<{ a?: number }>({}); |
| 113 | + |
| 114 | + const f = () => { |
| 115 | + 'use gpu'; |
| 116 | + if (not(getN()) && not(slot.$.a) && not(d.vec4f(1, 8, 8, 2)).x) { |
| 117 | + return 1; |
| 118 | + } |
| 119 | + return -1; |
| 120 | + }; |
| 121 | + |
| 122 | + expect(tgpu.resolve([f])).toMatchInlineSnapshot(` |
| 123 | + "fn f() -> i32 { |
| 124 | + if (((false && true) && false)) { |
| 125 | + return 1; |
| 126 | + } |
| 127 | + return -1; |
| 128 | + }" |
| 129 | + `); |
| 130 | + }); |
| 131 | + |
| 132 | + it('mimics JS on non-primitive values', ({ root }) => { |
| 133 | + const buffer = root.createUniform(d.mat4x4f); |
| 134 | + const testFn = tgpu.fn([d.vec3f, d.atomic(d.u32), d.ptrPrivate(d.u32)])((v, a, p) => { |
| 135 | + const _b0 = not(buffer); |
| 136 | + const _b1 = not(buffer.$); |
| 137 | + const _b2 = not(v); |
| 138 | + const _b3 = not(a); |
| 139 | + const _b4 = not(std.atomicLoad(a)); |
| 140 | + const _b5 = not(p); |
| 141 | + const _b6 = not(p.$); |
| 142 | + }); |
| 143 | + |
| 144 | + expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` |
| 145 | + "fn testFn(v: vec3f, a: atomic<u32>, p: ptr<private, u32>) { |
| 146 | + const _b0 = false; |
| 147 | + let _b1 = false; |
| 148 | + var _b2 = !(vec3<bool>(v)); |
| 149 | + let _b3 = false; |
| 150 | + let _b4 = !bool(atomicLoad(&a)); |
| 151 | + let _b5 = false; |
| 152 | + let _b6 = !bool((*p)); |
| 153 | + }" |
| 154 | + `); |
10 | 155 | }); |
11 | 156 | }); |
0 commit comments