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.
11 // ignore-fast: check-fast screws up repr paths
13 #[feature(macro_rules)];
15 #[allow(unused_must_use)];
16 #[allow(deprecated_owned_vector)];
19 use std::io::MemWriter;
26 impl fmt::Signed for A {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28 f.buf.write("aloha".as_bytes())
31 impl fmt::Signed for B {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 f.buf.write("adios".as_bytes())
37 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
40 // Make sure there's a poly formatter that takes anything
41 t!(format!("{:?}", 1), "1");
42 t!(format!("{:?}", A), "A");
43 t!(format!("{:?}", ()), "()");
44 t!(format!("{:?}", @(~1, "foo")), "@(~1, \"foo\")");
46 // Various edge cases without formats
48 t!(format!("hello"), "hello");
49 t!(format!("hello \\{"), "hello {");
51 // default formatters should work
52 t!(format!("{}", 1.0f32), "1");
53 t!(format!("{}", 1.0f64), "1");
54 t!(format!("{}", "a"), "a");
55 t!(format!("{}", ~"a"), "a");
56 t!(format!("{}", false), "false");
57 t!(format!("{}", 'a'), "a");
59 // At least exercise all the formats
60 t!(format!("{:b}", true), "true");
61 t!(format!("{:c}", '☃'), "☃");
62 t!(format!("{:d}", 10), "10");
63 t!(format!("{:i}", 10), "10");
64 t!(format!("{:u}", 10u), "10");
65 t!(format!("{:o}", 10u), "12");
66 t!(format!("{:x}", 10u), "a");
67 t!(format!("{:X}", 10u), "A");
68 t!(format!("{:s}", "foo"), "foo");
69 t!(format!("{:s}", ~"foo"), "foo");
70 t!(format!("{:p}", 0x1234 as *int), "0x1234");
71 t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
72 t!(format!("{:d}", A), "aloha");
73 t!(format!("{:d}", B), "adios");
74 t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
75 t!(format!("{1} {0}", 0, 1), "1 0");
76 t!(format!("{foo} {bar}", foo=0, bar=1), "0 1");
77 t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0");
78 t!(format!("{} {0}", "a"), "a a");
79 t!(format!("{foo_bar}", foo_bar=1), "1");
81 // Methods should probably work
82 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 0u), "c0");
83 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 1u), "a1");
84 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 2u), "b2");
85 t!(format!("{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}", 3u), "d3");
86 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "a"), "aa");
87 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "b"), "bb");
88 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "c"), "cc");
89 t!(format!("{0, select, a{a#} b{b#} c{c#} other{d#}}", "d"), "dd");
90 t!(format!("{1, select, a{#{0:s}} other{#}}", "b", "a"), "ab");
91 t!(format!("{1, select, a{#{0}} other{#}}", "c", "b"), "b");
93 // Formatting strings and their arguments
94 t!(format!("{:s}", "a"), "a");
95 t!(format!("{:4s}", "a"), "a ");
96 t!(format!("{:>4s}", "a"), " a");
97 t!(format!("{:<4s}", "a"), "a ");
98 t!(format!("{:.4s}", "a"), "a");
99 t!(format!("{:4.4s}", "a"), "a ");
100 t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
101 t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
102 t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
103 t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
104 t!(format!("{:2.4s}", "aaaaa"), "aaaa");
105 t!(format!("{:2.4s}", "aaaa"), "aaaa");
106 t!(format!("{:2.4s}", "aaa"), "aaa");
107 t!(format!("{:2.4s}", "aa"), "aa");
108 t!(format!("{:2.4s}", "a"), "a ");
109 t!(format!("{:0>2s}", "a"), "0a");
110 t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
111 t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
112 t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
113 t!(format!("{:1$s}", "a", 4), "a ");
114 t!(format!("{1:0$s}", 4, "a"), "a ");
115 t!(format!("{:a$s}", "a", a=4), "a ");
116 t!(format!("{:-#s}", "a"), "a");
117 t!(format!("{:+#s}", "a"), "a");
120 t!(format!("{:f}", 1.0f32), "1");
121 t!(format!("{:f}", 1.0f64), "1");
122 t!(format!("{:.3f}", 1.0f64), "1.000");
123 t!(format!("{:10.3f}", 1.0f64), " 1.000");
124 t!(format!("{:+10.3f}", 1.0f64), " +1.000");
125 t!(format!("{:+10.3f}", -1.0f64), " -1.000");
127 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
128 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
129 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
130 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
131 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
132 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
133 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
136 t!(format!("\\{"), "{");
137 t!(format!("\\}"), "}");
138 t!(format!("\\#"), "#");
139 t!(format!("\\\\"), "\\");
145 // make sure that format! doesn't move out of local variables
150 // make sure that format! doesn't cause spurious unused-unsafe warnings when
151 // it's inside of an outer unsafe block
153 let a: int = ::std::cast::transmute(3u);
159 // test that trailing commas are acceptable
160 format!("{}", "test",);
161 format!("{foo}", foo="test",);
164 // Basic test to make sure that we can invoke the `write!` macro with an
165 // io::Writer instance.
167 let mut buf = MemWriter::new();
168 write!(&mut buf as &mut io::Writer, "{}", 3);
170 let w = &mut buf as &mut io::Writer;
171 write!(w, "{foo}", foo=4);
172 write!(w, "{:s}", "hello");
173 writeln!(w, "{}", "line");
174 writeln!(w, "{foo}", foo="bar");
177 let s = str::from_utf8_owned(buf.unwrap()).unwrap();
178 t!(s, "34helloline\nbar\n");
181 // Just make sure that the macros are defined, there's not really a lot that we
182 // can do with them just yet (to test the output)
185 print!("{:?}", vec!(0u8));
187 println!("this is a {}", "test");
188 println!("{foo}", foo="bar");
191 // Just make sure that the macros are defined, there's not really a lot that we
192 // can do with them just yet (to test the output)
193 fn test_format_args() {
194 let mut buf = MemWriter::new();
196 let w = &mut buf as &mut io::Writer;
197 format_args!(|args| { fmt::write(w, args); }, "{}", 1);
198 format_args!(|args| { fmt::write(w, args); }, "test");
199 format_args!(|args| { fmt::write(w, args); }, "{test}", test=3);
201 let s = str::from_utf8_owned(buf.unwrap()).unwrap();
204 let s = format_args!(fmt::format, "hello {}", "world");
205 t!(s, "hello world");
209 // Make sure format!() arguments are always evaluated in a left-to-right
212 static mut FOO: int = 0;
218 assert_eq!(format!("{} {} {a} {b} {} {c}",
219 foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),