Align raymath functions with C versions #515
Merged
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.
Fixes #514
I realise that there are a lot of changes here, even if they're backed up by tests. Let me know if you want anything dropped or moved to a separate PR.
Summary of changes
go generateruns the generator.In addition to using
go test -fuzz, fuzz tests can be run sequentially from theraylibdirectory by passing-fuzztimeto the generator e.g.go run ./internal/cmd/genraymath -fuzztime 10s.Some fuzz inputs are tested when calling
go test, namely those specified manually viaf.Add(each fuzz test has one of these) and those in thetestdatadirectory. The inputs intestdataare ones which caused tests to fail when I was running fuzz tests.Fixed functions
Some functions produced very different results from the C version, while others were just a little off due to float32/float64 rounding.
Vector3ToFloatwas moved fromrcore.gotoraymath.goso it can be parsed by the generator. It also now calls through toVector3ToFloatVlikeMatrixToFloatdoes withMatrixToFloatV.Vector2LenSqrwas removed because it's the same asVector2LengthSqr.Edge cases
The above functions are not tested because they don't have equivalent C versions.
The above functions have more complicated signatures so I've chosen to write tests manually rather than overcomplicate the generation code. Their C bindings are still generated.
Methods
Since I'm parsing
raymath.goto generate tests, generating methods is simple. Each method calls through to the non-method version though the generator supports inlining these calls. Inlining would require runninggo generateif any of the implementations of functions inraymath.gochange but may have a small performance benefit.Adding new raymath functions in the future
Running
go generatefollowed bygo test -fuzz "^Fuzz{function_name}$"should be sufficient to test most new functions, unless they modify arguments passed in as pointers.