Skip to content

Commit

Permalink
Add tests for rounding of ties during float formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtribick committed Oct 20, 2022
1 parent 8487444 commit aa9837b
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions library/core/tests/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,66 @@ fn test_format_f64() {
assert_eq!("1234.6", format!("{:.1?}", 1234.56789f64));
}

#[test]
fn test_format_f64_rounds_ties_to_even() {
assert_eq!("0", format!("{:.0}", 0.5f64));
assert_eq!("2", format!("{:.0}", 1.5f64));
assert_eq!("2", format!("{:.0}", 2.5f64));
assert_eq!("4", format!("{:.0}", 3.5f64));
assert_eq!("4", format!("{:.0}", 4.5f64));
assert_eq!("6", format!("{:.0}", 5.5f64));
assert_eq!("128", format!("{:.0}", 127.5f64));
assert_eq!("128", format!("{:.0}", 128.5f64));
assert_eq!("0.2", format!("{:.1}", 0.25f64));
assert_eq!("0.8", format!("{:.1}", 0.75f64));
assert_eq!("0.12", format!("{:.2}", 0.125f64));
assert_eq!("0.88", format!("{:.2}", 0.875f64));
assert_eq!("0.062", format!("{:.3}", 0.062f64));
assert_eq!("-0", format!("{:.0}", -0.5f64));
assert_eq!("-2", format!("{:.0}", -1.5f64));
assert_eq!("-2", format!("{:.0}", -2.5f64));
assert_eq!("-4", format!("{:.0}", -3.5f64));
assert_eq!("-4", format!("{:.0}", -4.5f64));
assert_eq!("-6", format!("{:.0}", -5.5f64));
assert_eq!("-128", format!("{:.0}", -127.5f64));
assert_eq!("-128", format!("{:.0}", -128.5f64));
assert_eq!("-0.2", format!("{:.1}", -0.25f64));
assert_eq!("-0.8", format!("{:.1}", -0.75f64));
assert_eq!("-0.12", format!("{:.2}", -0.125f64));
assert_eq!("-0.88", format!("{:.2}", -0.875f64));
assert_eq!("-0.062", format!("{:.3}", -0.062f64));

assert_eq!("2e0", format!("{:.0e}", 1.5f64));
assert_eq!("2e0", format!("{:.0e}", 2.5f64));
assert_eq!("4e0", format!("{:.0e}", 3.5f64));
assert_eq!("4e0", format!("{:.0e}", 4.5f64));
assert_eq!("6e0", format!("{:.0e}", 5.5f64));
assert_eq!("1.28e2", format!("{:.2e}", 127.5f64));
assert_eq!("1.28e2", format!("{:.2e}", 128.5f64));
assert_eq!("-2e0", format!("{:.0e}", -1.5f64));
assert_eq!("-2e0", format!("{:.0e}", -2.5f64));
assert_eq!("-4e0", format!("{:.0e}", -3.5f64));
assert_eq!("-4e0", format!("{:.0e}", -4.5f64));
assert_eq!("-6e0", format!("{:.0e}", -5.5f64));
assert_eq!("-1.28e2", format!("{:.2e}", -127.5f64));
assert_eq!("-1.28e2", format!("{:.2e}", -128.5f64));

assert_eq!("2E0", format!("{:.0E}", 1.5f64));
assert_eq!("2E0", format!("{:.0E}", 2.5f64));
assert_eq!("4E0", format!("{:.0E}", 3.5f64));
assert_eq!("4E0", format!("{:.0E}", 4.5f64));
assert_eq!("6E0", format!("{:.0E}", 5.5f64));
assert_eq!("1.28E2", format!("{:.2E}", 127.5f64));
assert_eq!("1.28E2", format!("{:.2E}", 128.5f64));
assert_eq!("-2E0", format!("{:.0E}", -1.5f64));
assert_eq!("-2E0", format!("{:.0E}", -2.5f64));
assert_eq!("-4E0", format!("{:.0E}", -3.5f64));
assert_eq!("-4E0", format!("{:.0E}", -4.5f64));
assert_eq!("-6E0", format!("{:.0E}", -5.5f64));
assert_eq!("-1.28E2", format!("{:.2E}", -127.5f64));
assert_eq!("-1.28E2", format!("{:.2E}", -128.5f64));
}

#[test]
fn test_format_f32() {
assert_eq!("1", format!("{:.0}", 1.0f32));
Expand All @@ -50,6 +110,66 @@ fn test_format_f32() {
assert_eq!("1234.6", format!("{:.1?}", 1234.56789f32));
}

#[test]
fn test_format_f32_rounds_ties_to_even() {
assert_eq!("0", format!("{:.0}", 0.5f32));
assert_eq!("2", format!("{:.0}", 1.5f32));
assert_eq!("2", format!("{:.0}", 2.5f32));
assert_eq!("4", format!("{:.0}", 3.5f32));
assert_eq!("4", format!("{:.0}", 4.5f32));
assert_eq!("6", format!("{:.0}", 5.5f32));
assert_eq!("128", format!("{:.0}", 127.5f32));
assert_eq!("128", format!("{:.0}", 128.5f32));
assert_eq!("0.2", format!("{:.1}", 0.25f32));
assert_eq!("0.8", format!("{:.1}", 0.75f32));
assert_eq!("0.12", format!("{:.2}", 0.125f32));
assert_eq!("0.88", format!("{:.2}", 0.875f32));
assert_eq!("0.062", format!("{:.3}", 0.062f32));
assert_eq!("-0", format!("{:.0}", -0.5f32));
assert_eq!("-2", format!("{:.0}", -1.5f32));
assert_eq!("-2", format!("{:.0}", -2.5f32));
assert_eq!("-4", format!("{:.0}", -3.5f32));
assert_eq!("-4", format!("{:.0}", -4.5f32));
assert_eq!("-6", format!("{:.0}", -5.5f32));
assert_eq!("-128", format!("{:.0}", -127.5f32));
assert_eq!("-128", format!("{:.0}", -128.5f32));
assert_eq!("-0.2", format!("{:.1}", -0.25f32));
assert_eq!("-0.8", format!("{:.1}", -0.75f32));
assert_eq!("-0.12", format!("{:.2}", -0.125f32));
assert_eq!("-0.88", format!("{:.2}", -0.875f32));
assert_eq!("-0.062", format!("{:.3}", -0.062f32));

assert_eq!("2e0", format!("{:.0e}", 1.5f32));
assert_eq!("2e0", format!("{:.0e}", 2.5f32));
assert_eq!("4e0", format!("{:.0e}", 3.5f32));
assert_eq!("4e0", format!("{:.0e}", 4.5f32));
assert_eq!("6e0", format!("{:.0e}", 5.5f32));
assert_eq!("1.28e2", format!("{:.2e}", 127.5f32));
assert_eq!("1.28e2", format!("{:.2e}", 128.5f32));
assert_eq!("-2e0", format!("{:.0e}", -1.5f32));
assert_eq!("-2e0", format!("{:.0e}", -2.5f32));
assert_eq!("-4e0", format!("{:.0e}", -3.5f32));
assert_eq!("-4e0", format!("{:.0e}", -4.5f32));
assert_eq!("-6e0", format!("{:.0e}", -5.5f32));
assert_eq!("-1.28e2", format!("{:.2e}", -127.5f32));
assert_eq!("-1.28e2", format!("{:.2e}", -128.5f32));

assert_eq!("2E0", format!("{:.0E}", 1.5f32));
assert_eq!("2E0", format!("{:.0E}", 2.5f32));
assert_eq!("4E0", format!("{:.0E}", 3.5f32));
assert_eq!("4E0", format!("{:.0E}", 4.5f32));
assert_eq!("6E0", format!("{:.0E}", 5.5f32));
assert_eq!("1.28E2", format!("{:.2E}", 127.5f32));
assert_eq!("1.28E2", format!("{:.2E}", 128.5f32));
assert_eq!("-2E0", format!("{:.0E}", -1.5f32));
assert_eq!("-2E0", format!("{:.0E}", -2.5f32));
assert_eq!("-4E0", format!("{:.0E}", -3.5f32));
assert_eq!("-4E0", format!("{:.0E}", -4.5f32));
assert_eq!("-6E0", format!("{:.0E}", -5.5f32));
assert_eq!("-1.28E2", format!("{:.2E}", -127.5f32));
assert_eq!("-1.28E2", format!("{:.2E}", -128.5f32));
}

fn is_exponential(s: &str) -> bool {
s.contains("e") || s.contains("E")
}

0 comments on commit aa9837b

Please sign in to comment.