]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/int-template.rs
Revert rename of Div to Quot
[rust.git] / src / libcore / num / int-template.rs
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.
4 //
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.
10
11 use T = self::inst::T;
12
13 use from_str::FromStr;
14 use num::{ToStrRadix, FromStrRadix};
15 use num::{Zero, One, strconv};
16 use prelude::*;
17
18 pub use cmp::{min, max};
19
20 pub static bits : uint = inst::bits;
21 pub static bytes : uint = (inst::bits / 8);
22
23 pub static min_value: T = (-1 as T) << (bits - 1);
24 pub static max_value: T = min_value - 1 as T;
25
26 #[inline(always)]
27 pub fn add(x: T, y: T) -> T { x + y }
28 #[inline(always)]
29 pub fn sub(x: T, y: T) -> T { x - y }
30 #[inline(always)]
31 pub fn mul(x: T, y: T) -> T { x * y }
32 #[inline(always)]
33 pub fn div(x: T, y: T) -> T { x / y }
34
35 ///
36 /// Returns the remainder of y / x.
37 ///
38 /// # Examples
39 /// ~~~
40 /// assert!(int::rem(5 / 2) == 1);
41 /// ~~~
42 ///
43 /// When faced with negative numbers, the result copies the sign of the
44 /// dividend.
45 ///
46 /// ~~~
47 /// assert!(int::rem(2 / -3) ==  2);
48 /// ~~~
49 ///
50 /// ~~~
51 /// assert!(int::rem(-2 / 3) ==  -2);
52 /// ~~~
53 ///
54 ///
55 #[inline(always)]
56 pub fn rem(x: T, y: T) -> T { x % y }
57
58 #[inline(always)]
59 pub fn lt(x: T, y: T) -> bool { x < y }
60 #[inline(always)]
61 pub fn le(x: T, y: T) -> bool { x <= y }
62 #[inline(always)]
63 pub fn eq(x: T, y: T) -> bool { x == y }
64 #[inline(always)]
65 pub fn ne(x: T, y: T) -> bool { x != y }
66 #[inline(always)]
67 pub fn ge(x: T, y: T) -> bool { x >= y }
68 #[inline(always)]
69 pub fn gt(x: T, y: T) -> bool { x > y }
70
71 ///
72 /// Iterate over the range [`lo`..`hi`)
73 ///
74 /// # Arguments
75 ///
76 /// * `lo` - lower bound, inclusive
77 /// * `hi` - higher bound, exclusive
78 ///
79 /// # Examples
80 /// ~~~
81 /// let mut sum = 0;
82 /// for int::range(1, 5) |i| {
83 ///     sum += i;
84 /// }
85 /// assert!(sum == 10);
86 /// ~~~
87 ///
88 #[inline(always)]
89 /// Iterate over the range [`start`,`start`+`step`..`stop`)
90 pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) {
91     let mut i = start;
92     if step == 0 {
93         fail!(~"range_step called with step == 0");
94     } else if step > 0 { // ascending
95         while i < stop {
96             if !it(i) { break }
97             // avoiding overflow. break if i + step > max_value
98             if i > max_value - step { break; }
99             i += step;
100         }
101     } else { // descending
102         while i > stop {
103             if !it(i) { break }
104             // avoiding underflow. break if i + step < min_value
105             if i < min_value - step { break; }
106             i += step;
107         }
108     }
109 }
110
111 #[inline(always)]
112 /// Iterate over the range [`lo`..`hi`)
113 pub fn range(lo: T, hi: T, it: &fn(T) -> bool) {
114     range_step(lo, hi, 1 as T, it);
115 }
116
117 #[inline(always)]
118 /// Iterate over the range [`hi`..`lo`)
119 pub fn range_rev(hi: T, lo: T, it: &fn(T) -> bool) {
120     range_step(hi, lo, -1 as T, it);
121 }
122
123 /// Computes the bitwise complement
124 #[inline(always)]
125 pub fn compl(i: T) -> T {
126     -1 as T ^ i
127 }
128
129 /// Computes the absolute value
130 #[inline(always)]
131 pub fn abs(i: T) -> T { i.abs() }
132
133 impl Num for T {}
134
135 #[cfg(notest)]
136 impl Ord for T {
137     #[inline(always)]
138     fn lt(&self, other: &T) -> bool { return (*self) < (*other); }
139     #[inline(always)]
140     fn le(&self, other: &T) -> bool { return (*self) <= (*other); }
141     #[inline(always)]
142     fn ge(&self, other: &T) -> bool { return (*self) >= (*other); }
143     #[inline(always)]
144     fn gt(&self, other: &T) -> bool { return (*self) > (*other); }
145 }
146
147 #[cfg(notest)]
148 impl Eq for T {
149     #[inline(always)]
150     fn eq(&self, other: &T) -> bool { return (*self) == (*other); }
151     #[inline(always)]
152     fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
153 }
154
155 impl Orderable for T {
156     #[inline(always)]
157     fn min(&self, other: &T) -> T {
158         if *self < *other { *self } else { *other }
159     }
160
161     #[inline(always)]
162     fn max(&self, other: &T) -> T {
163         if *self > *other { *self } else { *other }
164     }
165
166     #[inline(always)]
167     fn clamp(&self, mn: &T, mx: &T) -> T {
168         if *self > *mx { *mx } else
169         if *self < *mn { *mn } else { *self }
170     }
171 }
172
173 impl Zero for T {
174     #[inline(always)]
175     fn zero() -> T { 0 }
176
177     #[inline(always)]
178     fn is_zero(&self) -> bool { *self == 0 }
179 }
180
181 impl One for T {
182     #[inline(always)]
183     fn one() -> T { 1 }
184 }
185
186 #[cfg(notest)]
187 impl Add<T,T> for T {
188     #[inline(always)]
189     fn add(&self, other: &T) -> T { *self + *other }
190 }
191
192 #[cfg(notest)]
193 impl Sub<T,T> for T {
194     #[inline(always)]
195     fn sub(&self, other: &T) -> T { *self - *other }
196 }
197
198 #[cfg(notest)]
199 impl Mul<T,T> for T {
200     #[inline(always)]
201     fn mul(&self, other: &T) -> T { *self * *other }
202 }
203
204 #[cfg(notest)]
205 impl Div<T,T> for T {
206     ///
207     /// Integer division, truncated towards 0. As this behaviour reflects the underlying
208     /// machine implementation it is more efficient than `Integer::div_floor`.
209     ///
210     /// # Examples
211     ///
212     /// ~~~
213     /// assert!( 8 /  3 ==  2);
214     /// assert!( 8 / -3 == -2);
215     /// assert!(-8 /  3 == -2);
216     /// assert!(-8 / -3 ==  2);
217
218     /// assert!( 1 /  2 ==  0);
219     /// assert!( 1 / -2 ==  0);
220     /// assert!(-1 /  2 ==  0);
221     /// assert!(-1 / -2 ==  0);
222     /// ~~~
223     ///
224     #[inline(always)]
225     fn div(&self, other: &T) -> T { *self / *other }
226 }
227
228 #[cfg(stage0,notest)]
229 impl Modulo<T,T> for T {
230     #[inline(always)]
231     fn modulo(&self, other: &T) -> T { *self % *other }
232 }
233 #[cfg(not(stage0),notest)]
234 impl Rem<T,T> for T {
235     ///
236     /// Returns the integer remainder after division, satisfying:
237     ///
238     /// ~~~
239     /// assert!((n / d) * d + (n % d) == n)
240     /// ~~~
241     ///
242     /// # Examples
243     ///
244     /// ~~~
245     /// assert!( 8 %  3 ==  2);
246     /// assert!( 8 % -3 ==  2);
247     /// assert!(-8 %  3 == -2);
248     /// assert!(-8 % -3 == -2);
249
250     /// assert!( 1 %  2 ==  1);
251     /// assert!( 1 % -2 ==  1);
252     /// assert!(-1 %  2 == -1);
253     /// assert!(-1 % -2 == -1);
254     /// ~~~
255     ///
256     #[inline(always)]
257     fn rem(&self, other: &T) -> T { *self % *other }
258 }
259
260 #[cfg(notest)]
261 impl Neg<T> for T {
262     #[inline(always)]
263     fn neg(&self) -> T { -*self }
264 }
265
266 impl Signed for T {
267     /// Computes the absolute value
268     #[inline(always)]
269     fn abs(&self) -> T {
270         if self.is_negative() { -*self } else { *self }
271     }
272
273     ///
274     /// # Returns
275     ///
276     /// - `0` if the number is zero
277     /// - `1` if the number is positive
278     /// - `-1` if the number is negative
279     ///
280     #[inline(always)]
281     fn signum(&self) -> T {
282         match *self {
283             n if n > 0 =>  1,
284             0          =>  0,
285             _          => -1,
286         }
287     }
288
289     /// Returns true if the number is positive
290     #[inline(always)]
291     fn is_positive(&self) -> bool { *self > 0 }
292
293     /// Returns true if the number is negative
294     #[inline(always)]
295     fn is_negative(&self) -> bool { *self < 0 }
296 }
297
298 impl Integer for T {
299     ///
300     /// Floored integer division
301     ///
302     /// # Examples
303     ///
304     /// ~~~
305     /// assert!(( 8).div_floor( 3) ==  2);
306     /// assert!(( 8).div_floor(-3) == -3);
307     /// assert!((-8).div_floor( 3) == -3);
308     /// assert!((-8).div_floor(-3) ==  2);
309     ///
310     /// assert!(( 1).div_floor( 2) ==  0);
311     /// assert!(( 1).div_floor(-2) == -1);
312     /// assert!((-1).div_floor( 2) == -1);
313     /// assert!((-1).div_floor(-2) ==  0);
314     /// ~~~
315     ///
316     #[inline(always)]
317     fn div_floor(&self, other: &T) -> T {
318         // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
319         // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
320         match self.div_rem(other) {
321             (d, r) if (r > 0 && *other < 0)
322                    || (r < 0 && *other > 0) => d - 1,
323             (d, _)                          => d,
324         }
325     }
326
327     ///
328     /// Integer modulo, satisfying:
329     ///
330     /// ~~~
331     /// assert!(n.div_floor(d) * d + n.mod_floor(d) == n)
332     /// ~~~
333     ///
334     /// # Examples
335     ///
336     /// ~~~
337     /// assert!(( 8).mod_floor( 3) ==  2);
338     /// assert!(( 8).mod_floor(-3) == -1);
339     /// assert!((-8).mod_floor( 3) ==  1);
340     /// assert!((-8).mod_floor(-3) == -2);
341     ///
342     /// assert!(( 1).mod_floor( 2) ==  1);
343     /// assert!(( 1).mod_floor(-2) == -1);
344     /// assert!((-1).mod_floor( 2) ==  1);
345     /// assert!((-1).mod_floor(-2) == -1);
346     /// ~~~
347     ///
348     #[inline(always)]
349     fn mod_floor(&self, other: &T) -> T {
350         // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
351         // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
352         match *self % *other {
353             r if (r > 0 && *other < 0)
354               || (r < 0 && *other > 0) => r + *other,
355             r                          => r,
356         }
357     }
358
359     /// Calculates `div_floor` and `mod_floor` simultaneously
360     #[inline(always)]
361     fn div_mod_floor(&self, other: &T) -> (T,T) {
362         // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
363         // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
364         match self.div_rem(other) {
365             (d, r) if (r > 0 && *other < 0)
366                    || (r < 0 && *other > 0) => (d - 1, r + *other),
367             (d, r)                          => (d, r),
368         }
369     }
370
371     /// Calculates `div` (`\`) and `rem` (`%`) simultaneously
372     #[inline(always)]
373     fn div_rem(&self, other: &T) -> (T,T) {
374         (*self / *other, *self % *other)
375     }
376
377     ///
378     /// Calculates the Greatest Common Divisor (GCD) of the number and `other`
379     ///
380     /// The result is always positive
381     ///
382     #[inline(always)]
383     fn gcd(&self, other: &T) -> T {
384         // Use Euclid's algorithm
385         let mut m = *self, n = *other;
386         while m != 0 {
387             let temp = m;
388             m = n % temp;
389             n = temp;
390         }
391         n.abs()
392     }
393
394     ///
395     /// Calculates the Lowest Common Multiple (LCM) of the number and `other`
396     ///
397     #[inline(always)]
398     fn lcm(&self, other: &T) -> T {
399         ((*self * *other) / self.gcd(other)).abs() // should not have to recaluculate abs
400     }
401
402     /// Returns `true` if the number can be divided by `other` without leaving a remainder
403     #[inline(always)]
404     fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
405
406     /// Returns `true` if the number is divisible by `2`
407     #[inline(always)]
408     fn is_even(&self) -> bool { self.is_multiple_of(&2) }
409
410     /// Returns `true` if the number is not divisible by `2`
411     #[inline(always)]
412     fn is_odd(&self) -> bool { !self.is_even() }
413 }
414
415 impl Bitwise for T {}
416
417 #[cfg(notest)]
418 impl BitOr<T,T> for T {
419     #[inline(always)]
420     fn bitor(&self, other: &T) -> T { *self | *other }
421 }
422
423 #[cfg(notest)]
424 impl BitAnd<T,T> for T {
425     #[inline(always)]
426     fn bitand(&self, other: &T) -> T { *self & *other }
427 }
428
429 #[cfg(notest)]
430 impl BitXor<T,T> for T {
431     #[inline(always)]
432     fn bitxor(&self, other: &T) -> T { *self ^ *other }
433 }
434
435 #[cfg(notest)]
436 impl Shl<T,T> for T {
437     #[inline(always)]
438     fn shl(&self, other: &T) -> T { *self << *other }
439 }
440
441 #[cfg(notest)]
442 impl Shr<T,T> for T {
443     #[inline(always)]
444     fn shr(&self, other: &T) -> T { *self >> *other }
445 }
446
447 #[cfg(notest)]
448 impl Not<T> for T {
449     #[inline(always)]
450     fn not(&self) -> T { !*self }
451 }
452
453 impl Bounded for T {
454     #[inline(always)]
455     fn min_value() -> T { min_value }
456
457     #[inline(always)]
458     fn max_value() -> T { max_value }
459 }
460
461 impl Int for T {}
462
463 // String conversion functions and impl str -> num
464
465 /// Parse a string as a number in base 10.
466 #[inline(always)]
467 pub fn from_str(s: &str) -> Option<T> {
468     strconv::from_str_common(s, 10u, true, false, false,
469                          strconv::ExpNone, false, false)
470 }
471
472 /// Parse a string as a number in the given base.
473 #[inline(always)]
474 pub fn from_str_radix(s: &str, radix: uint) -> Option<T> {
475     strconv::from_str_common(s, radix, true, false, false,
476                          strconv::ExpNone, false, false)
477 }
478
479 /// Parse a byte slice as a number in the given base.
480 #[inline(always)]
481 pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
482     strconv::from_str_bytes_common(buf, radix, true, false, false,
483                                strconv::ExpNone, false, false)
484 }
485
486 impl FromStr for T {
487     #[inline(always)]
488     fn from_str(s: &str) -> Option<T> {
489         from_str(s)
490     }
491 }
492
493 impl FromStrRadix for T {
494     #[inline(always)]
495     fn from_str_radix(s: &str, radix: uint) -> Option<T> {
496         from_str_radix(s, radix)
497     }
498 }
499
500 // String conversion functions and impl num -> str
501
502 /// Convert to a string as a byte slice in a given base.
503 #[inline(always)]
504 pub fn to_str_bytes<U>(n: T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
505     let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
506                             strconv::SignNeg, strconv::DigAll);
507     f(buf)
508 }
509
510 /// Convert to a string in base 10.
511 #[inline(always)]
512 pub fn to_str(num: T) -> ~str {
513     let (buf, _) = strconv::to_str_common(&num, 10u, false,
514                                       strconv::SignNeg, strconv::DigAll);
515     buf
516 }
517
518 /// Convert to a string in a given base.
519 #[inline(always)]
520 pub fn to_str_radix(num: T, radix: uint) -> ~str {
521     let (buf, _) = strconv::to_str_common(&num, radix, false,
522                                       strconv::SignNeg, strconv::DigAll);
523     buf
524 }
525
526 impl ToStr for T {
527     #[inline(always)]
528     fn to_str(&self) -> ~str {
529         to_str(*self)
530     }
531 }
532
533 impl ToStrRadix for T {
534     #[inline(always)]
535     fn to_str_radix(&self, radix: uint) -> ~str {
536         to_str_radix(*self, radix)
537     }
538 }
539
540 #[cfg(test)]
541 mod tests {
542     use super::*;
543     use super::inst::T;
544     use prelude::*;
545
546     #[test]
547     fn test_num() {
548         num::test_num(10 as T, 2 as T);
549     }
550
551     #[test]
552     fn test_orderable() {
553         assert_eq!((1 as T).min(&(2 as T)), 1 as T);
554         assert_eq!((2 as T).min(&(1 as T)), 1 as T);
555         assert_eq!((1 as T).max(&(2 as T)), 2 as T);
556         assert_eq!((2 as T).max(&(1 as T)), 2 as T);
557         assert_eq!((1 as T).clamp(&(2 as T), &(4 as T)), 2 as T);
558         assert_eq!((8 as T).clamp(&(2 as T), &(4 as T)), 4 as T);
559         assert_eq!((3 as T).clamp(&(2 as T), &(4 as T)), 3 as T);
560     }
561
562     #[test]
563     pub fn test_signed() {
564         assert_eq!((1 as T).abs(), 1 as T);
565         assert_eq!((0 as T).abs(), 0 as T);
566         assert_eq!((-1 as T).abs(), 1 as T);
567
568         assert_eq!((1 as T).signum(), 1 as T);
569         assert_eq!((0 as T).signum(), 0 as T);
570         assert_eq!((-0 as T).signum(), 0 as T);
571         assert_eq!((-1 as T).signum(), -1 as T);
572
573         assert!((1 as T).is_positive());
574         assert!(!(0 as T).is_positive());
575         assert!(!(-0 as T).is_positive());
576         assert!(!(-1 as T).is_positive());
577
578         assert!(!(1 as T).is_negative());
579         assert!(!(0 as T).is_negative());
580         assert!(!(-0 as T).is_negative());
581         assert!((-1 as T).is_negative());
582     }
583
584     ///
585     /// Checks that the division rule holds for:
586     ///
587     /// - `n`: numerator (dividend)
588     /// - `d`: denominator (divisor)
589     /// - `qr`: quotient and remainder
590     ///
591     #[cfg(test)]
592     fn test_division_rule((n,d): (T,T), (q,r): (T,T)) {
593         assert_eq!(d * q + r, n);
594     }
595
596     #[test]
597     fn test_div_rem() {
598         fn test_nd_dr(nd: (T,T), qr: (T,T)) {
599             let (n,d) = nd;
600             let separate_div_rem = (n / d, n % d);
601             let combined_div_rem = n.div_rem(&d);
602
603             assert_eq!(separate_div_rem, qr);
604             assert_eq!(combined_div_rem, qr);
605
606             test_division_rule(nd, separate_div_rem);
607             test_division_rule(nd, combined_div_rem);
608         }
609
610         test_nd_dr(( 8,  3), ( 2,  2));
611         test_nd_dr(( 8, -3), (-2,  2));
612         test_nd_dr((-8,  3), (-2, -2));
613         test_nd_dr((-8, -3), ( 2, -2));
614
615         test_nd_dr(( 1,  2), ( 0,  1));
616         test_nd_dr(( 1, -2), ( 0,  1));
617         test_nd_dr((-1,  2), ( 0, -1));
618         test_nd_dr((-1, -2), ( 0, -1));
619     }
620
621     #[test]
622     fn test_div_mod_floor() {
623         fn test_nd_dm(nd: (T,T), dm: (T,T)) {
624             let (n,d) = nd;
625             let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
626             let combined_div_mod_floor = n.div_mod_floor(&d);
627
628             assert_eq!(separate_div_mod_floor, dm);
629             assert_eq!(combined_div_mod_floor, dm);
630
631             test_division_rule(nd, separate_div_mod_floor);
632             test_division_rule(nd, combined_div_mod_floor);
633         }
634
635         test_nd_dm(( 8,  3), ( 2,  2));
636         test_nd_dm(( 8, -3), (-3, -1));
637         test_nd_dm((-8,  3), (-3,  1));
638         test_nd_dm((-8, -3), ( 2, -2));
639
640         test_nd_dm(( 1,  2), ( 0,  1));
641         test_nd_dm(( 1, -2), (-1, -1));
642         test_nd_dm((-1,  2), (-1,  1));
643         test_nd_dm((-1, -2), ( 0, -1));
644     }
645
646     #[test]
647     fn test_gcd() {
648         assert_eq!((10 as T).gcd(&2), 2 as T);
649         assert_eq!((10 as T).gcd(&3), 1 as T);
650         assert_eq!((0 as T).gcd(&3), 3 as T);
651         assert_eq!((3 as T).gcd(&3), 3 as T);
652         assert_eq!((56 as T).gcd(&42), 14 as T);
653         assert_eq!((3 as T).gcd(&-3), 3 as T);
654         assert_eq!((-6 as T).gcd(&3), 3 as T);
655         assert_eq!((-4 as T).gcd(&-2), 2 as T);
656     }
657
658     #[test]
659     fn test_lcm() {
660         assert_eq!((1 as T).lcm(&0), 0 as T);
661         assert_eq!((0 as T).lcm(&1), 0 as T);
662         assert_eq!((1 as T).lcm(&1), 1 as T);
663         assert_eq!((-1 as T).lcm(&1), 1 as T);
664         assert_eq!((1 as T).lcm(&-1), 1 as T);
665         assert_eq!((-1 as T).lcm(&-1), 1 as T);
666         assert_eq!((8 as T).lcm(&9), 72 as T);
667         assert_eq!((11 as T).lcm(&5), 55 as T);
668     }
669
670     #[test]
671     fn test_bitwise() {
672         assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));
673         assert_eq!(0b1000 as T, (0b1100 as T).bitand(&(0b1010 as T)));
674         assert_eq!(0b0110 as T, (0b1100 as T).bitxor(&(0b1010 as T)));
675         assert_eq!(0b1110 as T, (0b0111 as T).shl(&(1 as T)));
676         assert_eq!(0b0111 as T, (0b1110 as T).shr(&(1 as T)));
677         assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
678     }
679
680     #[test]
681     fn test_multiple_of() {
682         assert!((6 as T).is_multiple_of(&(6 as T)));
683         assert!((6 as T).is_multiple_of(&(3 as T)));
684         assert!((6 as T).is_multiple_of(&(1 as T)));
685         assert!((-8 as T).is_multiple_of(&(4 as T)));
686         assert!((8 as T).is_multiple_of(&(-1 as T)));
687         assert!((-8 as T).is_multiple_of(&(-2 as T)));
688     }
689
690     #[test]
691     fn test_even() {
692         assert_eq!((-4 as T).is_even(), true);
693         assert_eq!((-3 as T).is_even(), false);
694         assert_eq!((-2 as T).is_even(), true);
695         assert_eq!((-1 as T).is_even(), false);
696         assert_eq!((0 as T).is_even(), true);
697         assert_eq!((1 as T).is_even(), false);
698         assert_eq!((2 as T).is_even(), true);
699         assert_eq!((3 as T).is_even(), false);
700         assert_eq!((4 as T).is_even(), true);
701     }
702
703     #[test]
704     fn test_odd() {
705         assert_eq!((-4 as T).is_odd(), false);
706         assert_eq!((-3 as T).is_odd(), true);
707         assert_eq!((-2 as T).is_odd(), false);
708         assert_eq!((-1 as T).is_odd(), true);
709         assert_eq!((0 as T).is_odd(), false);
710         assert_eq!((1 as T).is_odd(), true);
711         assert_eq!((2 as T).is_odd(), false);
712         assert_eq!((3 as T).is_odd(), true);
713         assert_eq!((4 as T).is_odd(), false);
714     }
715
716     #[test]
717     fn test_bitcount() {
718         assert_eq!((0b010101 as T).population_count(), 3);
719     }
720
721     #[test]
722     fn test_primitive() {
723         assert_eq!(Primitive::bits::<T>(), sys::size_of::<T>() * 8);
724         assert_eq!(Primitive::bytes::<T>(), sys::size_of::<T>());
725     }
726
727     #[test]
728     fn test_from_str() {
729         assert_eq!(from_str(~"0"), Some(0 as T));
730         assert_eq!(from_str(~"3"), Some(3 as T));
731         assert_eq!(from_str(~"10"), Some(10 as T));
732         assert_eq!(i32::from_str(~"123456789"), Some(123456789 as i32));
733         assert_eq!(from_str(~"00100"), Some(100 as T));
734
735         assert_eq!(from_str(~"-1"), Some(-1 as T));
736         assert_eq!(from_str(~"-3"), Some(-3 as T));
737         assert_eq!(from_str(~"-10"), Some(-10 as T));
738         assert_eq!(i32::from_str(~"-123456789"), Some(-123456789 as i32));
739         assert_eq!(from_str(~"-00100"), Some(-100 as T));
740
741         assert!(from_str(~" ").is_none());
742         assert!(from_str(~"x").is_none());
743     }
744
745     #[test]
746     fn test_parse_bytes() {
747         use str::to_bytes;
748         assert_eq!(parse_bytes(to_bytes(~"123"), 10u), Some(123 as T));
749         assert_eq!(parse_bytes(to_bytes(~"1001"), 2u), Some(9 as T));
750         assert_eq!(parse_bytes(to_bytes(~"123"), 8u), Some(83 as T));
751         assert_eq!(i32::parse_bytes(to_bytes(~"123"), 16u), Some(291 as i32));
752         assert_eq!(i32::parse_bytes(to_bytes(~"ffff"), 16u), Some(65535 as i32));
753         assert_eq!(i32::parse_bytes(to_bytes(~"FFFF"), 16u), Some(65535 as i32));
754         assert_eq!(parse_bytes(to_bytes(~"z"), 36u), Some(35 as T));
755         assert_eq!(parse_bytes(to_bytes(~"Z"), 36u), Some(35 as T));
756
757         assert_eq!(parse_bytes(to_bytes(~"-123"), 10u), Some(-123 as T));
758         assert_eq!(parse_bytes(to_bytes(~"-1001"), 2u), Some(-9 as T));
759         assert_eq!(parse_bytes(to_bytes(~"-123"), 8u), Some(-83 as T));
760         assert_eq!(i32::parse_bytes(to_bytes(~"-123"), 16u), Some(-291 as i32));
761         assert_eq!(i32::parse_bytes(to_bytes(~"-ffff"), 16u), Some(-65535 as i32));
762         assert_eq!(i32::parse_bytes(to_bytes(~"-FFFF"), 16u), Some(-65535 as i32));
763         assert_eq!(parse_bytes(to_bytes(~"-z"), 36u), Some(-35 as T));
764         assert_eq!(parse_bytes(to_bytes(~"-Z"), 36u), Some(-35 as T));
765
766         assert!(parse_bytes(to_bytes(~"Z"), 35u).is_none());
767         assert!(parse_bytes(to_bytes(~"-9"), 2u).is_none());
768     }
769
770     #[test]
771     fn test_to_str() {
772         assert_eq!(to_str_radix(0 as T, 10u), ~"0");
773         assert_eq!(to_str_radix(1 as T, 10u), ~"1");
774         assert_eq!(to_str_radix(-1 as T, 10u), ~"-1");
775         assert_eq!(to_str_radix(127 as T, 16u), ~"7f");
776         assert_eq!(to_str_radix(100 as T, 10u), ~"100");
777
778     }
779
780     #[test]
781     fn test_int_to_str_overflow() {
782         let mut i8_val: i8 = 127_i8;
783         assert_eq!(i8::to_str(i8_val), ~"127");
784
785         i8_val += 1 as i8;
786         assert_eq!(i8::to_str(i8_val), ~"-128");
787
788         let mut i16_val: i16 = 32_767_i16;
789         assert_eq!(i16::to_str(i16_val), ~"32767");
790
791         i16_val += 1 as i16;
792         assert_eq!(i16::to_str(i16_val), ~"-32768");
793
794         let mut i32_val: i32 = 2_147_483_647_i32;
795         assert_eq!(i32::to_str(i32_val), ~"2147483647");
796
797         i32_val += 1 as i32;
798         assert_eq!(i32::to_str(i32_val), ~"-2147483648");
799
800         let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
801         assert_eq!(i64::to_str(i64_val), ~"9223372036854775807");
802
803         i64_val += 1 as i64;
804         assert_eq!(i64::to_str(i64_val), ~"-9223372036854775808");
805     }
806
807     #[test]
808     fn test_int_from_str_overflow() {
809         let mut i8_val: i8 = 127_i8;
810         assert_eq!(i8::from_str(~"127"), Some(i8_val));
811         assert!(i8::from_str(~"128").is_none());
812
813         i8_val += 1 as i8;
814         assert_eq!(i8::from_str(~"-128"), Some(i8_val));
815         assert!(i8::from_str(~"-129").is_none());
816
817         let mut i16_val: i16 = 32_767_i16;
818         assert_eq!(i16::from_str(~"32767"), Some(i16_val));
819         assert!(i16::from_str(~"32768").is_none());
820
821         i16_val += 1 as i16;
822         assert_eq!(i16::from_str(~"-32768"), Some(i16_val));
823         assert!(i16::from_str(~"-32769").is_none());
824
825         let mut i32_val: i32 = 2_147_483_647_i32;
826         assert_eq!(i32::from_str(~"2147483647"), Some(i32_val));
827         assert!(i32::from_str(~"2147483648").is_none());
828
829         i32_val += 1 as i32;
830         assert_eq!(i32::from_str(~"-2147483648"), Some(i32_val));
831         assert!(i32::from_str(~"-2147483649").is_none());
832
833         let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
834         assert_eq!(i64::from_str(~"9223372036854775807"), Some(i64_val));
835         assert!(i64::from_str(~"9223372036854775808").is_none());
836
837         i64_val += 1 as i64;
838         assert_eq!(i64::from_str(~"-9223372036854775808"), Some(i64_val));
839         assert!(i64::from_str(~"-9223372036854775809").is_none());
840     }
841
842     #[test]
843     fn test_ranges() {
844         let mut l = ~[];
845
846         for range(0,3) |i| {
847             l.push(i);
848         }
849         for range_rev(13,10) |i| {
850             l.push(i);
851         }
852         for range_step(20,26,2) |i| {
853             l.push(i);
854         }
855         for range_step(36,30,-2) |i| {
856             l.push(i);
857         }
858         for range_step(max_value - 2, max_value, 2) |i| {
859             l.push(i);
860         }
861         for range_step(max_value - 3, max_value, 2) |i| {
862             l.push(i);
863         }
864         for range_step(min_value + 2, min_value, -2) |i| {
865             l.push(i);
866         }
867         for range_step(min_value + 3, min_value, -2) |i| {
868             l.push(i);
869         }
870         assert_eq!(l, ~[0,1,2,
871                         13,12,11,
872                         20,22,24,
873                         36,34,32,
874                         max_value-2,
875                         max_value-3,max_value-1,
876                         min_value+2,
877                         min_value+3,min_value+1]);
878
879         // None of the `fail`s should execute.
880         for range(10,0) |_i| {
881             fail!(~"unreachable");
882         }
883         for range_rev(0,10) |_i| {
884             fail!(~"unreachable");
885         }
886         for range_step(10,0,1) |_i| {
887             fail!(~"unreachable");
888         }
889         for range_step(0,10,-1) |_i| {
890             fail!(~"unreachable");
891         }
892     }
893
894     #[test]
895     #[should_fail]
896     #[ignore(cfg(windows))]
897     fn test_range_step_zero_step() {
898         for range_step(0,10,0) |_i| {}
899     }
900 }