Clean up isize
, usize
. Don't bench allocation but formatting.
This commit is contained in:
parent
8160fc4786
commit
d909dad066
@ -14,5 +14,5 @@ mod num;
|
||||
fn test_format_flags() {
|
||||
// No residual flags left by pointer formatting
|
||||
let p = "".as_ptr();
|
||||
assert_eq!(format!("{:p} {:x}", p, 16u), format!("{:p} 10", p));
|
||||
assert_eq!(format!("{:p} {:x}", p, 16), format!("{:p} 10", p));
|
||||
}
|
||||
|
@ -16,115 +16,115 @@ 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!(format!("{}", 1i) == "1");
|
||||
assert!(format!("{}", 1is) == "1");
|
||||
assert!(format!("{}", 1i8) == "1");
|
||||
assert!(format!("{}", 1i16) == "1");
|
||||
assert!(format!("{}", 1i32) == "1");
|
||||
assert!(format!("{}", 1i64) == "1");
|
||||
assert!(format!("{}", -1i) == "-1");
|
||||
assert!(format!("{}", -1is) == "-1");
|
||||
assert!(format!("{}", -1i8) == "-1");
|
||||
assert!(format!("{}", -1i16) == "-1");
|
||||
assert!(format!("{}", -1i32) == "-1");
|
||||
assert!(format!("{}", -1i64) == "-1");
|
||||
assert!(format!("{:?}", 1i) == "1");
|
||||
assert!(format!("{:?}", 1is) == "1");
|
||||
assert!(format!("{:?}", 1i8) == "1");
|
||||
assert!(format!("{:?}", 1i16) == "1");
|
||||
assert!(format!("{:?}", 1i32) == "1");
|
||||
assert!(format!("{:?}", 1i64) == "1");
|
||||
assert!(format!("{:b}", 1i) == "1");
|
||||
assert!(format!("{:b}", 1is) == "1");
|
||||
assert!(format!("{:b}", 1i8) == "1");
|
||||
assert!(format!("{:b}", 1i16) == "1");
|
||||
assert!(format!("{:b}", 1i32) == "1");
|
||||
assert!(format!("{:b}", 1i64) == "1");
|
||||
assert!(format!("{:x}", 1i) == "1");
|
||||
assert!(format!("{:x}", 1is) == "1");
|
||||
assert!(format!("{:x}", 1i8) == "1");
|
||||
assert!(format!("{:x}", 1i16) == "1");
|
||||
assert!(format!("{:x}", 1i32) == "1");
|
||||
assert!(format!("{:x}", 1i64) == "1");
|
||||
assert!(format!("{:X}", 1i) == "1");
|
||||
assert!(format!("{:X}", 1is) == "1");
|
||||
assert!(format!("{:X}", 1i8) == "1");
|
||||
assert!(format!("{:X}", 1i16) == "1");
|
||||
assert!(format!("{:X}", 1i32) == "1");
|
||||
assert!(format!("{:X}", 1i64) == "1");
|
||||
assert!(format!("{:o}", 1i) == "1");
|
||||
assert!(format!("{:o}", 1is) == "1");
|
||||
assert!(format!("{:o}", 1i8) == "1");
|
||||
assert!(format!("{:o}", 1i16) == "1");
|
||||
assert!(format!("{:o}", 1i32) == "1");
|
||||
assert!(format!("{:o}", 1i64) == "1");
|
||||
|
||||
assert!(format!("{}", 1u) == "1");
|
||||
assert!(format!("{}", 1us) == "1");
|
||||
assert!(format!("{}", 1u8) == "1");
|
||||
assert!(format!("{}", 1u16) == "1");
|
||||
assert!(format!("{}", 1u32) == "1");
|
||||
assert!(format!("{}", 1u64) == "1");
|
||||
assert!(format!("{:?}", 1u) == "1");
|
||||
assert!(format!("{:?}", 1us) == "1");
|
||||
assert!(format!("{:?}", 1u8) == "1");
|
||||
assert!(format!("{:?}", 1u16) == "1");
|
||||
assert!(format!("{:?}", 1u32) == "1");
|
||||
assert!(format!("{:?}", 1u64) == "1");
|
||||
assert!(format!("{:b}", 1u) == "1");
|
||||
assert!(format!("{:b}", 1us) == "1");
|
||||
assert!(format!("{:b}", 1u8) == "1");
|
||||
assert!(format!("{:b}", 1u16) == "1");
|
||||
assert!(format!("{:b}", 1u32) == "1");
|
||||
assert!(format!("{:b}", 1u64) == "1");
|
||||
assert!(format!("{:x}", 1u) == "1");
|
||||
assert!(format!("{:x}", 1us) == "1");
|
||||
assert!(format!("{:x}", 1u8) == "1");
|
||||
assert!(format!("{:x}", 1u16) == "1");
|
||||
assert!(format!("{:x}", 1u32) == "1");
|
||||
assert!(format!("{:x}", 1u64) == "1");
|
||||
assert!(format!("{:X}", 1u) == "1");
|
||||
assert!(format!("{:X}", 1us) == "1");
|
||||
assert!(format!("{:X}", 1u8) == "1");
|
||||
assert!(format!("{:X}", 1u16) == "1");
|
||||
assert!(format!("{:X}", 1u32) == "1");
|
||||
assert!(format!("{:X}", 1u64) == "1");
|
||||
assert!(format!("{:o}", 1u) == "1");
|
||||
assert!(format!("{:o}", 1us) == "1");
|
||||
assert!(format!("{:o}", 1u8) == "1");
|
||||
assert!(format!("{:o}", 1u16) == "1");
|
||||
assert!(format!("{:o}", 1u32) == "1");
|
||||
assert!(format!("{:o}", 1u64) == "1");
|
||||
|
||||
// Test a larger number
|
||||
assert!(format!("{:b}", 55i) == "110111");
|
||||
assert!(format!("{:o}", 55i) == "67");
|
||||
assert!(format!("{}", 55i) == "55");
|
||||
assert!(format!("{:x}", 55i) == "37");
|
||||
assert!(format!("{:X}", 55i) == "37");
|
||||
assert!(format!("{:b}", 55) == "110111");
|
||||
assert!(format!("{:o}", 55) == "67");
|
||||
assert!(format!("{}", 55) == "55");
|
||||
assert!(format!("{:x}", 55) == "37");
|
||||
assert!(format!("{:X}", 55) == "37");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_zero() {
|
||||
assert!(format!("{}", 0i) == "0");
|
||||
assert!(format!("{:?}", 0i) == "0");
|
||||
assert!(format!("{:b}", 0i) == "0");
|
||||
assert!(format!("{:o}", 0i) == "0");
|
||||
assert!(format!("{:x}", 0i) == "0");
|
||||
assert!(format!("{:X}", 0i) == "0");
|
||||
assert!(format!("{}", 0) == "0");
|
||||
assert!(format!("{:?}", 0) == "0");
|
||||
assert!(format!("{:b}", 0) == "0");
|
||||
assert!(format!("{:o}", 0) == "0");
|
||||
assert!(format!("{:x}", 0) == "0");
|
||||
assert!(format!("{:X}", 0) == "0");
|
||||
|
||||
assert!(format!("{}", 0u) == "0");
|
||||
assert!(format!("{:?}", 0u) == "0");
|
||||
assert!(format!("{:b}", 0u) == "0");
|
||||
assert!(format!("{:o}", 0u) == "0");
|
||||
assert!(format!("{:x}", 0u) == "0");
|
||||
assert!(format!("{:X}", 0u) == "0");
|
||||
assert!(format!("{}", 0u32) == "0");
|
||||
assert!(format!("{:?}", 0u32) == "0");
|
||||
assert!(format!("{:b}", 0u32) == "0");
|
||||
assert!(format!("{:o}", 0u32) == "0");
|
||||
assert!(format!("{:x}", 0u32) == "0");
|
||||
assert!(format!("{:X}", 0u32) == "0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_int_flags() {
|
||||
assert!(format!("{:3}", 1i) == " 1");
|
||||
assert!(format!("{:>3}", 1i) == " 1");
|
||||
assert!(format!("{:>+3}", 1i) == " +1");
|
||||
assert!(format!("{:<3}", 1i) == "1 ");
|
||||
assert!(format!("{:#}", 1i) == "1");
|
||||
assert!(format!("{:#x}", 10i) == "0xa");
|
||||
assert!(format!("{:#X}", 10i) == "0xA");
|
||||
assert!(format!("{:#5x}", 10i) == " 0xa");
|
||||
assert!(format!("{:#o}", 10i) == "0o12");
|
||||
assert!(format!("{:08x}", 10i) == "0000000a");
|
||||
assert!(format!("{:8x}", 10i) == " a");
|
||||
assert!(format!("{:<8x}", 10i) == "a ");
|
||||
assert!(format!("{:>8x}", 10i) == " a");
|
||||
assert!(format!("{:#08x}", 10i) == "0x00000a");
|
||||
assert!(format!("{:08}", -10i) == "-0000010");
|
||||
assert!(format!("{:3}", 1) == " 1");
|
||||
assert!(format!("{:>3}", 1) == " 1");
|
||||
assert!(format!("{:>+3}", 1) == " +1");
|
||||
assert!(format!("{:<3}", 1) == "1 ");
|
||||
assert!(format!("{:#}", 1) == "1");
|
||||
assert!(format!("{:#x}", 10) == "0xa");
|
||||
assert!(format!("{:#X}", 10) == "0xA");
|
||||
assert!(format!("{:#5x}", 10) == " 0xa");
|
||||
assert!(format!("{:#o}", 10) == "0o12");
|
||||
assert!(format!("{:08x}", 10) == "0000000a");
|
||||
assert!(format!("{:8x}", 10) == " a");
|
||||
assert!(format!("{:<8x}", 10) == "a ");
|
||||
assert!(format!("{:>8x}", 10) == " a");
|
||||
assert!(format!("{:#08x}", 10) == "0x00000a");
|
||||
assert!(format!("{:08}", -10) == "-0000010");
|
||||
assert!(format!("{:x}", -1u8) == "ff");
|
||||
assert!(format!("{:X}", -1u8) == "FF");
|
||||
assert!(format!("{:b}", -1u8) == "11111111");
|
||||
@ -137,12 +137,12 @@ fn test_format_int_flags() {
|
||||
|
||||
#[test]
|
||||
fn test_format_int_sign_padding() {
|
||||
assert!(format!("{:+5}", 1i) == " +1");
|
||||
assert!(format!("{:+5}", -1i) == " -1");
|
||||
assert!(format!("{:05}", 1i) == "00001");
|
||||
assert!(format!("{:05}", -1i) == "-0001");
|
||||
assert!(format!("{:+05}", 1i) == "+0001");
|
||||
assert!(format!("{:+05}", -1i) == "-0001");
|
||||
assert!(format!("{:+5}", 1) == " +1");
|
||||
assert!(format!("{:+5}", -1) == " -1");
|
||||
assert!(format!("{:05}", 1) == "00001");
|
||||
assert!(format!("{:05}", -1) == "-0001");
|
||||
assert!(format!("{:+05}", 1) == "+0001");
|
||||
assert!(format!("{:+05}", -1) == "-0001");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -156,96 +156,98 @@ fn test_format_int_twos_complement() {
|
||||
|
||||
#[test]
|
||||
fn test_format_radix() {
|
||||
assert!(format!("{:04}", radix(3i, 2)) == "0011");
|
||||
assert!(format!("{}", radix(55i, 36)) == "1j");
|
||||
assert!(format!("{:04}", radix(3, 2)) == "0011");
|
||||
assert!(format!("{}", radix(55, 36)) == "1j");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_radix_base_too_large() {
|
||||
let _ = radix(55i, 37);
|
||||
let _ = radix(55, 37);
|
||||
}
|
||||
|
||||
mod uint {
|
||||
mod u32 {
|
||||
use test::Bencher;
|
||||
use core::fmt::radix;
|
||||
use std::rand::{weak_rng, Rng};
|
||||
use std::io::util::NullWriter;
|
||||
|
||||
#[bench]
|
||||
fn format_bin(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:b}", rng.gen::<uint>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:b}", rng.gen::<u32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_oct(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:o}", rng.gen::<uint>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:o}", rng.gen::<u32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_dec(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{}", rng.gen::<uint>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{}", rng.gen::<u32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_hex(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:x}", rng.gen::<uint>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:x}", rng.gen::<u32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_show(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:?}", rng.gen::<uint>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:?}", rng.gen::<u32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_base_36(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{}", radix(rng.gen::<u32>(), 36)) })
|
||||
}
|
||||
}
|
||||
|
||||
mod int {
|
||||
mod i32 {
|
||||
use test::Bencher;
|
||||
use core::fmt::radix;
|
||||
use std::rand::{weak_rng, Rng};
|
||||
use std::io::util::NullWriter;
|
||||
|
||||
#[bench]
|
||||
fn format_bin(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:b}", rng.gen::<int>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:b}", rng.gen::<i32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_oct(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:o}", rng.gen::<int>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:o}", rng.gen::<i32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_dec(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{}", rng.gen::<int>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{}", rng.gen::<i32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_hex(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:x}", rng.gen::<int>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:x}", rng.gen::<i32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_show(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{:?}", rng.gen::<int>()); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{:?}", rng.gen::<i32>()) })
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn format_base_36(b: &mut Bencher) {
|
||||
let mut rng = weak_rng();
|
||||
b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
|
||||
b.iter(|| { write!(&mut NullWriter, "{}", radix(rng.gen::<i32>(), 36)) })
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user