]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
auto merge of #19648 : mquandalle/rust/patch-1, r=alexcrichton
[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 // no-pretty-expanded unnecessary unsafe block generated
12 // ignore-lexer-test FIXME #15679
13
14 #![feature(macro_rules)]
15 #![deny(warnings)]
16 #![allow(unused_must_use)]
17
18 use std::fmt;
19 use std::io;
20
21 struct A;
22 struct B;
23 struct C;
24
25 impl fmt::LowerHex for A {
26     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27         f.write("aloha".as_bytes())
28     }
29 }
30 impl fmt::UpperHex for B {
31     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32         f.write("adios".as_bytes())
33     }
34 }
35 impl fmt::Show for C {
36     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37         f.pad_integral(true, "☃", "123".as_bytes())
38     }
39 }
40
41 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })
42
43 pub fn main() {
44     // Various edge cases without formats
45     t!(format!(""), "");
46     t!(format!("hello"), "hello");
47     t!(format!("hello {{"), "hello {");
48
49     // default formatters should work
50     t!(format!("{}", 1.0f32), "1");
51     t!(format!("{}", 1.0f64), "1");
52     t!(format!("{}", "a"), "a");
53     t!(format!("{}", "a".to_string()), "a");
54     t!(format!("{}", false), "false");
55     t!(format!("{}", 'a'), "a");
56
57     // At least exercise all the formats
58     t!(format!("{}", true), "true");
59     t!(format!("{}", '☃'), "☃");
60     t!(format!("{}", 10i), "10");
61     t!(format!("{}", 10i), "10");
62     t!(format!("{}", 10u), "10");
63     t!(format!("{:o}", 10u), "12");
64     t!(format!("{:x}", 10u), "a");
65     t!(format!("{:X}", 10u), "A");
66     t!(format!("{}", "foo"), "foo");
67     t!(format!("{}", "foo".to_string()), "foo");
68     t!(format!("{:p}", 0x1234 as *const int), "0x1234");
69     t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
70     t!(format!("{:x}", A), "aloha");
71     t!(format!("{:X}", B), "adios");
72     t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
73     t!(format!("{1} {0}", 0i, 1i), "1 0");
74     t!(format!("{foo} {bar}", foo=0i, bar=1i), "0 1");
75     t!(format!("{foo} {1} {bar} {0}", 0i, 1i, foo=2i, bar=3i), "2 1 3 0");
76     t!(format!("{} {0}", "a"), "a a");
77     t!(format!("{foo_bar}", foo_bar=1i), "1");
78     t!(format!("{}", 5i + 5i), "10");
79     t!(format!("{:#4}", C), "☃123");
80
81     let a: &fmt::Show = &1i;
82     t!(format!("{}", a), "1");
83
84     // Formatting strings and their arguments
85     t!(format!("{}", "a"), "a");
86     t!(format!("{:4}", "a"), "a   ");
87     t!(format!("{:4}", "☃"), "☃   ");
88     t!(format!("{:>4}", "a"), "   a");
89     t!(format!("{:<4}", "a"), "a   ");
90     t!(format!("{:^5}", "a"),  "  a  ");
91     t!(format!("{:^5}", "aa"), " aa  ");
92     t!(format!("{:^4}", "a"),  " a  ");
93     t!(format!("{:^4}", "aa"), " aa ");
94     t!(format!("{:.4}", "a"), "a");
95     t!(format!("{:4.4}", "a"), "a   ");
96     t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
97     t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
98     t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
99     t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
100     t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
101     t!(format!("{:2.4}", "aaaaa"), "aaaa");
102     t!(format!("{:2.4}", "aaaa"), "aaaa");
103     t!(format!("{:2.4}", "aaa"), "aaa");
104     t!(format!("{:2.4}", "aa"), "aa");
105     t!(format!("{:2.4}", "a"), "a ");
106     t!(format!("{:0>2}", "a"), "0a");
107     t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
108     t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
109     t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
110     t!(format!("{:1$}", "a", 4), "a   ");
111     t!(format!("{1:0$}", 4, "a"), "a   ");
112     t!(format!("{:a$}", "a", a=4), "a   ");
113     t!(format!("{:-#}", "a"), "a");
114     t!(format!("{:+#}", "a"), "a");
115
116     // Some float stuff
117     t!(format!("{:}", 1.0f32), "1");
118     t!(format!("{:}", 1.0f64), "1");
119     t!(format!("{:.3}", 1.0f64), "1.000");
120     t!(format!("{:10.3}", 1.0f64),   "     1.000");
121     t!(format!("{:+10.3}", 1.0f64),  "    +1.000");
122     t!(format!("{:+10.3}", -1.0f64), "    -1.000");
123
124     t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
125     t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
126     t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
127     t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
128     t!(format!("{:10.3e}", 1.2345e6f64),   "   1.234e6");
129     t!(format!("{:+10.3e}", 1.2345e6f64),  "  +1.234e6");
130     t!(format!("{:+10.3e}", -1.2345e6f64), "  -1.234e6");
131
132     // Escaping
133     t!(format!("{{"), "{");
134     t!(format!("}}"), "}");
135
136     test_write();
137     test_print();
138     test_order();
139
140     // make sure that format! doesn't move out of local variables
141     let a = box 3i;
142     format!("{}", a);
143     format!("{}", a);
144
145     // make sure that format! doesn't cause spurious unused-unsafe warnings when
146     // it's inside of an outer unsafe block
147     unsafe {
148         let a: int = ::std::mem::transmute(3u);
149         format!("{}", a);
150     }
151
152     test_format_args();
153
154     // test that trailing commas are acceptable
155     format!("{}", "test",);
156     format!("{foo}", foo="test",);
157 }
158
159 // Basic test to make sure that we can invoke the `write!` macro with an
160 // io::Writer instance.
161 fn test_write() {
162     let mut buf = Vec::new();
163     write!(&mut buf as &mut io::Writer, "{}", 3i);
164     {
165         let w = &mut buf as &mut io::Writer;
166         write!(w, "{foo}", foo=4i);
167         write!(w, "{}", "hello");
168         writeln!(w, "{}", "line");
169         writeln!(w, "{foo}", foo="bar");
170     }
171
172     let s = String::from_utf8(buf).unwrap();
173     t!(s, "34helloline\nbar\n");
174 }
175
176 // Just make sure that the macros are defined, there's not really a lot that we
177 // can do with them just yet (to test the output)
178 fn test_print() {
179     print!("hi");
180     print!("{}", vec!(0u8));
181     println!("hello");
182     println!("this is a {}", "test");
183     println!("{foo}", foo="bar");
184 }
185
186 // Just make sure that the macros are defined, there's not really a lot that we
187 // can do with them just yet (to test the output)
188 fn test_format_args() {
189     let mut buf = Vec::new();
190     {
191         let w = &mut buf as &mut io::Writer;
192         format_args!(|args| { write!(w, "{}", args); }, "{}", 1i);
193         format_args!(|args| { write!(w, "{}", args); }, "test");
194         format_args!(|args| { write!(w, "{}", args); }, "{test}", test=3i);
195     }
196     let s = String::from_utf8(buf).unwrap();
197     t!(s, "1test3");
198
199     let s = format_args!(fmt::format, "hello {}", "world");
200     t!(s, "hello world");
201     let s = format_args!(|args| {
202         format!("{}: {}", "args were", args)
203     }, "hello {}", "world");
204     t!(s, "args were: hello world");
205 }
206
207 fn test_order() {
208     // Make sure format!() arguments are always evaluated in a left-to-right
209     // ordering
210     fn foo() -> int {
211         static mut FOO: int = 0;
212         unsafe {
213             FOO += 1;
214             FOO
215         }
216     }
217     assert_eq!(format!("{} {} {a} {b} {} {c}",
218                        foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
219                "1 2 4 5 3 6".to_string());
220 }