]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / run-pass / ifmt.rs
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.
4 //
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.
10
11 // ignore-fast: check-fast screws up repr paths
12
13 #[feature(macro_rules)];
14 #[deny(warnings)];
15 #[allow(unused_must_use)];
16 #[allow(deprecated_owned_vector)];
17
18 use std::fmt;
19 use std::io::MemWriter;
20 use std::io;
21 use std::str;
22
23 struct A;
24 struct B;
25
26 impl fmt::Signed for A {
27     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28         f.buf.write("aloha".as_bytes())
29     }
30 }
31 impl fmt::Signed for B {
32     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33         f.buf.write("adios".as_bytes())
34     }
35 }
36
37 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a, $b.to_owned()) })
38
39 pub fn main() {
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\")");
45
46     // Various edge cases without formats
47     t!(format!(""), "");
48     t!(format!("hello"), "hello");
49     t!(format!("hello \\{"), "hello {");
50
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");
58
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");
80
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");
92
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");
118
119     // Some float stuff
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");
126
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");
134
135     // Escaping
136     t!(format!("\\{"), "{");
137     t!(format!("\\}"), "}");
138     t!(format!("\\#"), "#");
139     t!(format!("\\\\"), "\\");
140
141     test_write();
142     test_print();
143     test_order();
144
145     // make sure that format! doesn't move out of local variables
146     let a = ~3;
147     format!("{:?}", a);
148     format!("{:?}", a);
149
150     // make sure that format! doesn't cause spurious unused-unsafe warnings when
151     // it's inside of an outer unsafe block
152     unsafe {
153         let a: int = ::std::cast::transmute(3u);
154         format!("{}", a);
155     }
156
157     test_format_args();
158
159     // test that trailing commas are acceptable
160     format!("{}", "test",);
161     format!("{foo}", foo="test",);
162 }
163
164 // Basic test to make sure that we can invoke the `write!` macro with an
165 // io::Writer instance.
166 fn test_write() {
167     let mut buf = MemWriter::new();
168     write!(&mut buf as &mut io::Writer, "{}", 3);
169     {
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");
175     }
176
177     let s = str::from_utf8_owned(buf.unwrap()).unwrap();
178     t!(s, "34helloline\nbar\n");
179 }
180
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)
183 fn test_print() {
184     print!("hi");
185     print!("{:?}", vec!(0u8));
186     println!("hello");
187     println!("this is a {}", "test");
188     println!("{foo}", foo="bar");
189 }
190
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();
195     {
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);
200     }
201     let s = str::from_utf8_owned(buf.unwrap()).unwrap();
202     t!(s, "1test3");
203
204     let s = format_args!(fmt::format, "hello {}", "world");
205     t!(s, "hello world");
206 }
207
208 fn test_order() {
209     // Make sure format!() arguments are always evaluated in a left-to-right
210     // ordering
211     fn foo() -> int {
212         static mut FOO: int = 0;
213         unsafe {
214             FOO += 1;
215             FOO
216         }
217     }
218     assert_eq!(format!("{} {} {a} {b} {} {c}",
219                        foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
220                ~"1 2 4 5 3 6");
221 }