Skip to content

Commit 639e03b

Browse files
committed
feat: more tests
1 parent 403d524 commit 639e03b

File tree

7 files changed

+51
-31
lines changed

7 files changed

+51
-31
lines changed

src/curr/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ mod bytes;
44
#[cfg(feature = "hex")]
55
impl super::Hash {
66
pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
7-
Ok(super::Hash(create::hex::padded_hex_from_str(s)?))
7+
Ok(super::Hash(crate::hex::padded_hex_from_str(s)?))
88
}
99
}

src/curr/hash/bytes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl TryFrom<HashIdPreimage> for Hash {
1515
}
1616
}
1717

18+
#[cfg(feature = "alloc")]
1819
impl TryFrom<HashIdPreimage> for stellar_strkey::Contract {
1920
type Error = super::super::Error;
2021
fn try_from(value: HashIdPreimage) -> Result<Self, Self::Error> {

src/curr/num_conversions.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts {
2121
}
2222
}
2323

24+
impl From<UInt256Parts> for (u128, u128) {
25+
fn from(parts: UInt256Parts) -> Self {
26+
let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo);
27+
let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo);
28+
(hi, lo)
29+
}
30+
}
31+
2432
impl From<(i128, i128)> for Int256Parts {
2533
fn from((hi, lo): (i128, i128)) -> Self {
2634
let Int128Parts {
@@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts {
4048
}
4149
}
4250

51+
impl From<Int256Parts> for (i128, i128) {
52+
fn from(parts: Int256Parts) -> Self {
53+
let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo);
54+
let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo);
55+
(hi, lo)
56+
}
57+
}
58+
4359
impl From<u128> for UInt128Parts {
4460
fn from(val: u128) -> Self {
4561
let hi = (val >> 64) as u64;
@@ -99,14 +115,3 @@ impl FromStr for Int128Parts {
99115
Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into())
100116
}
101117
}
102-
103-
#[cfg(test)]
104-
mod test {
105-
use super::*;
106-
#[test]
107-
fn round_trip_u128() {
108-
let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128;
109-
let xdr_val: UInt128Parts = u128_val.into();
110-
assert_eq!(xdr_val.into(), u128_val);
111-
}
112-
}

src/next/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ mod bytes;
44
#[cfg(feature = "hex")]
55
impl super::Hash {
66
pub fn from_hex(s: &str) -> Result<Self, hex::FromHexError> {
7-
Ok(super::Hash(create::hex::padded_hex_from_str(s)?))
7+
Ok(super::Hash(crate::hex::padded_hex_from_str(s)?))
88
}
99
}

src/next/hash/bytes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl TryFrom<HashIdPreimage> for Hash {
1515
}
1616
}
1717

18+
#[cfg(feature = "alloc")]
1819
impl TryFrom<HashIdPreimage> for stellar_strkey::Contract {
1920
type Error = super::super::Error;
2021
fn try_from(value: HashIdPreimage) -> Result<Self, Self::Error> {

src/next/num_conversions.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ impl From<(u128, u128)> for UInt256Parts {
2121
}
2222
}
2323

24+
impl From<UInt256Parts> for (u128, u128) {
25+
fn from(parts: UInt256Parts) -> Self {
26+
let hi = (u128::from(parts.hi_hi) << 64) | u128::from(parts.hi_lo);
27+
let lo = (u128::from(parts.lo_hi) << 64) | u128::from(parts.lo_lo);
28+
(hi, lo)
29+
}
30+
}
31+
2432
impl From<(i128, i128)> for Int256Parts {
2533
fn from((hi, lo): (i128, i128)) -> Self {
2634
let Int128Parts {
@@ -40,6 +48,14 @@ impl From<(i128, i128)> for Int256Parts {
4048
}
4149
}
4250

51+
impl From<Int256Parts> for (i128, i128) {
52+
fn from(parts: Int256Parts) -> Self {
53+
let hi = (i128::from(parts.hi_hi) << 64) | i128::from(parts.hi_lo);
54+
let lo = (i128::from(parts.lo_hi) << 64) | i128::from(parts.lo_lo);
55+
(hi, lo)
56+
}
57+
}
58+
4359
impl From<u128> for UInt128Parts {
4460
fn from(val: u128) -> Self {
4561
let hi = (val >> 64) as u64;
@@ -99,14 +115,3 @@ impl FromStr for Int128Parts {
99115
Ok(i128::from_str(s).map_err(|_| Self::Err::Invalid)?.into())
100116
}
101117
}
102-
103-
#[cfg(test)]
104-
mod test {
105-
use super::*;
106-
#[test]
107-
fn round_trip_u128() {
108-
let u128_val: u128 = 0x1234567890abcdef1234567890abcdefu128;
109-
let xdr_val: UInt128Parts = u128_val.into();
110-
assert_eq!(xdr_val.into(), u128_val);
111-
}
112-
}

tests/num_conversions.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#[cfg(feature = "curr")]
88
use stellar_xdr::curr as stellar_xdr;
9-
// #[cfg(feature = "next")]
10-
// use stellar_xdr::next as stellar_xdr;
9+
#[cfg(feature = "next")]
10+
use stellar_xdr::next as stellar_xdr;
1111

12-
use stellar_xdr::{Int128Parts, UInt128Parts};
12+
use stellar_xdr::{Int128Parts, UInt128Parts, UInt256Parts};
1313

1414
#[test]
1515
fn round_trip_u128() {
@@ -29,8 +29,16 @@ fn round_trip_i128() {
2929

3030
#[test]
3131
fn round_trip_u256() {
32-
let u256_val = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefu128;
33-
let xdr_val: UInt128Parts = u256_val.into();
34-
let u256_val2: u128 = xdr_val.into();
35-
assert_eq!(u256_val, u256_val2);
32+
let (hi, lo) = (0x1234567890abcdefu128, 0x1234567890abcdefu128);
33+
let xdr_val: UInt256Parts = (hi, lo).into();
34+
let (hi2, lo2): (u128, u128) = xdr_val.into();
35+
assert_eq!((hi, lo), (hi2, lo2));
36+
}
37+
38+
#[test]
39+
fn round_trip_i256() {
40+
let (hi, lo) = (0x1234567890abcdefi128, 0x1234567890abcdefi128);
41+
let xdr_val: Int256Parts = (hi, lo).into();
42+
let (hi2, lo2): (i128, i128) = xdr_val.into();
43+
assert_eq!((hi, lo), (hi2, lo2));
3644
}

0 commit comments

Comments
 (0)