1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 #![feature(macro_rules)]
14 #![allow(unused_must_use)]
15 #![allow(deprecated_owned_vector)]
18 use std::io::MemWriter;
25 impl fmt::Signed for A {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 f.buf.write("aloha".as_bytes())
30 impl fmt::Signed for B {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 f.buf.write("adios".as_bytes())
36 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })
39 // Make sure there's a poly formatter that takes anything
40 t!(format!("{:?}", 1), "1");
41 t!(format!("{:?}", A), "A");
42 t!(format!("{:?}", ()), "()");
43 t!(format!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
45 // Various edge cases without formats
47 t!(format!("hello"), "hello");
48 t!(format!("hello \\{"), "hello {");
50 // default formatters should work
51 t!(format!("{}", 1.0f32), "1");
52 t!(format!("{}", 1.0f64), "1");
53 t!(format!("{}", "a"), "a");
54 t!(format!("{}", "a".to_owned()), "a");
55 t!(format!("{}", false), "false");
56 t!(format!("{}", 'a'), "a");
58 // At least exercise all the formats
59 t!(format!("{:b}", true), "true");
60 t!(format!("{:c}", '☃'), "☃");
61 t!(format!("{:d}", 10), "10");
62 t!(format!("{:i}", 10), "10");
63 t!(format!("{:u}", 10u), "10");
64 t!(format!("{:o}", 10u), "12");
65 t!(format!("{:x}", 10u), "a");
66 t!(format!("{:X}", 10u), "A");
67 t!(format!("{:s}", "foo"), "foo");
68 t!(format!("{:s}", "foo".to_owned()), "foo");
69 t!(format!("{:p}", 0x1234 as *int), "0x1234");
70 t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
71 t!(format!("{:d}", A), "aloha");
72 t!(format!("{:d}", B), "adios");
73 t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
74 t!(format!("{1} {0}", 0, 1), "1 0");
75 t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
76 t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
77 t!(format!("{} {0}", "a"), "a a");
78 t!(format!("{foo_bar}", foo_bar=1), "1");
80 // Methods should probably work
81 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
82 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
83 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
84 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
85 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
86 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
87 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
88 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
89 t!(format!("{1, select, a{#{0:s}} other{#}}", "b", "a"), "ab");
90 t!(format!("{1, select, a{#{0}} other{#}}", "c", "b"), "b");
92 // Formatting strings and their arguments
93 t!(format!("{:s}", "a"), "a");
94 t!(format!("{:4s}", "a"), "a ");
95 t!(format!("{:>4s}", "a"), " a");
96 t!(format!("{:<4s}", "a"), "a ");
97 t!(format!("{:.4s}", "a"), "a");
98 t!(format!("{:4.4s}", "a"), "a ");
99 t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
100 t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
101 t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
102 t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
103 t!(format!("{:2.4s}", "aaaaa"), "aaaa");
104 t!(format!("{:2.4s}", "aaaa"), "aaaa");
105 t!(format!("{:2.4s}", "aaa"), "aaa");
106 t!(format!("{:2.4s}", "aa"), "aa");
107 t!(format!("{:2.4s}", "a"), "a ");
108 t!(format!("{:0>2s}", "a"), "0a");
109 t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
110 t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
111 t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
112 t!(format!("{:1$s}", "a", 4), "a ");
113 t!(format!("{1:0$s}", 4, "a"), "a ");
114 t!(format!("{:a$s}", "a", a=4), "a ");
115 t!(format!("{:-#s}", "a"), "a");
116 t!(format!("{:+#s}", "a"), "a");
119 t!(format!("{:f}", 1.0f32), "1");
120 t!(format!("{:f}", 1.0f64), "1");
121 t!(format!("{:.3f}", 1.0f64), "1.000");
122 t!(format!("{:10.3f}", 1.0f64), " 1.000");
123 t!(format!("{:+10.3f}", 1.0f64), " +1.000");
124 t!(format!("{:+10.3f}", -1.0f64), " -1.000");
126 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
127 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
128 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
129 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
130 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
131 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
132 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
135 t!(format!("\\{"), "{");
136 t!(format!("\\}"), "}");
137 t!(format!("\\#"), "#");
138 t!(format!("\\\\"), "\\");
144 // make sure that format! doesn't move out of local variables
149 // make sure that format! doesn't cause spurious unused-unsafe warnings when
150 // it's inside of an outer unsafe block
152 let a: int = ::std::cast::transmute(3u);
158 // test that trailing commas are acceptable
159 format!("{}", "test",);
160 format!("{foo}", foo="test",);
163 // Basic test to make sure that we can invoke the `write!` macro with an
164 // io::Writer instance.
166 let mut buf = MemWriter::new();
167 write!(&mut buf as &mut io::Writer, "{}", 3);
169 let w = &mut buf as &mut io::Writer;
170 write!(w, "{foo}", foo=4);
171 write!(w, "{:s}", "hello");
172 writeln!(w, "{}", "line");
173 writeln!(w, "{foo}", foo="bar");
176 let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_owned();
177 t!(s, "34helloline\nbar\n");
180 // Just make sure that the macros are defined, there's not really a lot that we
181 // can do with them just yet (to test the output)
184 print!("{:?}", vec!(0u8));
186 println!("this is a {}", "test");
187 println!("{foo}", foo="bar");
190 // Just make sure that the macros are defined, there's not really a lot that we
191 // can do with them just yet (to test the output)
192 fn test_format_args() {
193 let mut buf = MemWriter::new();
195 let w = &mut buf as &mut io::Writer;
196 format_args!(|args| { fmt::write(w, args); }, "{}", 1);
197 format_args!(|args| { fmt::write(w, args); }, "test");
198 format_args!(|args| { fmt::write(w, args); }, "{test}", test=3);
200 let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_owned();
203 let s = format_args!(fmt::format, "hello {}", "world");
204 t!(s, "hello world");
205 let s = format_args!(|args| {
206 format!("{}: {}", "args were", args)
207 }, "hello {}", "world");
208 t!(s, "args were: hello world");
212 // Make sure format!() arguments are always evaluated in a left-to-right
215 static mut FOO: int = 0;
221 assert_eq!(format!("{} {} {a} {b} {} {c}",
222 foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
223 "1 2 4 5 3 6".to_owned());