Skip to content
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

Add inline tests for signed integers #332

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Some addition here.
- Some addition here.
- [#332](https://github.com/FuelLabs/sway-libs/pull/332) Adds additional arithmetic operation tests for signed integers.

### Changed

- Some change here.
- [#332](https://github.com/FuelLabs/sway-libs/pull/332) Switches signed integer tests from SDK tests to inline tests.
- Some change here.

### Fixed

- Some fix here.
- Some fix here.
- [#332](https://github.com/FuelLabs/sway-libs/pull/332) Fixes signed integers to not revert when unsafe math and overflow is enabled.

### Breaking

Expand Down
30 changes: 27 additions & 3 deletions libs/src/signed_integers/i128.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::{convert::TryFrom, u128::U128};
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled, u128::U128};
use ::signed_integers::common::WrappingNeg;
use ::signed_integers::errors::Error;

Expand Down Expand Up @@ -308,7 +308,9 @@ impl Add for I128 {
impl Divide for I128 {
/// Divide a I128 by a I128. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}
let mut res = Self::new();
if (self.underlying > Self::indent()
|| self.underlying == Self::indent())
Expand Down Expand Up @@ -419,7 +421,7 @@ impl WrappingNeg for I128 {
impl TryFrom<U128> for I128 {
fn try_from(value: U128) -> Option<Self> {
// as the minimal value of I128 is -I128::indent() (1 << 63) we should add I128::indent() (1 << 63)
if value < U128::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -429,6 +431,16 @@ impl TryFrom<U128> for I128 {
}
}

impl TryInto<U128> for I128 {
fn try_into(self) -> Option<U128> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I128> for U128 {
fn try_from(value: I128) -> Option<Self> {
if value >= I128::zero() {
Expand All @@ -438,3 +450,15 @@ impl TryFrom<I128> for U128 {
}
}
}

impl TryInto<I128> for U128 {
fn try_into(self) -> Option<I128> {
if self < I128::indent() {
Some(I128 {
underlying: self + I128::indent(),
})
} else {
None
}
}
}
31 changes: 28 additions & 3 deletions libs/src/signed_integers/i16.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::convert::TryFrom;
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled};
use ::signed_integers::errors::Error;
use ::signed_integers::common::WrappingNeg;

Expand Down Expand Up @@ -300,7 +300,10 @@ impl Add for I16 {
impl Divide for I16 {
/// Divide a I16 by a I16. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}

let mut res = Self::new();
if self.underlying >= Self::indent()
&& divisor.underlying > Self::indent()
Expand Down Expand Up @@ -409,7 +412,7 @@ impl WrappingNeg for I16 {
impl TryFrom<u16> for I16 {
fn try_from(value: u16) -> Option<Self> {
// as the minimal value of I16 is -I16::indent() (1 << 15) we should add I16::indent() (1 << 15)
if value < u16::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -419,6 +422,16 @@ impl TryFrom<u16> for I16 {
}
}

impl TryInto<u16> for I16 {
fn try_into(self) -> Option<u16> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I16> for u16 {
fn try_from(value: I16) -> Option<Self> {
if value >= I16::zero() {
Expand All @@ -428,3 +441,15 @@ impl TryFrom<I16> for u16 {
}
}
}

impl TryInto<I16> for u16 {
fn try_into(self) -> Option<I16> {
if self < I16::indent() {
Some(I16 {
underlying: self + I16::indent(),
})
} else {
None
}
}
}
31 changes: 28 additions & 3 deletions libs/src/signed_integers/i256.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::convert::TryFrom;
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled};
use ::signed_integers::common::WrappingNeg;
use ::signed_integers::errors::Error;

Expand Down Expand Up @@ -304,7 +304,10 @@ impl Add for I256 {
impl Divide for I256 {
/// Divide a I256 by a I256. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}

let mut res = Self::new();
let indent = Self::indent();

Expand Down Expand Up @@ -400,7 +403,7 @@ impl WrappingNeg for I256 {
impl TryFrom<u256> for I256 {
fn try_from(value: u256) -> Option<Self> {
// as the minimal value of I256 is -I256::indent() (1 << 63) we should add I256::indent() (1 << 63)
if value < u256::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -410,6 +413,16 @@ impl TryFrom<u256> for I256 {
}
}

impl TryInto<u256> for I256 {
fn try_into(self) -> Option<u256> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I256> for u256 {
fn try_from(value: I256) -> Option<Self> {
if value >= I256::zero() {
Expand All @@ -419,3 +432,15 @@ impl TryFrom<I256> for u256 {
}
}
}

impl TryInto<I256> for u256 {
fn try_into(self) -> Option<I256> {
if self < I256::indent() {
Some(I256 {
underlying: self + I256::indent(),
})
} else {
None
}
}
}
31 changes: 28 additions & 3 deletions libs/src/signed_integers/i32.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::convert::TryFrom;
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled};
use ::signed_integers::common::WrappingNeg;
use ::signed_integers::errors::Error;

Expand Down Expand Up @@ -358,7 +358,10 @@ impl Multiply for I32 {
impl Divide for I32 {
/// Divide a I32 by a I32. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}

let mut res = Self::new();
if self.underlying >= Self::indent()
&& divisor.underlying > Self::indent()
Expand Down Expand Up @@ -409,7 +412,7 @@ impl WrappingNeg for I32 {
impl TryFrom<u32> for I32 {
fn try_from(value: u32) -> Option<Self> {
// as the minimal value of I32 is 2147483648 (1 << 31) we should add I32::indent() (1 << 31)
if value < u32::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -419,6 +422,16 @@ impl TryFrom<u32> for I32 {
}
}

impl TryInto<u32> for I32 {
fn try_into(self) -> Option<u32> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I32> for u32 {
fn try_from(value: I32) -> Option<Self> {
if value >= I32::zero() {
Expand All @@ -428,3 +441,15 @@ impl TryFrom<I32> for u32 {
}
}
}

impl TryInto<I32> for u32 {
fn try_into(self) -> Option<I32> {
if self < I32::indent() {
Some(I32 {
underlying: self + I32::indent(),
})
} else {
None
}
}
}
31 changes: 28 additions & 3 deletions libs/src/signed_integers/i64.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::convert::TryFrom;
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled};
use ::signed_integers::common::WrappingNeg;
use ::signed_integers::errors::Error;

Expand Down Expand Up @@ -359,7 +359,10 @@ impl Multiply for I64 {
impl Divide for I64 {
/// Divide a I64 by a I64. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}

let mut res = Self::new();
if self.underlying >= Self::indent()
&& divisor.underlying > Self::indent()
Expand Down Expand Up @@ -410,7 +413,7 @@ impl WrappingNeg for I64 {
impl TryFrom<u64> for I64 {
fn try_from(value: u64) -> Option<Self> {
// as the minimal value of I64 is -I64::indent() (1 << 63) we should add I64::indent() (1 << 63)
if value < u64::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -420,6 +423,16 @@ impl TryFrom<u64> for I64 {
}
}

impl TryInto<u64> for I64 {
fn try_into(self) -> Option<u64> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I64> for u64 {
fn try_from(value: I64) -> Option<Self> {
if value >= I64::zero() {
Expand All @@ -429,3 +442,15 @@ impl TryFrom<I64> for u64 {
}
}
}

impl TryInto<I64> for u64 {
fn try_into(self) -> Option<I64> {
if self < I64::indent() {
Some(I64 {
underlying: self + I64::indent(),
})
} else {
None
}
}
}
31 changes: 28 additions & 3 deletions libs/src/signed_integers/i8.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::convert::TryFrom;
use std::{convert::{TryFrom, TryInto}, flags::panic_on_unsafe_math_enabled};
use ::signed_integers::errors::Error;
use ::signed_integers::common::WrappingNeg;

Expand Down Expand Up @@ -300,7 +300,10 @@ impl Add for I8 {
impl Divide for I8 {
/// Divide a I8 by a I8. Panics if divisor is zero.
fn divide(self, divisor: Self) -> Self {
require(divisor != Self::new(), Error::ZeroDivisor);
if panic_on_unsafe_math_enabled() {
require(divisor != Self::new(), Error::ZeroDivisor);
}

let mut res = Self::new();
if self.underlying >= Self::indent()
&& divisor.underlying > Self::indent()
Expand Down Expand Up @@ -409,7 +412,7 @@ impl WrappingNeg for I8 {
impl TryFrom<u8> for I8 {
fn try_from(value: u8) -> Option<Self> {
// as the minimal value of I8 is -I8::indent() (1 << 7) we should add I8::indent() (1 << 7)
if value < u8::max() - Self::indent() {
if value < Self::indent() {
Some(Self {
underlying: value + Self::indent(),
})
Expand All @@ -419,6 +422,16 @@ impl TryFrom<u8> for I8 {
}
}

impl TryInto<u8> for I8 {
fn try_into(self) -> Option<u8> {
if self.underlying >= Self::indent() {
Some(self.underlying - Self::indent())
} else {
None
}
}
}

impl TryFrom<I8> for u8 {
fn try_from(value: I8) -> Option<Self> {
if value >= I8::zero() {
Expand All @@ -428,3 +441,15 @@ impl TryFrom<I8> for u8 {
}
}
}

impl TryInto<I8> for u8 {
fn try_into(self) -> Option<I8> {
if self < I8::indent() {
Some(I8 {
underlying: self + I8::indent(),
})
} else {
None
}
}
}
Loading
Loading