2013-07-29 03:12:41 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
struct A;
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
#[fmt="foo"]
|
|
|
|
impl fmt::Signed for A {
|
|
|
|
fn fmt(_: &A, f: &mut fmt::Formatter) { f.buf.write("aloha".as_bytes()); }
|
|
|
|
}
|
|
|
|
impl fmt::Signed for B {
|
|
|
|
fn fmt(_: &B, f: &mut fmt::Formatter) { f.buf.write("adios".as_bytes()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2013-08-10 02:28:47 -05:00
|
|
|
macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Make sure there's a poly formatter that takes anything
|
2013-08-10 02:28:47 -05:00
|
|
|
t!(ifmt!("{}", 1), "1");
|
|
|
|
t!(ifmt!("{}", A), "{}");
|
|
|
|
t!(ifmt!("{}", ()), "()");
|
|
|
|
t!(ifmt!("{}", @(~1, "foo")), "@(~1, \"foo\")");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Various edge cases without formats
|
2013-08-10 02:28:47 -05:00
|
|
|
t!(ifmt!(""), "");
|
|
|
|
t!(ifmt!("hello"), "hello");
|
|
|
|
t!(ifmt!("hello \\{"), "hello {");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// At least exercise all the formats
|
2013-08-10 02:28:47 -05:00
|
|
|
t!(ifmt!("{:b}", true), "true");
|
|
|
|
t!(ifmt!("{:c}", '☃'), "☃");
|
|
|
|
t!(ifmt!("{:d}", 10), "10");
|
|
|
|
t!(ifmt!("{:i}", 10), "10");
|
|
|
|
t!(ifmt!("{:u}", 10u), "10");
|
|
|
|
t!(ifmt!("{:o}", 10u), "12");
|
|
|
|
t!(ifmt!("{:x}", 10u), "a");
|
|
|
|
t!(ifmt!("{:X}", 10u), "A");
|
|
|
|
t!(ifmt!("{:s}", "foo"), "foo");
|
|
|
|
t!(ifmt!("{:p}", 0x1234 as *int), "0x1234");
|
|
|
|
t!(ifmt!("{:p}", 0x1234 as *mut int), "0x1234");
|
|
|
|
t!(ifmt!("{:d}", A), "aloha");
|
|
|
|
t!(ifmt!("{:d}", B), "adios");
|
|
|
|
t!(ifmt!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
|
|
|
|
t!(ifmt!("{1} {0}", 0, 1), "1 0");
|
|
|
|
t!(ifmt!("{foo} {bar}", foo=0, bar=1), "0 1");
|
|
|
|
t!(ifmt!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
|
|
|
|
t!(ifmt!("{} {0:s}", "a"), "a a");
|
|
|
|
t!(ifmt!("{} {0}", "a"), "\"a\" \"a\"");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Methods should probably work
|
2013-08-10 02:28:47 -05:00
|
|
|
t!(ifmt!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
|
|
|
|
t!(ifmt!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
|
|
|
|
t!(ifmt!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
|
|
|
|
t!(ifmt!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
|
|
|
|
t!(ifmt!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
|
|
|
|
t!(ifmt!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
|
|
|
|
t!(ifmt!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
|
|
|
|
t!(ifmt!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
|
|
|
|
t!(ifmt!("{1, select, a{#{0:s}} other{#{1}}}", "b", "a"), "ab");
|
|
|
|
t!(ifmt!("{1, select, a{#{0}} other{#{1}}}", "c", "b"), "bb");
|
|
|
|
|
|
|
|
// Formatting strings and their arguments
|
|
|
|
t!(ifmt!("{:s}", "a"), "a");
|
|
|
|
t!(ifmt!("{:4s}", "a"), "a ");
|
|
|
|
t!(ifmt!("{:>4s}", "a"), " a");
|
|
|
|
t!(ifmt!("{:<4s}", "a"), "a ");
|
|
|
|
t!(ifmt!("{:.4s}", "a"), "a");
|
|
|
|
t!(ifmt!("{:4.4s}", "a"), "a ");
|
|
|
|
t!(ifmt!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:2.4s}", "aaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:2.4s}", "aaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:2.4s}", "aaa"), "aaa");
|
|
|
|
t!(ifmt!("{:2.4s}", "aa"), "aa");
|
|
|
|
t!(ifmt!("{:2.4s}", "a"), "a ");
|
|
|
|
t!(ifmt!("{:0>2s}", "a"), "0a");
|
|
|
|
t!(ifmt!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(ifmt!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
|
|
|
|
t!(ifmt!("{:1$s}", "a", 4), "a ");
|
|
|
|
t!(ifmt!("{:-#s}", "a"), "a");
|
|
|
|
t!(ifmt!("{:+#s}", "a"), "a");
|
|
|
|
|
2013-08-10 15:38:32 -05:00
|
|
|
// 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.
|
|
|
|
t!(ifmt!("{:d}", -1i), "-1");
|
|
|
|
t!(ifmt!("{:d}", -1i8), "1");
|
|
|
|
t!(ifmt!("{:d}", -1i16), "1");
|
|
|
|
t!(ifmt!("{:d}", -1i32), "1");
|
|
|
|
t!(ifmt!("{:d}", -1i64), "1");
|
|
|
|
t!(ifmt!("{:t}", -1i), "1");
|
|
|
|
t!(ifmt!("{:t}", -1i8), "1");
|
|
|
|
t!(ifmt!("{:t}", -1i16), "1");
|
|
|
|
t!(ifmt!("{:t}", -1i32), "1");
|
|
|
|
t!(ifmt!("{:t}", -1i64), "1");
|
|
|
|
t!(ifmt!("{:x}", -1i), "1");
|
|
|
|
t!(ifmt!("{:x}", -1i8), "1");
|
|
|
|
t!(ifmt!("{:x}", -1i16), "1");
|
|
|
|
t!(ifmt!("{:x}", -1i32), "1");
|
|
|
|
t!(ifmt!("{:x}", -1i64), "1");
|
|
|
|
t!(ifmt!("{:X}", -1i), "1");
|
|
|
|
t!(ifmt!("{:X}", -1i8), "1");
|
|
|
|
t!(ifmt!("{:X}", -1i16), "1");
|
|
|
|
t!(ifmt!("{:X}", -1i32), "1");
|
|
|
|
t!(ifmt!("{:X}", -1i64), "1");
|
|
|
|
t!(ifmt!("{:o}", -1i), "1");
|
|
|
|
t!(ifmt!("{:o}", -1i8), "1");
|
|
|
|
t!(ifmt!("{:o}", -1i16), "1");
|
|
|
|
t!(ifmt!("{:o}", -1i32), "1");
|
|
|
|
t!(ifmt!("{:o}", -1i64), "1");
|
|
|
|
|
|
|
|
t!(ifmt!("{:d}", 1u), "1");
|
|
|
|
t!(ifmt!("{:d}", 1u8), "1");
|
|
|
|
t!(ifmt!("{:d}", 1u16), "1");
|
|
|
|
t!(ifmt!("{:d}", 1u32), "1");
|
|
|
|
t!(ifmt!("{:d}", 1u64), "1");
|
|
|
|
t!(ifmt!("{:t}", 1u), "1");
|
|
|
|
t!(ifmt!("{:t}", 1u8), "1");
|
|
|
|
t!(ifmt!("{:t}", 1u16), "1");
|
|
|
|
t!(ifmt!("{:t}", 1u32), "1");
|
|
|
|
t!(ifmt!("{:t}", 1u64), "1");
|
|
|
|
t!(ifmt!("{:x}", 1u), "1");
|
|
|
|
t!(ifmt!("{:x}", 1u8), "1");
|
|
|
|
t!(ifmt!("{:x}", 1u16), "1");
|
|
|
|
t!(ifmt!("{:x}", 1u32), "1");
|
|
|
|
t!(ifmt!("{:x}", 1u64), "1");
|
|
|
|
t!(ifmt!("{:X}", 1u), "1");
|
|
|
|
t!(ifmt!("{:X}", 1u8), "1");
|
|
|
|
t!(ifmt!("{:X}", 1u16), "1");
|
|
|
|
t!(ifmt!("{:X}", 1u32), "1");
|
|
|
|
t!(ifmt!("{:X}", 1u64), "1");
|
|
|
|
t!(ifmt!("{:o}", 1u), "1");
|
|
|
|
t!(ifmt!("{:o}", 1u8), "1");
|
|
|
|
t!(ifmt!("{:o}", 1u16), "1");
|
|
|
|
t!(ifmt!("{:o}", 1u32), "1");
|
|
|
|
t!(ifmt!("{:o}", 1u64), "1");
|
|
|
|
|
|
|
|
// Test the flags for formatting integers
|
|
|
|
t!(ifmt!("{:3d}", 1), "1 ");
|
|
|
|
t!(ifmt!("{:>3d}", 1), " 1");
|
|
|
|
t!(ifmt!("{:#d}", 1), "1");
|
|
|
|
t!(ifmt!("{:#x}", 10u), "0xa");
|
|
|
|
t!(ifmt!("{:#X}", 10u), "0xA");
|
|
|
|
t!(ifmt!("{:#5x}", 10u), "0xa ");
|
|
|
|
t!(ifmt!("{:#o}", 10u), "0o12");
|
|
|
|
|
2013-08-10 02:28:47 -05:00
|
|
|
// Precision overrides 0-padding
|
|
|
|
// FIXME #2481: Recent gcc's report some of these as warnings
|
|
|
|
/*t!(ifmt!("{:0>6.5d}", 0), ~" 00000");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5u}", 0u), ~" 00000");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5x}", 0u), ~" 00000");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5d}", 10), ~" 00010");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5d}", -10), ~"-00010");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5u}", 10u), ~" 00010");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5s}", ~"t!"), ~" t!");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5c}", 'A'), ~" A");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5x}", 127u), ~" 0007f");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5X}", 127u), ~" 0007F");*/
|
|
|
|
/*t!(ifmt!("{:0>6.5o}", 10u), ~" 00012");*/
|
|
|
|
|
|
|
|
// Signed combinations
|
|
|
|
/*t!(ifmt!("{:5d}", 1), ~" 1");*/
|
|
|
|
/*t!(ifmt!("{: >5d}", -1), ~" -1");*/
|
|
|
|
/*t!(ifmt!("{:+5d}", 1), ~" +1");*/
|
|
|
|
/*t!(ifmt!("{:+5d}", -1), ~" -1");*/
|
|
|
|
/*t!(ifmt!("{:0>5d}", 1), ~" 0001");*/
|
|
|
|
/*t!(ifmt!("{:0>5d}", -1), ~"-0001");*/
|
|
|
|
/*t!(ifmt!("{:0>+5d}", 1), ~"+0001");*/
|
|
|
|
/*t!(ifmt!("{:0>+5d}", -1), ~"-0001");*/
|
|
|
|
/*t!(ifmt!("%- 5d", 1), ~" 1 ");*/
|
|
|
|
/*t!(ifmt!("%- 5d", -1), ~"-1 ");*/
|
|
|
|
/*t!(ifmt!("%-+5d", 1), ~"+1 ");*/
|
|
|
|
/*t!(ifmt!("%-+5d", -1), ~"-1 ");*/
|
|
|
|
/*t!(ifmt!("%- 05d", 1), ~" 1 ");*/
|
|
|
|
/*t!(ifmt!("%- 05d", -1), ~"-1 ");*/
|
|
|
|
/*t!(ifmt!("%-+05d", 1), ~"+1 ");*/
|
|
|
|
/*t!(ifmt!("%-+05d", -1), ~"-1 ");*/
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|