about summary refs log tree commit diff
path: root/src/libcoretest/num
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2014-06-28 13:57:36 -0700
committerSteven Fackler <sfackler@gmail.com>2014-06-29 15:57:21 -0700
commit1ed646eaf7d09455a086afa11bcd83a7d2a6b0f4 (patch)
tree3c7bc58191bc3b260c4522c115ddf869b986193f /src/libcoretest/num
parentff94f867d29a90ab59060c10a62f65994776a8c4 (diff)
downloadrust-1ed646eaf7d09455a086afa11bcd83a7d2a6b0f4.tar.gz
rust-1ed646eaf7d09455a086afa11bcd83a7d2a6b0f4.zip
Extract tests from libcore to a separate crate
Libcore's test infrastructure is complicated by the fact that many lang
items are defined in the crate. The current approach (realcore/realstd
imports) is hacky and hard to work with (tests inside of core::cmp
haven't been run for months!).

Moving tests to a separate crate does mean that they can only test the
public API of libcore, but I don't feel that that is too much of an
issue. The only tests that I had to get rid of were some checking the
various numeric formatters, but those are also exercised through normal
format! calls in other tests.
Diffstat (limited to 'src/libcoretest/num')
-rw-r--r--src/libcoretest/num/i16.rs11
-rw-r--r--src/libcoretest/num/i32.rs11
-rw-r--r--src/libcoretest/num/i64.rs11
-rw-r--r--src/libcoretest/num/i8.rs11
-rw-r--r--src/libcoretest/num/int.rs11
-rw-r--r--src/libcoretest/num/int_macros.rs160
-rw-r--r--src/libcoretest/num/mod.rs39
-rw-r--r--src/libcoretest/num/u16.rs11
-rw-r--r--src/libcoretest/num/u32.rs11
-rw-r--r--src/libcoretest/num/u64.rs11
-rw-r--r--src/libcoretest/num/u8.rs11
-rw-r--r--src/libcoretest/num/uint.rs11
-rw-r--r--src/libcoretest/num/uint_macros.rs118
13 files changed, 427 insertions, 0 deletions
diff --git a/src/libcoretest/num/i16.rs b/src/libcoretest/num/i16.rs
new file mode 100644
index 00000000000..f3c2d67cdeb
--- /dev/null
+++ b/src/libcoretest/num/i16.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+int_module!(i16, i16)
diff --git a/src/libcoretest/num/i32.rs b/src/libcoretest/num/i32.rs
new file mode 100644
index 00000000000..7232fc7505d
--- /dev/null
+++ b/src/libcoretest/num/i32.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+int_module!(i32, i32)
diff --git a/src/libcoretest/num/i64.rs b/src/libcoretest/num/i64.rs
new file mode 100644
index 00000000000..075b8448f35
--- /dev/null
+++ b/src/libcoretest/num/i64.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+int_module!(i64, i64)
diff --git a/src/libcoretest/num/i8.rs b/src/libcoretest/num/i8.rs
new file mode 100644
index 00000000000..9e0439f2818
--- /dev/null
+++ b/src/libcoretest/num/i8.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+int_module!(i8, i8)
diff --git a/src/libcoretest/num/int.rs b/src/libcoretest/num/int.rs
new file mode 100644
index 00000000000..f01ec3f0310
--- /dev/null
+++ b/src/libcoretest/num/int.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+int_module!(int, int)
diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs
new file mode 100644
index 00000000000..940b036ca90
--- /dev/null
+++ b/src/libcoretest/num/int_macros.rs
@@ -0,0 +1,160 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+#![macro_escape]
+
+macro_rules! int_module (($T:ty, $T_i:ident) => (
+#[cfg(test)]
+mod tests {
+    use core::$T_i::*;
+    use core::int;
+    use num;
+    use core::num::CheckedDiv;
+
+    #[test]
+    fn test_overflows() {
+        assert!(MAX > 0);
+        assert!(MIN <= 0);
+        assert!(MIN + MAX + 1 == 0);
+    }
+
+    #[test]
+    fn test_num() {
+        num::test_num(10 as $T, 2 as $T);
+    }
+
+    #[test]
+    pub fn test_abs() {
+        assert!((1 as $T).abs() == 1 as $T);
+        assert!((0 as $T).abs() == 0 as $T);
+        assert!((-1 as $T).abs() == 1 as $T);
+    }
+
+    #[test]
+    fn test_abs_sub() {
+        assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
+        assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
+        assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
+        assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
+    }
+
+    #[test]
+    fn test_signum() {
+        assert!((1 as $T).signum() == 1 as $T);
+        assert!((0 as $T).signum() == 0 as $T);
+        assert!((-0 as $T).signum() == 0 as $T);
+        assert!((-1 as $T).signum() == -1 as $T);
+    }
+
+    #[test]
+    fn test_is_positive() {
+        assert!((1 as $T).is_positive());
+        assert!(!(0 as $T).is_positive());
+        assert!(!(-0 as $T).is_positive());
+        assert!(!(-1 as $T).is_positive());
+    }
+
+    #[test]
+    fn test_is_negative() {
+        assert!(!(1 as $T).is_negative());
+        assert!(!(0 as $T).is_negative());
+        assert!(!(-0 as $T).is_negative());
+        assert!((-1 as $T).is_negative());
+    }
+
+    #[test]
+    fn test_bitwise_operators() {
+        assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
+        assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
+        assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
+        assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
+        assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
+        assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
+    }
+
+    static A: $T = 0b0101100;
+    static B: $T = 0b0100001;
+    static C: $T = 0b1111001;
+
+    static _0: $T = 0;
+    static _1: $T = !0;
+
+    #[test]
+    fn test_count_ones() {
+        assert!(A.count_ones() == 3);
+        assert!(B.count_ones() == 2);
+        assert!(C.count_ones() == 5);
+    }
+
+    #[test]
+    fn test_count_zeros() {
+        assert!(A.count_zeros() == BITS as $T - 3);
+        assert!(B.count_zeros() == BITS as $T - 2);
+        assert!(C.count_zeros() == BITS as $T - 5);
+    }
+
+    #[test]
+    fn test_rotate() {
+        assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
+        assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
+        assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
+
+        // Rotating these should make no difference
+        //
+        // We test using 124 bits because to ensure that overlong bit shifts do
+        // not cause undefined behaviour. See #10183.
+        assert_eq!(_0.rotate_left(124), _0);
+        assert_eq!(_1.rotate_left(124), _1);
+        assert_eq!(_0.rotate_right(124), _0);
+        assert_eq!(_1.rotate_right(124), _1);
+    }
+
+    #[test]
+    fn test_swap_bytes() {
+        assert_eq!(A.swap_bytes().swap_bytes(), A);
+        assert_eq!(B.swap_bytes().swap_bytes(), B);
+        assert_eq!(C.swap_bytes().swap_bytes(), C);
+
+        // Swapping these should make no difference
+        assert_eq!(_0.swap_bytes(), _0);
+        assert_eq!(_1.swap_bytes(), _1);
+    }
+
+    #[test]
+    fn test_le() {
+        assert_eq!(Int::from_le(A.to_le()), A);
+        assert_eq!(Int::from_le(B.to_le()), B);
+        assert_eq!(Int::from_le(C.to_le()), C);
+        assert_eq!(Int::from_le(_0), _0);
+        assert_eq!(Int::from_le(_1), _1);
+        assert_eq!(_0.to_le(), _0);
+        assert_eq!(_1.to_le(), _1);
+    }
+
+    #[test]
+    fn test_be() {
+        assert_eq!(Int::from_be(A.to_be()), A);
+        assert_eq!(Int::from_be(B.to_be()), B);
+        assert_eq!(Int::from_be(C.to_be()), C);
+        assert_eq!(Int::from_be(_0), _0);
+        assert_eq!(Int::from_be(_1), _1);
+        assert_eq!(_0.to_be(), _0);
+        assert_eq!(_1.to_be(), _1);
+    }
+
+    #[test]
+    fn test_signed_checked_div() {
+        assert!(10i.checked_div(&2) == Some(5));
+        assert!(5i.checked_div(&0) == None);
+        assert!(int::MIN.checked_div(&-1) == None);
+    }
+}
+
+))
diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs
new file mode 100644
index 00000000000..8bb238c0b66
--- /dev/null
+++ b/src/libcoretest/num/mod.rs
@@ -0,0 +1,39 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+use core::num::cast;
+
+mod int_macros;
+mod i8;
+mod i16;
+mod i32;
+mod i64;
+mod int;
+mod uint_macros;
+mod u8;
+mod u16;
+mod u32;
+mod u64;
+mod uint;
+
+/// Helper function for testing numeric operations
+pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) {
+    assert_eq!(ten.add(&two),  cast(12i).unwrap());
+    assert_eq!(ten.sub(&two),  cast(8i).unwrap());
+    assert_eq!(ten.mul(&two),  cast(20i).unwrap());
+    assert_eq!(ten.div(&two),  cast(5i).unwrap());
+    assert_eq!(ten.rem(&two),  cast(0i).unwrap());
+
+    assert_eq!(ten.add(&two),  ten + two);
+    assert_eq!(ten.sub(&two),  ten - two);
+    assert_eq!(ten.mul(&two),  ten * two);
+    assert_eq!(ten.div(&two),  ten / two);
+    assert_eq!(ten.rem(&two),  ten % two);
+}
diff --git a/src/libcoretest/num/u16.rs b/src/libcoretest/num/u16.rs
new file mode 100644
index 00000000000..d6aa6476678
--- /dev/null
+++ b/src/libcoretest/num/u16.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+uint_module!(u16, u16)
diff --git a/src/libcoretest/num/u32.rs b/src/libcoretest/num/u32.rs
new file mode 100644
index 00000000000..218e79df5ae
--- /dev/null
+++ b/src/libcoretest/num/u32.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+uint_module!(u32, u32)
diff --git a/src/libcoretest/num/u64.rs b/src/libcoretest/num/u64.rs
new file mode 100644
index 00000000000..f78d4813503
--- /dev/null
+++ b/src/libcoretest/num/u64.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+uint_module!(u64, u64)
diff --git a/src/libcoretest/num/u8.rs b/src/libcoretest/num/u8.rs
new file mode 100644
index 00000000000..bb08072320b
--- /dev/null
+++ b/src/libcoretest/num/u8.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+uint_module!(u8, u8)
diff --git a/src/libcoretest/num/uint.rs b/src/libcoretest/num/uint.rs
new file mode 100644
index 00000000000..0db865f4cde
--- /dev/null
+++ b/src/libcoretest/num/uint.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+uint_module!(uint, uint)
diff --git a/src/libcoretest/num/uint_macros.rs b/src/libcoretest/num/uint_macros.rs
new file mode 100644
index 00000000000..2272af67daf
--- /dev/null
+++ b/src/libcoretest/num/uint_macros.rs
@@ -0,0 +1,118 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// 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.
+
+#![macro_escape]
+
+macro_rules! uint_module (($T:ty, $T_i:ident) => (
+#[cfg(test)]
+mod tests {
+    use core::$T_i::*;
+    use num;
+    use core::num::CheckedDiv;
+
+    #[test]
+    fn test_overflows() {
+        assert!(MAX > 0);
+        assert!(MIN <= 0);
+        assert!(MIN + MAX + 1 == 0);
+    }
+
+    #[test]
+    fn test_num() {
+        num::test_num(10 as $T, 2 as $T);
+    }
+
+    #[test]
+    fn test_bitwise_operators() {
+        assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
+        assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
+        assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
+        assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
+        assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
+        assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
+    }
+
+    static A: $T = 0b0101100;
+    static B: $T = 0b0100001;
+    static C: $T = 0b1111001;
+
+    static _0: $T = 0;
+    static _1: $T = !0;
+
+    #[test]
+    fn test_count_ones() {
+        assert!(A.count_ones() == 3);
+        assert!(B.count_ones() == 2);
+        assert!(C.count_ones() == 5);
+    }
+
+    #[test]
+    fn test_count_zeros() {
+        assert!(A.count_zeros() == BITS as $T - 3);
+        assert!(B.count_zeros() == BITS as $T - 2);
+        assert!(C.count_zeros() == BITS as $T - 5);
+    }
+
+    #[test]
+    fn test_rotate() {
+        assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
+        assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
+        assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
+
+        // Rotating these should make no difference
+        //
+        // We test using 124 bits because to ensure that overlong bit shifts do
+        // not cause undefined behaviour. See #10183.
+        assert_eq!(_0.rotate_left(124), _0);
+        assert_eq!(_1.rotate_left(124), _1);
+        assert_eq!(_0.rotate_right(124), _0);
+        assert_eq!(_1.rotate_right(124), _1);
+    }
+
+    #[test]
+    fn test_swap_bytes() {
+        assert_eq!(A.swap_bytes().swap_bytes(), A);
+        assert_eq!(B.swap_bytes().swap_bytes(), B);
+        assert_eq!(C.swap_bytes().swap_bytes(), C);
+
+        // Swapping these should make no difference
+        assert_eq!(_0.swap_bytes(), _0);
+        assert_eq!(_1.swap_bytes(), _1);
+    }
+
+    #[test]
+    fn test_le() {
+        assert_eq!(Int::from_le(A.to_le()), A);
+        assert_eq!(Int::from_le(B.to_le()), B);
+        assert_eq!(Int::from_le(C.to_le()), C);
+        assert_eq!(Int::from_le(_0), _0);
+        assert_eq!(Int::from_le(_1), _1);
+        assert_eq!(_0.to_le(), _0);
+        assert_eq!(_1.to_le(), _1);
+    }
+
+    #[test]
+    fn test_be() {
+        assert_eq!(Int::from_be(A.to_be()), A);
+        assert_eq!(Int::from_be(B.to_be()), B);
+        assert_eq!(Int::from_be(C.to_be()), C);
+        assert_eq!(Int::from_be(_0), _0);
+        assert_eq!(Int::from_be(_1), _1);
+        assert_eq!(_0.to_be(), _0);
+        assert_eq!(_1.to_be(), _1);
+    }
+
+    #[test]
+    fn test_unsigned_checked_div() {
+        assert!(10u.checked_div(&2) == Some(5));
+        assert!(5u.checked_div(&0) == None);
+    }
+}
+))