]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | // Copyright 2012 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 | //! Operations and constants for `u32` | |
12 | ||
13 | use num::NumCast; | |
14 | ||
15 | mod inst { | |
16 | pub type T = u32; | |
17 | #[allow(non_camel_case_types)] | |
18 | pub type T_SIGNED = i32; | |
19 | pub static bits: uint = 32; | |
20 | } | |
21 | ||
22 | impl NumCast for u32 { | |
23 | /** | |
24 | * Cast `n` to a `u32` | |
25 | */ | |
26 | #[inline(always)] | |
27 | fn from<N:NumCast>(n: N) -> u32 { n.to_u32() } | |
28 | ||
29 | #[inline(always)] fn to_u8(&self) -> u8 { *self as u8 } | |
30 | #[inline(always)] fn to_u16(&self) -> u16 { *self as u16 } | |
31 | #[inline(always)] fn to_u32(&self) -> u32 { *self } | |
32 | #[inline(always)] fn to_u64(&self) -> u64 { *self as u64 } | |
33 | #[inline(always)] fn to_uint(&self) -> uint { *self as uint } | |
34 | ||
35 | #[inline(always)] fn to_i8(&self) -> i8 { *self as i8 } | |
36 | #[inline(always)] fn to_i16(&self) -> i16 { *self as i16 } | |
37 | #[inline(always)] fn to_i32(&self) -> i32 { *self as i32 } | |
38 | #[inline(always)] fn to_i64(&self) -> i64 { *self as i64 } | |
39 | #[inline(always)] fn to_int(&self) -> int { *self as int } | |
40 | ||
41 | #[inline(always)] fn to_f32(&self) -> f32 { *self as f32 } | |
42 | #[inline(always)] fn to_f64(&self) -> f64 { *self as f64 } | |
43 | #[inline(always)] fn to_float(&self) -> float { *self as float } | |
44 | } | |
45 | ||
46 | #[test] | |
47 | fn test_numcast() { | |
48 | assert!((20u == 20u64.to_uint())); | |
49 | assert!((20u8 == 20u64.to_u8())); | |
50 | assert!((20u16 == 20u64.to_u16())); | |
51 | assert!((20u32 == 20u64.to_u32())); | |
52 | assert!((20u64 == 20u64.to_u64())); | |
53 | assert!((20i == 20u64.to_int())); | |
54 | assert!((20i8 == 20u64.to_i8())); | |
55 | assert!((20i16 == 20u64.to_i16())); | |
56 | assert!((20i32 == 20u64.to_i32())); | |
57 | assert!((20i64 == 20u64.to_i64())); | |
58 | assert!((20f == 20u64.to_float())); | |
59 | assert!((20f32 == 20u64.to_f32())); | |
60 | assert!((20f64 == 20u64.to_f64())); | |
61 | ||
62 | assert!((20u64 == NumCast::from(20u))); | |
63 | assert!((20u64 == NumCast::from(20u8))); | |
64 | assert!((20u64 == NumCast::from(20u16))); | |
65 | assert!((20u64 == NumCast::from(20u32))); | |
66 | assert!((20u64 == NumCast::from(20u64))); | |
67 | assert!((20u64 == NumCast::from(20i))); | |
68 | assert!((20u64 == NumCast::from(20i8))); | |
69 | assert!((20u64 == NumCast::from(20i16))); | |
70 | assert!((20u64 == NumCast::from(20i32))); | |
71 | assert!((20u64 == NumCast::from(20i64))); | |
72 | assert!((20u64 == NumCast::from(20f))); | |
73 | assert!((20u64 == NumCast::from(20f32))); | |
74 | assert!((20u64 == NumCast::from(20f64))); | |
75 | ||
76 | assert!((20u64 == num::cast(20u))); | |
77 | assert!((20u64 == num::cast(20u8))); | |
78 | assert!((20u64 == num::cast(20u16))); | |
79 | assert!((20u64 == num::cast(20u32))); | |
80 | assert!((20u64 == num::cast(20u64))); | |
81 | assert!((20u64 == num::cast(20i))); | |
82 | assert!((20u64 == num::cast(20i8))); | |
83 | assert!((20u64 == num::cast(20i16))); | |
84 | assert!((20u64 == num::cast(20i32))); | |
85 | assert!((20u64 == num::cast(20i64))); | |
86 | assert!((20u64 == num::cast(20f))); | |
87 | assert!((20u64 == num::cast(20f32))); | |
88 | assert!((20u64 == num::cast(20f64))); | |
89 | } |