[Lib] int.rs, uint.rs: added max_value, min_value
This commit is contained in:
parent
064f9dd93e
commit
454333368c
4 changed files with 35 additions and 0 deletions
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue