]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ifmt.rs
Add `impl Show for &Show`
[rust.git] / src / test / run-pass / ifmt.rs
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.
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
12
13 #![feature(macro_rules, managed_boxes)]
14 #![deny(warnings)]
15 #![allow(unused_must_use)]
16
17 extern crate debug;
18
19 use std::fmt;
20 use std::gc::GC;
21 use std::io::MemWriter;
22 use std::io;
23 use std::str;
24
25 struct A;
26 struct B;
27
28 impl fmt::Signed for A {
29     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30         f.write("aloha".as_bytes())
31     }
32 }
33 impl fmt::Signed for B {
34     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35         f.write("adios".as_bytes())
36     }
37 }
38
39 macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })
40
41 pub fn main() {
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\")");
47
48     // Various edge cases without formats
49     t!(format!(""), "");
50     t!(format!("hello"), "hello");
51     t!(format!("hello {{"), "hello {");
52
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");
60
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");
83
84     let a: &fmt::Show = &1i;
85     t!(format!("{}", a), "1");
86
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");
112
113     // Some float stuff
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");
120
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");
128
129     // Escaping
130     t!(format!("{{"), "{");
131     t!(format!("}}"), "}");
132
133     test_write();
134     test_print();
135     test_order();
136
137     // make sure that format! doesn't move out of local variables
138     let a = box 3i;
139     format!("{:?}", a);
140     format!("{:?}", a);
141
142     // make sure that format! doesn't cause spurious unused-unsafe warnings when
143     // it's inside of an outer unsafe block
144     unsafe {
145         let a: int = ::std::mem::transmute(3u);
146         format!("{}", a);
147     }
148
149     test_format_args();
150
151     // test that trailing commas are acceptable
152     format!("{}", "test",);
153     format!("{foo}", foo="test",);
154 }
155
156 // Basic test to make sure that we can invoke the `write!` macro with an
157 // io::Writer instance.
158 fn test_write() {
159     let mut buf = MemWriter::new();
160     write!(&mut buf as &mut io::Writer, "{}", 3i);
161     {
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");
167     }
168
169     let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
170     t!(s, "34helloline\nbar\n");
171 }
172
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)
175 fn test_print() {
176     print!("hi");
177     print!("{:?}", vec!(0u8));
178     println!("hello");
179     println!("this is a {}", "test");
180     println!("{foo}", foo="bar");
181 }
182
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();
187     {
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);
192     }
193     let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
194     t!(s, "1test3");
195
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");
202 }
203
204 fn test_order() {
205     // Make sure format!() arguments are always evaluated in a left-to-right
206     // ordering
207     fn foo() -> int {
208         static mut FOO: int = 0;
209         unsafe {
210             FOO += 1;
211             FOO
212         }
213     }
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());
217 }