]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
rollup merge of #20482: kmcallister/macro-reform
[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
17 use std::fmt;
18
19 struct A;
20 struct B;
21 struct C;
22
23 impl fmt::LowerHex for A {
24     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25         f.write_str("aloha")
26     }
27 }
28 impl fmt::UpperHex for B {
29     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30         f.write_str("adios")
31     }
32 }
33 impl fmt::Show for C {
34     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35         f.pad_integral(true, "☃", "123")
36     }
37 }
38
39 macro_rules! t {
40     ($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) }
41 }
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!("{:?}", true), "true");
64     t!(format!("{:o}", 10u), "12");
65     t!(format!("{:x}", 10u), "a");
66     t!(format!("{:X}", 10u), "A");
67     t!(format!("{}", "foo"), "foo");
68     t!(format!("{}", "foo".to_string()), "foo");
69     t!(format!("{:p}", 0x1234 as *const int), "0x1234");
70     t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
71     t!(format!("{:x}", A), "aloha");
72     t!(format!("{:X}", B), "adios");
73     t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
74     t!(format!("{1} {0}", 0i, 1i), "1 0");
75     t!(format!("{foo} {bar}", foo=0i, bar=1i), "0 1");
76     t!(format!("{foo} {1} {bar} {0}", 0i, 1i, foo=2i, bar=3i), "2 1 3 0");
77     t!(format!("{} {0}", "a"), "a a");
78     t!(format!("{foo_bar}", foo_bar=1i), "1");
79     t!(format!("{}", 5i + 5i), "10");
80     t!(format!("{:#4}", C), "☃123");
81
82     let a: &fmt::Show = &1i;
83     t!(format!("{}", a), "1");
84
85     // Formatting strings and their arguments
86     t!(format!("{}", "a"), "a");
87     t!(format!("{:4}", "a"), "a   ");
88     t!(format!("{:4}", "☃"), "☃   ");
89     t!(format!("{:>4}", "a"), "   a");
90     t!(format!("{:<4}", "a"), "a   ");
91     t!(format!("{:^5}", "a"),  "  a  ");
92     t!(format!("{:^5}", "aa"), " aa  ");
93     t!(format!("{:^4}", "a"),  " a  ");
94     t!(format!("{:^4}", "aa"), " aa ");
95     t!(format!("{:.4}", "a"), "a");
96     t!(format!("{:4.4}", "a"), "a   ");
97     t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
98     t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
99     t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
100     t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
101     t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
102     t!(format!("{:2.4}", "aaaaa"), "aaaa");
103     t!(format!("{:2.4}", "aaaa"), "aaaa");
104     t!(format!("{:2.4}", "aaa"), "aaa");
105     t!(format!("{:2.4}", "aa"), "aa");
106     t!(format!("{:2.4}", "a"), "a ");
107     t!(format!("{:0>2}", "a"), "0a");
108     t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
109     t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
110     t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
111     t!(format!("{:1$}", "a", 4), "a   ");
112     t!(format!("{1:0$}", 4, "a"), "a   ");
113     t!(format!("{:a$}", "a", a=4), "a   ");
114     t!(format!("{:-#}", "a"), "a");
115     t!(format!("{:+#}", "a"), "a");
116
117     // Some float stuff
118     t!(format!("{:}", 1.0f32), "1");
119     t!(format!("{:}", 1.0f64), "1");
120     t!(format!("{:.3}", 1.0f64), "1.000");
121     t!(format!("{:10.3}", 1.0f64),   "     1.000");
122     t!(format!("{:+10.3}", 1.0f64),  "    +1.000");
123     t!(format!("{:+10.3}", -1.0f64), "    -1.000");
124
125     t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
126     t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
127     t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
128     t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
129     t!(format!("{:10.3e}", 1.2345e6f64),   "   1.234e6");
130     t!(format!("{:+10.3e}", 1.2345e6f64),  "  +1.234e6");
131     t!(format!("{:+10.3e}", -1.2345e6f64), "  -1.234e6");
132
133     // Escaping
134     t!(format!("{{"), "{");
135     t!(format!("}}"), "}");
136
137     test_write();
138     test_print();
139     test_order();
140
141     // make sure that format! doesn't move out of local variables
142     let a = box 3i;
143     format!("{}", a);
144     format!("{}", a);
145
146     // make sure that format! doesn't cause spurious unused-unsafe warnings when
147     // it's inside of an outer unsafe block
148     unsafe {
149         let a: int = ::std::mem::transmute(3u);
150         format!("{}", a);
151     }
152
153     test_format_args();
154
155     // test that trailing commas are acceptable
156     format!("{}", "test",);
157     format!("{foo}", foo="test",);
158 }
159
160 // Basic test to make sure that we can invoke the `write!` macro with an
161 // io::Writer instance.
162 fn test_write() {
163     use std::fmt::Writer;
164     let mut buf = String::new();
165     write!(&mut buf, "{}", 3i);
166     {
167         let w = &mut buf;
168         write!(w, "{foo}", foo=4i);
169         write!(w, "{}", "hello");
170         writeln!(w, "{}", "line");
171         writeln!(w, "{foo}", foo="bar");
172     }
173
174     t!(buf, "34helloline\nbar\n");
175 }
176
177 // Just make sure that the macros are defined, there's not really a lot that we
178 // can do with them just yet (to test the output)
179 fn test_print() {
180     print!("hi");
181     print!("{}", vec!(0u8));
182     println!("hello");
183     println!("this is a {}", "test");
184     println!("{foo}", foo="bar");
185 }
186
187 // Just make sure that the macros are defined, there's not really a lot that we
188 // can do with them just yet (to test the output)
189 fn test_format_args() {
190     use std::fmt::Writer;
191     let mut buf = String::new();
192     {
193         let w = &mut buf;
194         write!(w, "{}", format_args!("{}", 1i));
195         write!(w, "{}", format_args!("test"));
196         write!(w, "{}", format_args!("{test}", test=3i));
197     }
198     let s = buf;
199     t!(s, "1test3");
200
201     let s = fmt::format(format_args!("hello {}", "world"));
202     t!(s, "hello world");
203     let s = format!("{}: {}", "args were", format_args!("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 }