add more tests in {display,from_str}.rs
This commit is contained in:
parent
1960c702c6
commit
e5afe1d6a4
|
@ -505,6 +505,24 @@ mod tests {
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn format() {
|
||||||
|
let pos = I16F16::from_num(12.3);
|
||||||
|
assert_eq!(format!("{:+}", pos), "+12.3");
|
||||||
|
assert_eq!(format!("{:+08}", pos), "+00012.3");
|
||||||
|
assert_eq!(format!("{:+#08}", pos), "+00012.3");
|
||||||
|
assert_eq!(format!("{:+08X}", pos), "+0C.4CCD");
|
||||||
|
assert_eq!(format!("{:+08.1X}", pos), "+0000C.5");
|
||||||
|
assert_eq!(format!("{:+#08X}", pos), "+0xC.4CCD");
|
||||||
|
assert_eq!(format!("{:+#08.1X}", pos), "+0x00C.5");
|
||||||
|
|
||||||
|
assert_eq!(format!("{:#<8}", pos), "12.3####");
|
||||||
|
assert_eq!(format!("{:#^8}", pos), "##12.3##");
|
||||||
|
assert_eq!(format!("{:#^9}", pos), "##12.3###");
|
||||||
|
assert_eq!(format!("{:#>8}", pos), "####12.3");
|
||||||
|
assert_eq!(format!("{:#^08}", pos), "000012.3");
|
||||||
|
}
|
||||||
|
|
||||||
fn trim_frac_zeros(mut x: &str) -> &str {
|
fn trim_frac_zeros(mut x: &str) -> &str {
|
||||||
while x.ends_with('0') {
|
while x.ends_with('0') {
|
||||||
x = &x[..x.len() - 1];
|
x = &x[..x.len() - 1];
|
||||||
|
@ -711,6 +729,26 @@ mod tests {
|
||||||
assert_eq!(format!("{:.3X}", i), "DD.DD0");
|
assert_eq!(format!("{:.3X}", i), "DD.DD0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compare_frac0_int() {
|
||||||
|
for u in 0..=255u8 {
|
||||||
|
let i = u as i8;
|
||||||
|
let (ifix, ufix) = (I8F0::from_bits(i), U8F0::from_bits(u));
|
||||||
|
assert_eq!(ifix.to_string(), i.to_string());
|
||||||
|
assert_eq!(ufix.to_string(), u.to_string());
|
||||||
|
if i >= 0 {
|
||||||
|
assert_eq!(format!("{:#X}", ifix), format!("{:#X}", i));
|
||||||
|
assert_eq!(format!("{:#b}", ifix), format!("{:#b}", i));
|
||||||
|
} else {
|
||||||
|
let abs_i = i.wrapping_neg() as u8;
|
||||||
|
assert_eq!(format!("{:#X}", ifix), format!("-{:#X}", abs_i));
|
||||||
|
assert_eq!(format!("{:#b}", ifix), format!("-{:#b}", abs_i));
|
||||||
|
}
|
||||||
|
assert_eq!(format!("{:#x}", ufix), format!("{:#x}", u));
|
||||||
|
assert_eq!(format!("{:#o}", ufix), format!("{:#o}", u));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compare_frac4_float() {
|
fn compare_frac4_float() {
|
||||||
for u in 0..=255u8 {
|
for u in 0..=255u8 {
|
||||||
|
|
|
@ -855,6 +855,92 @@ mod tests {
|
||||||
string::{String, ToString},
|
string::{String, ToString},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn overflowing() {
|
||||||
|
let overflow = ParseFixedError {
|
||||||
|
kind: ParseErrorKind::Overflow,
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str("15.5"),
|
||||||
|
Ok((U4F4::from_bits(0xF8), false))
|
||||||
|
);
|
||||||
|
assert_eq!(U4F4::from_str("15.5"), Ok(U4F4::from_bits(0xF8)));
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str("31.5"),
|
||||||
|
Ok((U4F4::from_bits(0xF8), true))
|
||||||
|
);
|
||||||
|
assert_eq!(U4F4::from_str("31.5"), Err(overflow));
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str("271.5"),
|
||||||
|
Ok((U4F4::from_bits(0xF8), true))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
U8F0::overflowing_from_str("271"),
|
||||||
|
Ok((U8F0::from_bits(0x0F), true))
|
||||||
|
);
|
||||||
|
let longer_than_8 = format!("{}", (1 << 30) + 15);
|
||||||
|
assert_eq!(
|
||||||
|
U8F0::overflowing_from_str(&longer_than_8),
|
||||||
|
Ok((U8F0::from_bits(0x0F), true))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str_binary("1111.1000"),
|
||||||
|
Ok((U4F4::from_bits(0xF8), false))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::from_str_binary("1111.1000"),
|
||||||
|
Ok(U4F4::from_bits(0xF8))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str_binary("11111.1000"),
|
||||||
|
Ok((U4F4::from_bits(0xF8), true))
|
||||||
|
);
|
||||||
|
assert_eq!(U4F4::from_str_binary("11111.1000"), Err(overflow));
|
||||||
|
assert_eq!(
|
||||||
|
U8F0::overflowing_from_str_binary("100001111"),
|
||||||
|
Ok((U8F0::from_bits(0x0F), true))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str_octal("17.7"),
|
||||||
|
Ok((U4F4::from_bits(0xFE), false))
|
||||||
|
);
|
||||||
|
assert_eq!(U4F4::from_str_octal("17.7"), Ok(U4F4::from_bits(0xFE)));
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str_octal("77.7"),
|
||||||
|
Ok((U4F4::from_bits(0xFE), true))
|
||||||
|
);
|
||||||
|
assert_eq!(U4F4::from_str_octal("77.7"), Err(overflow));
|
||||||
|
assert_eq!(
|
||||||
|
U4F4::overflowing_from_str_octal("707.7"),
|
||||||
|
Ok((U4F4::from_bits(0x7E), true))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
U8F0::overflowing_from_str_octal("1307"),
|
||||||
|
Ok((U8F0::from_bits(0o307), true))
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
U6F10::overflowing_from_str_hex("3F.8"),
|
||||||
|
Ok((U6F10::from_bits(0xFE00), false))
|
||||||
|
);
|
||||||
|
assert_eq!(U6F10::from_str_hex("3F.8"), Ok(U6F10::from_bits(0xFE00)));
|
||||||
|
assert_eq!(
|
||||||
|
U6F10::overflowing_from_str_hex("FF.8"),
|
||||||
|
Ok((U6F10::from_bits(0xFE00), true))
|
||||||
|
);
|
||||||
|
assert_eq!(U6F10::from_str_hex("FF.8"), Err(overflow));
|
||||||
|
assert_eq!(
|
||||||
|
U6F10::overflowing_from_str_hex("F0F.8"),
|
||||||
|
Ok((U6F10::from_bits(0x3E00), true))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
U16F0::overflowing_from_str_hex("100FF"),
|
||||||
|
Ok((U16F0::from_bits(0x00FF), true))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_dec_8() {
|
fn check_dec_8() {
|
||||||
let two_pow = 8f64.exp2();
|
let two_pow = 8f64.exp2();
|
||||||
|
@ -977,6 +1063,10 @@ mod tests {
|
||||||
Round::Nearest,
|
Round::Nearest,
|
||||||
);
|
);
|
||||||
assert_eq!(x, Some(42_010_168_377_579_896_403_540_037_811_203_677_112));
|
assert_eq!(x, Some(42_010_168_377_579_896_403_540_037_811_203_677_112));
|
||||||
|
|
||||||
|
let eights = 888_888_888_888_888_888_888_888_888;
|
||||||
|
let narrow = <u128 as DecToBin>::dec_to_bin((eights, zeros), 40, Round::Nearest);
|
||||||
|
assert_eq!(narrow, Some(977_343_669_134));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue