2014-04-30 22:23:26 -07:00
|
|
|
// Copyright 2012-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]
|
|
|
|
#![doc(hidden)]
|
|
|
|
|
|
|
|
macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
|
|
|
|
|
|
|
|
pub static BITS : uint = $bits;
|
|
|
|
pub static BYTES : uint = ($bits / 8);
|
|
|
|
|
|
|
|
pub static MIN: $T = 0 as $T;
|
|
|
|
pub static MAX: $T = 0 as $T - 1 as $T;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use num;
|
|
|
|
use num::CheckedDiv;
|
|
|
|
use num::Bitwise;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_overflows() {
|
|
|
|
assert!(MAX > 0);
|
|
|
|
assert!(MIN <= 0);
|
2014-05-01 10:47:18 -07:00
|
|
|
assert!(MIN + MAX + 1 == 0);
|
2014-04-30 22:23:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_num() {
|
|
|
|
num::test_num(10 as $T, 2 as $T);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bitwise() {
|
2014-05-01 10:47:18 -07:00
|
|
|
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());
|
2014-04-30 22:23:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_count_ones() {
|
2014-05-01 10:47:18 -07:00
|
|
|
assert!((0b0101100 as $T).count_ones() == 3);
|
|
|
|
assert!((0b0100001 as $T).count_ones() == 2);
|
|
|
|
assert!((0b1111001 as $T).count_ones() == 5);
|
2014-04-30 22:23:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_count_zeros() {
|
2014-05-01 10:47:18 -07:00
|
|
|
assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
|
|
|
|
assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
|
|
|
|
assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
|
2014-04-30 22:23:26 -07:00
|
|
|
}
|
|
|
|
|
2014-06-12 21:19:17 -07:00
|
|
|
#[test]
|
|
|
|
fn test_rotate() {
|
|
|
|
let n: $T = 0b0101100; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
|
|
|
|
let n: $T = 0b0100001; assert_eq!(n.rotate_left(3).rotate_left(2).rotate_right(5), n);
|
|
|
|
let n: $T = 0b1111001; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
let n: $T = 0; assert_eq!(n.rotate_left(124), n);
|
|
|
|
let n: $T = MAX; assert_eq!(n.rotate_left(124), n);
|
|
|
|
let n: $T = 0; assert_eq!(n.rotate_right(124), n);
|
|
|
|
let n: $T = MAX; assert_eq!(n.rotate_right(124), n);
|
|
|
|
}
|
|
|
|
|
2014-04-30 22:23:26 -07:00
|
|
|
#[test]
|
|
|
|
fn test_unsigned_checked_div() {
|
2014-05-01 10:47:18 -07:00
|
|
|
assert!(10u.checked_div(&2) == Some(5));
|
|
|
|
assert!(5u.checked_div(&0) == None);
|
2014-04-30 22:23:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
))
|