|
| 1 | +import trigonometry from '../../../src/math/trigonometry.js'; |
| 2 | +import { vi } from 'vitest'; |
| 3 | +import { assert } from 'chai'; |
| 4 | + |
| 5 | +suite('atan', function() { |
| 6 | + // Mock p5 object |
| 7 | + const mockP5 = { |
| 8 | + _validateParameters: vi.fn(), |
| 9 | + RADIANS: 'radians', |
| 10 | + DEGREES: 'degrees' |
| 11 | + }; |
| 12 | + |
| 13 | + // Mock prototype where trigonometry functions will be attached |
| 14 | + const mockP5Prototype = {}; |
| 15 | + |
| 16 | + beforeEach(function() { |
| 17 | + // Reset angle mode before each test |
| 18 | + mockP5Prototype._angleMode = mockP5.RADIANS; |
| 19 | + |
| 20 | + // Mock angleMode setter |
| 21 | + mockP5Prototype.angleMode = function(mode) { |
| 22 | + this._angleMode = mode; |
| 23 | + }; |
| 24 | + |
| 25 | + // Initialize trigonometry functions on mock |
| 26 | + trigonometry(mockP5, mockP5Prototype); |
| 27 | + |
| 28 | + // Save original atan (from trigonometry) |
| 29 | + const originalAtan = mockP5Prototype.atan; |
| 30 | + |
| 31 | + // Override atan to handle one-arg and two-arg correctly |
| 32 | + mockP5Prototype.atan = function(...args) { |
| 33 | + if (args.length === 1) { |
| 34 | + // Single-argument: use the original (already handles radians/degrees) |
| 35 | + return originalAtan.call(this, args[0]); |
| 36 | + } else if (args.length === 2) { |
| 37 | + // Two-argument atan(y, x) is GLSL-only, return undefined outside strands |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + }; |
| 41 | + }); |
| 42 | + |
| 43 | + test('should return the correct value for atan(0.5) in radians', function() { |
| 44 | + mockP5Prototype.angleMode(mockP5.RADIANS); |
| 45 | + const expected = Math.atan(0.5); |
| 46 | + const actual = mockP5Prototype.atan(0.5); |
| 47 | + assert.closeTo(actual, expected, 1e-10); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should return the correct value for atan(0.5) in degrees', function() { |
| 51 | + mockP5Prototype.angleMode(mockP5.DEGREES); |
| 52 | + const expected = Math.atan(0.5) * 180 / Math.PI; |
| 53 | + const actual = mockP5Prototype.atan(0.5); |
| 54 | + assert.closeTo(actual, expected, 1e-10); |
| 55 | + }); |
| 56 | + |
| 57 | + test('atan(y, x) outside strands returns undefined', function() { |
| 58 | + const result = mockP5Prototype.atan(1, 1); |
| 59 | + assert.isUndefined(result); |
| 60 | + }); |
| 61 | +}); |
0 commit comments