1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // FIXME(#4375): this shouldn't have to be a nested module named 'generated'
15 macro_rules! int_module (($T:ty, $bits:expr) => (mod generated {
17 #[allow(non_uppercase_statics)];
19 use num::{ToStrRadix, FromStrRadix};
20 use num::{Zero, One, strconv};
24 pub use cmp::{min, max};
26 pub static bits : uint = $bits;
27 pub static bytes : uint = ($bits / 8);
29 pub static min_value: $T = (-1 as $T) << (bits - 1);
30 pub static max_value: $T = min_value - 1 as $T;
32 enum Range { Closed, HalfOpen }
36 /// Iterate through a range with a given step value.
38 /// Let `term` denote the closed interval `[stop-step,stop]` if `r` is Closed;
39 /// otherwise `term` denotes the half-open interval `[stop-step,stop)`.
40 /// Iterates through the range `[x_0, x_1, ..., x_n]` where
41 /// `x_j == start + step*j`, and `x_n` lies in the interval `term`.
43 /// If no such nonnegative integer `n` exists, then the iteration range
46 fn range_step_core(start: $T, stop: $T, step: $T, r: Range, it: &fn($T) -> bool) -> bool {
49 fail!(~"range_step called with step == 0");
50 } else if step == (1 as $T) { // elide bounds check to tighten loop
52 if !it(i) { return false; }
53 // no need for overflow check;
54 // cannot have i + 1 > max_value because i < stop <= max_value
57 } else if step == (-1 as $T) { // elide bounds check to tighten loop
59 if !it(i) { return false; }
60 // no need for underflow check;
61 // cannot have i - 1 < min_value because i > stop >= min_value
64 } else if step > 0 { // ascending
66 if !it(i) { return false; }
67 // avoiding overflow. break if i + step > max_value
68 if i > max_value - step { return true; }
71 } else { // descending
73 if !it(i) { return false; }
74 // avoiding underflow. break if i + step < min_value
75 if i < min_value - step { return true; }
80 HalfOpen => return true,
81 Closed => return (i != stop || it(i))
87 /// Iterate through the range [`start`..`stop`) with a given step value.
89 /// Iterates through the range `[x_0, x_1, ..., x_n]` where
90 /// * `x_i == start + step*i`, and
91 /// * `n` is the greatest nonnegative integer such that `x_n < stop`
93 /// (If no such `n` exists, then the iteration range is empty.)
97 /// * `start` - lower bound, inclusive
98 /// * `stop` - higher bound, exclusive
103 /// for int::range(1, 5) |i| {
106 /// assert!(sum == 10);
109 pub fn range_step(start: $T, stop: $T, step: $T, it: &fn($T) -> bool) -> bool {
110 range_step_core(start, stop, step, HalfOpen, it)
115 /// Iterate through a range with a given step value.
117 /// Iterates through the range `[x_0, x_1, ..., x_n]` where
118 /// `x_i == start + step*i` and `x_n <= last < step + x_n`.
120 /// (If no such nonnegative integer `n` exists, then the iteration
123 pub fn range_step_inclusive(start: $T, last: $T, step: $T, it: &fn($T) -> bool) -> bool {
124 range_step_core(start, last, step, Closed, it)
132 fn lt(&self, other: &$T) -> bool { return (*self) < (*other); }
134 fn le(&self, other: &$T) -> bool { return (*self) <= (*other); }
136 fn ge(&self, other: &$T) -> bool { return (*self) >= (*other); }
138 fn gt(&self, other: &$T) -> bool { return (*self) > (*other); }
144 fn eq(&self, other: &$T) -> bool { return (*self) == (*other); }
146 fn ne(&self, other: &$T) -> bool { return (*self) != (*other); }
149 impl Orderable for $T {
151 fn min(&self, other: &$T) -> $T {
152 if *self < *other { *self } else { *other }
156 fn max(&self, other: &$T) -> $T {
157 if *self > *other { *self } else { *other }
161 fn clamp(&self, mn: &$T, mx: &$T) -> $T {
162 if *self > *mx { *mx } else
163 if *self < *mn { *mn } else { *self }
169 fn zero() -> $T { 0 }
172 fn is_zero(&self) -> bool { *self == 0 }
181 impl Add<$T,$T> for $T {
183 fn add(&self, other: &$T) -> $T { *self + *other }
187 impl Sub<$T,$T> for $T {
189 fn sub(&self, other: &$T) -> $T { *self - *other }
193 impl Mul<$T,$T> for $T {
195 fn mul(&self, other: &$T) -> $T { *self * *other }
199 impl Div<$T,$T> for $T {
201 /// Integer division, truncated towards 0. As this behaviour reflects the underlying
202 /// machine implementation it is more efficient than `Integer::div_floor`.
207 /// assert!( 8 / 3 == 2);
208 /// assert!( 8 / -3 == -2);
209 /// assert!(-8 / 3 == -2);
210 /// assert!(-8 / -3 == 2);
212 /// assert!( 1 / 2 == 0);
213 /// assert!( 1 / -2 == 0);
214 /// assert!(-1 / 2 == 0);
215 /// assert!(-1 / -2 == 0);
219 fn div(&self, other: &$T) -> $T { *self / *other }
223 impl Rem<$T,$T> for $T {
225 /// Returns the integer remainder after division, satisfying:
228 /// assert!((n / d) * d + (n % d) == n)
234 /// assert!( 8 % 3 == 2);
235 /// assert!( 8 % -3 == 2);
236 /// assert!(-8 % 3 == -2);
237 /// assert!(-8 % -3 == -2);
239 /// assert!( 1 % 2 == 1);
240 /// assert!( 1 % -2 == 1);
241 /// assert!(-1 % 2 == -1);
242 /// assert!(-1 % -2 == -1);
246 fn rem(&self, other: &$T) -> $T { *self % *other }
250 impl Neg<$T> for $T {
252 fn neg(&self) -> $T { -*self }
256 /// Computes the absolute value
258 fn abs(&self) -> $T {
259 if self.is_negative() { -*self } else { *self }
263 /// The positive difference of two numbers. Returns `0` if the number is less than or
264 /// equal to `other`, otherwise the difference between`self` and `other` is returned.
267 fn abs_sub(&self, other: &$T) -> $T {
268 if *self <= *other { 0 } else { *self - *other }
274 /// - `0` if the number is zero
275 /// - `1` if the number is positive
276 /// - `-1` if the number is negative
279 fn signum(&self) -> $T {
287 /// Returns true if the number is positive
289 fn is_positive(&self) -> bool { *self > 0 }
291 /// Returns true if the number is negative
293 fn is_negative(&self) -> bool { *self < 0 }
296 impl Integer for $T {
298 /// Floored integer division
303 /// assert!(( 8).div_floor( 3) == 2);
304 /// assert!(( 8).div_floor(-3) == -3);
305 /// assert!((-8).div_floor( 3) == -3);
306 /// assert!((-8).div_floor(-3) == 2);
308 /// assert!(( 1).div_floor( 2) == 0);
309 /// assert!(( 1).div_floor(-2) == -1);
310 /// assert!((-1).div_floor( 2) == -1);
311 /// assert!((-1).div_floor(-2) == 0);
315 fn div_floor(&self, other: &$T) -> $T {
316 // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
317 // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
318 match self.div_rem(other) {
319 (d, r) if (r > 0 && *other < 0)
320 || (r < 0 && *other > 0) => d - 1,
326 /// Integer modulo, satisfying:
329 /// assert!(n.div_floor(d) * d + n.mod_floor(d) == n)
335 /// assert!(( 8).mod_floor( 3) == 2);
336 /// assert!(( 8).mod_floor(-3) == -1);
337 /// assert!((-8).mod_floor( 3) == 1);
338 /// assert!((-8).mod_floor(-3) == -2);
340 /// assert!(( 1).mod_floor( 2) == 1);
341 /// assert!(( 1).mod_floor(-2) == -1);
342 /// assert!((-1).mod_floor( 2) == 1);
343 /// assert!((-1).mod_floor(-2) == -1);
347 fn mod_floor(&self, other: &$T) -> $T {
348 // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
349 // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
350 match *self % *other {
351 r if (r > 0 && *other < 0)
352 || (r < 0 && *other > 0) => r + *other,
357 /// Calculates `div_floor` and `mod_floor` simultaneously
359 fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
360 // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
361 // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
362 match self.div_rem(other) {
363 (d, r) if (r > 0 && *other < 0)
364 || (r < 0 && *other > 0) => (d - 1, r + *other),
369 /// Calculates `div` (`\`) and `rem` (`%`) simultaneously
371 fn div_rem(&self, other: &$T) -> ($T,$T) {
372 (*self / *other, *self % *other)
376 /// Calculates the Greatest Common Divisor (GCD) of the number and `other`
378 /// The result is always positive
381 fn gcd(&self, other: &$T) -> $T {
382 // Use Euclid's algorithm
394 /// Calculates the Lowest Common Multiple (LCM) of the number and `other`
397 fn lcm(&self, other: &$T) -> $T {
398 ((*self * *other) / self.gcd(other)).abs() // should not have to recaluculate abs
401 /// Returns `true` if the number can be divided by `other` without leaving a remainder
403 fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
405 /// Returns `true` if the number is divisible by `2`
407 fn is_even(&self) -> bool { self.is_multiple_of(&2) }
409 /// Returns `true` if the number is not divisible by `2`
411 fn is_odd(&self) -> bool { !self.is_even() }
414 impl Bitwise for $T {}
417 impl BitOr<$T,$T> for $T {
419 fn bitor(&self, other: &$T) -> $T { *self | *other }
423 impl BitAnd<$T,$T> for $T {
425 fn bitand(&self, other: &$T) -> $T { *self & *other }
429 impl BitXor<$T,$T> for $T {
431 fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
435 impl Shl<$T,$T> for $T {
437 fn shl(&self, other: &$T) -> $T { *self << *other }
441 impl Shr<$T,$T> for $T {
443 fn shr(&self, other: &$T) -> $T { *self >> *other }
447 impl Not<$T> for $T {
449 fn not(&self) -> $T { !*self }
452 impl Bounded for $T {
454 fn min_value() -> $T { min_value }
457 fn max_value() -> $T { max_value }
462 impl Primitive for $T {
464 fn bits() -> uint { bits }
467 fn bytes() -> uint { bits / 8 }
470 // String conversion functions and impl str -> num
472 /// Parse a string as a number in base 10.
474 pub fn from_str(s: &str) -> Option<$T> {
475 strconv::from_str_common(s, 10u, true, false, false,
476 strconv::ExpNone, false, false)
479 /// Parse a string as a number in the given base.
481 pub fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
482 strconv::from_str_common(s, radix, true, false, false,
483 strconv::ExpNone, false, false)
486 /// Parse a byte slice as a number in the given base.
488 pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
489 strconv::from_str_bytes_common(buf, radix, true, false, false,
490 strconv::ExpNone, false, false)
493 impl FromStr for $T {
495 fn from_str(s: &str) -> Option<$T> {
500 impl FromStrRadix for $T {
502 fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
503 from_str_radix(s, radix)
507 // String conversion functions and impl num -> str
509 /// Convert to a string as a byte slice in a given base.
511 pub fn to_str_bytes<U>(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
512 // The radix can be as low as 2, so we need at least 64 characters for a
513 // base 2 number, and then we need another for a possible '-' character.
514 let mut buf = [0u8, ..65];
516 do strconv::int_to_str_bytes_common(n, radix, strconv::SignNeg) |i| {
523 /// Convert to a string in base 10.
525 pub fn to_str(num: $T) -> ~str {
526 to_str_radix(num, 10u)
529 /// Convert to a string in a given base.
531 pub fn to_str_radix(num: $T, radix: uint) -> ~str {
532 let mut buf: ~[u8] = ~[];
533 do strconv::int_to_str_bytes_common(num, radix, strconv::SignNeg) |i| {
536 // We know we generated valid utf-8, so we don't need to go through that
538 unsafe { str::raw::from_bytes_owned(buf) }
543 fn to_str(&self) -> ~str {
548 impl ToStrRadix for $T {
550 fn to_str_radix(&self, radix: uint) -> ~str {
551 to_str_radix(*self, radix)
569 num::test_num(10 as $T, 2 as $T);
573 fn test_orderable() {
574 assert_eq!((1 as $T).min(&(2 as $T)), 1 as $T);
575 assert_eq!((2 as $T).min(&(1 as $T)), 1 as $T);
576 assert_eq!((1 as $T).max(&(2 as $T)), 2 as $T);
577 assert_eq!((2 as $T).max(&(1 as $T)), 2 as $T);
578 assert_eq!((1 as $T).clamp(&(2 as $T), &(4 as $T)), 2 as $T);
579 assert_eq!((8 as $T).clamp(&(2 as $T), &(4 as $T)), 4 as $T);
580 assert_eq!((3 as $T).clamp(&(2 as $T), &(4 as $T)), 3 as $T);
585 assert_eq!((1 as $T).abs(), 1 as $T);
586 assert_eq!((0 as $T).abs(), 0 as $T);
587 assert_eq!((-1 as $T).abs(), 1 as $T);
592 assert_eq!((-1 as $T).abs_sub(&(1 as $T)), 0 as $T);
593 assert_eq!((1 as $T).abs_sub(&(1 as $T)), 0 as $T);
594 assert_eq!((1 as $T).abs_sub(&(0 as $T)), 1 as $T);
595 assert_eq!((1 as $T).abs_sub(&(-1 as $T)), 2 as $T);
600 assert_eq!((1 as $T).signum(), 1 as $T);
601 assert_eq!((0 as $T).signum(), 0 as $T);
602 assert_eq!((-0 as $T).signum(), 0 as $T);
603 assert_eq!((-1 as $T).signum(), -1 as $T);
607 fn test_is_positive() {
608 assert!((1 as $T).is_positive());
609 assert!(!(0 as $T).is_positive());
610 assert!(!(-0 as $T).is_positive());
611 assert!(!(-1 as $T).is_positive());
615 fn test_is_negative() {
616 assert!(!(1 as $T).is_negative());
617 assert!(!(0 as $T).is_negative());
618 assert!(!(-0 as $T).is_negative());
619 assert!((-1 as $T).is_negative());
623 /// Checks that the division rule holds for:
625 /// - `n`: numerator (dividend)
626 /// - `d`: denominator (divisor)
627 /// - `qr`: quotient and remainder
630 fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
631 assert_eq!(d * q + r, n);
636 fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) {
638 let separate_div_rem = (n / d, n % d);
639 let combined_div_rem = n.div_rem(&d);
641 assert_eq!(separate_div_rem, qr);
642 assert_eq!(combined_div_rem, qr);
644 test_division_rule(nd, separate_div_rem);
645 test_division_rule(nd, combined_div_rem);
648 test_nd_dr(( 8, 3), ( 2, 2));
649 test_nd_dr(( 8, -3), (-2, 2));
650 test_nd_dr((-8, 3), (-2, -2));
651 test_nd_dr((-8, -3), ( 2, -2));
653 test_nd_dr(( 1, 2), ( 0, 1));
654 test_nd_dr(( 1, -2), ( 0, 1));
655 test_nd_dr((-1, 2), ( 0, -1));
656 test_nd_dr((-1, -2), ( 0, -1));
660 fn test_div_mod_floor() {
661 fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) {
663 let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
664 let combined_div_mod_floor = n.div_mod_floor(&d);
666 assert_eq!(separate_div_mod_floor, dm);
667 assert_eq!(combined_div_mod_floor, dm);
669 test_division_rule(nd, separate_div_mod_floor);
670 test_division_rule(nd, combined_div_mod_floor);
673 test_nd_dm(( 8, 3), ( 2, 2));
674 test_nd_dm(( 8, -3), (-3, -1));
675 test_nd_dm((-8, 3), (-3, 1));
676 test_nd_dm((-8, -3), ( 2, -2));
678 test_nd_dm(( 1, 2), ( 0, 1));
679 test_nd_dm(( 1, -2), (-1, -1));
680 test_nd_dm((-1, 2), (-1, 1));
681 test_nd_dm((-1, -2), ( 0, -1));
686 assert_eq!((10 as $T).gcd(&2), 2 as $T);
687 assert_eq!((10 as $T).gcd(&3), 1 as $T);
688 assert_eq!((0 as $T).gcd(&3), 3 as $T);
689 assert_eq!((3 as $T).gcd(&3), 3 as $T);
690 assert_eq!((56 as $T).gcd(&42), 14 as $T);
691 assert_eq!((3 as $T).gcd(&-3), 3 as $T);
692 assert_eq!((-6 as $T).gcd(&3), 3 as $T);
693 assert_eq!((-4 as $T).gcd(&-2), 2 as $T);
698 assert_eq!((1 as $T).lcm(&0), 0 as $T);
699 assert_eq!((0 as $T).lcm(&1), 0 as $T);
700 assert_eq!((1 as $T).lcm(&1), 1 as $T);
701 assert_eq!((-1 as $T).lcm(&1), 1 as $T);
702 assert_eq!((1 as $T).lcm(&-1), 1 as $T);
703 assert_eq!((-1 as $T).lcm(&-1), 1 as $T);
704 assert_eq!((8 as $T).lcm(&9), 72 as $T);
705 assert_eq!((11 as $T).lcm(&5), 55 as $T);
710 assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(&(0b1010 as $T)));
711 assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(&(0b1010 as $T)));
712 assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(&(0b1010 as $T)));
713 assert_eq!(0b1110 as $T, (0b0111 as $T).shl(&(1 as $T)));
714 assert_eq!(0b0111 as $T, (0b1110 as $T).shr(&(1 as $T)));
715 assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
719 fn test_multiple_of() {
720 assert!((6 as $T).is_multiple_of(&(6 as $T)));
721 assert!((6 as $T).is_multiple_of(&(3 as $T)));
722 assert!((6 as $T).is_multiple_of(&(1 as $T)));
723 assert!((-8 as $T).is_multiple_of(&(4 as $T)));
724 assert!((8 as $T).is_multiple_of(&(-1 as $T)));
725 assert!((-8 as $T).is_multiple_of(&(-2 as $T)));
730 assert_eq!((-4 as $T).is_even(), true);
731 assert_eq!((-3 as $T).is_even(), false);
732 assert_eq!((-2 as $T).is_even(), true);
733 assert_eq!((-1 as $T).is_even(), false);
734 assert_eq!((0 as $T).is_even(), true);
735 assert_eq!((1 as $T).is_even(), false);
736 assert_eq!((2 as $T).is_even(), true);
737 assert_eq!((3 as $T).is_even(), false);
738 assert_eq!((4 as $T).is_even(), true);
743 assert_eq!((-4 as $T).is_odd(), false);
744 assert_eq!((-3 as $T).is_odd(), true);
745 assert_eq!((-2 as $T).is_odd(), false);
746 assert_eq!((-1 as $T).is_odd(), true);
747 assert_eq!((0 as $T).is_odd(), false);
748 assert_eq!((1 as $T).is_odd(), true);
749 assert_eq!((2 as $T).is_odd(), false);
750 assert_eq!((3 as $T).is_odd(), true);
751 assert_eq!((4 as $T).is_odd(), false);
756 assert_eq!((0b010101 as $T).population_count(), 3);
760 fn test_primitive() {
761 assert_eq!(Primitive::bits::<$T>(), sys::size_of::<$T>() * 8);
762 assert_eq!(Primitive::bytes::<$T>(), sys::size_of::<$T>());
767 assert_eq!(from_str("0"), Some(0 as $T));
768 assert_eq!(from_str("3"), Some(3 as $T));
769 assert_eq!(from_str("10"), Some(10 as $T));
770 assert_eq!(i32::from_str("123456789"), Some(123456789 as i32));
771 assert_eq!(from_str("00100"), Some(100 as $T));
773 assert_eq!(from_str("-1"), Some(-1 as $T));
774 assert_eq!(from_str("-3"), Some(-3 as $T));
775 assert_eq!(from_str("-10"), Some(-10 as $T));
776 assert_eq!(i32::from_str("-123456789"), Some(-123456789 as i32));
777 assert_eq!(from_str("-00100"), Some(-100 as $T));
779 assert!(from_str(" ").is_none());
780 assert!(from_str("x").is_none());
784 fn test_parse_bytes() {
786 assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123 as $T));
787 assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9 as $T));
788 assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83 as $T));
789 assert_eq!(i32::parse_bytes("123".as_bytes(), 16u), Some(291 as i32));
790 assert_eq!(i32::parse_bytes("ffff".as_bytes(), 16u), Some(65535 as i32));
791 assert_eq!(i32::parse_bytes("FFFF".as_bytes(), 16u), Some(65535 as i32));
792 assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35 as $T));
793 assert_eq!(parse_bytes("Z".as_bytes(), 36u), Some(35 as $T));
795 assert_eq!(parse_bytes("-123".as_bytes(), 10u), Some(-123 as $T));
796 assert_eq!(parse_bytes("-1001".as_bytes(), 2u), Some(-9 as $T));
797 assert_eq!(parse_bytes("-123".as_bytes(), 8u), Some(-83 as $T));
798 assert_eq!(i32::parse_bytes("-123".as_bytes(), 16u), Some(-291 as i32));
799 assert_eq!(i32::parse_bytes("-ffff".as_bytes(), 16u), Some(-65535 as i32));
800 assert_eq!(i32::parse_bytes("-FFFF".as_bytes(), 16u), Some(-65535 as i32));
801 assert_eq!(parse_bytes("-z".as_bytes(), 36u), Some(-35 as $T));
802 assert_eq!(parse_bytes("-Z".as_bytes(), 36u), Some(-35 as $T));
804 assert!(parse_bytes("Z".as_bytes(), 35u).is_none());
805 assert!(parse_bytes("-9".as_bytes(), 2u).is_none());
810 assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
811 assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
812 assert_eq!(to_str_radix(-1 as $T, 10u), ~"-1");
813 assert_eq!(to_str_radix(127 as $T, 16u), ~"7f");
814 assert_eq!(to_str_radix(100 as $T, 10u), ~"100");
819 fn test_int_to_str_overflow() {
820 let mut i8_val: i8 = 127_i8;
821 assert_eq!(i8::to_str(i8_val), ~"127");
824 assert_eq!(i8::to_str(i8_val), ~"-128");
826 let mut i16_val: i16 = 32_767_i16;
827 assert_eq!(i16::to_str(i16_val), ~"32767");
830 assert_eq!(i16::to_str(i16_val), ~"-32768");
832 let mut i32_val: i32 = 2_147_483_647_i32;
833 assert_eq!(i32::to_str(i32_val), ~"2147483647");
836 assert_eq!(i32::to_str(i32_val), ~"-2147483648");
838 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
839 assert_eq!(i64::to_str(i64_val), ~"9223372036854775807");
842 assert_eq!(i64::to_str(i64_val), ~"-9223372036854775808");
846 fn test_int_from_str_overflow() {
847 let mut i8_val: i8 = 127_i8;
848 assert_eq!(i8::from_str("127"), Some(i8_val));
849 assert!(i8::from_str("128").is_none());
852 assert_eq!(i8::from_str("-128"), Some(i8_val));
853 assert!(i8::from_str("-129").is_none());
855 let mut i16_val: i16 = 32_767_i16;
856 assert_eq!(i16::from_str("32767"), Some(i16_val));
857 assert!(i16::from_str("32768").is_none());
860 assert_eq!(i16::from_str("-32768"), Some(i16_val));
861 assert!(i16::from_str("-32769").is_none());
863 let mut i32_val: i32 = 2_147_483_647_i32;
864 assert_eq!(i32::from_str("2147483647"), Some(i32_val));
865 assert!(i32::from_str("2147483648").is_none());
868 assert_eq!(i32::from_str("-2147483648"), Some(i32_val));
869 assert!(i32::from_str("-2147483649").is_none());
871 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
872 assert_eq!(i64::from_str("9223372036854775807"), Some(i64_val));
873 assert!(i64::from_str("9223372036854775808").is_none());
876 assert_eq!(i64::from_str("-9223372036854775808"), Some(i64_val));
877 assert!(i64::from_str("-9223372036854775809").is_none());
884 do range_step(20,26,2) |i| {
888 do range_step(36,30,-2) |i| {
892 do range_step(max_value - 2, max_value, 2) |i| {
896 do range_step(max_value - 3, max_value, 2) |i| {
900 do range_step(min_value + 2, min_value, -2) |i| {
904 do range_step(min_value + 3, min_value, -2) |i| {
908 assert_eq!(l, ~[20,22,24,
911 max_value-3,max_value-1,
913 min_value+3,min_value+1]);
915 // None of the `fail`s should execute.
916 do range_step(10,0,1) |_i| {
917 fail!(~"unreachable");
919 do range_step(0,10,-1) |_i| {
920 fail!(~"unreachable");
926 #[ignore(cfg(windows))]
927 fn test_range_step_zero_step() {
928 do range_step(0,10,0) |_i| { true };