-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix: allow single-arg atan() outside strands; add unit test #8096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import trigonometry from '../../../src/math/trigonometry.js'; | ||
import { vi } from 'vitest'; | ||
import { assert } from 'chai'; | ||
|
||
suite('atan', function() { | ||
// Mock p5 object | ||
const mockP5 = { | ||
_validateParameters: vi.fn(), | ||
RADIANS: 'radians', | ||
DEGREES: 'degrees' | ||
}; | ||
|
||
// Mock prototype where trigonometry functions will be attached | ||
const mockP5Prototype = {}; | ||
|
||
beforeEach(function() { | ||
// Reset angle mode before each test | ||
mockP5Prototype._angleMode = mockP5.RADIANS; | ||
|
||
// Mock angleMode setter | ||
mockP5Prototype.angleMode = function(mode) { | ||
this._angleMode = mode; | ||
}; | ||
|
||
// Initialize trigonometry functions on mock | ||
trigonometry(mockP5, mockP5Prototype); | ||
|
||
// Save original atan (from trigonometry) | ||
const originalAtan = mockP5Prototype.atan; | ||
|
||
// Override atan to handle one-arg and two-arg correctly | ||
mockP5Prototype.atan = function(...args) { | ||
if (args.length === 1) { | ||
// Single-argument: use the original (already handles radians/degrees) | ||
return originalAtan.call(this, args[0]); | ||
} else if (args.length === 2) { | ||
// Two-argument atan(y, x) is GLSL-only, return undefined outside strands | ||
return undefined; | ||
} | ||
}; | ||
}); | ||
|
||
test('should return the correct value for atan(0.5) in radians', function() { | ||
mockP5Prototype.angleMode(mockP5.RADIANS); | ||
const expected = Math.atan(0.5); | ||
const actual = mockP5Prototype.atan(0.5); | ||
assert.closeTo(actual, expected, 1e-10); | ||
}); | ||
|
||
test('should return the correct value for atan(0.5) in degrees', function() { | ||
mockP5Prototype.angleMode(mockP5.DEGREES); | ||
const expected = Math.atan(0.5) * 180 / Math.PI; | ||
const actual = mockP5Prototype.atan(0.5); | ||
assert.closeTo(actual, expected, 1e-10); | ||
}); | ||
|
||
test('atan(y, x) outside strands returns undefined', function() { | ||
const result = mockP5Prototype.atan(1, 1); | ||
assert.isUndefined(result); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we test the actual
atan
without testing a mock version? I think the only thing we really need a test for is just that, outside of strands, you can callatan
with one argument, which it already supports without needing an override.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason being, it becomes a bit unclear how much of the functionality we're testing is just in the mock once we start doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @davepagurek ,
I’ve updated the atan unit test to only verify single-argument behavior (in both radians and degrees) using the actual function, without overriding or mocking it.
Could you please confirm if this approach looks good before I commit and push the changes?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That approach looks right! maybe just evaluate the two functions on a calculator to get as specific of a value to compare to as possible for your radians and degrees cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification @davepagurek. I’ve updated the tests to directly compare against pre-calculated values for atan(0.5) in both radians and degrees. Here’s the full updated test file :
Could you confirm if this looks good before I push the changes? Happy to make any further adjustments if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing and confirming, @davepagurek! I’ve pushed the changes accordingly. Please let me know if there’s anything else needed.