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.
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.
11 // no-pretty-expanded unnecessary unsafe block generated
12 // ignore-lexer-test FIXME #15679
15 #![allow(unused_must_use)]
16 #![allow(unknown_features)]
18 #![feature(box_syntax)]
26 impl fmt::LowerHex for A {
27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31 impl fmt::UpperHex for B {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 impl fmt::Display for C {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 f.pad_integral(true, "☃", "123")
43 ($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) }
47 // Various edge cases without formats
49 t!(format!("hello"), "hello");
50 t!(format!("hello {{"), "hello {");
52 // default formatters should work
53 t!(format!("{}", 1.0f32), "1");
54 t!(format!("{}", 1.0f64), "1");
55 t!(format!("{}", "a"), "a");
56 t!(format!("{}", "a".to_string()), "a");
57 t!(format!("{}", false), "false");
58 t!(format!("{}", 'a'), "a");
60 // At least exercise all the formats
61 t!(format!("{}", true), "true");
62 t!(format!("{}", '☃'), "☃");
63 t!(format!("{}", 10i), "10");
64 t!(format!("{}", 10u), "10");
65 t!(format!("{:?}", '☃'), "'\\u{2603}'");
66 t!(format!("{:?}", 10i), "10");
67 t!(format!("{:?}", 10u), "10");
68 t!(format!("{:?}", "true"), "\"true\"");
69 t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
70 t!(format!("{:o}", 10u), "12");
71 t!(format!("{:x}", 10u), "a");
72 t!(format!("{:X}", 10u), "A");
73 t!(format!("{}", "foo"), "foo");
74 t!(format!("{}", "foo".to_string()), "foo");
75 t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
76 t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
77 t!(format!("{:x}", A), "aloha");
78 t!(format!("{:X}", B), "adios");
79 t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
80 t!(format!("{1} {0}", 0i, 1i), "1 0");
81 t!(format!("{foo} {bar}", foo=0i, bar=1is), "0 1");
82 t!(format!("{foo} {1} {bar} {0}", 0is, 1is, foo=2is, bar=3is), "2 1 3 0");
83 t!(format!("{} {0}", "a"), "a a");
84 t!(format!("{foo_bar}", foo_bar=1i), "1");
85 t!(format!("{}", 5i + 5i), "10");
86 t!(format!("{:#4}", C), "☃123");
89 // let a: &fmt::Debug = &1i;
90 // t!(format!("{:?}", a), "1");
93 // Formatting strings and their arguments
94 t!(format!("{}", "a"), "a");
95 t!(format!("{:4}", "a"), "a ");
96 t!(format!("{:4}", "☃"), "☃ ");
97 t!(format!("{:>4}", "a"), " a");
98 t!(format!("{:<4}", "a"), "a ");
99 t!(format!("{:^5}", "a"), " a ");
100 t!(format!("{:^5}", "aa"), " aa ");
101 t!(format!("{:^4}", "a"), " a ");
102 t!(format!("{:^4}", "aa"), " aa ");
103 t!(format!("{:.4}", "a"), "a");
104 t!(format!("{:4.4}", "a"), "a ");
105 t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
106 t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
107 t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
108 t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
109 t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
110 t!(format!("{:2.4}", "aaaaa"), "aaaa");
111 t!(format!("{:2.4}", "aaaa"), "aaaa");
112 t!(format!("{:2.4}", "aaa"), "aaa");
113 t!(format!("{:2.4}", "aa"), "aa");
114 t!(format!("{:2.4}", "a"), "a ");
115 t!(format!("{:0>2}", "a"), "0a");
116 t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
117 t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
118 t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
119 t!(format!("{:1$}", "a", 4), "a ");
120 t!(format!("{1:0$}", 4, "a"), "a ");
121 t!(format!("{:a$}", "a", a=4), "a ");
122 t!(format!("{:-#}", "a"), "a");
123 t!(format!("{:+#}", "a"), "a");
126 t!(format!("{:}", 1.0f32), "1");
127 t!(format!("{:}", 1.0f64), "1");
128 t!(format!("{:.3}", 1.0f64), "1.000");
129 t!(format!("{:10.3}", 1.0f64), " 1.000");
130 t!(format!("{:+10.3}", 1.0f64), " +1.000");
131 t!(format!("{:+10.3}", -1.0f64), " -1.000");
133 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
134 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
135 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
136 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
137 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
138 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
139 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
142 t!(format!("{{"), "{");
143 t!(format!("}}"), "}");
149 // make sure that format! doesn't move out of local variables
154 // make sure that format! doesn't cause spurious unused-unsafe warnings when
155 // it's inside of an outer unsafe block
157 let a: isize = ::std::mem::transmute(3u);
163 // test that trailing commas are acceptable
164 format!("{}", "test",);
165 format!("{foo}", foo="test",);
168 // Basic test to make sure that we can invoke the `write!` macro with an
169 // io::Writer instance.
171 use std::fmt::Writer;
172 let mut buf = String::new();
173 write!(&mut buf, "{}", 3i);
176 write!(w, "{foo}", foo=4i);
177 write!(w, "{}", "hello");
178 writeln!(w, "{}", "line");
179 writeln!(w, "{foo}", foo="bar");
182 t!(buf, "34helloline\nbar\n");
185 // Just make sure that the macros are defined, there's not really a lot that we
186 // can do with them just yet (to test the output)
189 print!("{:?}", vec!(0u8));
191 println!("this is a {}", "test");
192 println!("{foo}", foo="bar");
195 // Just make sure that the macros are defined, there's not really a lot that we
196 // can do with them just yet (to test the output)
197 fn test_format_args() {
198 use std::fmt::Writer;
199 let mut buf = String::new();
202 write!(w, "{}", format_args!("{}", 1i));
203 write!(w, "{}", format_args!("test"));
204 write!(w, "{}", format_args!("{test}", test=3i));
209 let s = fmt::format(format_args!("hello {}", "world"));
210 t!(s, "hello world");
211 let s = format!("{}: {}", "args were", format_args!("hello {}", "world"));
212 t!(s, "args were: hello world");
216 // Make sure format!() arguments are always evaluated in a left-to-right
219 static mut FOO: isize = 0;
225 assert_eq!(format!("{} {} {a} {b} {} {c}",
226 foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
227 "1 2 4 5 3 6".to_string());