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

Add Binary, Octal to BigUint. Add UpperHex, LowerHex, Binary, Octal to BigInt

Is the testing for these enough? Any suggestions to improve them?
This commit is contained in:
Homu 2016-01-18 23:27:35 -08:00
commit 516587ce2e
1 changed files with 91 additions and 0 deletions

View File

@ -255,6 +255,18 @@ impl fmt::UpperHex for BigUint {
} }
} }
impl fmt::Binary for BigUint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(2))
}
}
impl fmt::Octal for BigUint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(8))
}
}
impl FromStr for BigUint { impl FromStr for BigUint {
type Err = ParseBigIntError; type Err = ParseBigIntError;
@ -1838,6 +1850,30 @@ impl fmt::Display for BigInt {
} }
} }
impl fmt::Binary for BigInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(2))
}
}
impl fmt::Octal for BigInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(8))
}
}
impl fmt::LowerHex for BigInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(16))
}
}
impl fmt::UpperHex for BigInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_str_radix(16).to_ascii_uppercase())
}
}
impl FromStr for BigInt { impl FromStr for BigInt {
type Err = ParseBigIntError; type Err = ParseBigIntError;
@ -3741,6 +3777,24 @@ mod biguint_tests {
assert_eq!(format!("{:X}", hello), "48656C6C6F20776F726C6421"); assert_eq!(format!("{:X}", hello), "48656C6C6F20776F726C6421");
} }
#[test]
fn test_binary() {
let a = BigUint::parse_bytes(b"A", 16).unwrap();
let hello = BigUint::parse_bytes("224055342307539".as_bytes(), 10).unwrap();
assert_eq!(format!("{:b}", a), "1010");
assert_eq!(format!("{:b}", hello), "110010111100011011110011000101101001100011010011");
}
#[test]
fn test_octal() {
let a = BigUint::parse_bytes(b"A", 16).unwrap();
let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
assert_eq!(format!("{:o}", a), "12");
assert_eq!(format!("{:o}", hello), "22062554330674403566756233062041");
}
#[test] #[test]
fn test_factor() { fn test_factor() {
fn factor(n: usize) -> BigUint { fn factor(n: usize) -> BigUint {
@ -4027,6 +4081,7 @@ mod bigint_tests {
} }
} }
#[test] #[test]
fn test_hash() { fn test_hash() {
let a = BigInt::new(NoSign, vec!()); let a = BigInt::new(NoSign, vec!());
@ -4704,6 +4759,42 @@ mod bigint_tests {
let _y = x.to_string(); let _y = x.to_string();
} }
#[test]
fn test_lower_hex() {
let a = BigInt::parse_bytes(b"A", 16).unwrap();
let hello = BigInt::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 = BigInt::parse_bytes(b"A", 16).unwrap();
let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
assert_eq!(format!("{:X}", a), "A");
assert_eq!(format!("{:X}", hello), "-48656C6C6F20776F726C6421");
}
#[test]
fn test_binary() {
let a = BigInt::parse_bytes(b"A", 16).unwrap();
let hello = BigInt::parse_bytes("-224055342307539".as_bytes(), 10).unwrap();
assert_eq!(format!("{:b}", a), "1010");
assert_eq!(format!("{:b}", hello), "-110010111100011011110011000101101001100011010011");
}
#[test]
fn test_octal() {
let a = BigInt::parse_bytes(b"A", 16).unwrap();
let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
assert_eq!(format!("{:o}", a), "12");
assert_eq!(format!("{:o}", hello), "-22062554330674403566756233062041");
}
#[test] #[test]
fn test_neg() { fn test_neg() {
assert!(-BigInt::new(Plus, vec!(1, 1, 1)) == assert!(-BigInt::new(Plus, vec!(1, 1, 1)) ==