X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibstd%2Ffmt%2Fnum.rs;h=1abfca50b54077a19eb11ae8cdacf9202f29ca7d;hb=919889a1d688a6bbe2edac8705f048f06b1b455c;hp=88b21f1f87b9e92406c7cfe608dfbb82854f9ee7;hpb=b75683cadf6c4c55360202cd6a0106be80532451;p=rust.git diff --git a/src/libstd/fmt/num.rs b/src/libstd/fmt/num.rs index 88b21f1f87b..1abfca50b54 100644 --- a/src/libstd/fmt/num.rs +++ b/src/libstd/fmt/num.rs @@ -138,7 +138,7 @@ fn digit(&self, x: u8) -> u8 { /// /// ~~~ /// use std::fmt::radix; -/// assert_eq!(format!("{}", radix(55, 36)), ~"1j"); +/// assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned()); /// ~~~ pub fn radix(x: T, base: u8) -> RadixFmt { RadixFmt(x, Radix::new(base)) @@ -192,6 +192,7 @@ mod tests { use fmt::radix; use super::{Binary, Octal, Decimal, LowerHex, UpperHex}; use super::{GenericRadix, Radix}; + use str::StrSlice; #[test] fn test_radix_base() { @@ -243,143 +244,143 @@ fn test_format_int() { // Formatting integers should select the right implementation based off // the type of the argument. Also, hex/octal/binary should be defined // for integers, but they shouldn't emit the negative sign. - assert_eq!(format!("{}", 1i), ~"1"); - assert_eq!(format!("{}", 1i8), ~"1"); - assert_eq!(format!("{}", 1i16), ~"1"); - assert_eq!(format!("{}", 1i32), ~"1"); - assert_eq!(format!("{}", 1i64), ~"1"); - assert_eq!(format!("{:d}", -1i), ~"-1"); - assert_eq!(format!("{:d}", -1i8), ~"-1"); - assert_eq!(format!("{:d}", -1i16), ~"-1"); - assert_eq!(format!("{:d}", -1i32), ~"-1"); - assert_eq!(format!("{:d}", -1i64), ~"-1"); - assert_eq!(format!("{:t}", 1i), ~"1"); - assert_eq!(format!("{:t}", 1i8), ~"1"); - assert_eq!(format!("{:t}", 1i16), ~"1"); - assert_eq!(format!("{:t}", 1i32), ~"1"); - assert_eq!(format!("{:t}", 1i64), ~"1"); - assert_eq!(format!("{:x}", 1i), ~"1"); - assert_eq!(format!("{:x}", 1i8), ~"1"); - assert_eq!(format!("{:x}", 1i16), ~"1"); - assert_eq!(format!("{:x}", 1i32), ~"1"); - assert_eq!(format!("{:x}", 1i64), ~"1"); - assert_eq!(format!("{:X}", 1i), ~"1"); - assert_eq!(format!("{:X}", 1i8), ~"1"); - assert_eq!(format!("{:X}", 1i16), ~"1"); - assert_eq!(format!("{:X}", 1i32), ~"1"); - assert_eq!(format!("{:X}", 1i64), ~"1"); - assert_eq!(format!("{:o}", 1i), ~"1"); - assert_eq!(format!("{:o}", 1i8), ~"1"); - assert_eq!(format!("{:o}", 1i16), ~"1"); - assert_eq!(format!("{:o}", 1i32), ~"1"); - assert_eq!(format!("{:o}", 1i64), ~"1"); - - assert_eq!(format!("{}", 1u), ~"1"); - assert_eq!(format!("{}", 1u8), ~"1"); - assert_eq!(format!("{}", 1u16), ~"1"); - assert_eq!(format!("{}", 1u32), ~"1"); - assert_eq!(format!("{}", 1u64), ~"1"); - assert_eq!(format!("{:u}", 1u), ~"1"); - assert_eq!(format!("{:u}", 1u8), ~"1"); - assert_eq!(format!("{:u}", 1u16), ~"1"); - assert_eq!(format!("{:u}", 1u32), ~"1"); - assert_eq!(format!("{:u}", 1u64), ~"1"); - assert_eq!(format!("{:t}", 1u), ~"1"); - assert_eq!(format!("{:t}", 1u8), ~"1"); - assert_eq!(format!("{:t}", 1u16), ~"1"); - assert_eq!(format!("{:t}", 1u32), ~"1"); - assert_eq!(format!("{:t}", 1u64), ~"1"); - assert_eq!(format!("{:x}", 1u), ~"1"); - assert_eq!(format!("{:x}", 1u8), ~"1"); - assert_eq!(format!("{:x}", 1u16), ~"1"); - assert_eq!(format!("{:x}", 1u32), ~"1"); - assert_eq!(format!("{:x}", 1u64), ~"1"); - assert_eq!(format!("{:X}", 1u), ~"1"); - assert_eq!(format!("{:X}", 1u8), ~"1"); - assert_eq!(format!("{:X}", 1u16), ~"1"); - assert_eq!(format!("{:X}", 1u32), ~"1"); - assert_eq!(format!("{:X}", 1u64), ~"1"); - assert_eq!(format!("{:o}", 1u), ~"1"); - assert_eq!(format!("{:o}", 1u8), ~"1"); - assert_eq!(format!("{:o}", 1u16), ~"1"); - assert_eq!(format!("{:o}", 1u32), ~"1"); - assert_eq!(format!("{:o}", 1u64), ~"1"); + assert_eq!(format!("{}", 1i), "1".to_owned()); + assert_eq!(format!("{}", 1i8), "1".to_owned()); + assert_eq!(format!("{}", 1i16), "1".to_owned()); + assert_eq!(format!("{}", 1i32), "1".to_owned()); + assert_eq!(format!("{}", 1i64), "1".to_owned()); + assert_eq!(format!("{:d}", -1i), "-1".to_owned()); + assert_eq!(format!("{:d}", -1i8), "-1".to_owned()); + assert_eq!(format!("{:d}", -1i16), "-1".to_owned()); + assert_eq!(format!("{:d}", -1i32), "-1".to_owned()); + assert_eq!(format!("{:d}", -1i64), "-1".to_owned()); + assert_eq!(format!("{:t}", 1i), "1".to_owned()); + assert_eq!(format!("{:t}", 1i8), "1".to_owned()); + assert_eq!(format!("{:t}", 1i16), "1".to_owned()); + assert_eq!(format!("{:t}", 1i32), "1".to_owned()); + assert_eq!(format!("{:t}", 1i64), "1".to_owned()); + assert_eq!(format!("{:x}", 1i), "1".to_owned()); + assert_eq!(format!("{:x}", 1i8), "1".to_owned()); + assert_eq!(format!("{:x}", 1i16), "1".to_owned()); + assert_eq!(format!("{:x}", 1i32), "1".to_owned()); + assert_eq!(format!("{:x}", 1i64), "1".to_owned()); + assert_eq!(format!("{:X}", 1i), "1".to_owned()); + assert_eq!(format!("{:X}", 1i8), "1".to_owned()); + assert_eq!(format!("{:X}", 1i16), "1".to_owned()); + assert_eq!(format!("{:X}", 1i32), "1".to_owned()); + assert_eq!(format!("{:X}", 1i64), "1".to_owned()); + assert_eq!(format!("{:o}", 1i), "1".to_owned()); + assert_eq!(format!("{:o}", 1i8), "1".to_owned()); + assert_eq!(format!("{:o}", 1i16), "1".to_owned()); + assert_eq!(format!("{:o}", 1i32), "1".to_owned()); + assert_eq!(format!("{:o}", 1i64), "1".to_owned()); + + assert_eq!(format!("{}", 1u), "1".to_owned()); + assert_eq!(format!("{}", 1u8), "1".to_owned()); + assert_eq!(format!("{}", 1u16), "1".to_owned()); + assert_eq!(format!("{}", 1u32), "1".to_owned()); + assert_eq!(format!("{}", 1u64), "1".to_owned()); + assert_eq!(format!("{:u}", 1u), "1".to_owned()); + assert_eq!(format!("{:u}", 1u8), "1".to_owned()); + assert_eq!(format!("{:u}", 1u16), "1".to_owned()); + assert_eq!(format!("{:u}", 1u32), "1".to_owned()); + assert_eq!(format!("{:u}", 1u64), "1".to_owned()); + assert_eq!(format!("{:t}", 1u), "1".to_owned()); + assert_eq!(format!("{:t}", 1u8), "1".to_owned()); + assert_eq!(format!("{:t}", 1u16), "1".to_owned()); + assert_eq!(format!("{:t}", 1u32), "1".to_owned()); + assert_eq!(format!("{:t}", 1u64), "1".to_owned()); + assert_eq!(format!("{:x}", 1u), "1".to_owned()); + assert_eq!(format!("{:x}", 1u8), "1".to_owned()); + assert_eq!(format!("{:x}", 1u16), "1".to_owned()); + assert_eq!(format!("{:x}", 1u32), "1".to_owned()); + assert_eq!(format!("{:x}", 1u64), "1".to_owned()); + assert_eq!(format!("{:X}", 1u), "1".to_owned()); + assert_eq!(format!("{:X}", 1u8), "1".to_owned()); + assert_eq!(format!("{:X}", 1u16), "1".to_owned()); + assert_eq!(format!("{:X}", 1u32), "1".to_owned()); + assert_eq!(format!("{:X}", 1u64), "1".to_owned()); + assert_eq!(format!("{:o}", 1u), "1".to_owned()); + assert_eq!(format!("{:o}", 1u8), "1".to_owned()); + assert_eq!(format!("{:o}", 1u16), "1".to_owned()); + assert_eq!(format!("{:o}", 1u32), "1".to_owned()); + assert_eq!(format!("{:o}", 1u64), "1".to_owned()); // Test a larger number - assert_eq!(format!("{:t}", 55), ~"110111"); - assert_eq!(format!("{:o}", 55), ~"67"); - assert_eq!(format!("{:d}", 55), ~"55"); - assert_eq!(format!("{:x}", 55), ~"37"); - assert_eq!(format!("{:X}", 55), ~"37"); + assert_eq!(format!("{:t}", 55), "110111".to_owned()); + assert_eq!(format!("{:o}", 55), "67".to_owned()); + assert_eq!(format!("{:d}", 55), "55".to_owned()); + assert_eq!(format!("{:x}", 55), "37".to_owned()); + assert_eq!(format!("{:X}", 55), "37".to_owned()); } #[test] fn test_format_int_zero() { - assert_eq!(format!("{}", 0i), ~"0"); - assert_eq!(format!("{:d}", 0i), ~"0"); - assert_eq!(format!("{:t}", 0i), ~"0"); - assert_eq!(format!("{:o}", 0i), ~"0"); - assert_eq!(format!("{:x}", 0i), ~"0"); - assert_eq!(format!("{:X}", 0i), ~"0"); - - assert_eq!(format!("{}", 0u), ~"0"); - assert_eq!(format!("{:u}", 0u), ~"0"); - assert_eq!(format!("{:t}", 0u), ~"0"); - assert_eq!(format!("{:o}", 0u), ~"0"); - assert_eq!(format!("{:x}", 0u), ~"0"); - assert_eq!(format!("{:X}", 0u), ~"0"); + assert_eq!(format!("{}", 0i), "0".to_owned()); + assert_eq!(format!("{:d}", 0i), "0".to_owned()); + assert_eq!(format!("{:t}", 0i), "0".to_owned()); + assert_eq!(format!("{:o}", 0i), "0".to_owned()); + assert_eq!(format!("{:x}", 0i), "0".to_owned()); + assert_eq!(format!("{:X}", 0i), "0".to_owned()); + + assert_eq!(format!("{}", 0u), "0".to_owned()); + assert_eq!(format!("{:u}", 0u), "0".to_owned()); + assert_eq!(format!("{:t}", 0u), "0".to_owned()); + assert_eq!(format!("{:o}", 0u), "0".to_owned()); + assert_eq!(format!("{:x}", 0u), "0".to_owned()); + assert_eq!(format!("{:X}", 0u), "0".to_owned()); } #[test] fn test_format_int_flags() { - assert_eq!(format!("{:3d}", 1), ~" 1"); - assert_eq!(format!("{:>3d}", 1), ~" 1"); - assert_eq!(format!("{:>+3d}", 1), ~" +1"); - assert_eq!(format!("{:<3d}", 1), ~"1 "); - assert_eq!(format!("{:#d}", 1), ~"1"); - assert_eq!(format!("{:#x}", 10), ~"0xa"); - assert_eq!(format!("{:#X}", 10), ~"0xA"); - assert_eq!(format!("{:#5x}", 10), ~" 0xa"); - assert_eq!(format!("{:#o}", 10), ~"0o12"); - assert_eq!(format!("{:08x}", 10), ~"0000000a"); - assert_eq!(format!("{:8x}", 10), ~" a"); - assert_eq!(format!("{:<8x}", 10), ~"a "); - assert_eq!(format!("{:>8x}", 10), ~" a"); - assert_eq!(format!("{:#08x}", 10), ~"0x00000a"); - assert_eq!(format!("{:08d}", -10), ~"-0000010"); - assert_eq!(format!("{:x}", -1u8), ~"ff"); - assert_eq!(format!("{:X}", -1u8), ~"FF"); - assert_eq!(format!("{:t}", -1u8), ~"11111111"); - assert_eq!(format!("{:o}", -1u8), ~"377"); - assert_eq!(format!("{:#x}", -1u8), ~"0xff"); - assert_eq!(format!("{:#X}", -1u8), ~"0xFF"); - assert_eq!(format!("{:#t}", -1u8), ~"0b11111111"); - assert_eq!(format!("{:#o}", -1u8), ~"0o377"); + assert_eq!(format!("{:3d}", 1), " 1".to_owned()); + assert_eq!(format!("{:>3d}", 1), " 1".to_owned()); + assert_eq!(format!("{:>+3d}", 1), " +1".to_owned()); + assert_eq!(format!("{:<3d}", 1), "1 ".to_owned()); + assert_eq!(format!("{:#d}", 1), "1".to_owned()); + assert_eq!(format!("{:#x}", 10), "0xa".to_owned()); + assert_eq!(format!("{:#X}", 10), "0xA".to_owned()); + assert_eq!(format!("{:#5x}", 10), " 0xa".to_owned()); + assert_eq!(format!("{:#o}", 10), "0o12".to_owned()); + assert_eq!(format!("{:08x}", 10), "0000000a".to_owned()); + assert_eq!(format!("{:8x}", 10), " a".to_owned()); + assert_eq!(format!("{:<8x}", 10), "a ".to_owned()); + assert_eq!(format!("{:>8x}", 10), " a".to_owned()); + assert_eq!(format!("{:#08x}", 10), "0x00000a".to_owned()); + assert_eq!(format!("{:08d}", -10), "-0000010".to_owned()); + assert_eq!(format!("{:x}", -1u8), "ff".to_owned()); + assert_eq!(format!("{:X}", -1u8), "FF".to_owned()); + assert_eq!(format!("{:t}", -1u8), "11111111".to_owned()); + assert_eq!(format!("{:o}", -1u8), "377".to_owned()); + assert_eq!(format!("{:#x}", -1u8), "0xff".to_owned()); + assert_eq!(format!("{:#X}", -1u8), "0xFF".to_owned()); + assert_eq!(format!("{:#t}", -1u8), "0b11111111".to_owned()); + assert_eq!(format!("{:#o}", -1u8), "0o377".to_owned()); } #[test] fn test_format_int_sign_padding() { - assert_eq!(format!("{:+5d}", 1), ~" +1"); - assert_eq!(format!("{:+5d}", -1), ~" -1"); - assert_eq!(format!("{:05d}", 1), ~"00001"); - assert_eq!(format!("{:05d}", -1), ~"-0001"); - assert_eq!(format!("{:+05d}", 1), ~"+0001"); - assert_eq!(format!("{:+05d}", -1), ~"-0001"); + assert_eq!(format!("{:+5d}", 1), " +1".to_owned()); + assert_eq!(format!("{:+5d}", -1), " -1".to_owned()); + assert_eq!(format!("{:05d}", 1), "00001".to_owned()); + assert_eq!(format!("{:05d}", -1), "-0001".to_owned()); + assert_eq!(format!("{:+05d}", 1), "+0001".to_owned()); + assert_eq!(format!("{:+05d}", -1), "-0001".to_owned()); } #[test] fn test_format_int_twos_complement() { use {i8, i16, i32, i64}; - assert_eq!(format!("{}", i8::MIN), ~"-128"); - assert_eq!(format!("{}", i16::MIN), ~"-32768"); - assert_eq!(format!("{}", i32::MIN), ~"-2147483648"); - assert_eq!(format!("{}", i64::MIN), ~"-9223372036854775808"); + assert_eq!(format!("{}", i8::MIN), "-128".to_owned()); + assert_eq!(format!("{}", i16::MIN), "-32768".to_owned()); + assert_eq!(format!("{}", i32::MIN), "-2147483648".to_owned()); + assert_eq!(format!("{}", i64::MIN), "-9223372036854775808".to_owned()); } #[test] fn test_format_radix() { - assert_eq!(format!("{:04}", radix(3, 2)), ~"0011"); - assert_eq!(format!("{}", radix(55, 36)), ~"1j"); + assert_eq!(format!("{:04}", radix(3, 2)), "0011".to_owned()); + assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned()); } #[test]