Settle on the format/write/print family of names

This commit is contained in:
Alex Crichton 2013-08-23 18:14:11 -07:00
parent 67512f717e
commit eb836dd61e
10 changed files with 293 additions and 253 deletions

View file

@ -9,66 +9,66 @@
// except according to those terms.
fn main() {
// bad arguments to the ifmt! call
// bad arguments to the format! call
ifmt!(); //~ ERROR: expects at least one
ifmt!("{}"); //~ ERROR: invalid reference to argument
format!(); //~ ERROR: requires at least a format string
format!("{}"); //~ ERROR: invalid reference to argument
ifmt!("{1}", 1); //~ ERROR: invalid reference to argument `1`
format!("{1}", 1); //~ ERROR: invalid reference to argument `1`
//~^ ERROR: argument never used
ifmt!("{foo}"); //~ ERROR: no argument named `foo`
format!("{foo}"); //~ ERROR: no argument named `foo`
ifmt!("{}", 1, 2); //~ ERROR: argument never used
ifmt!("{1}", 1, 2); //~ ERROR: argument never used
ifmt!("{}", 1, foo=2); //~ ERROR: named argument never used
ifmt!("{foo}", 1, foo=2); //~ ERROR: argument never used
ifmt!("", foo=2); //~ ERROR: named argument never used
format!("{}", 1, 2); //~ ERROR: argument never used
format!("{1}", 1, 2); //~ ERROR: argument never used
format!("{}", 1, foo=2); //~ ERROR: named argument never used
format!("{foo}", 1, foo=2); //~ ERROR: argument never used
format!("", foo=2); //~ ERROR: named argument never used
ifmt!("{0:d} {0:s}", 1); //~ ERROR: redeclared with type `s`
ifmt!("{foo:d} {foo:s}", foo=1); //~ ERROR: redeclared with type `s`
format!("{0:d} {0:s}", 1); //~ ERROR: redeclared with type `s`
format!("{foo:d} {foo:s}", foo=1); //~ ERROR: redeclared with type `s`
ifmt!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
ifmt!("#"); //~ ERROR: `#` reference used
ifmt!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
ifmt!("" 1); //~ ERROR: expected token: `,`
ifmt!("", 1 1); //~ ERROR: expected token: `,`
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
format!("#"); //~ ERROR: `#` reference used
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
format!("" 1); //~ ERROR: expected token: `,`
format!("", 1 1); //~ ERROR: expected token: `,`
ifmt!("{0, select, a{} a{} other{}}", "a"); //~ ERROR: duplicate selector
ifmt!("{0, plural, =1{} =1{} other{}}", 1u); //~ ERROR: duplicate selector
ifmt!("{0, plural, one{} one{} other{}}", 1u); //~ ERROR: duplicate selector
format!("{0, select, a{} a{} other{}}", "a"); //~ ERROR: duplicate selector
format!("{0, plural, =1{} =1{} other{}}", 1u); //~ ERROR: duplicate selector
format!("{0, plural, one{} one{} other{}}", 1u); //~ ERROR: duplicate selector
// bad syntax of the format string
ifmt!("{"); //~ ERROR: unterminated format string
ifmt!("\\ "); //~ ERROR: invalid escape
ifmt!("\\"); //~ ERROR: expected an escape
format!("{"); //~ ERROR: unterminated format string
format!("\\ "); //~ ERROR: invalid escape
format!("\\"); //~ ERROR: expected an escape
ifmt!("{0, }", 1); //~ ERROR: expected method
ifmt!("{0, foo}", 1); //~ ERROR: unknown method
ifmt!("{0, select}", "a"); //~ ERROR: must be followed by
ifmt!("{0, plural}", 1); //~ ERROR: must be followed by
format!("{0, }", 1); //~ ERROR: expected method
format!("{0, foo}", 1); //~ ERROR: unknown method
format!("{0, select}", "a"); //~ ERROR: must be followed by
format!("{0, plural}", 1); //~ ERROR: must be followed by
ifmt!("{0, select, a{{}", 1); //~ ERROR: must be terminated
ifmt!("{0, select, {} other{}}", "a"); //~ ERROR: empty selector
ifmt!("{0, select, other{} other{}}", "a"); //~ ERROR: multiple `other`
ifmt!("{0, plural, offset: other{}}", "a"); //~ ERROR: must be an integer
ifmt!("{0, plural, offset 1 other{}}", "a"); //~ ERROR: be followed by `:`
ifmt!("{0, plural, =a{} other{}}", "a"); //~ ERROR: followed by an integer
ifmt!("{0, plural, a{} other{}}", "a"); //~ ERROR: unexpected plural
ifmt!("{0, select, a{}}", "a"); //~ ERROR: must provide an `other`
ifmt!("{0, plural, =1{}}", "a"); //~ ERROR: must provide an `other`
format!("{0, select, a{{}", 1); //~ ERROR: must be terminated
format!("{0, select, {} other{}}", "a"); //~ ERROR: empty selector
format!("{0, select, other{} other{}}", "a"); //~ ERROR: multiple `other`
format!("{0, plural, offset: other{}}", "a"); //~ ERROR: must be an integer
format!("{0, plural, offset 1 other{}}", "a"); //~ ERROR: be followed by `:`
format!("{0, plural, =a{} other{}}", "a"); //~ ERROR: followed by an integer
format!("{0, plural, a{} other{}}", "a"); //~ ERROR: unexpected plural
format!("{0, select, a{}}", "a"); //~ ERROR: must provide an `other`
format!("{0, plural, =1{}}", "a"); //~ ERROR: must provide an `other`
ifmt!("{0, plural, other{{0:s}}}", "a"); //~ ERROR: previously used as
ifmt!("{:s} {0, plural, other{}}", "a"); //~ ERROR: argument used to
ifmt!("{0, select, other{}} \
{0, plural, other{}}", "a");
format!("{0, plural, other{{0:s}}}", "a"); //~ ERROR: previously used as
format!("{:s} {0, plural, other{}}", "a"); //~ ERROR: argument used to
format!("{0, select, other{}} \
{0, plural, other{}}", "a");
//~^ ERROR: declared with multiple formats
// It should be illegal to use implicit placement arguments nested inside of
// format strings because otherwise the "internal pointer of which argument
// is next" would be invalidated if different cases had different numbers of
// arguments.
ifmt!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
ifmt!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
ifmt!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
}

View file

@ -9,6 +9,6 @@
// except according to those terms.
fn main() {
ifmt!("{0, plural, other{}}", "a");
format!("{0, plural, other{}}", "a");
//~^ ERROR: expected uint but found
}

View file

@ -9,6 +9,6 @@
// except according to those terms.
fn main() {
ifmt!("{0, select, other{}}", 2);
format!("{0, select, other{}}", 2);
//~^ ERROR: expected &str but found integral
}

View file

@ -9,6 +9,6 @@
// except according to those terms.
fn main() {
ifmt!("{:d}", "3");
format!("{:d}", "3");
//~^ ERROR: failed to find an implementation of trait std::fmt::Signed
}

View file

@ -9,6 +9,6 @@
// except according to those terms.
fn main() {
ifmt!("{:notimplemented}", "3");
format!("{:notimplemented}", "3");
//~^ ERROR: unknown format trait `notimplemented`
}

View file

@ -26,208 +26,223 @@ macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
pub fn main() {
// Make sure there's a poly formatter that takes anything
t!(ifmt!("{:?}", 1), "1");
t!(ifmt!("{:?}", A), "{}");
t!(ifmt!("{:?}", ()), "()");
t!(ifmt!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
t!(format!("{:?}", 1), "1");
t!(format!("{:?}", A), "{}");
t!(format!("{:?}", ()), "()");
t!(format!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
// Various edge cases without formats
t!(ifmt!(""), "");
t!(ifmt!("hello"), "hello");
t!(ifmt!("hello \\{"), "hello {");
t!(format!(""), "");
t!(format!("hello"), "hello");
t!(format!("hello \\{"), "hello {");
// default formatters should work
t!(ifmt!("{}", 1i), "1");
t!(ifmt!("{}", 1i8), "1");
t!(ifmt!("{}", 1i16), "1");
t!(ifmt!("{}", 1i32), "1");
t!(ifmt!("{}", 1i64), "1");
t!(ifmt!("{}", 1u), "1");
t!(ifmt!("{}", 1u8), "1");
t!(ifmt!("{}", 1u16), "1");
t!(ifmt!("{}", 1u32), "1");
t!(ifmt!("{}", 1u64), "1");
t!(ifmt!("{}", 1.0f), "1");
t!(ifmt!("{}", 1.0f32), "1");
t!(ifmt!("{}", 1.0f64), "1");
t!(ifmt!("{}", "a"), "a");
t!(ifmt!("{}", ~"a"), "a");
t!(ifmt!("{}", @"a"), "a");
t!(ifmt!("{}", false), "false");
t!(ifmt!("{}", 'a'), "a");
t!(format!("{}", 1i), "1");
t!(format!("{}", 1i8), "1");
t!(format!("{}", 1i16), "1");
t!(format!("{}", 1i32), "1");
t!(format!("{}", 1i64), "1");
t!(format!("{}", 1u), "1");
t!(format!("{}", 1u8), "1");
t!(format!("{}", 1u16), "1");
t!(format!("{}", 1u32), "1");
t!(format!("{}", 1u64), "1");
t!(format!("{}", 1.0f), "1");
t!(format!("{}", 1.0f32), "1");
t!(format!("{}", 1.0f64), "1");
t!(format!("{}", "a"), "a");
t!(format!("{}", ~"a"), "a");
t!(format!("{}", @"a"), "a");
t!(format!("{}", false), "false");
t!(format!("{}", 'a'), "a");
// At least exercise all the formats
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!("{:s}", ~"foo"), "foo");
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");
t!(format!("{:b}", true), "true");
t!(format!("{:c}", '☃'), "");
t!(format!("{:d}", 10), "10");
t!(format!("{:i}", 10), "10");
t!(format!("{:u}", 10u), "10");
t!(format!("{:o}", 10u), "12");
t!(format!("{:x}", 10u), "a");
t!(format!("{:X}", 10u), "A");
t!(format!("{:s}", "foo"), "foo");
t!(format!("{:s}", ~"foo"), "foo");
t!(format!("{:s}", @"foo"), "foo");
t!(format!("{:p}", 0x1234 as *int), "0x1234");
t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
t!(format!("{:d}", A), "aloha");
t!(format!("{:d}", B), "adios");
t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
t!(format!("{1} {0}", 0, 1), "1 0");
t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
t!(format!("{} {0:s}", "a"), "a a");
t!(format!("{} {0}", "a"), "a a");
// Methods should probably work
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");
t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
t!(format!("{1, select, a{#{0:s}} other{#{1}}}", "b", "a"), "ab");
t!(format!("{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");
t!(format!("{:s}", "a"), "a");
t!(format!("{:4s}", "a"), "a ");
t!(format!("{:>4s}", "a"), " a");
t!(format!("{:<4s}", "a"), "a ");
t!(format!("{:.4s}", "a"), "a");
t!(format!("{:4.4s}", "a"), "a ");
t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaa"), "aaaa");
t!(format!("{:2.4s}", "aaa"), "aaa");
t!(format!("{:2.4s}", "aa"), "aa");
t!(format!("{:2.4s}", "a"), "a ");
t!(format!("{:0>2s}", "a"), "0a");
t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
t!(format!("{:1$s}", "a", 4), "a ");
t!(format!("{:-#s}", "a"), "a");
t!(format!("{:+#s}", "a"), "a");
// 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!(format!("{:d}", -1i), "-1");
t!(format!("{:d}", -1i8), "-1");
t!(format!("{:d}", -1i16), "-1");
t!(format!("{:d}", -1i32), "-1");
t!(format!("{:d}", -1i64), "-1");
t!(format!("{:t}", 1i), "1");
t!(format!("{:t}", 1i8), "1");
t!(format!("{:t}", 1i16), "1");
t!(format!("{:t}", 1i32), "1");
t!(format!("{:t}", 1i64), "1");
t!(format!("{:x}", 1i), "1");
t!(format!("{:x}", 1i8), "1");
t!(format!("{:x}", 1i16), "1");
t!(format!("{:x}", 1i32), "1");
t!(format!("{:x}", 1i64), "1");
t!(format!("{:X}", 1i), "1");
t!(format!("{:X}", 1i8), "1");
t!(format!("{:X}", 1i16), "1");
t!(format!("{:X}", 1i32), "1");
t!(format!("{:X}", 1i64), "1");
t!(format!("{:o}", 1i), "1");
t!(format!("{:o}", 1i8), "1");
t!(format!("{:o}", 1i16), "1");
t!(format!("{:o}", 1i32), "1");
t!(format!("{:o}", 1i64), "1");
t!(ifmt!("{:u}", 1u), "1");
t!(ifmt!("{:u}", 1u8), "1");
t!(ifmt!("{:u}", 1u16), "1");
t!(ifmt!("{:u}", 1u32), "1");
t!(ifmt!("{:u}", 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");
t!(format!("{:u}", 1u), "1");
t!(format!("{:u}", 1u8), "1");
t!(format!("{:u}", 1u16), "1");
t!(format!("{:u}", 1u32), "1");
t!(format!("{:u}", 1u64), "1");
t!(format!("{:t}", 1u), "1");
t!(format!("{:t}", 1u8), "1");
t!(format!("{:t}", 1u16), "1");
t!(format!("{:t}", 1u32), "1");
t!(format!("{:t}", 1u64), "1");
t!(format!("{:x}", 1u), "1");
t!(format!("{:x}", 1u8), "1");
t!(format!("{:x}", 1u16), "1");
t!(format!("{:x}", 1u32), "1");
t!(format!("{:x}", 1u64), "1");
t!(format!("{:X}", 1u), "1");
t!(format!("{:X}", 1u8), "1");
t!(format!("{:X}", 1u16), "1");
t!(format!("{:X}", 1u32), "1");
t!(format!("{:X}", 1u64), "1");
t!(format!("{:o}", 1u), "1");
t!(format!("{:o}", 1u8), "1");
t!(format!("{:o}", 1u16), "1");
t!(format!("{:o}", 1u32), "1");
t!(format!("{:o}", 1u64), "1");
// Test the flags for formatting integers
t!(ifmt!("{:3d}", 1), " 1");
t!(ifmt!("{:>3d}", 1), " 1");
t!(ifmt!("{:>+3d}", 1), " +1");
t!(ifmt!("{:<3d}", 1), "1 ");
t!(ifmt!("{:#d}", 1), "1");
t!(ifmt!("{:#x}", 10), "0xa");
t!(ifmt!("{:#X}", 10), "0xA");
t!(ifmt!("{:#5x}", 10), " 0xa");
t!(ifmt!("{:#o}", 10), "0o12");
t!(ifmt!("{:08x}", 10), "0000000a");
t!(ifmt!("{:8x}", 10), " a");
t!(ifmt!("{:<8x}", 10), "a ");
t!(ifmt!("{:>8x}", 10), " a");
t!(ifmt!("{:#08x}", 10), "0x00000a");
t!(ifmt!("{:08d}", -10), "-0000010");
t!(ifmt!("{:x}", -1u8), "ff");
t!(ifmt!("{:X}", -1u8), "FF");
t!(ifmt!("{:t}", -1u8), "11111111");
t!(ifmt!("{:o}", -1u8), "377");
t!(ifmt!("{:#x}", -1u8), "0xff");
t!(ifmt!("{:#X}", -1u8), "0xFF");
t!(ifmt!("{:#t}", -1u8), "0b11111111");
t!(ifmt!("{:#o}", -1u8), "0o377");
t!(format!("{:3d}", 1), " 1");
t!(format!("{:>3d}", 1), " 1");
t!(format!("{:>+3d}", 1), " +1");
t!(format!("{:<3d}", 1), "1 ");
t!(format!("{:#d}", 1), "1");
t!(format!("{:#x}", 10), "0xa");
t!(format!("{:#X}", 10), "0xA");
t!(format!("{:#5x}", 10), " 0xa");
t!(format!("{:#o}", 10), "0o12");
t!(format!("{:08x}", 10), "0000000a");
t!(format!("{:8x}", 10), " a");
t!(format!("{:<8x}", 10), "a ");
t!(format!("{:>8x}", 10), " a");
t!(format!("{:#08x}", 10), "0x00000a");
t!(format!("{:08d}", -10), "-0000010");
t!(format!("{:x}", -1u8), "ff");
t!(format!("{:X}", -1u8), "FF");
t!(format!("{:t}", -1u8), "11111111");
t!(format!("{:o}", -1u8), "377");
t!(format!("{:#x}", -1u8), "0xff");
t!(format!("{:#X}", -1u8), "0xFF");
t!(format!("{:#t}", -1u8), "0b11111111");
t!(format!("{:#o}", -1u8), "0o377");
// Signed combinations
t!(ifmt!("{:+5d}", 1), " +1");
t!(ifmt!("{:+5d}", -1), " -1");
t!(ifmt!("{:05d}", 1), "00001");
t!(ifmt!("{:05d}", -1), "-0001");
t!(ifmt!("{:+05d}", 1), "+0001");
t!(ifmt!("{:+05d}", -1), "-0001");
t!(format!("{:+5d}", 1), " +1");
t!(format!("{:+5d}", -1), " -1");
t!(format!("{:05d}", 1), "00001");
t!(format!("{:05d}", -1), "-0001");
t!(format!("{:+05d}", 1), "+0001");
t!(format!("{:+05d}", -1), "-0001");
// Some float stuff
t!(ifmt!("{:f}", 1.0f), "1");
t!(ifmt!("{:f}", 1.0f32), "1");
t!(ifmt!("{:f}", 1.0f64), "1");
t!(ifmt!("{:.3f}", 1.0f), "1.000");
t!(ifmt!("{:10.3f}", 1.0f), " 1.000");
t!(ifmt!("{:+10.3f}", 1.0f), " +1.000");
t!(ifmt!("{:+10.3f}", -1.0f), " -1.000");
t!(format!("{:f}", 1.0f), "1");
t!(format!("{:f}", 1.0f32), "1");
t!(format!("{:f}", 1.0f64), "1");
t!(format!("{:.3f}", 1.0f), "1.000");
t!(format!("{:10.3f}", 1.0f), " 1.000");
t!(format!("{:+10.3f}", 1.0f), " +1.000");
t!(format!("{:+10.3f}", -1.0f), " -1.000");
test_ifmtf();
test_write();
test_print();
}
fn test_ifmtf() {
// Basic test to make sure that we can invoke the `write!` macro with an
// io::Writer instance.
fn test_write() {
use std::rt::io::Decorator;
use std::rt::io::mem::MemWriter;
use std::rt::io;
use std::str;
let mut buf = MemWriter::new();
ifmtf!(&mut buf as &mut io::Writer, "{}", 3);
write!(&mut buf as &mut io::Writer, "{}", 3);
{
let w = &mut buf as &mut io::Writer;
ifmtf!(w, "{foo}", foo=4);
ifmtf!(w, "{:s}", "hello");
write!(w, "{foo}", foo=4);
write!(w, "{:s}", "hello");
writeln!(w, "{}", "line");
writeln!(w, "{foo}", foo="bar");
}
let s = str::from_bytes_owned(buf.inner());
t!(s, "34hello");
t!(s, "34helloline\nbar\n");
}
// Just make sure that the macros are defined, there's not really a lot that we
// can do with them just yet (to test the output)
fn test_print() {
print!(1);
print!("{:?}", ~[0u8]);
println!("hello");
println!("this is a {}", "test");
println!("{foo}", foo="bar");
}