integer.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright 2013-2014 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. //! Integer trait and functions.
  11. use {Num, Signed};
  12. pub trait Integer
  13. : Sized + Num + Ord
  14. {
  15. /// Floored integer division.
  16. ///
  17. /// # Examples
  18. ///
  19. /// ~~~
  20. /// # use num::Integer;
  21. /// assert!(( 8).div_floor(& 3) == 2);
  22. /// assert!(( 8).div_floor(&-3) == -3);
  23. /// assert!((-8).div_floor(& 3) == -3);
  24. /// assert!((-8).div_floor(&-3) == 2);
  25. ///
  26. /// assert!(( 1).div_floor(& 2) == 0);
  27. /// assert!(( 1).div_floor(&-2) == -1);
  28. /// assert!((-1).div_floor(& 2) == -1);
  29. /// assert!((-1).div_floor(&-2) == 0);
  30. /// ~~~
  31. fn div_floor(&self, other: &Self) -> Self;
  32. /// Floored integer modulo, satisfying:
  33. ///
  34. /// ~~~
  35. /// # use num::Integer;
  36. /// # let n = 1; let d = 1;
  37. /// assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)
  38. /// ~~~
  39. ///
  40. /// # Examples
  41. ///
  42. /// ~~~
  43. /// # use num::Integer;
  44. /// assert!(( 8).mod_floor(& 3) == 2);
  45. /// assert!(( 8).mod_floor(&-3) == -1);
  46. /// assert!((-8).mod_floor(& 3) == 1);
  47. /// assert!((-8).mod_floor(&-3) == -2);
  48. ///
  49. /// assert!(( 1).mod_floor(& 2) == 1);
  50. /// assert!(( 1).mod_floor(&-2) == -1);
  51. /// assert!((-1).mod_floor(& 2) == 1);
  52. /// assert!((-1).mod_floor(&-2) == -1);
  53. /// ~~~
  54. fn mod_floor(&self, other: &Self) -> Self;
  55. /// Greatest Common Divisor (GCD).
  56. ///
  57. /// # Examples
  58. ///
  59. /// ~~~
  60. /// # use num::Integer;
  61. /// assert_eq!(6.gcd(&8), 2);
  62. /// assert_eq!(7.gcd(&3), 1);
  63. /// ~~~
  64. fn gcd(&self, other: &Self) -> Self;
  65. /// Lowest Common Multiple (LCM).
  66. ///
  67. /// # Examples
  68. ///
  69. /// ~~~
  70. /// # use num::Integer;
  71. /// assert_eq!(7.lcm(&3), 21);
  72. /// assert_eq!(2.lcm(&4), 4);
  73. /// ~~~
  74. fn lcm(&self, other: &Self) -> Self;
  75. /// Deprecated, use `is_multiple_of` instead.
  76. fn divides(&self, other: &Self) -> bool;
  77. /// Returns `true` if `other` is a multiple of `self`.
  78. ///
  79. /// # Examples
  80. ///
  81. /// ~~~
  82. /// # use num::Integer;
  83. /// assert_eq!(9.is_multiple_of(&3), true);
  84. /// assert_eq!(3.is_multiple_of(&9), false);
  85. /// ~~~
  86. fn is_multiple_of(&self, other: &Self) -> bool;
  87. /// Returns `true` if the number is even.
  88. ///
  89. /// # Examples
  90. ///
  91. /// ~~~
  92. /// # use num::Integer;
  93. /// assert_eq!(3.is_even(), false);
  94. /// assert_eq!(4.is_even(), true);
  95. /// ~~~
  96. fn is_even(&self) -> bool;
  97. /// Returns `true` if the number is odd.
  98. ///
  99. /// # Examples
  100. ///
  101. /// ~~~
  102. /// # use num::Integer;
  103. /// assert_eq!(3.is_odd(), true);
  104. /// assert_eq!(4.is_odd(), false);
  105. /// ~~~
  106. fn is_odd(&self) -> bool;
  107. /// Simultaneous truncated integer division and modulus.
  108. /// Returns `(quotient, remainder)`.
  109. ///
  110. /// # Examples
  111. ///
  112. /// ~~~
  113. /// # use num::Integer;
  114. /// assert_eq!(( 8).div_rem( &3), ( 2, 2));
  115. /// assert_eq!(( 8).div_rem(&-3), (-2, 2));
  116. /// assert_eq!((-8).div_rem( &3), (-2, -2));
  117. /// assert_eq!((-8).div_rem(&-3), ( 2, -2));
  118. ///
  119. /// assert_eq!(( 1).div_rem( &2), ( 0, 1));
  120. /// assert_eq!(( 1).div_rem(&-2), ( 0, 1));
  121. /// assert_eq!((-1).div_rem( &2), ( 0, -1));
  122. /// assert_eq!((-1).div_rem(&-2), ( 0, -1));
  123. /// ~~~
  124. #[inline]
  125. fn div_rem(&self, other: &Self) -> (Self, Self);
  126. /// Simultaneous floored integer division and modulus.
  127. /// Returns `(quotient, remainder)`.
  128. ///
  129. /// # Examples
  130. ///
  131. /// ~~~
  132. /// # use num::Integer;
  133. /// assert_eq!(( 8).div_mod_floor( &3), ( 2, 2));
  134. /// assert_eq!(( 8).div_mod_floor(&-3), (-3, -1));
  135. /// assert_eq!((-8).div_mod_floor( &3), (-3, 1));
  136. /// assert_eq!((-8).div_mod_floor(&-3), ( 2, -2));
  137. ///
  138. /// assert_eq!(( 1).div_mod_floor( &2), ( 0, 1));
  139. /// assert_eq!(( 1).div_mod_floor(&-2), (-1, -1));
  140. /// assert_eq!((-1).div_mod_floor( &2), (-1, 1));
  141. /// assert_eq!((-1).div_mod_floor(&-2), ( 0, -1));
  142. /// ~~~
  143. fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
  144. (self.div_floor(other), self.mod_floor(other))
  145. }
  146. }
  147. /// Simultaneous integer division and modulus
  148. #[inline] pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) { x.div_rem(&y) }
  149. /// Floored integer division
  150. #[inline] pub fn div_floor<T: Integer>(x: T, y: T) -> T { x.div_floor(&y) }
  151. /// Floored integer modulus
  152. #[inline] pub fn mod_floor<T: Integer>(x: T, y: T) -> T { x.mod_floor(&y) }
  153. /// Simultaneous floored integer division and modulus
  154. #[inline] pub fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) { x.div_mod_floor(&y) }
  155. /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The
  156. /// result is always positive.
  157. #[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
  158. /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
  159. #[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
  160. macro_rules! impl_integer_for_isize {
  161. ($T:ty, $test_mod:ident) => (
  162. impl Integer for $T {
  163. /// Floored integer division
  164. #[inline]
  165. fn div_floor(&self, other: &$T) -> $T {
  166. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  167. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  168. match self.div_rem(other) {
  169. (d, r) if (r > 0 && *other < 0)
  170. || (r < 0 && *other > 0) => d - 1,
  171. (d, _) => d,
  172. }
  173. }
  174. /// Floored integer modulo
  175. #[inline]
  176. fn mod_floor(&self, other: &$T) -> $T {
  177. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  178. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  179. match *self % *other {
  180. r if (r > 0 && *other < 0)
  181. || (r < 0 && *other > 0) => r + *other,
  182. r => r,
  183. }
  184. }
  185. /// Calculates `div_floor` and `mod_floor` simultaneously
  186. #[inline]
  187. fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
  188. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  189. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  190. match self.div_rem(other) {
  191. (d, r) if (r > 0 && *other < 0)
  192. || (r < 0 && *other > 0) => (d - 1, r + *other),
  193. (d, r) => (d, r),
  194. }
  195. }
  196. /// Calculates the Greatest Common Divisor (GCD) of the number and
  197. /// `other`. The result is always positive.
  198. #[inline]
  199. fn gcd(&self, other: &$T) -> $T {
  200. // Use Stein's algorithm
  201. let mut m = *self;
  202. let mut n = *other;
  203. if m == 0 || n == 0 { return (m | n).abs() }
  204. // find common factors of 2
  205. let shift = (m | n).trailing_zeros();
  206. // The algorithm needs positive numbers, but the minimum value
  207. // can't be represented as a positive one.
  208. // It's also a power of two, so the gcd can be
  209. // calculated by bitshifting in that case
  210. // Assuming two's complement, the number created by the shift
  211. // is positive for all numbers except gcd = abs(min value)
  212. // The call to .abs() causes a panic in debug mode
  213. if m == <$T>::min_value() || n == <$T>::min_value() {
  214. return (1 << shift).abs()
  215. }
  216. // guaranteed to be positive now, rest like unsigned algorithm
  217. m = m.abs();
  218. n = n.abs();
  219. // divide n and m by 2 until odd
  220. // m inside loop
  221. n >>= n.trailing_zeros();
  222. while m != 0 {
  223. m >>= m.trailing_zeros();
  224. if n > m { ::std::mem::swap(&mut n, &mut m) }
  225. m -= n;
  226. }
  227. n << shift
  228. }
  229. /// Calculates the Lowest Common Multiple (LCM) of the number and
  230. /// `other`.
  231. #[inline]
  232. fn lcm(&self, other: &$T) -> $T {
  233. // should not have to recalculate abs
  234. (*self * (*other / self.gcd(other))).abs()
  235. }
  236. /// Deprecated, use `is_multiple_of` instead.
  237. #[inline]
  238. fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
  239. /// Returns `true` if the number is a multiple of `other`.
  240. #[inline]
  241. fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
  242. /// Returns `true` if the number is divisible by `2`
  243. #[inline]
  244. fn is_even(&self) -> bool { (*self) & 1 == 0 }
  245. /// Returns `true` if the number is not divisible by `2`
  246. #[inline]
  247. fn is_odd(&self) -> bool { !self.is_even() }
  248. /// Simultaneous truncated integer division and modulus.
  249. #[inline]
  250. fn div_rem(&self, other: &$T) -> ($T, $T) {
  251. (*self / *other, *self % *other)
  252. }
  253. }
  254. #[cfg(test)]
  255. mod $test_mod {
  256. use Integer;
  257. /// Checks that the division rule holds for:
  258. ///
  259. /// - `n`: numerator (dividend)
  260. /// - `d`: denominator (divisor)
  261. /// - `qr`: quotient and remainder
  262. #[cfg(test)]
  263. fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
  264. assert_eq!(d * q + r, n);
  265. }
  266. #[test]
  267. fn test_div_rem() {
  268. fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) {
  269. let (n,d) = nd;
  270. let separate_div_rem = (n / d, n % d);
  271. let combined_div_rem = n.div_rem(&d);
  272. assert_eq!(separate_div_rem, qr);
  273. assert_eq!(combined_div_rem, qr);
  274. test_division_rule(nd, separate_div_rem);
  275. test_division_rule(nd, combined_div_rem);
  276. }
  277. test_nd_dr(( 8, 3), ( 2, 2));
  278. test_nd_dr(( 8, -3), (-2, 2));
  279. test_nd_dr((-8, 3), (-2, -2));
  280. test_nd_dr((-8, -3), ( 2, -2));
  281. test_nd_dr(( 1, 2), ( 0, 1));
  282. test_nd_dr(( 1, -2), ( 0, 1));
  283. test_nd_dr((-1, 2), ( 0, -1));
  284. test_nd_dr((-1, -2), ( 0, -1));
  285. }
  286. #[test]
  287. fn test_div_mod_floor() {
  288. fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) {
  289. let (n,d) = nd;
  290. let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
  291. let combined_div_mod_floor = n.div_mod_floor(&d);
  292. assert_eq!(separate_div_mod_floor, dm);
  293. assert_eq!(combined_div_mod_floor, dm);
  294. test_division_rule(nd, separate_div_mod_floor);
  295. test_division_rule(nd, combined_div_mod_floor);
  296. }
  297. test_nd_dm(( 8, 3), ( 2, 2));
  298. test_nd_dm(( 8, -3), (-3, -1));
  299. test_nd_dm((-8, 3), (-3, 1));
  300. test_nd_dm((-8, -3), ( 2, -2));
  301. test_nd_dm(( 1, 2), ( 0, 1));
  302. test_nd_dm(( 1, -2), (-1, -1));
  303. test_nd_dm((-1, 2), (-1, 1));
  304. test_nd_dm((-1, -2), ( 0, -1));
  305. }
  306. #[test]
  307. fn test_gcd() {
  308. assert_eq!((10 as $T).gcd(&2), 2 as $T);
  309. assert_eq!((10 as $T).gcd(&3), 1 as $T);
  310. assert_eq!((0 as $T).gcd(&3), 3 as $T);
  311. assert_eq!((3 as $T).gcd(&3), 3 as $T);
  312. assert_eq!((56 as $T).gcd(&42), 14 as $T);
  313. assert_eq!((3 as $T).gcd(&-3), 3 as $T);
  314. assert_eq!((-6 as $T).gcd(&3), 3 as $T);
  315. assert_eq!((-4 as $T).gcd(&-2), 2 as $T);
  316. }
  317. #[test]
  318. fn test_gcd_cmp_with_euclidean() {
  319. fn euclidean_gcd(mut m: $T, mut n: $T) -> $T {
  320. while m != 0 {
  321. ::std::mem::swap(&mut m, &mut n);
  322. m %= n;
  323. }
  324. n.abs()
  325. }
  326. // gcd(-128, b) = 128 is not representable as positive value
  327. // for i8
  328. for i in -127..127 {
  329. for j in -127..127 {
  330. assert_eq!(euclidean_gcd(i,j), i.gcd(&j));
  331. }
  332. }
  333. // last value
  334. // FIXME: Use inclusive ranges for above loop when implemented
  335. let i = 127;
  336. for j in -127..127 {
  337. assert_eq!(euclidean_gcd(i,j), i.gcd(&j));
  338. }
  339. assert_eq!(127.gcd(&127), 127);
  340. }
  341. #[test]
  342. fn test_gcd_min_val() {
  343. let min = <$T>::min_value();
  344. let max = <$T>::max_value();
  345. let max_pow2 = max / 2 + 1;
  346. assert_eq!(min.gcd(&max), 1 as $T);
  347. assert_eq!(max.gcd(&min), 1 as $T);
  348. assert_eq!(min.gcd(&max_pow2), max_pow2);
  349. assert_eq!(max_pow2.gcd(&min), max_pow2);
  350. assert_eq!(min.gcd(&42), 2 as $T);
  351. assert_eq!((42 as $T).gcd(&min), 2 as $T);
  352. }
  353. #[test]
  354. #[should_panic]
  355. fn test_gcd_min_val_min_val() {
  356. let min = <$T>::min_value();
  357. assert!(min.gcd(&min) >= 0);
  358. }
  359. #[test]
  360. #[should_panic]
  361. fn test_gcd_min_val_0() {
  362. let min = <$T>::min_value();
  363. assert!(min.gcd(&0) >= 0);
  364. }
  365. #[test]
  366. #[should_panic]
  367. fn test_gcd_0_min_val() {
  368. let min = <$T>::min_value();
  369. assert!((0 as $T).gcd(&min) >= 0);
  370. }
  371. #[test]
  372. fn test_lcm() {
  373. assert_eq!((1 as $T).lcm(&0), 0 as $T);
  374. assert_eq!((0 as $T).lcm(&1), 0 as $T);
  375. assert_eq!((1 as $T).lcm(&1), 1 as $T);
  376. assert_eq!((-1 as $T).lcm(&1), 1 as $T);
  377. assert_eq!((1 as $T).lcm(&-1), 1 as $T);
  378. assert_eq!((-1 as $T).lcm(&-1), 1 as $T);
  379. assert_eq!((8 as $T).lcm(&9), 72 as $T);
  380. assert_eq!((11 as $T).lcm(&5), 55 as $T);
  381. }
  382. #[test]
  383. fn test_even() {
  384. assert_eq!((-4 as $T).is_even(), true);
  385. assert_eq!((-3 as $T).is_even(), false);
  386. assert_eq!((-2 as $T).is_even(), true);
  387. assert_eq!((-1 as $T).is_even(), false);
  388. assert_eq!((0 as $T).is_even(), true);
  389. assert_eq!((1 as $T).is_even(), false);
  390. assert_eq!((2 as $T).is_even(), true);
  391. assert_eq!((3 as $T).is_even(), false);
  392. assert_eq!((4 as $T).is_even(), true);
  393. }
  394. #[test]
  395. fn test_odd() {
  396. assert_eq!((-4 as $T).is_odd(), false);
  397. assert_eq!((-3 as $T).is_odd(), true);
  398. assert_eq!((-2 as $T).is_odd(), false);
  399. assert_eq!((-1 as $T).is_odd(), true);
  400. assert_eq!((0 as $T).is_odd(), false);
  401. assert_eq!((1 as $T).is_odd(), true);
  402. assert_eq!((2 as $T).is_odd(), false);
  403. assert_eq!((3 as $T).is_odd(), true);
  404. assert_eq!((4 as $T).is_odd(), false);
  405. }
  406. }
  407. )
  408. }
  409. impl_integer_for_isize!(i8, test_integer_i8);
  410. impl_integer_for_isize!(i16, test_integer_i16);
  411. impl_integer_for_isize!(i32, test_integer_i32);
  412. impl_integer_for_isize!(i64, test_integer_i64);
  413. impl_integer_for_isize!(isize, test_integer_isize);
  414. macro_rules! impl_integer_for_usize {
  415. ($T:ty, $test_mod:ident) => (
  416. impl Integer for $T {
  417. /// Unsigned integer division. Returns the same result as `div` (`/`).
  418. #[inline]
  419. fn div_floor(&self, other: &$T) -> $T { *self / *other }
  420. /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
  421. #[inline]
  422. fn mod_floor(&self, other: &$T) -> $T { *self % *other }
  423. /// Calculates the Greatest Common Divisor (GCD) of the number and `other`
  424. #[inline]
  425. fn gcd(&self, other: &$T) -> $T {
  426. // Use Stein's algorithm
  427. let mut m = *self;
  428. let mut n = *other;
  429. if m == 0 || n == 0 { return m | n }
  430. // find common factors of 2
  431. let shift = (m | n).trailing_zeros();
  432. // divide n and m by 2 until odd
  433. // m inside loop
  434. n >>= n.trailing_zeros();
  435. while m != 0 {
  436. m >>= m.trailing_zeros();
  437. if n > m { ::std::mem::swap(&mut n, &mut m) }
  438. m -= n;
  439. }
  440. n << shift
  441. }
  442. /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
  443. #[inline]
  444. fn lcm(&self, other: &$T) -> $T {
  445. *self * (*other / self.gcd(other))
  446. }
  447. /// Deprecated, use `is_multiple_of` instead.
  448. #[inline]
  449. fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
  450. /// Returns `true` if the number is a multiple of `other`.
  451. #[inline]
  452. fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
  453. /// Returns `true` if the number is divisible by `2`.
  454. #[inline]
  455. fn is_even(&self) -> bool { (*self) & 1 == 0 }
  456. /// Returns `true` if the number is not divisible by `2`.
  457. #[inline]
  458. fn is_odd(&self) -> bool { !(*self).is_even() }
  459. /// Simultaneous truncated integer division and modulus.
  460. #[inline]
  461. fn div_rem(&self, other: &$T) -> ($T, $T) {
  462. (*self / *other, *self % *other)
  463. }
  464. }
  465. #[cfg(test)]
  466. mod $test_mod {
  467. use Integer;
  468. #[test]
  469. fn test_div_mod_floor() {
  470. assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T);
  471. assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T);
  472. assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T));
  473. assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T);
  474. assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T);
  475. assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T));
  476. assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T);
  477. assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T);
  478. assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T));
  479. }
  480. #[test]
  481. fn test_gcd() {
  482. assert_eq!((10 as $T).gcd(&2), 2 as $T);
  483. assert_eq!((10 as $T).gcd(&3), 1 as $T);
  484. assert_eq!((0 as $T).gcd(&3), 3 as $T);
  485. assert_eq!((3 as $T).gcd(&3), 3 as $T);
  486. assert_eq!((56 as $T).gcd(&42), 14 as $T);
  487. }
  488. #[test]
  489. fn test_gcd_cmp_with_euclidean() {
  490. fn euclidean_gcd(mut m: $T, mut n: $T) -> $T {
  491. while m != 0 {
  492. ::std::mem::swap(&mut m, &mut n);
  493. m %= n;
  494. }
  495. n
  496. }
  497. for i in 0..255 {
  498. for j in 0..255 {
  499. assert_eq!(euclidean_gcd(i,j), i.gcd(&j));
  500. }
  501. }
  502. // last value
  503. // FIXME: Use inclusive ranges for above loop when implemented
  504. let i = 255;
  505. for j in 0..255 {
  506. assert_eq!(euclidean_gcd(i,j), i.gcd(&j));
  507. }
  508. assert_eq!(255.gcd(&255), 255);
  509. }
  510. #[test]
  511. fn test_lcm() {
  512. assert_eq!((1 as $T).lcm(&0), 0 as $T);
  513. assert_eq!((0 as $T).lcm(&1), 0 as $T);
  514. assert_eq!((1 as $T).lcm(&1), 1 as $T);
  515. assert_eq!((8 as $T).lcm(&9), 72 as $T);
  516. assert_eq!((11 as $T).lcm(&5), 55 as $T);
  517. assert_eq!((15 as $T).lcm(&17), 255 as $T);
  518. }
  519. #[test]
  520. fn test_is_multiple_of() {
  521. assert!((6 as $T).is_multiple_of(&(6 as $T)));
  522. assert!((6 as $T).is_multiple_of(&(3 as $T)));
  523. assert!((6 as $T).is_multiple_of(&(1 as $T)));
  524. }
  525. #[test]
  526. fn test_even() {
  527. assert_eq!((0 as $T).is_even(), true);
  528. assert_eq!((1 as $T).is_even(), false);
  529. assert_eq!((2 as $T).is_even(), true);
  530. assert_eq!((3 as $T).is_even(), false);
  531. assert_eq!((4 as $T).is_even(), true);
  532. }
  533. #[test]
  534. fn test_odd() {
  535. assert_eq!((0 as $T).is_odd(), false);
  536. assert_eq!((1 as $T).is_odd(), true);
  537. assert_eq!((2 as $T).is_odd(), false);
  538. assert_eq!((3 as $T).is_odd(), true);
  539. assert_eq!((4 as $T).is_odd(), false);
  540. }
  541. }
  542. )
  543. }
  544. impl_integer_for_usize!(u8, test_integer_u8);
  545. impl_integer_for_usize!(u16, test_integer_u16);
  546. impl_integer_for_usize!(u32, test_integer_u32);
  547. impl_integer_for_usize!(u64, test_integer_u64);
  548. impl_integer_for_usize!(usize, test_integer_usize);
  549. #[test]
  550. fn test_lcm_overflow() {
  551. macro_rules! check {
  552. ($t:ty, $x:expr, $y:expr, $r:expr) => { {
  553. let x: $t = $x;
  554. let y: $t = $y;
  555. let o = x.checked_mul(y);
  556. assert!(o.is_none(),
  557. "sanity checking that {} input {} * {} overflows",
  558. stringify!($t), x, y);
  559. assert_eq!(x.lcm(&y), $r);
  560. assert_eq!(y.lcm(&x), $r);
  561. } }
  562. }
  563. // Original bug (Issue #166)
  564. check!(i64, 46656000000000000, 600, 46656000000000000);
  565. check!(i8, 0x40, 0x04, 0x40);
  566. check!(u8, 0x80, 0x02, 0x80);
  567. check!(i16, 0x40_00, 0x04, 0x40_00);
  568. check!(u16, 0x80_00, 0x02, 0x80_00);
  569. check!(i32, 0x4000_0000, 0x04, 0x4000_0000);
  570. check!(u32, 0x8000_0000, 0x02, 0x8000_0000);
  571. check!(i64, 0x4000_0000_0000_0000, 0x04, 0x4000_0000_0000_0000);
  572. check!(u64, 0x8000_0000_0000_0000, 0x02, 0x8000_0000_0000_0000);
  573. }