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