[Lib] int.rs, uint.rs: added max_value, min_value

This commit is contained in:
David Rajchenbach-Teller 2011-10-17 17:26:58 +02:00 committed by Brian Anderson
parent 064f9dd93e
commit 454333368c
4 changed files with 35 additions and 0 deletions

View file

@ -1,3 +1,10 @@
fn max_value() -> int {
ret min_value() - 1;
}
fn min_value() -> int {
ret (-1 << (sys::size_of::<int>() * 8u as int - 1)) as int;
}
pure fn add(x: int, y: int) -> int { ret x + y; }

View file

@ -1,5 +1,19 @@
/**
* Return the minimal value for an uint.
*
* This is always 0
*/
pure fn min_value() -> uint { ret 0u; }
/**
* Return the maximal value for an uint.
*
* This is 2^wordsize - 1
*/
pure fn max_value() -> uint {
ret 0u - 1u;
}
fn add(x: uint, y: uint) -> uint { ret x + y; }
fn sub(x: uint, y: uint) -> uint { ret x - y; }

View file

@ -23,3 +23,10 @@ fn test_pow() {
assert (int::pow(-3, 3u) == -27);
assert (int::pow(4, 9u) == 262144);
}
#[test]
fn test_overflows() {
assert (int::max_value() > 0);
assert (int::min_value() <= 0);
assert (int::min_value() + int::max_value() + 1 == 0);
}

View file

@ -47,3 +47,10 @@ fn test_next_power_of_two() {
assert (uint::next_power_of_two(38u) == 64u);
assert (uint::next_power_of_two(39u) == 64u);
}
#[test]
fn test_overflows() {
assert (uint::max_value() > 0u);
assert (uint::min_value() <= 0u);
assert (uint::min_value() + uint::max_value() + 1u == 0u);
}