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