lib.rs 23 KB

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