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