Skip to content

Add inline tests for all arithmetic cases in u8, u16, u32, u64, U128, and u256 #6995

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 5 commits into from
Mar 7, 2025

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Mar 5, 2025

Description

This PR adds arithmetic test cases to ensure u8, u16, u32, u64, U128, and u256 behave as expected in normal conditions, when overflow is disabled, when unsafe math is enabled, and edge cases.

Previously, the modulo and square root functions of U128 would not revert, even if unsafe math is enabled.

The following test cases are new for inline tests:

u8

  • Add
  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide
  • Divide behavior on unsafe math
  • Divide by zero reverts
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • 0th root will fail
  • Subtract
  • Subtract underflow still reverts with unsafe math enabled

u16

  • Add
  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide
  • Divide behavior on unsafe math
  • Divide by zero reverts
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • 0th root will fail
  • Subtract
  • Subtract underflow still reverts with unsafe math enabled

u32

  • Add
  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide
  • Divide behavior on unsafe math
  • Divide by zero reverts
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • 0th root will fail
  • Subtract
  • Subtract underflow still reverts with unsafe math enabled

u64

  • Add
  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide
  • Divide behavior on unsafe math
  • Divide by zero reverts
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • 0th root will fail
  • Subtract
  • Subtract underflow still reverts with unsafe math enabled

U128

  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide behavior on unsafe math
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • Subtract underflow still reverts with unsafe math enabled

u256

  • Add
  • Revert Add on overflow
  • Revert on unsafe math on overflow
  • Binary log of zero fails on overflow disabled
  • Binary log of zero returns zero on unsafe math
  • Divide
  • Divide behavior on unsafe math
  • Divide by zero reverts
  • Log still reverts when overflow enabled
  • Log behavior on unsafe math enabled
  • Log parity with Rust tests
  • Modulo zero still reverts with overflow enabled
  • Multiply
  • Multiply overflow still reverts on unsafe math enabled
  • Pow still reverts on unsafe math enabled
  • Root behavior the same on overflow enabled
  • Root behavior the same on unsafe math enabled
  • Subtract
  • Subtract underflow still reverts with unsafe math enabled

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic added the lib: std Standard library label Mar 5, 2025
@bitzoic bitzoic self-assigned this Mar 5, 2025
@bitzoic bitzoic requested review from a team as code owners March 5, 2025 17:15
@bitzoic bitzoic temporarily deployed to fuel-sway-bot March 5, 2025 17:15 — with GitHub Actions Inactive
@K1-R1 K1-R1 temporarily deployed to fuel-sway-bot March 5, 2025 18:07 — with GitHub Actions Inactive
@bitzoic bitzoic temporarily deployed to fuel-sway-bot March 6, 2025 12:22 — with GitHub Actions Inactive
@IGI-111 IGI-111 enabled auto-merge (squash) March 7, 2025 11:24
@IGI-111 IGI-111 temporarily deployed to fuel-sway-bot March 7, 2025 11:24 — with GitHub Actions Inactive
@IGI-111 IGI-111 merged commit 27be22c into master Mar 7, 2025
40 checks passed
@IGI-111 IGI-111 deleted the bitzoic-div branch March 7, 2025 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants