]>
Commit | Line | Data |
---|---|---|
9346a6ac | 1 | // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT |
1a4d82fc JJ |
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 | |
1a4d82fc JJ |
12 | |
13 | #![deny(warnings)] | |
14 | #![allow(unused_must_use)] | |
15 | #![allow(unknown_features)] | |
16 | #![feature(box_syntax)] | |
17 | ||
62682a34 | 18 | use std::fmt::{self, Write}; |
c34b1796 | 19 | use std::usize; |
1a4d82fc JJ |
20 | |
21 | struct A; | |
22 | struct B; | |
23 | struct C; | |
62682a34 | 24 | struct D; |
1a4d82fc JJ |
25 | |
26 | impl fmt::LowerHex for A { | |
27 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
28 | f.write_str("aloha") | |
29 | } | |
30 | } | |
31 | impl fmt::UpperHex for B { | |
32 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
33 | f.write_str("adios") | |
34 | } | |
35 | } | |
85aaf69f | 36 | impl fmt::Display for C { |
1a4d82fc JJ |
37 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
38 | f.pad_integral(true, "☃", "123") | |
39 | } | |
40 | } | |
62682a34 SL |
41 | impl fmt::Binary for D { |
42 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
43 | try!(f.write_str("aa")); | |
44 | try!(f.write_char('☃')); | |
45 | f.write_str("bb") | |
46 | } | |
47 | } | |
1a4d82fc JJ |
48 | |
49 | macro_rules! t { | |
85aaf69f | 50 | ($a:expr, $b:expr) => { assert_eq!($a, $b) } |
1a4d82fc JJ |
51 | } |
52 | ||
53 | pub fn main() { | |
54 | // Various edge cases without formats | |
55 | t!(format!(""), ""); | |
56 | t!(format!("hello"), "hello"); | |
57 | t!(format!("hello {{"), "hello {"); | |
58 | ||
59 | // default formatters should work | |
60 | t!(format!("{}", 1.0f32), "1"); | |
61 | t!(format!("{}", 1.0f64), "1"); | |
62 | t!(format!("{}", "a"), "a"); | |
63 | t!(format!("{}", "a".to_string()), "a"); | |
64 | t!(format!("{}", false), "false"); | |
65 | t!(format!("{}", 'a'), "a"); | |
66 | ||
67 | // At least exercise all the formats | |
68 | t!(format!("{}", true), "true"); | |
69 | t!(format!("{}", '☃'), "☃"); | |
85aaf69f SL |
70 | t!(format!("{}", 10), "10"); |
71 | t!(format!("{}", 10_usize), "10"); | |
1a4d82fc | 72 | t!(format!("{:?}", '☃'), "'\\u{2603}'"); |
85aaf69f SL |
73 | t!(format!("{:?}", 10), "10"); |
74 | t!(format!("{:?}", 10_usize), "10"); | |
1a4d82fc JJ |
75 | t!(format!("{:?}", "true"), "\"true\""); |
76 | t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\""); | |
85aaf69f SL |
77 | t!(format!("{:o}", 10_usize), "12"); |
78 | t!(format!("{:x}", 10_usize), "a"); | |
79 | t!(format!("{:X}", 10_usize), "A"); | |
1a4d82fc JJ |
80 | t!(format!("{}", "foo"), "foo"); |
81 | t!(format!("{}", "foo".to_string()), "foo"); | |
9346a6ac AL |
82 | if cfg!(target_pointer_width = "32") { |
83 | t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234"); | |
84 | t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234"); | |
85 | } else { | |
86 | t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234"); | |
87 | t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234"); | |
88 | } | |
85aaf69f SL |
89 | t!(format!("{:p}", 0x1234 as *const isize), "0x1234"); |
90 | t!(format!("{:p}", 0x1234 as *mut isize), "0x1234"); | |
1a4d82fc JJ |
91 | t!(format!("{:x}", A), "aloha"); |
92 | t!(format!("{:X}", B), "adios"); | |
93 | t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃"); | |
85aaf69f SL |
94 | t!(format!("{1} {0}", 0, 1), "1 0"); |
95 | t!(format!("{foo} {bar}", foo=0, bar=1), "0 1"); | |
96 | t!(format!("{foo} {1} {bar} {0}", 0, 1, foo=2, bar=3), "2 1 3 0"); | |
1a4d82fc | 97 | t!(format!("{} {0}", "a"), "a a"); |
85aaf69f SL |
98 | t!(format!("{foo_bar}", foo_bar=1), "1"); |
99 | t!(format!("{}", 5 + 5), "10"); | |
1a4d82fc | 100 | t!(format!("{:#4}", C), "☃123"); |
62682a34 | 101 | t!(format!("{:b}", D), "aa☃bb"); |
1a4d82fc | 102 | |
9346a6ac AL |
103 | let a: &fmt::Debug = &1; |
104 | t!(format!("{:?}", a), "1"); | |
1a4d82fc JJ |
105 | |
106 | ||
107 | // Formatting strings and their arguments | |
108 | t!(format!("{}", "a"), "a"); | |
109 | t!(format!("{:4}", "a"), "a "); | |
110 | t!(format!("{:4}", "☃"), "☃ "); | |
111 | t!(format!("{:>4}", "a"), " a"); | |
112 | t!(format!("{:<4}", "a"), "a "); | |
113 | t!(format!("{:^5}", "a"), " a "); | |
114 | t!(format!("{:^5}", "aa"), " aa "); | |
115 | t!(format!("{:^4}", "a"), " a "); | |
116 | t!(format!("{:^4}", "aa"), " aa "); | |
117 | t!(format!("{:.4}", "a"), "a"); | |
118 | t!(format!("{:4.4}", "a"), "a "); | |
119 | t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
120 | t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
121 | t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
122 | t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
123 | t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
124 | t!(format!("{:2.4}", "aaaaa"), "aaaa"); | |
125 | t!(format!("{:2.4}", "aaaa"), "aaaa"); | |
126 | t!(format!("{:2.4}", "aaa"), "aaa"); | |
127 | t!(format!("{:2.4}", "aa"), "aa"); | |
128 | t!(format!("{:2.4}", "a"), "a "); | |
129 | t!(format!("{:0>2}", "a"), "0a"); | |
130 | t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa"); | |
131 | t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa"); | |
132 | t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa"); | |
133 | t!(format!("{:1$}", "a", 4), "a "); | |
134 | t!(format!("{1:0$}", 4, "a"), "a "); | |
135 | t!(format!("{:a$}", "a", a=4), "a "); | |
136 | t!(format!("{:-#}", "a"), "a"); | |
137 | t!(format!("{:+#}", "a"), "a"); | |
138 | ||
139 | // Some float stuff | |
140 | t!(format!("{:}", 1.0f32), "1"); | |
141 | t!(format!("{:}", 1.0f64), "1"); | |
142 | t!(format!("{:.3}", 1.0f64), "1.000"); | |
143 | t!(format!("{:10.3}", 1.0f64), " 1.000"); | |
144 | t!(format!("{:+10.3}", 1.0f64), " +1.000"); | |
145 | t!(format!("{:+10.3}", -1.0f64), " -1.000"); | |
146 | ||
147 | t!(format!("{:e}", 1.2345e6f32), "1.2345e6"); | |
148 | t!(format!("{:e}", 1.2345e6f64), "1.2345e6"); | |
149 | t!(format!("{:E}", 1.2345e6f64), "1.2345E6"); | |
150 | t!(format!("{:.3e}", 1.2345e6f64), "1.234e6"); | |
151 | t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6"); | |
152 | t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6"); | |
153 | t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6"); | |
154 | ||
9346a6ac AL |
155 | // Float edge cases |
156 | t!(format!("{}", -0.0), "0"); | |
157 | t!(format!("{:?}", -0.0), "-0"); | |
158 | t!(format!("{:?}", 0.0), "0"); | |
159 | ||
160 | ||
c34b1796 AL |
161 | // Test that pointers don't get truncated. |
162 | { | |
163 | let val = usize::MAX; | |
164 | let exp = format!("{:#x}", val); | |
165 | t!(format!("{:p}", val as *const isize), exp); | |
166 | } | |
167 | ||
1a4d82fc JJ |
168 | // Escaping |
169 | t!(format!("{{"), "{"); | |
170 | t!(format!("}}"), "}"); | |
171 | ||
172 | test_write(); | |
173 | test_print(); | |
174 | test_order(); | |
175 | ||
176 | // make sure that format! doesn't move out of local variables | |
c34b1796 | 177 | let a: Box<_> = box 3; |
1a4d82fc JJ |
178 | format!("{}", a); |
179 | format!("{}", a); | |
180 | ||
181 | // make sure that format! doesn't cause spurious unused-unsafe warnings when | |
182 | // it's inside of an outer unsafe block | |
183 | unsafe { | |
85aaf69f | 184 | let a: isize = ::std::mem::transmute(3_usize); |
1a4d82fc JJ |
185 | format!("{}", a); |
186 | } | |
187 | ||
188 | test_format_args(); | |
189 | ||
190 | // test that trailing commas are acceptable | |
191 | format!("{}", "test",); | |
192 | format!("{foo}", foo="test",); | |
193 | } | |
194 | ||
195 | // Basic test to make sure that we can invoke the `write!` macro with an | |
85aaf69f | 196 | // fmt::Write instance. |
1a4d82fc | 197 | fn test_write() { |
85aaf69f | 198 | use std::fmt::Write; |
1a4d82fc | 199 | let mut buf = String::new(); |
85aaf69f | 200 | write!(&mut buf, "{}", 3); |
1a4d82fc JJ |
201 | { |
202 | let w = &mut buf; | |
85aaf69f | 203 | write!(w, "{foo}", foo=4); |
1a4d82fc JJ |
204 | write!(w, "{}", "hello"); |
205 | writeln!(w, "{}", "line"); | |
206 | writeln!(w, "{foo}", foo="bar"); | |
d9579d0f AL |
207 | w.write_char('☃'); |
208 | w.write_str("str"); | |
1a4d82fc JJ |
209 | } |
210 | ||
d9579d0f | 211 | t!(buf, "34helloline\nbar\n☃str"); |
1a4d82fc JJ |
212 | } |
213 | ||
214 | // Just make sure that the macros are defined, there's not really a lot that we | |
215 | // can do with them just yet (to test the output) | |
216 | fn test_print() { | |
217 | print!("hi"); | |
218 | print!("{:?}", vec!(0u8)); | |
219 | println!("hello"); | |
220 | println!("this is a {}", "test"); | |
221 | println!("{foo}", foo="bar"); | |
222 | } | |
223 | ||
224 | // Just make sure that the macros are defined, there's not really a lot that we | |
225 | // can do with them just yet (to test the output) | |
226 | fn test_format_args() { | |
85aaf69f | 227 | use std::fmt::Write; |
1a4d82fc JJ |
228 | let mut buf = String::new(); |
229 | { | |
230 | let w = &mut buf; | |
85aaf69f | 231 | write!(w, "{}", format_args!("{}", 1)); |
1a4d82fc | 232 | write!(w, "{}", format_args!("test")); |
85aaf69f | 233 | write!(w, "{}", format_args!("{test}", test=3)); |
1a4d82fc JJ |
234 | } |
235 | let s = buf; | |
236 | t!(s, "1test3"); | |
237 | ||
238 | let s = fmt::format(format_args!("hello {}", "world")); | |
239 | t!(s, "hello world"); | |
240 | let s = format!("{}: {}", "args were", format_args!("hello {}", "world")); | |
241 | t!(s, "args were: hello world"); | |
242 | } | |
243 | ||
244 | fn test_order() { | |
245 | // Make sure format!() arguments are always evaluated in a left-to-right | |
246 | // ordering | |
85aaf69f SL |
247 | fn foo() -> isize { |
248 | static mut FOO: isize = 0; | |
1a4d82fc JJ |
249 | unsafe { |
250 | FOO += 1; | |
251 | FOO | |
252 | } | |
253 | } | |
254 | assert_eq!(format!("{} {} {a} {b} {} {c}", | |
255 | foo(), foo(), foo(), a=foo(), b=foo(), c=foo()), | |
256 | "1 2 4 5 3 6".to_string()); | |
257 | } |