-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-// no-pretty-expanded unnecessary unsafe block generated
-// ignore-lexer-test FIXME #15679
-
#![deny(warnings)]
#![allow(unused_must_use)]
-#![allow(unknown_features)]
+#![allow(unused_features)]
#![feature(box_syntax)]
-use std::fmt;
+use std::fmt::{self, Write};
use std::usize;
struct A;
struct B;
struct C;
+struct D;
impl fmt::LowerHex for A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad_integral(true, "☃", "123")
}
}
+impl fmt::Binary for D {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("aa")?;
+ f.write_char('☃')?;
+ f.write_str("bb")
+ }
+}
macro_rules! t {
($a:expr, $b:expr) => { assert_eq!($a, $b) }
t!(format!("{}", '☃'), "☃");
t!(format!("{}", 10), "10");
t!(format!("{}", 10_usize), "10");
- t!(format!("{:?}", '☃'), "'\\u{2603}'");
+ t!(format!("{:?}", '☃'), "'☃'");
t!(format!("{:?}", 10), "10");
t!(format!("{:?}", 10_usize), "10");
t!(format!("{:?}", "true"), "\"true\"");
t!(format!("{:?}", "foo\nbar"), "\"foo\\nbar\"");
+ t!(format!("{:?}", "foo\n\"bar\"\r\n\'baz\'\t\\qux\\"),
+ r#""foo\n\"bar\"\r\n\'baz\'\t\\qux\\""#);
+ t!(format!("{:?}", "foo\0bar\x01baz\u{7f}q\u{75}x"),
+ r#""foo\u{0}bar\u{1}baz\u{7f}qux""#);
t!(format!("{:o}", 10_usize), "12");
t!(format!("{:x}", 10_usize), "a");
t!(format!("{:X}", 10_usize), "A");
t!(format!("{}", "foo"), "foo");
t!(format!("{}", "foo".to_string()), "foo");
+ if cfg!(target_pointer_width = "32") {
+ t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
+ t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
+ } else {
+ t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
+ t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
+ }
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
t!(format!("{:x}", A), "aloha");
t!(format!("{foo_bar}", foo_bar=1), "1");
t!(format!("{}", 5 + 5), "10");
t!(format!("{:#4}", C), "☃123");
+ t!(format!("{:b}", D), "aa☃bb");
- // FIXME(#20676)
- // let a: &fmt::Debug = &1;
- // t!(format!("{:?}", a), "1");
+ let a: &fmt::Debug = &1;
+ t!(format!("{:?}", a), "1");
// Formatting strings and their arguments
t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
- t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
+ t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), " aaaa");
t!(format!("{:2.4}", "aaaaa"), "aaaa");
t!(format!("{:2.4}", "aaaa"), "aaaa");
t!(format!("{:2.4}", "aaa"), "aaa");
t!(format!("{:a$}", "a", a=4), "a ");
t!(format!("{:-#}", "a"), "a");
t!(format!("{:+#}", "a"), "a");
+ t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
// Some float stuff
t!(format!("{:}", 1.0f32), "1");
t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
+ // Float edge cases
+ t!(format!("{}", -0.0), "0");
+ t!(format!("{:?}", -0.0), "-0");
+ t!(format!("{:?}", 0.0), "0");
+
+
+ // Ergonomic format_args!
+ t!(format!("{0:x} {0:X}", 15), "f F");
+ t!(format!("{0:x} {0:X} {}", 15), "f F 15");
+ // NOTE: For now the longer test cases must not be followed immediately by
+ // >1 empty lines, or the pretty printer will break. Since no one wants to
+ // touch the current pretty printer (#751), we have no choice but to work
+ // around it. Some of the following test cases are also affected.
+ t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF");
+ t!(format!("{a:x} {a:X}", a=15), "f F");
+
+ // And its edge cases
+ t!(format!("{a:.0$} {b:.0$} {0:.0$}\n{a:.c$} {b:.c$} {c:.c$}",
+ 4, a="abcdefg", b="hijklmn", c=3),
+ "abcd hijk 4\nabc hij 3");
+ t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg");
+ t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2");
+
+
// Test that pointers don't get truncated.
{
let val = usize::MAX;
test_write();
test_print();
test_order();
+ test_once();
// make sure that format! doesn't move out of local variables
let a: Box<_> = box 3;
write!(w, "{}", "hello");
writeln!(w, "{}", "line");
writeln!(w, "{foo}", foo="bar");
+ w.write_char('☃');
+ w.write_str("str");
}
- t!(buf, "34helloline\nbar\n");
+ t!(buf, "34helloline\nbar\n☃str");
}
// Just make sure that the macros are defined, there's not really a lot that we
// can do with them just yet (to test the output)
fn test_print() {
print!("hi");
- print!("{:?}", vec!(0u8));
+ print!("{:?}", vec![0u8]);
println!("hello");
println!("this is a {}", "test");
println!("{foo}", foo="bar");
foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
"1 2 4 5 3 6".to_string());
}
+
+fn test_once() {
+ // Make sure each argument are evaluted only once even though it may be
+ // formatted multiple times
+ fn foo() -> isize {
+ static mut FOO: isize = 0;
+ unsafe {
+ FOO += 1;
+ FOO
+ }
+ }
+ assert_eq!(format!("{0} {0} {0} {a} {a} {a}", foo(), a=foo()),
+ "1 1 1 2 2 2".to_string());
+}