2014-01-16 20:54:50 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
2013-07-29 03:12:41 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-10 23:57:11 -05:00
|
|
|
// no-pretty-expanded unnecessary unsafe block generated
|
2014-07-21 17:57:14 -05:00
|
|
|
// ignore-lexer-test FIXME #15679
|
2014-05-10 23:57:11 -05:00
|
|
|
|
2014-07-26 23:05:03 -05:00
|
|
|
#![feature(macro_rules)]
|
2014-04-03 19:55:06 -05:00
|
|
|
#![deny(warnings)]
|
2014-04-14 10:30:31 -05:00
|
|
|
#![allow(unused_must_use)]
|
2013-09-05 22:50:10 -05:00
|
|
|
|
2014-05-22 13:28:01 -05:00
|
|
|
extern crate debug;
|
|
|
|
|
2013-07-29 03:12:41 -05:00
|
|
|
use std::fmt;
|
2014-06-11 21:33:52 -05:00
|
|
|
use std::gc::GC;
|
2014-01-15 15:25:09 -06:00
|
|
|
use std::io::MemWriter;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
2013-08-28 04:22:45 -05:00
|
|
|
use std::str;
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
struct A;
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
impl fmt::Signed for A {
|
2014-02-05 06:55:13 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 16:05:06 -05:00
|
|
|
f.write("aloha".as_bytes())
|
2014-01-30 13:56:51 -06:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
impl fmt::Signed for B {
|
2014-02-05 06:55:13 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 16:05:06 -05:00
|
|
|
f.write("adios".as_bytes())
|
2014-01-30 13:56:51 -06:00
|
|
|
}
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
2014-05-02 21:32:47 -05:00
|
|
|
macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })
|
2013-08-20 02:40:27 -05:00
|
|
|
|
2013-07-29 03:12:41 -05:00
|
|
|
pub fn main() {
|
|
|
|
// Make sure there's a poly formatter that takes anything
|
2014-04-21 16:58:52 -05:00
|
|
|
t!(format!("{:?}", 1i), "1");
|
2013-08-31 00:28:59 -05:00
|
|
|
t!(format!("{:?}", A), "A");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:?}", ()), "()");
|
2014-04-21 16:58:52 -05:00
|
|
|
t!(format!("{:?}", box(GC) (box 1i, "foo")), "box(GC) (box 1, \"foo\")");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
|
|
|
// Various edge cases without formats
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!(""), "");
|
|
|
|
t!(format!("hello"), "hello");
|
2014-05-28 11:24:28 -05:00
|
|
|
t!(format!("hello {{"), "hello {");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2013-08-14 22:40:15 -05:00
|
|
|
// default formatters should work
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{}", 1.0f32), "1");
|
|
|
|
t!(format!("{}", 1.0f64), "1");
|
|
|
|
t!(format!("{}", "a"), "a");
|
2014-05-25 05:10:11 -05:00
|
|
|
t!(format!("{}", "a".to_string()), "a");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{}", false), "false");
|
|
|
|
t!(format!("{}", 'a'), "a");
|
2013-08-14 22:40:15 -05:00
|
|
|
|
2013-07-29 03:12:41 -05:00
|
|
|
// At least exercise all the formats
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:b}", true), "true");
|
|
|
|
t!(format!("{:c}", '☃'), "☃");
|
2014-04-21 16:58:52 -05:00
|
|
|
t!(format!("{:d}", 10i), "10");
|
|
|
|
t!(format!("{:i}", 10i), "10");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:u}", 10u), "10");
|
|
|
|
t!(format!("{:o}", 10u), "12");
|
|
|
|
t!(format!("{:x}", 10u), "a");
|
|
|
|
t!(format!("{:X}", 10u), "A");
|
|
|
|
t!(format!("{:s}", "foo"), "foo");
|
2014-05-25 05:10:11 -05:00
|
|
|
t!(format!("{:s}", "foo".to_string()), "foo");
|
2014-06-25 14:47:34 -05:00
|
|
|
t!(format!("{:p}", 0x1234 as *const int), "0x1234");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
|
|
|
|
t!(format!("{:d}", A), "aloha");
|
|
|
|
t!(format!("{:d}", B), "adios");
|
|
|
|
t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
|
2014-04-21 16:58:52 -05:00
|
|
|
t!(format!("{1} {0}", 0i, 1i), "1 0");
|
|
|
|
t!(format!("{foo} {bar}", foo=0i, bar=1i), "0 1");
|
|
|
|
t!(format!("{foo} {1} {bar} {0}", 0i, 1i, foo=2i, bar=3i), "2 1 3 0");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{} {0}", "a"), "a a");
|
2014-04-21 16:58:52 -05:00
|
|
|
t!(format!("{foo_bar}", foo_bar=1i), "1");
|
|
|
|
t!(format!("{:d}", 5i + 5i), "10");
|
2013-07-29 03:12:41 -05:00
|
|
|
|
2014-06-30 14:33:37 -05:00
|
|
|
let a: &fmt::Show = &1i;
|
|
|
|
t!(format!("{}", a), "1");
|
|
|
|
|
2013-08-10 02:28:47 -05:00
|
|
|
// Formatting strings and their arguments
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:s}", "a"), "a");
|
|
|
|
t!(format!("{:4s}", "a"), "a ");
|
|
|
|
t!(format!("{:>4s}", "a"), " a");
|
|
|
|
t!(format!("{:<4s}", "a"), "a ");
|
|
|
|
t!(format!("{:.4s}", "a"), "a");
|
|
|
|
t!(format!("{:4.4s}", "a"), "a ");
|
|
|
|
t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(format!("{:2.4s}", "aaaaa"), "aaaa");
|
|
|
|
t!(format!("{:2.4s}", "aaaa"), "aaaa");
|
|
|
|
t!(format!("{:2.4s}", "aaa"), "aaa");
|
|
|
|
t!(format!("{:2.4s}", "aa"), "aa");
|
|
|
|
t!(format!("{:2.4s}", "a"), "a ");
|
|
|
|
t!(format!("{:0>2s}", "a"), "0a");
|
|
|
|
t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
|
|
|
|
t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
|
2013-10-12 22:00:58 -05:00
|
|
|
t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:1$s}", "a", 4), "a ");
|
2013-10-12 22:00:58 -05:00
|
|
|
t!(format!("{1:0$s}", 4, "a"), "a ");
|
|
|
|
t!(format!("{:a$s}", "a", a=4), "a ");
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:-#s}", "a"), "a");
|
|
|
|
t!(format!("{:+#s}", "a"), "a");
|
2013-08-10 02:28:47 -05:00
|
|
|
|
2013-08-10 20:46:44 -05:00
|
|
|
// Some float stuff
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(format!("{:f}", 1.0f32), "1");
|
|
|
|
t!(format!("{:f}", 1.0f64), "1");
|
2013-09-26 01:26:09 -05:00
|
|
|
t!(format!("{:.3f}", 1.0f64), "1.000");
|
|
|
|
t!(format!("{:10.3f}", 1.0f64), " 1.000");
|
|
|
|
t!(format!("{:+10.3f}", 1.0f64), " +1.000");
|
|
|
|
t!(format!("{:+10.3f}", -1.0f64), " -1.000");
|
2013-08-23 20:14:11 -05:00
|
|
|
|
2014-01-16 20:54:50 -06:00
|
|
|
t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
|
|
|
|
t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
|
|
|
|
t!(format!("{:E}", 1.2345e6f64), "1.2345E6");
|
|
|
|
t!(format!("{:.3e}", 1.2345e6f64), "1.234e6");
|
|
|
|
t!(format!("{:10.3e}", 1.2345e6f64), " 1.234e6");
|
|
|
|
t!(format!("{:+10.3e}", 1.2345e6f64), " +1.234e6");
|
|
|
|
t!(format!("{:+10.3e}", -1.2345e6f64), " -1.234e6");
|
|
|
|
|
2013-09-02 23:38:46 -05:00
|
|
|
// Escaping
|
2014-05-28 11:24:28 -05:00
|
|
|
t!(format!("{{"), "{");
|
|
|
|
t!(format!("}}"), "}");
|
2013-09-02 23:38:46 -05:00
|
|
|
|
2013-08-23 20:14:11 -05:00
|
|
|
test_write();
|
|
|
|
test_print();
|
2014-02-27 19:07:27 -06:00
|
|
|
test_order();
|
2013-09-03 01:53:13 -05:00
|
|
|
|
|
|
|
// make sure that format! doesn't move out of local variables
|
2014-04-21 16:58:52 -05:00
|
|
|
let a = box 3i;
|
2013-09-03 01:53:13 -05:00
|
|
|
format!("{:?}", a);
|
|
|
|
format!("{:?}", a);
|
2013-09-05 22:50:10 -05:00
|
|
|
|
|
|
|
// make sure that format! doesn't cause spurious unused-unsafe warnings when
|
|
|
|
// it's inside of an outer unsafe block
|
|
|
|
unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let a: int = ::std::mem::transmute(3u);
|
2013-09-05 22:50:10 -05:00
|
|
|
format!("{}", a);
|
|
|
|
}
|
2013-08-28 04:22:45 -05:00
|
|
|
|
|
|
|
test_format_args();
|
2013-09-18 15:51:07 -05:00
|
|
|
|
|
|
|
// test that trailing commas are acceptable
|
|
|
|
format!("{}", "test",);
|
|
|
|
format!("{foo}", foo="test",);
|
2013-07-29 03:12:41 -05:00
|
|
|
}
|
|
|
|
|
2013-08-23 20:14:11 -05:00
|
|
|
// Basic test to make sure that we can invoke the `write!` macro with an
|
|
|
|
// io::Writer instance.
|
|
|
|
fn test_write() {
|
2013-08-20 02:40:27 -05:00
|
|
|
let mut buf = MemWriter::new();
|
2014-04-21 16:58:52 -05:00
|
|
|
write!(&mut buf as &mut io::Writer, "{}", 3i);
|
2013-08-20 02:40:27 -05:00
|
|
|
{
|
|
|
|
let w = &mut buf as &mut io::Writer;
|
2014-04-21 16:58:52 -05:00
|
|
|
write!(w, "{foo}", foo=4i);
|
2013-08-23 20:14:11 -05:00
|
|
|
write!(w, "{:s}", "hello");
|
|
|
|
writeln!(w, "{}", "line");
|
|
|
|
writeln!(w, "{foo}", foo="bar");
|
2013-08-20 02:40:27 -05:00
|
|
|
}
|
|
|
|
|
2014-05-25 05:17:19 -05:00
|
|
|
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
|
2013-08-23 20:14:11 -05:00
|
|
|
t!(s, "34helloline\nbar\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2013-08-24 14:46:55 -05:00
|
|
|
print!("hi");
|
2014-03-05 17:28:08 -06:00
|
|
|
print!("{:?}", vec!(0u8));
|
2013-08-23 20:14:11 -05:00
|
|
|
println!("hello");
|
|
|
|
println!("this is a {}", "test");
|
|
|
|
println!("{foo}", foo="bar");
|
2013-08-20 02:40:27 -05:00
|
|
|
}
|
2013-08-28 04:22:45 -05:00
|
|
|
|
|
|
|
// 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_format_args() {
|
|
|
|
let mut buf = MemWriter::new();
|
|
|
|
{
|
|
|
|
let w = &mut buf as &mut io::Writer;
|
2014-04-21 16:58:52 -05:00
|
|
|
format_args!(|args| { write!(w, "{}", args); }, "{}", 1i);
|
2014-05-10 16:05:06 -05:00
|
|
|
format_args!(|args| { write!(w, "{}", args); }, "test");
|
2014-04-21 16:58:52 -05:00
|
|
|
format_args!(|args| { write!(w, "{}", args); }, "{test}", test=3i);
|
2013-08-28 04:22:45 -05:00
|
|
|
}
|
2014-05-25 05:17:19 -05:00
|
|
|
let s = str::from_utf8(buf.unwrap().as_slice()).unwrap().to_string();
|
2013-08-28 04:22:45 -05:00
|
|
|
t!(s, "1test3");
|
|
|
|
|
|
|
|
let s = format_args!(fmt::format, "hello {}", "world");
|
|
|
|
t!(s, "hello world");
|
2014-05-02 21:32:47 -05:00
|
|
|
let s = format_args!(|args| {
|
|
|
|
format!("{}: {}", "args were", args)
|
|
|
|
}, "hello {}", "world");
|
|
|
|
t!(s, "args were: hello world");
|
2013-08-28 04:22:45 -05:00
|
|
|
}
|
2014-02-27 19:07:27 -06:00
|
|
|
|
|
|
|
fn test_order() {
|
|
|
|
// Make sure format!() arguments are always evaluated in a left-to-right
|
|
|
|
// ordering
|
|
|
|
fn foo() -> int {
|
|
|
|
static mut FOO: int = 0;
|
|
|
|
unsafe {
|
|
|
|
FOO += 1;
|
|
|
|
FOO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(format!("{} {} {a} {b} {} {c}",
|
|
|
|
foo(), foo(), foo(), a=foo(), b=foo(), c=foo()),
|
2014-05-25 05:17:19 -05:00
|
|
|
"1 2 4 5 3 6".to_string());
|
2014-02-27 19:07:27 -06:00
|
|
|
}
|