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
13 #![feature(macro_rules, managed_boxes)]
15 #![allow(unused_must_use)]
21 use std::io::MemWriter;
28 impl fmt::Signed for A {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 f.write("aloha".as_bytes())
33 impl fmt::Signed for B {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 f.write("adios".as_bytes())
39 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })
42 // Make sure there's a poly formatter that takes anything
43 t!(format!("{:?}", 1i), "1");
44 t!(format!("{:?}", A), "A");
45 t!(format!("{:?}", ()), "()");
46 t!(format!("{:?}", box(GC) (box 1i, "foo")), "box(GC) (box 1, \"foo\")");
48 // Various edge cases without formats
50 t!(format!("hello"), "hello");
51 t!(format!("hello {{"), "hello {");
53 // default formatters should work
54 t!(format!("{}", 1.0f32), "1");
55 t!(format!("{}", 1.0f64), "1");
56 t!(format!("{}", "a"), "a");
57 t!(format!("{}", "a".to_string()), "a");
58 t!(format!("{}", false), "false");
59 t!(format!("{}", 'a'), "a");
61 // At least exercise all the formats
62 t!(format!("{:b}", true), "true");
63 t!(format!("{:c}", '☃'), "☃");
64 t!(format!("{:d}", 10i), "10");
65 t!(format!("{:i}", 10i), "10");
66 t!(format!("{:u}", 10u), "10");
67 t!(format!("{:o}", 10u), "12");
68 t!(format!("{:x}", 10u), "a");
69 t!(format!("{:X}", 10u), "A");
70 t!(format!("{:s}", "foo"), "foo");
71 t!(format!("{:s}", "foo".to_string()), "foo");
72 t!(format!("{:p}", 0x1234 as *const int), "0x1234");
73 t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
74 t!(format!("{:d}", A), "aloha");
75 t!(format!("{:d}", B), "adios");
76 t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
77 t!(format!("{1} {0}", 0i, 1i), "1 0");
78 t!(format!("{foo} {bar}", foo=0i, bar=1i), "0 1");
79 t!(format!("{foo} {1} {bar} {0}", 0i, 1i, foo=2i, bar=3i), "2 1 3 0");
80 t!(format!("{} {0}", "a"), "a a");
81 t!(format!("{foo_bar}", foo_bar=1i), "1");
82 t!(format!("{:d}", 5i + 5i), "10");
84 let a: &fmt::Show = &1i;
85 t!(format!("{}", a), "1");
87 // Formatting strings and their arguments
88 t!(format!("{:s}", "a"), "a");
89 t!(format!("{:4s}", "a"), "a ");
90 t!(format!("{:>4s}", "a"), " a");
91 t!(format!("{:<4s}", "a"), "a ");
92 t!(format!("{:.4s}", "a"), "a");
93 t!(format!("{:4.4s}", "a"), "a ");
94 t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
95 t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
96 t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
97 t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
98 t!(format!("{:2.4s}", "aaaaa"), "aaaa");
99 t!(format!("{:2.4s}", "aaaa"), "aaaa");
100 t!(format!("{:2.4s}", "aaa"), "aaa");
101 t!(format!("{:2.4s}", "aa"), "aa");
102 t!(format!("{:2.4s}", "a"), "a ");
103 t!(format!("{:0>2s}", "a"), "0a");
104 t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
105 t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
106 t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
107 t!(format!("{:1$s}", "a", 4), "a ");
108 t!(format!("{1:0$s}", 4, "a"), "a ");
109 t!(format!("{:a$s}", "a", a=4), "a ");
110 t!(format!("{:-#s}", "a"), "a");
111 t!(format!("{:+#s}", "a"), "a");
114 t!(format!("{:f}", 1.0f32), "1");
115 t!(format!("{:f}", 1.0f64), "1");
116 t!(format!("{:.3f}", 1.0f64), "1.000");
117 t!(format!("{:10.3f}", 1.0f64), " 1.000");
118 t!(format!("{:+10.3f}", 1.0f64), " +1.000");
119 t!(format!("{:+10.3f}", -1.0f64), " -1.000");
121 t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
122 t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
123 t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
124 t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
125 t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
126 t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
127 t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
130 t!(format!("{{"), "{");
131 t!(format!("}}"), "}");
137 // make sure that format! doesn't move out of local variables
142 // make sure that format! doesn't cause spurious unused-unsafe warnings when
143 // it's inside of an outer unsafe block
145 let a: int = ::std::mem::transmute(3u);
151 // test that trailing commas are acceptable
152 format!("{}", "test",);
153 format!("{foo}", foo="test",);
156 // Basic test to make sure that we can invoke the `write!` macro with an
157 // io::Writer instance.
159 let mut buf = MemWriter::new();
160 write!(&mut buf as &mut io::Writer, "{}", 3i);
162 let w = &mut buf as &mut io::Writer;
163 write!(w, "{foo}", foo=4i);
164 write!(w, "{:s}", "hello");
165 writeln!(w, "{}", "line");
166 writeln!(w, "{foo}", foo="bar");
169 let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
170 t!(s, "34helloline\nbar\n");
173 // Just make sure that the macros are defined, there's not really a lot that we
174 // can do with them just yet (to test the output)
177 print!("{:?}", vec!(0u8));
179 println!("this is a {}", "test");
180 println!("{foo}", foo="bar");
183 // Just make sure that the macros are defined, there's not really a lot that we
184 // can do with them just yet (to test the output)
185 fn test_format_args() {
186 let mut buf = MemWriter::new();
188 let w = &mut buf as &mut io::Writer;
189 format_args!(|args| { write!(w, "{}", args); }, "{}", 1i);
190 format_args!(|args| { write!(w, "{}", args); }, "test");
191 format_args!(|args| { write!(w, "{}", args); }, "{test}", test=3i);
193 let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
196 let s = format_args!(fmt::format, "hello {}", "world");
197 t!(s, "hello world");
198 let s = format_args!(|args| {
199 format!("{}: {}", "args were", args)
200 }, "hello {}", "world");
201 t!(s, "args were: hello world");
205 // Make sure format!() arguments are always evaluated in a left-to-right
208 static mut FOO: int = 0;
214 assert_eq!(format!("{} {} {a} {b} {} {c}",
215 foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
216 "1 2 4 5 3 6".to_string());