Skip to content

Commit 92f8027

Browse files
committed
Improve testing exponential formatting
Use autofcg to detect if integers support exponential formatting. If so, autocfg sets the has_int_exp_fmt cfg, used in tests.
1 parent 92064f3 commit 92f8027

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ default = ["bigint-std", "std"]
4545
std = ["num-integer/std", "num-traits/std"]
4646
bigint = ["num-bigint"]
4747
bigint-std = ["bigint", "num-bigint/std"]
48+
49+
[build-dependencies]
50+
autocfg = "1.0.0"

build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fn main() {
2+
let ac = autocfg::new();
3+
if ac.probe_expression("format!(\"{:e}\", 0_i32)") {
4+
if !(ac.probe_expression("format!(\"{:e}\", 0_i8)") &&
5+
ac.probe_expression("format!(\"{:e}\", 0_i16)") &&
6+
ac.probe_expression("format!(\"{:e}\", 0_i32)") &&
7+
ac.probe_expression("format!(\"{:e}\", 0_i64)") &&
8+
ac.probe_expression("format!(\"{:e}\", 0_i128)") &&
9+
ac.probe_expression("format!(\"{:e}\", 0_u8)") &&
10+
ac.probe_expression("format!(\"{:e}\", 0_u16)") &&
11+
ac.probe_expression("format!(\"{:e}\", 0_u32)") &&
12+
ac.probe_expression("format!(\"{:e}\", 0_u64)") &&
13+
ac.probe_expression("format!(\"{:e}\", 0_u128)")) {
14+
panic!("Some integer types implement *Exp traits, but not others")
15+
}
16+
println!("cargo:rustc-cfg=has_int_exp_fmt");
17+
}
18+
19+
autocfg::rerun_path(file!());
20+
}

src/lib.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ macro_rules! impl_formatting {
10271027
}
10281028
#[cfg(not(feature = "std"))]
10291029
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1030-
plus = if f.sign_plus() && self.numer >= T::zero() {
1030+
let plus = if f.sign_plus() && self.numer >= T::zero() {
10311031
"+"
10321032
} else {
10331033
""
@@ -1450,6 +1450,10 @@ mod test {
14501450
numer: isize::MAX - 1,
14511451
denom: 1,
14521452
};
1453+
pub const _BILLION: Rational = Ratio {
1454+
numer: 1_000_000_000,
1455+
denom: 1,
1456+
};
14531457

14541458
#[cfg(feature = "bigint")]
14551459
pub fn to_big(n: Rational) -> BigRational {
@@ -1769,31 +1773,24 @@ mod test {
17691773
assert_fmt_eq!(format_args!("{:X}", -half_i8), "FF/2");
17701774
assert_fmt_eq!(format_args!("{:#X}", -half_i8), "0xFF/0x2");
17711775

1772-
let _one_tenth_1_f = Ratio {
1773-
numer: 0.1_f32,
1774-
denom: 1.0_f32,
1775-
};
1776-
let _1000_f = Ratio {
1777-
numer: 1000.0_f32,
1778-
denom: 1.0_f32,
1779-
};
1780-
let _1_big_f = Ratio {
1781-
numer: 1.0_f32,
1782-
denom: 3.14159e38,
1783-
};
1784-
// assert_fmt_eq!(format_args!("{:e}", _one_tenth_1_f), "1e-1");
1785-
// assert_fmt_eq!(format_args!("{:#e}", _one_tenth_1_f), "1e-1");
1786-
// assert_fmt_eq!(format_args!("{:e}", _1000_f), "1e3");
1787-
// assert_fmt_eq!(format_args!("{:#e}", _1000_f), "1e3");
1788-
// assert_fmt_eq!(format_args!("{:e}", _1_big_f), "1e0/3.14159e38");
1789-
// assert_fmt_eq!(format_args!("{:#e}", _1_big_f), "1e0/3.14159e38");
1790-
1791-
// assert_fmt_eq!(format_args!("{:E}", _one_tenth_1_f), "1E-1");
1792-
// assert_fmt_eq!(format_args!("{:#E}", _one_tenth_1_f), "1E-1");
1793-
// assert_fmt_eq!(format_args!("{:E}", _1000_f), "1E3");
1794-
// assert_fmt_eq!(format_args!("{:#E}", _1000_f), "1E3");
1795-
// assert_fmt_eq!(format_args!("{:E}", _1_big_f), "1E0/3.14159E38");
1796-
// assert_fmt_eq!(format_args!("{:#E}", _1_big_f), "1E0/3.14159E38");
1776+
#[cfg(has_int_exp_fmt)]
1777+
{
1778+
assert_fmt_eq!(format_args!("{:e}", -_2), "-2e0");
1779+
assert_fmt_eq!(format_args!("{:#e}", -_2), "-2e0");
1780+
assert_fmt_eq!(format_args!("{:+e}", -_2), "-2e0");
1781+
assert_fmt_eq!(format_args!("{:e}", _BILLION), "1e9");
1782+
assert_fmt_eq!(format_args!("{:+e}", _BILLION), "+1e9");
1783+
assert_fmt_eq!(format_args!("{:e}", _BILLION.recip()), "1e0/1e9");
1784+
assert_fmt_eq!(format_args!("{:+e}", _BILLION.recip()), "+1e0/1e9");
1785+
1786+
assert_fmt_eq!(format_args!("{:E}", -_2), "-2E0");
1787+
assert_fmt_eq!(format_args!("{:#E}", -_2), "-2E0");
1788+
assert_fmt_eq!(format_args!("{:+E}", -_2), "-2E0");
1789+
assert_fmt_eq!(format_args!("{:E}", _BILLION), "1E9");
1790+
assert_fmt_eq!(format_args!("{:+E}", _BILLION), "+1E9");
1791+
assert_fmt_eq!(format_args!("{:E}", _BILLION.recip()), "1E0/1E9");
1792+
assert_fmt_eq!(format_args!("{:+E}", _BILLION.recip()), "+1E0/1E9");
1793+
}
17971794
}
17981795

17991796
mod arith {

0 commit comments

Comments
 (0)