Add 'uint/tests/' from commit '716691c42b794ad004d95df5ecdc77fe7d3b9d3b'

git-subtree-dir: uint/tests
git-subtree-mainline: 9144702316
git-subtree-split: 716691c42b
This commit is contained in:
David Palm 2018-07-13 15:19:51 +02:00
commit 646fe8cfb7
5 changed files with 1351 additions and 0 deletions

19
uint/tests/Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "tests"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[build-dependencies]
rustc_version = "0.2"
[dependencies]
crunchy = "0.1.5"
ethereum-types = { version = "0.3.1", path ="../ethereum-types", features = ["std", "heapsizeof"] }
quickcheck = "0.6"
serde_json = "1.0"
# TODO: remove `branch` when https://github.com/paritytech/parity-common/pull/12 lands
uint = { git = "https://github.com/paritytech/parity-common", branch = "add-fixed-hash", features = ["impl_quickcheck_arbitrary"] }
[features]
use_asm = ["uint/use_asm", "ethereum-types/use_asm"]

View File

@ -0,0 +1,21 @@
use ethereum_types::{U128, H128};
#[test]
fn should_format_and_debug_correctly() {
let test = |x: usize, hex: &'static str, display: &'static str| {
let hash = H128::from(U128::from(x));
assert_eq!(format!("{}", hash), format!("0x{}", display));
assert_eq!(format!("{:?}", hash), format!("0x{}", hex));
assert_eq!(format!("{:x}", hash), hex);
assert_eq!(format!("{:#x}", hash), format!("0x{}", hex));
};
test(0x1, "00000000000000000000000000000001", "0000…0001");
test(0xf, "0000000000000000000000000000000f", "0000…000f");
test(0x10, "00000000000000000000000000000010", "0000…0010");
test(0xff, "000000000000000000000000000000ff", "0000…00ff");
test(0x100, "00000000000000000000000000000100", "0000…0100");
test(0xfff, "00000000000000000000000000000fff", "0000…0fff");
test(0x1000, "00000000000000000000000000001000", "0000…1000");
}

21
uint/tests/src/lib.rs Normal file
View File

@ -0,0 +1,21 @@
extern crate core;
extern crate ethereum_types;
#[cfg(test)]
#[macro_use]
extern crate uint;
#[cfg(test)]
#[macro_use]
extern crate crunchy;
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
#[cfg(test)]
extern crate serde_json;
#[cfg(test)]
pub mod hash_tests;
#[cfg(test)]
pub mod uint_tests;
#[cfg(test)]
pub mod serialization;

View File

@ -0,0 +1,95 @@
use ethereum_types::{U256, U512, H160, H256};
use serde_json as ser;
macro_rules! test {
($name: ident, $test_name: ident) => {
#[test]
fn $test_name() {
let tests = vec![
($name::from(0), "0x0"),
($name::from(1), "0x1"),
($name::from(2), "0x2"),
($name::from(10), "0xa"),
($name::from(15), "0xf"),
($name::from(15), "0xf"),
($name::from(16), "0x10"),
($name::from(1_000), "0x3e8"),
($name::from(100_000), "0x186a0"),
($name::from(u64::max_value()), "0xffffffffffffffff"),
($name::from(u64::max_value()) + 1, "0x10000000000000000"),
];
for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}
// Invalid examples
assert!(ser::from_str::<$name>("\"0x\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"0xg\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"10\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"0\"").unwrap_err().is_data());
}
}
}
test!(U256, test_u256);
test!(U512, test_u512);
#[test]
fn test_large_values() {
assert_eq!(
ser::to_string_pretty(&!U256::zero()).unwrap(),
"\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
);
assert!(
ser::from_str::<U256>("\"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"").unwrap_err().is_data()
);
}
#[test]
fn test_h160() {
let tests = vec![
(H160::from(0), "0x0000000000000000000000000000000000000000"),
(H160::from(2), "0x0000000000000000000000000000000000000002"),
(H160::from(15), "0x000000000000000000000000000000000000000f"),
(H160::from(16), "0x0000000000000000000000000000000000000010"),
(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
];
for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}
}
#[test]
fn test_h256() {
let tests = vec![
(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
];
for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}
}
#[test]
fn test_invalid() {
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
}

1195
uint/tests/src/uint_tests.rs Normal file

File diff suppressed because it is too large Load Diff