]>
git.proxmox.com Git - rustc.git/blob - src/test/run-pass/ifmt.rs
1 // Copyright 2014-2015 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
14 #![allow(unused_must_use)]
15 #![allow(unused_features)]
16 #![feature(box_syntax)]
17 #![feature(question_mark)]
19 use std
::fmt
::{self, Write}
;
27 impl fmt
::LowerHex
for A
{
28 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
32 impl fmt
::UpperHex
for B
{
33 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
37 impl fmt
::Display
for C
{
38 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
39 f
.pad_integral(true, "☃", "123")
42 impl fmt
::Binary
for D
{
43 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
51 ($a
:expr
, $b
:expr
) => { assert_eq!($a, $b) }
55 // Various edge cases without formats
57 t
!(format
!("hello"), "hello");
58 t
!(format
!("hello {{"), "hello {");
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");
68 // At least exercise all the formats
69 t
!(format
!("{}", true), "true");
70 t
!(format
!("{}", '☃'
), "☃");
71 t
!(format
!("{}", 10), "10");
72 t
!(format
!("{}", 10_usize
), "10");
73 t
!(format
!("{:?}", '☃'
), "'\\u{2603}'");
74 t
!(format
!("{:?}", 10), "10");
75 t
!(format
!("{:?}", 10_usize
), "10");
76 t
!(format
!("{:?}", "true"), "\"true\"");
77 t
!(format
!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
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""#);
82 t
!(format
!("{:o}", 10_usize
), "12");
83 t
!(format
!("{:x}", 10_usize
), "a");
84 t
!(format
!("{:X}", 10_usize
), "A");
85 t
!(format
!("{}", "foo"), "foo");
86 t
!(format
!("{}", "foo".to_string()), "foo");
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");
91 t
!(format
!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
92 t
!(format
!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
94 t
!(format
!("{:p}", 0x1234 as *const isize), "0x1234");
95 t
!(format
!("{:p}", 0x1234 as *mut isize), "0x1234");
96 t
!(format
!("{:x}", A
), "aloha");
97 t
!(format
!("{:X}", B
), "adios");
98 t
!(format
!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
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");
102 t
!(format
!("{} {0}", "a"), "a a");
103 t
!(format
!("{foo_bar}", foo_bar
=1), "1");
104 t
!(format
!("{}", 5 + 5), "10");
105 t
!(format
!("{:#4}", C
), "☃123");
106 t
!(format
!("{:b}", D
), "aa☃bb");
108 let a
: &fmt
::Debug
= &1;
109 t
!(format
!("{:?}", a
), "1");
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");
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");
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");
161 t
!(format
!("{}", -0.0), "0");
162 t
!(format
!("{:?}", -0.0), "-0");
163 t
!(format
!("{:?}", 0.0), "0");
166 // Test that pointers don't get truncated.
168 let val
= usize::MAX
;
169 let exp
= format
!("{:#x}", val
);
170 t
!(format
!("{:p}", val
as *const isize), exp
);
174 t
!(format
!("{{"), "{");
175 t
!(format
!("}}"), "}");
181 // make sure that format! doesn't move out of local variables
182 let a
: Box
<_
> = box 3;
186 // make sure that format! doesn't cause spurious unused-unsafe warnings when
187 // it's inside of an outer unsafe block
189 let a
: isize = ::std
::mem
::transmute(3_usize
);
195 // test that trailing commas are acceptable
196 format
!("{}", "test",);
197 format
!("{foo}", foo
="test",);
200 // Basic test to make sure that we can invoke the `write!` macro with an
201 // fmt::Write instance.
204 let mut buf
= String
::new();
205 write
!(&mut buf
, "{}", 3);
208 write
!(w
, "{foo}", foo
=4);
209 write
!(w
, "{}", "hello");
210 writeln
!(w
, "{}", "line");
211 writeln
!(w
, "{foo}", foo
="bar");
216 t
!(buf
, "34helloline\nbar\n☃str");
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)
223 print
!("{:?}", vec
!(0u8));
225 println
!("this is a {}", "test");
226 println
!("{foo}", foo
="bar");
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)
231 fn test_format_args() {
233 let mut buf
= String
::new();
236 write
!(w
, "{}", format_args
!("{}", 1));
237 write
!(w
, "{}", format_args
!("test"));
238 write
!(w
, "{}", format_args
!("{test}", test
=3));
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");
250 // Make sure format!() arguments are always evaluated in a left-to-right
253 static mut FOO
: isize = 0;
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());