fix: better edge case handling and test suite improvements#9
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refines the core fixed-point multiplication/division helpers to handle edge cases (sign handling, overflows, and division by zero) more robustly, and significantly expands the test coverage across integer widths and Soroban U256/I256 types.
Changes:
- Tightens
mul_div_floor/mul_div_ceilimplementations foru64,u128,i64,i128,U256, andI256to usechecked_*operations, correct sign-sensitive floor/ceil semantics, and safe widening fallbacks (u128/i128andU256/I256). - Greatly expands and restructures test suites to cover zero, negative, overflow, “phantom overflow”, and exact/rounding scenarios for both plain and Soroban-scaled fixed-point operations.
- Relaxes the
soroban-sdkdependency constraint from23.0.2to the23major series inCargo.toml.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/u64.rs |
Refactors mul_div_floor/mul_div_ceil to use u128 as a checked widening fallback with u64::try_from, and replaces legacy fixed_* tests with direct mul_div_* tests plus updated fixed_* coverage. |
src/u256.rs |
Implements proper ceiling behavior in mul_div_ceil using remainder-based adjustment and adds comprehensive U256 tests including overflow, div-by-zero (panic) and helper for U256::MAX. |
src/u128.rs |
Updates div_ceil to use checked_div for safer division and introduces extensive tests for mul_div_*, scaled Soroban variants, and fixed_* operations over many edge cases. |
src/i64.rs |
Fixes sign-conditional logic in signed mul_div_floor/mul_div_ceil (true mathematical floor/ceil for negative rationals), uses i128 widening with try_from, and adds a rich matrix of signed test cases. |
src/i256.rs |
Corrects sign handling in I256 mul_div_floor/mul_div_ceil, adds helpers for 256-bit min/max, and significantly broadens tests for positive/negative, zero, and overflow scenarios and fixed_* methods. |
src/i128.rs |
Reimplements div_floor/div_ceil around sign-mismatch detection and checked_* ops, wires scaled Soroban variants through these helpers, and adds large, symmetric test suites for plain and Soroban fixed-point behavior; one minor comment in div_floor now mis-describes the subtraction (not the code). |
Cargo.toml |
Adjusts the soroban-sdk dependency to the 23 major version range to align with newer SDK releases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.