Properly make hex numbers

This commit is contained in:
Eryn Wells 2016-12-27 09:52:26 -07:00
parent 798539d219
commit 9ac596187d

View file

@ -87,8 +87,8 @@ impl NumberBuilder {
fn place_value(digit: char) -> Option<f64> { fn place_value(digit: char) -> Option<f64> {
match digit { match digit {
'0' ... '9' => Some((digit as u32 - '0' as u32) as f64), '0' ... '9' => Some((digit as u32 - '0' as u32) as f64),
'a' ... 'f' => Some((digit as u32 - 'a' as u32) as f64), 'a' ... 'f' => Some((digit as u32 - 'a' as u32 + 10) as f64),
'A' ... 'F' => Some((digit as u32 - 'A' as u32) as f64), 'A' ... 'F' => Some((digit as u32 - 'A' as u32 + 10) as f64),
_ => None, _ => None,
} }
} }
@ -158,4 +158,15 @@ mod tests {
b.extend_decimal_value('4'); b.extend_decimal_value('4');
assert_eq!(b.resolve().value, 5.34); assert_eq!(b.resolve().value, 5.34);
} }
#[test]
fn builds_hex() {
let mut b = NumberBuilder::new();
b.radix(Radix::Hex).extend_value('4');
assert_eq!(b.resolve().value, 0x4 as f64);
b.extend_value('A');
assert_eq!(b.resolve().value, 0x4A as f64);
b.extend_value('6');
assert_eq!(b.resolve().value, 0x4A6 as f64);
}
} }