|
| 1 | +import { test } from 'typegpu-testing-utility'; |
| 2 | +import { expect, vi } from 'vitest'; |
| 3 | +import tgpu, { d } from 'typegpu'; |
| 4 | + |
| 5 | +import { expectSnippetOf } from '../utils/parseResolved.ts'; |
| 6 | + |
| 7 | +test('multiplying i32 with a float literal should implicitly convert to an f32', () => { |
| 8 | + using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 9 | + |
| 10 | + function main() { |
| 11 | + 'use gpu'; |
| 12 | + const a = d.i32(1) * 0.001; |
| 13 | + const int = d.i32(1); |
| 14 | + return int * 0.001; |
| 15 | + } |
| 16 | + |
| 17 | + expect(tgpu.resolve([main])).toMatchInlineSnapshot(` |
| 18 | + "fn main() -> f32 { |
| 19 | + const a = 1e-3f; |
| 20 | + const int = 1i; |
| 21 | + return (f32(int) * 1e-3f); |
| 22 | + }" |
| 23 | + `); |
| 24 | + |
| 25 | + expectSnippetOf(() => { |
| 26 | + 'use gpu'; |
| 27 | + return d.i32(1) * 0.001; |
| 28 | + }).toStrictEqual([0.001, d.f32, 'constant']); |
| 29 | + |
| 30 | + expect(consoleWarnSpy.mock.calls).toMatchInlineSnapshot(` |
| 31 | + [ |
| 32 | + [ |
| 33 | + "Implicit conversions from [ |
| 34 | + 1: i32 |
| 35 | + ] to f32 are supported, but not recommended. |
| 36 | + Consider using explicit conversions instead.", |
| 37 | + ], |
| 38 | + [ |
| 39 | + "Implicit conversions from [ |
| 40 | + int: i32 |
| 41 | + ] to f32 are supported, but not recommended. |
| 42 | + Consider using explicit conversions instead.", |
| 43 | + ], |
| 44 | + [ |
| 45 | + "Implicit conversions from [ |
| 46 | + 1: i32 |
| 47 | + ] to f32 are supported, but not recommended. |
| 48 | + Consider using explicit conversions instead.", |
| 49 | + ], |
| 50 | + ] |
| 51 | + `); |
| 52 | +}); |
| 53 | + |
| 54 | +test('multiplying u32 with a float literal should implicitly convert to an f32', () => { |
| 55 | + using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 56 | + |
| 57 | + function main() { |
| 58 | + 'use gpu'; |
| 59 | + const a = d.u32(10) * 0.001; |
| 60 | + const int = d.u32(100); |
| 61 | + return int * 0.001; |
| 62 | + } |
| 63 | + |
| 64 | + expect(tgpu.resolve([main])).toMatchInlineSnapshot(` |
| 65 | + "fn main() -> f32 { |
| 66 | + const a = 0.01f; |
| 67 | + const int = 100u; |
| 68 | + return (f32(int) * 1e-3f); |
| 69 | + }" |
| 70 | + `); |
| 71 | + |
| 72 | + expectSnippetOf(() => { |
| 73 | + 'use gpu'; |
| 74 | + return d.u32(1) * 0.001; |
| 75 | + }).toStrictEqual([0.001, d.f32, 'constant']); |
| 76 | + |
| 77 | + expect(consoleWarnSpy.mock.calls).toMatchInlineSnapshot(` |
| 78 | + [ |
| 79 | + [ |
| 80 | + "Implicit conversions from [ |
| 81 | + 10: u32 |
| 82 | + ] to f32 are supported, but not recommended. |
| 83 | + Consider using explicit conversions instead.", |
| 84 | + ], |
| 85 | + [ |
| 86 | + "Implicit conversions from [ |
| 87 | + int: u32 |
| 88 | + ] to f32 are supported, but not recommended. |
| 89 | + Consider using explicit conversions instead.", |
| 90 | + ], |
| 91 | + [ |
| 92 | + "Implicit conversions from [ |
| 93 | + 1: u32 |
| 94 | + ] to f32 are supported, but not recommended. |
| 95 | + Consider using explicit conversions instead.", |
| 96 | + ], |
| 97 | + ] |
| 98 | + `); |
| 99 | +}); |
| 100 | + |
| 101 | +test('multiplying u32 with an i32 should implicitly convert to an i32', () => { |
| 102 | + using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 103 | + |
| 104 | + function main() { |
| 105 | + 'use gpu'; |
| 106 | + const uint = d.u32(5); |
| 107 | + const int = d.i32(3); |
| 108 | + return uint * int; |
| 109 | + } |
| 110 | + |
| 111 | + expect(tgpu.resolve([main])).toMatchInlineSnapshot(` |
| 112 | + "fn main() -> i32 { |
| 113 | + const uint = 5u; |
| 114 | + const int = 3i; |
| 115 | + return (i32(uint) * int); |
| 116 | + }" |
| 117 | + `); |
| 118 | + |
| 119 | + expect(consoleWarnSpy.mock.calls).toMatchInlineSnapshot(` |
| 120 | + [ |
| 121 | + [ |
| 122 | + "Implicit conversions from [ |
| 123 | + uint: u32 |
| 124 | + ] to i32 are supported, but not recommended. |
| 125 | + Consider using explicit conversions instead.", |
| 126 | + ], |
| 127 | + ] |
| 128 | + `); |
| 129 | +}); |
0 commit comments