Skip to content

Commit

Permalink
feat: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Nov 6, 2024
1 parent 403d524 commit 639e03b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/curr/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod bytes;
#[cfg(feature = "hex")]
impl super::Hash {
pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
Ok(super::Hash(create::hex::padded_hex_from_str(s)?))
Ok(super::Hash(crate::hex::padded_hex_from_str(s)?))
}
}
1 change: 1 addition & 0 deletions src/curr/hash/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl TryFrom<HashIdPreimage> for Hash {
}
}

#[cfg(feature = "alloc")]
impl TryFrom<HashIdPreimage> for stellar_strkey::Contract {
type Error = super::super::Error;
fn try_from(value: HashIdPreimage) -> Result<Self, Self::Error> {
Expand Down
27 changes: 16 additions & 11 deletions src/curr/num_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts {
}
}

impl From<UInt256Parts> for (u128, u128) {
fn from(parts: UInt256Parts) -> Self {
let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo);
let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo);
(hi, lo)
}
}

impl From<(i128, i128)> for Int256Parts {
fn from((hi, lo): (i128, i128)) -> Self {
let Int128Parts {
Expand All @@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts {
}
}

impl From<Int256Parts> for (i128, i128) {
fn from(parts: Int256Parts) -> Self {
let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo);
let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo);
(hi, lo)
}
}

impl From<u128> for UInt128Parts {
fn from(val: u128) -> Self {
let hi = (val >> 64) as u64;
Expand Down Expand Up @@ -99,14 +115,3 @@ impl FromStr for Int128Parts {
Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into())
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn round_trip_u128() {
let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128;
let xdr_val: UInt128Parts = u128_val.into();
assert_eq!(xdr_val.into(), u128_val);
}
}
2 changes: 1 addition & 1 deletion src/next/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod bytes;
#[cfg(feature = "hex")]
impl super::Hash {
pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
Ok(super::Hash(create::hex::padded_hex_from_str(s)?))
Ok(super::Hash(crate::hex::padded_hex_from_str(s)?))
}
}
1 change: 1 addition & 0 deletions src/next/hash/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl TryFrom<HashIdPreimage> for Hash {
}
}

#[cfg(feature = "alloc")]
impl TryFrom<HashIdPreimage> for stellar_strkey::Contract {
type Error = super::super::Error;
fn try_from(value: HashIdPreimage) -> Result<Self, Self::Error> {
Expand Down
27 changes: 16 additions & 11 deletions src/next/num_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts {
}
}

impl From<UInt256Parts> for (u128, u128) {
fn from(parts: UInt256Parts) -> Self {
let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo);
let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo);
(hi, lo)
}
}

impl From<(i128, i128)> for Int256Parts {
fn from((hi, lo): (i128, i128)) -> Self {
let Int128Parts {
Expand All @@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts {
}
}

impl From<Int256Parts> for (i128, i128) {
fn from(parts: Int256Parts) -> Self {
let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo);
let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo);
(hi, lo)
}
}

impl From<u128> for UInt128Parts {
fn from(val: u128) -> Self {
let hi = (val >> 64) as u64;
Expand Down Expand Up @@ -99,14 +115,3 @@ impl FromStr for Int128Parts {
Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into())
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn round_trip_u128() {
let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128;
let xdr_val: UInt128Parts = u128_val.into();
assert_eq!(xdr_val.into(), u128_val);
}
}
22 changes: 15 additions & 7 deletions tests/num_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#[cfg(feature = "curr")]
use stellar_xdr::curr as stellar_xdr;
// #[cfg(feature = "next")]
// use stellar_xdr::next as stellar_xdr;
#[cfg(feature = "next")]
use stellar_xdr::next as stellar_xdr;

use stellar_xdr::{Int128Parts, UInt128Parts};
use stellar_xdr::{Int128Parts, UInt128Parts, UInt256Parts};

#[test]
fn round_trip_u128() {
Expand All @@ -29,8 +29,16 @@ fn round_trip_i128() {

#[test]
fn round_trip_u256() {
let u256_val = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefu128;
let xdr_val: UInt128Parts = u256_val.into();
let u256_val2: u128 = xdr_val.into();
assert_eq!(u256_val, u256_val2);
let (hi, lo) = (0x1234567890abcdefu128, 0x1234567890abcdefu128);
let xdr_val: UInt256Parts = (hi, lo).into();
let (hi2, lo2): (u128, u128) = xdr_val.into();
assert_eq!((hi, lo), (hi2, lo2));
}

#[test]
fn round_trip_i256() {
let (hi, lo) = (0x1234567890abcdefi128, 0x1234567890abcdefi128);
let xdr_val: Int256Parts = (hi, lo).into();
let (hi2, lo2): (i128, i128) = xdr_val.into();
assert_eq!((hi, lo), (hi2, lo2));
}

0 comments on commit 639e03b

Please sign in to comment.