Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1018 #1041

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib/int.rs
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
8 changes: 8 additions & 0 deletions src/lib/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ native "rust" mod rustrt {
type rng =
obj {
fn next() -> u32;
fn next_float() -> float;
};

resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }

fn mk_rng() -> rng {
obj rt_rng(c: @rand_res) {
fn next() -> u32 { ret rustrt::rand_next(**c); }
fn next_float() -> float {
let u1 = rustrt::rand_next(**c) as float;
let u2 = rustrt::rand_next(**c) as float;
let u3 = rustrt::rand_next(**c) as float;
let scale = u32::max_value() as float;
ret ((u1 / scale + u2) / scale + u3) / scale;
}
}
ret rt_rng(@rand_res(rustrt::rand_new()));
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/std.rc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod char;
mod int;
mod uint;
mod u8;
mod u32;
mod u64;
mod vec;
mod str;
Expand Down
13 changes: 13 additions & 0 deletions src/lib/u32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pure fn max_value() -> u32 { ret 4294967296u32; }
pure fn min_value() -> u32 { ret 0u32; }

//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//
3 changes: 3 additions & 0 deletions src/lib/u64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pure fn max_value() -> u64 { ret 18446744073709551615u64; }
pure fn min_value() -> u64 { ret 0u64; }

fn to_str(n: u64, radix: uint) -> str {
assert (0u < radix && radix <= 16u);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/u8.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

pure fn max_value() -> u8 { ret 255u8; }
pure fn min_value() -> u8 { ret 0u8; }

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

Expand Down
16 changes: 15 additions & 1 deletion src/lib/uint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@

/**
* 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; }

Expand Down
7 changes: 7 additions & 0 deletions src/test/stdtest/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 7 additions & 0 deletions src/test/stdtest/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}