Auto merge of #158 - anchovieshat:master, r=cuviper

Add LowerHex and UpperHex formatting

These were mysteriously missing from BigUint.
This commit is contained in:
Homu 2016-01-18 10:47:39 -08:00
commit e3bc794ca4
1 changed files with 31 additions and 0 deletions

View File

@ -76,6 +76,7 @@ use std::fmt;
use std::cmp::Ordering::{self, Less, Greater, Equal}; use std::cmp::Ordering::{self, Less, Greater, Equal};
use std::{f32, f64}; use std::{f32, f64};
use std::{u8, i64, u64}; use std::{u8, i64, u64};
use std::ascii::AsciiExt;
// Some of the tests of non-RNG-based functionality are randomized using the // Some of the tests of non-RNG-based functionality are randomized using the
// RNG-based functionality, so the RNG-based functionality needs to be enabled // RNG-based functionality, so the RNG-based functionality needs to be enabled
@ -242,6 +243,18 @@ impl fmt::Display for BigUint {
} }
} }
impl fmt::LowerHex for BigUint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(16))
}
}
impl fmt::UpperHex for BigUint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(16).to_ascii_uppercase())
}
}
impl FromStr for BigUint { impl FromStr for BigUint {
type Err = ParseBigIntError; type Err = ParseBigIntError;
@ -3710,6 +3723,24 @@ mod biguint_tests {
} }
} }
#[test]
fn test_lower_hex() {
let a = BigUint::parse_bytes(b"A", 16).unwrap();
let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
assert_eq!(format!("{:x}", a), "a");
assert_eq!(format!("{:x}", hello), "48656c6c6f20776f726c6421");
}
#[test]
fn test_upper_hex() {
let a = BigUint::parse_bytes(b"A", 16).unwrap();
let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
assert_eq!(format!("{:X}", a), "A");
assert_eq!(format!("{:X}", hello), "48656C6C6F20776F726C6421");
}
#[test] #[test]
fn test_factor() { fn test_factor() {
fn factor(n: usize) -> BigUint { fn factor(n: usize) -> BigUint {