rust/src/libcoretest/fmt/num.rs

252 lines
8.1 KiB
Rust
Raw Normal View History

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2014-10-27 17:37:07 -05:00
#![allow(unsigned_negation)]
use core::fmt::radix;
#[test]
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!("{}", 1i8) == "1");
assert!(format!("{}", 1i16) == "1");
assert!(format!("{}", 1i32) == "1");
assert!(format!("{}", 1i64) == "1");
assert!(format!("{}", -1i) == "-1");
assert!(format!("{}", -1i8) == "-1");
assert!(format!("{}", -1i16) == "-1");
assert!(format!("{}", -1i32) == "-1");
assert!(format!("{}", -1i64) == "-1");
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
assert!(format!("{:?}", 1i) == "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}", 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}", 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}", 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}", 1i8) == "1");
assert!(format!("{:o}", 1i16) == "1");
assert!(format!("{:o}", 1i32) == "1");
assert!(format!("{:o}", 1i64) == "1");
assert!(format!("{}", 1u) == "1");
assert!(format!("{}", 1u8) == "1");
assert!(format!("{}", 1u16) == "1");
assert!(format!("{}", 1u32) == "1");
assert!(format!("{}", 1u64) == "1");
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
assert!(format!("{:?}", 1u) == "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}", 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}", 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}", 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}", 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");
}
#[test]
fn test_format_int_zero() {
assert!(format!("{}", 0i) == "0");
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
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!("{}", 0u) == "0");
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 17:45:07 -06:00
assert!(format!("{:?}", 0u) == "0");
assert!(format!("{:b}", 0u) == "0");
assert!(format!("{:o}", 0u) == "0");
assert!(format!("{:x}", 0u) == "0");
assert!(format!("{:X}", 0u) == "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!("{:x}", -1u8) == "ff");
assert!(format!("{:X}", -1u8) == "FF");
assert!(format!("{:b}", -1u8) == "11111111");
assert!(format!("{:o}", -1u8) == "377");
assert!(format!("{:#x}", -1u8) == "0xff");
assert!(format!("{:#X}", -1u8) == "0xFF");
assert!(format!("{:#b}", -1u8) == "0b11111111");
assert!(format!("{:#o}", -1u8) == "0o377");
}
#[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");
}
#[test]
fn test_format_int_twos_complement() {
use core::{i8, i16, i32, i64};
assert!(format!("{}", i8::MIN) == "-128");
assert!(format!("{}", i16::MIN) == "-32768");
assert!(format!("{}", i32::MIN) == "-2147483648");
assert!(format!("{}", i64::MIN) == "-9223372036854775808");
}
#[test]
fn test_format_radix() {
assert!(format!("{:04}", radix(3i, 2)) == "0011");
assert!(format!("{}", radix(55i, 36)) == "1j");
}
#[test]
#[should_fail]
fn test_radix_base_too_large() {
let _ = radix(55i, 37);
}
mod uint {
use test::Bencher;
use core::fmt::radix;
use std::rand::{weak_rng, Rng};
#[bench]
fn format_bin(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:b}", rng.gen::<uint>()); })
}
#[bench]
fn format_oct(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:o}", rng.gen::<uint>()); })
}
#[bench]
fn format_dec(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{}", rng.gen::<uint>()); })
}
#[bench]
fn format_hex(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:x}", rng.gen::<uint>()); })
}
#[bench]
fn format_show(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:?}", rng.gen::<uint>()); })
}
#[bench]
fn format_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
}
}
mod int {
use test::Bencher;
use core::fmt::radix;
use std::rand::{weak_rng, Rng};
#[bench]
fn format_bin(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:b}", rng.gen::<int>()); })
}
#[bench]
fn format_oct(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:o}", rng.gen::<int>()); })
}
#[bench]
fn format_dec(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{}", rng.gen::<int>()); })
}
#[bench]
fn format_hex(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:x}", rng.gen::<int>()); })
}
#[bench]
fn format_show(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{:?}", rng.gen::<int>()); })
}
#[bench]
fn format_base_36(b: &mut Bencher) {
let mut rng = weak_rng();
b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
}
}