Skip to content

Commit 4543333

Browse files
David Rajchenbach-Tellerbrson
authored andcommitted
[Lib] int.rs, uint.rs: added max_value, min_value
1 parent 064f9dd commit 4543333

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/lib/int.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
fn max_value() -> int {
2+
ret min_value() - 1;
3+
}
4+
5+
fn min_value() -> int {
6+
ret (-1 << (sys::size_of::<int>() * 8u as int - 1)) as int;
7+
}
18

29

310
pure fn add(x: int, y: int) -> int { ret x + y; }

src/lib/uint.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
/**
2+
* Return the minimal value for an uint.
3+
*
4+
* This is always 0
5+
*/
16
pure fn min_value() -> uint { ret 0u; }
27

8+
/**
9+
* Return the maximal value for an uint.
10+
*
11+
* This is 2^wordsize - 1
12+
*/
13+
pure fn max_value() -> uint {
14+
ret 0u - 1u;
15+
}
16+
317
fn add(x: uint, y: uint) -> uint { ret x + y; }
418

519
fn sub(x: uint, y: uint) -> uint { ret x - y; }

src/test/stdtest/int.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ fn test_pow() {
2323
assert (int::pow(-3, 3u) == -27);
2424
assert (int::pow(4, 9u) == 262144);
2525
}
26+
27+
#[test]
28+
fn test_overflows() {
29+
assert (int::max_value() > 0);
30+
assert (int::min_value() <= 0);
31+
assert (int::min_value() + int::max_value() + 1 == 0);
32+
}

src/test/stdtest/uint.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ fn test_next_power_of_two() {
4747
assert (uint::next_power_of_two(38u) == 64u);
4848
assert (uint::next_power_of_two(39u) == 64u);
4949
}
50+
51+
#[test]
52+
fn test_overflows() {
53+
assert (uint::max_value() > 0u);
54+
assert (uint::min_value() <= 0u);
55+
assert (uint::min_value() + uint::max_value() + 1u == 0u);
56+
}

0 commit comments

Comments
 (0)