]> git.lizzy.rs Git - rust.git/commitdiff
[Lib] int.rs, uint.rs: added max_value, min_value
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Mon, 17 Oct 2011 15:26:58 +0000 (17:26 +0200)
committerBrian Anderson <banderson@mozilla.com>
Mon, 17 Oct 2011 18:22:57 +0000 (11:22 -0700)
src/lib/int.rs
src/lib/uint.rs
src/test/stdtest/int.rs
src/test/stdtest/uint.rs

index fe7689e3e28729fbc2d7e1edf4318f617dcb982a..4b420d96c27a230ccdc443c0b342eb1c9f18d61b 100644 (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; }
index 040d54842e39364ccb031eb1f859070783d46532..84b6bc5b1e07e9a31b44424ad7a2a1f387d34693 100644 (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; }
index 61d3448a175e9827806f920c4117c61bc7d47a7a..4a87908e5a64f6583debc1414a4ed5c7db74e1fc 100644 (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);
+}
\ No newline at end of file
index 7361c76b70bbb6bc106f638a25701e4da1d6e6cd..141b59ace82a871e80ac8d78e6d38f023e20cf25 100644 (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);
+}
\ No newline at end of file