]> git.proxmox.com Git - rustc.git/blame - src/libcoretest/fmt/num.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcoretest / fmt / num.rs
CommitLineData
1a4d82fc
JJ
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.
1a4d82fc
JJ
10
11#[test]
12fn test_format_int() {
13 // Formatting integers should select the right implementation based off
14 // the type of the argument. Also, hex/octal/binary should be defined
15 // for integers, but they shouldn't emit the negative sign.
85aaf69f 16 assert!(format!("{}", 1isize) == "1");
1a4d82fc
JJ
17 assert!(format!("{}", 1i8) == "1");
18 assert!(format!("{}", 1i16) == "1");
19 assert!(format!("{}", 1i32) == "1");
20 assert!(format!("{}", 1i64) == "1");
85aaf69f 21 assert!(format!("{}", -1isize) == "-1");
1a4d82fc
JJ
22 assert!(format!("{}", -1i8) == "-1");
23 assert!(format!("{}", -1i16) == "-1");
24 assert!(format!("{}", -1i32) == "-1");
25 assert!(format!("{}", -1i64) == "-1");
85aaf69f
SL
26 assert!(format!("{:?}", 1isize) == "1");
27 assert!(format!("{:?}", 1i8) == "1");
28 assert!(format!("{:?}", 1i16) == "1");
29 assert!(format!("{:?}", 1i32) == "1");
30 assert!(format!("{:?}", 1i64) == "1");
31 assert!(format!("{:b}", 1isize) == "1");
1a4d82fc
JJ
32 assert!(format!("{:b}", 1i8) == "1");
33 assert!(format!("{:b}", 1i16) == "1");
34 assert!(format!("{:b}", 1i32) == "1");
35 assert!(format!("{:b}", 1i64) == "1");
85aaf69f 36 assert!(format!("{:x}", 1isize) == "1");
1a4d82fc
JJ
37 assert!(format!("{:x}", 1i8) == "1");
38 assert!(format!("{:x}", 1i16) == "1");
39 assert!(format!("{:x}", 1i32) == "1");
40 assert!(format!("{:x}", 1i64) == "1");
85aaf69f 41 assert!(format!("{:X}", 1isize) == "1");
1a4d82fc
JJ
42 assert!(format!("{:X}", 1i8) == "1");
43 assert!(format!("{:X}", 1i16) == "1");
44 assert!(format!("{:X}", 1i32) == "1");
45 assert!(format!("{:X}", 1i64) == "1");
85aaf69f 46 assert!(format!("{:o}", 1isize) == "1");
1a4d82fc
JJ
47 assert!(format!("{:o}", 1i8) == "1");
48 assert!(format!("{:o}", 1i16) == "1");
49 assert!(format!("{:o}", 1i32) == "1");
50 assert!(format!("{:o}", 1i64) == "1");
51
85aaf69f 52 assert!(format!("{}", 1usize) == "1");
1a4d82fc
JJ
53 assert!(format!("{}", 1u8) == "1");
54 assert!(format!("{}", 1u16) == "1");
55 assert!(format!("{}", 1u32) == "1");
56 assert!(format!("{}", 1u64) == "1");
85aaf69f
SL
57 assert!(format!("{:?}", 1usize) == "1");
58 assert!(format!("{:?}", 1u8) == "1");
59 assert!(format!("{:?}", 1u16) == "1");
60 assert!(format!("{:?}", 1u32) == "1");
61 assert!(format!("{:?}", 1u64) == "1");
62 assert!(format!("{:b}", 1usize) == "1");
1a4d82fc
JJ
63 assert!(format!("{:b}", 1u8) == "1");
64 assert!(format!("{:b}", 1u16) == "1");
65 assert!(format!("{:b}", 1u32) == "1");
66 assert!(format!("{:b}", 1u64) == "1");
85aaf69f 67 assert!(format!("{:x}", 1usize) == "1");
1a4d82fc
JJ
68 assert!(format!("{:x}", 1u8) == "1");
69 assert!(format!("{:x}", 1u16) == "1");
70 assert!(format!("{:x}", 1u32) == "1");
71 assert!(format!("{:x}", 1u64) == "1");
85aaf69f 72 assert!(format!("{:X}", 1usize) == "1");
1a4d82fc
JJ
73 assert!(format!("{:X}", 1u8) == "1");
74 assert!(format!("{:X}", 1u16) == "1");
75 assert!(format!("{:X}", 1u32) == "1");
76 assert!(format!("{:X}", 1u64) == "1");
85aaf69f 77 assert!(format!("{:o}", 1usize) == "1");
1a4d82fc
JJ
78 assert!(format!("{:o}", 1u8) == "1");
79 assert!(format!("{:o}", 1u16) == "1");
80 assert!(format!("{:o}", 1u32) == "1");
81 assert!(format!("{:o}", 1u64) == "1");
82
83 // Test a larger number
85aaf69f
SL
84 assert!(format!("{:b}", 55) == "110111");
85 assert!(format!("{:o}", 55) == "67");
86 assert!(format!("{}", 55) == "55");
87 assert!(format!("{:x}", 55) == "37");
88 assert!(format!("{:X}", 55) == "37");
1a4d82fc
JJ
89}
90
91#[test]
92fn test_format_int_zero() {
85aaf69f
SL
93 assert!(format!("{}", 0) == "0");
94 assert!(format!("{:?}", 0) == "0");
95 assert!(format!("{:b}", 0) == "0");
96 assert!(format!("{:o}", 0) == "0");
97 assert!(format!("{:x}", 0) == "0");
98 assert!(format!("{:X}", 0) == "0");
99
100 assert!(format!("{}", 0u32) == "0");
101 assert!(format!("{:?}", 0u32) == "0");
102 assert!(format!("{:b}", 0u32) == "0");
103 assert!(format!("{:o}", 0u32) == "0");
104 assert!(format!("{:x}", 0u32) == "0");
105 assert!(format!("{:X}", 0u32) == "0");
1a4d82fc
JJ
106}
107
108#[test]
109fn test_format_int_flags() {
85aaf69f
SL
110 assert!(format!("{:3}", 1) == " 1");
111 assert!(format!("{:>3}", 1) == " 1");
112 assert!(format!("{:>+3}", 1) == " +1");
113 assert!(format!("{:<3}", 1) == "1 ");
114 assert!(format!("{:#}", 1) == "1");
115 assert!(format!("{:#x}", 10) == "0xa");
116 assert!(format!("{:#X}", 10) == "0xA");
117 assert!(format!("{:#5x}", 10) == " 0xa");
118 assert!(format!("{:#o}", 10) == "0o12");
119 assert!(format!("{:08x}", 10) == "0000000a");
120 assert!(format!("{:8x}", 10) == " a");
121 assert!(format!("{:<8x}", 10) == "a ");
122 assert!(format!("{:>8x}", 10) == " a");
123 assert!(format!("{:#08x}", 10) == "0x00000a");
124 assert!(format!("{:08}", -10) == "-0000010");
c34b1796
AL
125 assert!(format!("{:x}", !0u8) == "ff");
126 assert!(format!("{:X}", !0u8) == "FF");
127 assert!(format!("{:b}", !0u8) == "11111111");
128 assert!(format!("{:o}", !0u8) == "377");
129 assert!(format!("{:#x}", !0u8) == "0xff");
130 assert!(format!("{:#X}", !0u8) == "0xFF");
131 assert!(format!("{:#b}", !0u8) == "0b11111111");
132 assert!(format!("{:#o}", !0u8) == "0o377");
1a4d82fc
JJ
133}
134
135#[test]
136fn test_format_int_sign_padding() {
85aaf69f
SL
137 assert!(format!("{:+5}", 1) == " +1");
138 assert!(format!("{:+5}", -1) == " -1");
139 assert!(format!("{:05}", 1) == "00001");
140 assert!(format!("{:05}", -1) == "-0001");
141 assert!(format!("{:+05}", 1) == "+0001");
142 assert!(format!("{:+05}", -1) == "-0001");
1a4d82fc
JJ
143}
144
145#[test]
146fn test_format_int_twos_complement() {
147 use core::{i8, i16, i32, i64};
148 assert!(format!("{}", i8::MIN) == "-128");
149 assert!(format!("{}", i16::MIN) == "-32768");
150 assert!(format!("{}", i32::MIN) == "-2147483648");
151 assert!(format!("{}", i64::MIN) == "-9223372036854775808");
152}