Merge pull request #18 from paritytech/td-format

Add 0x when printing hex values.
This commit is contained in:
Marek Kotewicz 2018-02-15 13:03:51 +01:00 committed by GitHub
commit c1b08970cd
3 changed files with 25 additions and 2 deletions

20
src/hash_tests.rs Normal file
View File

@ -0,0 +1,20 @@
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);
};
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");
}

View File

@ -13,6 +13,8 @@ extern crate quickcheck;
#[cfg(test)]
extern crate serde_json;
#[cfg(test)]
pub mod hash_tests;
#[cfg(test)]
pub mod uint_tests;
#[cfg(test)]

View File

@ -254,8 +254,9 @@ fn uint256_pow_overflow_panic() {
#[test]
fn should_format_and_debug_correctly() {
let test = |x: usize, hex: &'static str, dbg: &'static str| {
assert_eq!(format!("{:?}", U256::from(x)), dbg);
let test = |x: usize, hex: &'static str, display: &'static str| {
assert_eq!(format!("{}", U256::from(x)), display);
assert_eq!(format!("{:?}", U256::from(x)), format!("0x{}", hex));
assert_eq!(format!("{:x}", U256::from(x)), hex);
};