bigint.rs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. use {BigDigit, BigUint, big_digit};
  2. use {Sign, BigInt, RandBigInt, ToBigInt};
  3. use Sign::{Minus, NoSign, Plus};
  4. use std::cmp::Ordering::{Less, Equal, Greater};
  5. use std::{f32, f64};
  6. use std::{i8, i16, i32, i64, isize};
  7. use std::iter::repeat;
  8. use std::{u8, u16, u32, u64, usize};
  9. use std::ops::Neg;
  10. use rand::thread_rng;
  11. use integer::Integer;
  12. use traits::{Zero, One, Signed, ToPrimitive, FromPrimitive, Num, Float};
  13. /// Assert that an op works for all val/ref combinations
  14. macro_rules! assert_op {
  15. ($left:ident $op:tt $right:ident == $expected:expr) => {
  16. assert_eq!((&$left) $op (&$right), $expected);
  17. assert_eq!((&$left) $op $right.clone(), $expected);
  18. assert_eq!($left.clone() $op (&$right), $expected);
  19. assert_eq!($left.clone() $op $right.clone(), $expected);
  20. };
  21. }
  22. #[test]
  23. fn test_from_biguint() {
  24. fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {
  25. let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_usize(inp_n).unwrap());
  26. let ans = BigInt {
  27. sign: ans_s,
  28. data: FromPrimitive::from_usize(ans_n).unwrap(),
  29. };
  30. assert_eq!(inp, ans);
  31. }
  32. check(Plus, 1, Plus, 1);
  33. check(Plus, 0, NoSign, 0);
  34. check(Minus, 1, Minus, 1);
  35. check(NoSign, 1, NoSign, 0);
  36. }
  37. #[test]
  38. fn test_from_bytes_be() {
  39. fn check(s: &str, result: &str) {
  40. assert_eq!(BigInt::from_bytes_be(Plus, s.as_bytes()),
  41. BigInt::parse_bytes(result.as_bytes(), 10).unwrap());
  42. }
  43. check("A", "65");
  44. check("AA", "16705");
  45. check("AB", "16706");
  46. check("Hello world!", "22405534230753963835153736737");
  47. assert_eq!(BigInt::from_bytes_be(Plus, &[]), Zero::zero());
  48. assert_eq!(BigInt::from_bytes_be(Minus, &[]), Zero::zero());
  49. }
  50. #[test]
  51. fn test_to_bytes_be() {
  52. fn check(s: &str, result: &str) {
  53. let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
  54. let (sign, v) = b.to_bytes_be();
  55. assert_eq!((Plus, s.as_bytes()), (sign, &*v));
  56. }
  57. check("A", "65");
  58. check("AA", "16705");
  59. check("AB", "16706");
  60. check("Hello world!", "22405534230753963835153736737");
  61. let b: BigInt = Zero::zero();
  62. assert_eq!(b.to_bytes_be(), (NoSign, vec![0]));
  63. // Test with leading/trailing zero bytes and a full BigDigit of value 0
  64. let b = BigInt::from_str_radix("00010000000000000200", 16).unwrap();
  65. assert_eq!(b.to_bytes_be(), (Plus, vec![1, 0, 0, 0, 0, 0, 0, 2, 0]));
  66. }
  67. #[test]
  68. fn test_from_bytes_le() {
  69. fn check(s: &str, result: &str) {
  70. assert_eq!(BigInt::from_bytes_le(Plus, s.as_bytes()),
  71. BigInt::parse_bytes(result.as_bytes(), 10).unwrap());
  72. }
  73. check("A", "65");
  74. check("AA", "16705");
  75. check("BA", "16706");
  76. check("!dlrow olleH", "22405534230753963835153736737");
  77. assert_eq!(BigInt::from_bytes_le(Plus, &[]), Zero::zero());
  78. assert_eq!(BigInt::from_bytes_le(Minus, &[]), Zero::zero());
  79. }
  80. #[test]
  81. fn test_to_bytes_le() {
  82. fn check(s: &str, result: &str) {
  83. let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
  84. let (sign, v) = b.to_bytes_le();
  85. assert_eq!((Plus, s.as_bytes()), (sign, &*v));
  86. }
  87. check("A", "65");
  88. check("AA", "16705");
  89. check("BA", "16706");
  90. check("!dlrow olleH", "22405534230753963835153736737");
  91. let b: BigInt = Zero::zero();
  92. assert_eq!(b.to_bytes_le(), (NoSign, vec![0]));
  93. // Test with leading/trailing zero bytes and a full BigDigit of value 0
  94. let b = BigInt::from_str_radix("00010000000000000200", 16).unwrap();
  95. assert_eq!(b.to_bytes_le(), (Plus, vec![0, 2, 0, 0, 0, 0, 0, 0, 1]));
  96. }
  97. #[test]
  98. fn test_to_signed_bytes_le() {
  99. fn check(s: &str, result: Vec<u8>) {
  100. assert_eq!(BigInt::parse_bytes(s.as_bytes(), 10).unwrap().to_signed_bytes_le(),
  101. result);
  102. }
  103. check("0", vec![0]);
  104. check("32767", vec![0xff, 0x7f]);
  105. check("-1", vec![0xff]);
  106. check("16777216", vec![0, 0, 0, 1]);
  107. check("-100", vec![156]);
  108. check("-8388608", vec![0, 0, 0x80]);
  109. check("-192", vec![0x40, 0xff]);
  110. }
  111. #[test]
  112. fn test_from_signed_bytes_le() {
  113. fn check(s: &[u8], result: &str) {
  114. assert_eq!(BigInt::from_signed_bytes_le(s),
  115. BigInt::parse_bytes(result.as_bytes(), 10).unwrap());
  116. }
  117. check(&[], "0");
  118. check(&[0], "0");
  119. check(&[0; 10], "0");
  120. check(&[0xff, 0x7f], "32767");
  121. check(&[0xff], "-1");
  122. check(&[0, 0, 0, 1], "16777216");
  123. check(&[156], "-100");
  124. check(&[0, 0, 0x80], "-8388608");
  125. check(&[0xff; 10], "-1");
  126. check(&[0x40, 0xff], "-192");
  127. }
  128. #[test]
  129. fn test_to_signed_bytes_be() {
  130. fn check(s: &str, result: Vec<u8>) {
  131. assert_eq!(BigInt::parse_bytes(s.as_bytes(), 10).unwrap().to_signed_bytes_be(),
  132. result);
  133. }
  134. check("0", vec![0]);
  135. check("32767", vec![0x7f, 0xff]);
  136. check("-1", vec![255]);
  137. check("16777216", vec![1, 0, 0, 0]);
  138. check("-100", vec![156]);
  139. check("-8388608", vec![128, 0, 0]);
  140. check("-192", vec![0xff, 0x40]);
  141. }
  142. #[test]
  143. fn test_from_signed_bytes_be() {
  144. fn check(s: &[u8], result: &str) {
  145. assert_eq!(BigInt::from_signed_bytes_be(s),
  146. BigInt::parse_bytes(result.as_bytes(), 10).unwrap());
  147. }
  148. check(&[], "0");
  149. check(&[0], "0");
  150. check(&[0; 10], "0");
  151. check(&[127, 255], "32767");
  152. check(&[255], "-1");
  153. check(&[1, 0, 0, 0], "16777216");
  154. check(&[156], "-100");
  155. check(&[128, 0, 0], "-8388608");
  156. check(&[255; 10], "-1");
  157. check(&[0xff, 0x40], "-192");
  158. }
  159. #[test]
  160. fn test_cmp() {
  161. let vs: [&[BigDigit]; 4] = [&[2 as BigDigit], &[1, 1], &[2, 1], &[1, 1, 1]];
  162. let mut nums = Vec::new();
  163. for s in vs.iter().rev() {
  164. nums.push(BigInt::from_slice(Minus, *s));
  165. }
  166. nums.push(Zero::zero());
  167. nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, *s)));
  168. for (i, ni) in nums.iter().enumerate() {
  169. for (j0, nj) in nums[i..].iter().enumerate() {
  170. let j = i + j0;
  171. if i == j {
  172. assert_eq!(ni.cmp(nj), Equal);
  173. assert_eq!(nj.cmp(ni), Equal);
  174. assert_eq!(ni, nj);
  175. assert!(!(ni != nj));
  176. assert!(ni <= nj);
  177. assert!(ni >= nj);
  178. assert!(!(ni < nj));
  179. assert!(!(ni > nj));
  180. } else {
  181. assert_eq!(ni.cmp(nj), Less);
  182. assert_eq!(nj.cmp(ni), Greater);
  183. assert!(!(ni == nj));
  184. assert!(ni != nj);
  185. assert!(ni <= nj);
  186. assert!(!(ni >= nj));
  187. assert!(ni < nj);
  188. assert!(!(ni > nj));
  189. assert!(!(nj <= ni));
  190. assert!(nj >= ni);
  191. assert!(!(nj < ni));
  192. assert!(nj > ni);
  193. }
  194. }
  195. }
  196. }
  197. #[test]
  198. fn test_hash() {
  199. use hash;
  200. let a = BigInt::new(NoSign, vec![]);
  201. let b = BigInt::new(NoSign, vec![0]);
  202. let c = BigInt::new(Plus, vec![1]);
  203. let d = BigInt::new(Plus, vec![1, 0, 0, 0, 0, 0]);
  204. let e = BigInt::new(Plus, vec![0, 0, 0, 0, 0, 1]);
  205. let f = BigInt::new(Minus, vec![1]);
  206. assert!(hash(&a) == hash(&b));
  207. assert!(hash(&b) != hash(&c));
  208. assert!(hash(&c) == hash(&d));
  209. assert!(hash(&d) != hash(&e));
  210. assert!(hash(&c) != hash(&f));
  211. }
  212. #[test]
  213. fn test_convert_i64() {
  214. fn check(b1: BigInt, i: i64) {
  215. let b2: BigInt = FromPrimitive::from_i64(i).unwrap();
  216. assert!(b1 == b2);
  217. assert!(b1.to_i64().unwrap() == i);
  218. }
  219. check(Zero::zero(), 0);
  220. check(One::one(), 1);
  221. check(i64::MIN.to_bigint().unwrap(), i64::MIN);
  222. check(i64::MAX.to_bigint().unwrap(), i64::MAX);
  223. assert_eq!((i64::MAX as u64 + 1).to_bigint().unwrap().to_i64(), None);
  224. assert_eq!(BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i64(),
  225. None);
  226. assert_eq!(BigInt::from_biguint(Minus,
  227. BigUint::new(vec![1, 0, 0, 1 << (big_digit::BITS - 1)]))
  228. .to_i64(),
  229. None);
  230. assert_eq!(BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i64(),
  231. None);
  232. }
  233. #[test]
  234. fn test_convert_u64() {
  235. fn check(b1: BigInt, u: u64) {
  236. let b2: BigInt = FromPrimitive::from_u64(u).unwrap();
  237. assert!(b1 == b2);
  238. assert!(b1.to_u64().unwrap() == u);
  239. }
  240. check(Zero::zero(), 0);
  241. check(One::one(), 1);
  242. check(u64::MIN.to_bigint().unwrap(), u64::MIN);
  243. check(u64::MAX.to_bigint().unwrap(), u64::MAX);
  244. assert_eq!(BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u64(),
  245. None);
  246. let max_value: BigUint = FromPrimitive::from_u64(u64::MAX).unwrap();
  247. assert_eq!(BigInt::from_biguint(Minus, max_value).to_u64(), None);
  248. assert_eq!(BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u64(),
  249. None);
  250. }
  251. #[test]
  252. fn test_convert_f32() {
  253. fn check(b1: &BigInt, f: f32) {
  254. let b2 = BigInt::from_f32(f).unwrap();
  255. assert_eq!(b1, &b2);
  256. assert_eq!(b1.to_f32().unwrap(), f);
  257. let neg_b1 = -b1;
  258. let neg_b2 = BigInt::from_f32(-f).unwrap();
  259. assert_eq!(neg_b1, neg_b2);
  260. assert_eq!(neg_b1.to_f32().unwrap(), -f);
  261. }
  262. check(&BigInt::zero(), 0.0);
  263. check(&BigInt::one(), 1.0);
  264. check(&BigInt::from(u16::MAX), 2.0.powi(16) - 1.0);
  265. check(&BigInt::from(1u64 << 32), 2.0.powi(32));
  266. check(&BigInt::from_slice(Plus, &[0, 0, 1]), 2.0.powi(64));
  267. check(&((BigInt::one() << 100) + (BigInt::one() << 123)),
  268. 2.0.powi(100) + 2.0.powi(123));
  269. check(&(BigInt::one() << 127), 2.0.powi(127));
  270. check(&(BigInt::from((1u64 << 24) - 1) << (128 - 24)), f32::MAX);
  271. // keeping all 24 digits with the bits at different offsets to the BigDigits
  272. let x: u32 = 0b00000000101111011111011011011101;
  273. let mut f = x as f32;
  274. let mut b = BigInt::from(x);
  275. for _ in 0..64 {
  276. check(&b, f);
  277. f *= 2.0;
  278. b = b << 1;
  279. }
  280. // this number when rounded to f64 then f32 isn't the same as when rounded straight to f32
  281. let mut n: i64 = 0b0000000000111111111111111111111111011111111111111111111111111111;
  282. assert!((n as f64) as f32 != n as f32);
  283. assert_eq!(BigInt::from(n).to_f32(), Some(n as f32));
  284. n = -n;
  285. assert!((n as f64) as f32 != n as f32);
  286. assert_eq!(BigInt::from(n).to_f32(), Some(n as f32));
  287. // test rounding up with the bits at different offsets to the BigDigits
  288. let mut f = ((1u64 << 25) - 1) as f32;
  289. let mut b = BigInt::from(1u64 << 25);
  290. for _ in 0..64 {
  291. assert_eq!(b.to_f32(), Some(f));
  292. f *= 2.0;
  293. b = b << 1;
  294. }
  295. // rounding
  296. assert_eq!(BigInt::from_f32(-f32::consts::PI),
  297. Some(BigInt::from(-3i32)));
  298. assert_eq!(BigInt::from_f32(-f32::consts::E), Some(BigInt::from(-2i32)));
  299. assert_eq!(BigInt::from_f32(-0.99999), Some(BigInt::zero()));
  300. assert_eq!(BigInt::from_f32(-0.5), Some(BigInt::zero()));
  301. assert_eq!(BigInt::from_f32(-0.0), Some(BigInt::zero()));
  302. assert_eq!(BigInt::from_f32(f32::MIN_POSITIVE / 2.0),
  303. Some(BigInt::zero()));
  304. assert_eq!(BigInt::from_f32(f32::MIN_POSITIVE), Some(BigInt::zero()));
  305. assert_eq!(BigInt::from_f32(0.5), Some(BigInt::zero()));
  306. assert_eq!(BigInt::from_f32(0.99999), Some(BigInt::zero()));
  307. assert_eq!(BigInt::from_f32(f32::consts::E), Some(BigInt::from(2u32)));
  308. assert_eq!(BigInt::from_f32(f32::consts::PI), Some(BigInt::from(3u32)));
  309. // special float values
  310. assert_eq!(BigInt::from_f32(f32::NAN), None);
  311. assert_eq!(BigInt::from_f32(f32::INFINITY), None);
  312. assert_eq!(BigInt::from_f32(f32::NEG_INFINITY), None);
  313. // largest BigInt that will round to a finite f32 value
  314. let big_num = (BigInt::one() << 128) - BigInt::one() - (BigInt::one() << (128 - 25));
  315. assert_eq!(big_num.to_f32(), Some(f32::MAX));
  316. assert_eq!((&big_num + BigInt::one()).to_f32(), None);
  317. assert_eq!((-&big_num).to_f32(), Some(f32::MIN));
  318. assert_eq!(((-&big_num) - BigInt::one()).to_f32(), None);
  319. assert_eq!(((BigInt::one() << 128) - BigInt::one()).to_f32(), None);
  320. assert_eq!((BigInt::one() << 128).to_f32(), None);
  321. assert_eq!((-((BigInt::one() << 128) - BigInt::one())).to_f32(), None);
  322. assert_eq!((-(BigInt::one() << 128)).to_f32(), None);
  323. }
  324. #[test]
  325. fn test_convert_f64() {
  326. fn check(b1: &BigInt, f: f64) {
  327. let b2 = BigInt::from_f64(f).unwrap();
  328. assert_eq!(b1, &b2);
  329. assert_eq!(b1.to_f64().unwrap(), f);
  330. let neg_b1 = -b1;
  331. let neg_b2 = BigInt::from_f64(-f).unwrap();
  332. assert_eq!(neg_b1, neg_b2);
  333. assert_eq!(neg_b1.to_f64().unwrap(), -f);
  334. }
  335. check(&BigInt::zero(), 0.0);
  336. check(&BigInt::one(), 1.0);
  337. check(&BigInt::from(u32::MAX), 2.0.powi(32) - 1.0);
  338. check(&BigInt::from(1u64 << 32), 2.0.powi(32));
  339. check(&BigInt::from_slice(Plus, &[0, 0, 1]), 2.0.powi(64));
  340. check(&((BigInt::one() << 100) + (BigInt::one() << 152)),
  341. 2.0.powi(100) + 2.0.powi(152));
  342. check(&(BigInt::one() << 1023), 2.0.powi(1023));
  343. check(&(BigInt::from((1u64 << 53) - 1) << (1024 - 53)), f64::MAX);
  344. // keeping all 53 digits with the bits at different offsets to the BigDigits
  345. let x: u64 = 0b0000000000011110111110110111111101110111101111011111011011011101;
  346. let mut f = x as f64;
  347. let mut b = BigInt::from(x);
  348. for _ in 0..128 {
  349. check(&b, f);
  350. f *= 2.0;
  351. b = b << 1;
  352. }
  353. // test rounding up with the bits at different offsets to the BigDigits
  354. let mut f = ((1u64 << 54) - 1) as f64;
  355. let mut b = BigInt::from(1u64 << 54);
  356. for _ in 0..128 {
  357. assert_eq!(b.to_f64(), Some(f));
  358. f *= 2.0;
  359. b = b << 1;
  360. }
  361. // rounding
  362. assert_eq!(BigInt::from_f64(-f64::consts::PI),
  363. Some(BigInt::from(-3i32)));
  364. assert_eq!(BigInt::from_f64(-f64::consts::E), Some(BigInt::from(-2i32)));
  365. assert_eq!(BigInt::from_f64(-0.99999), Some(BigInt::zero()));
  366. assert_eq!(BigInt::from_f64(-0.5), Some(BigInt::zero()));
  367. assert_eq!(BigInt::from_f64(-0.0), Some(BigInt::zero()));
  368. assert_eq!(BigInt::from_f64(f64::MIN_POSITIVE / 2.0),
  369. Some(BigInt::zero()));
  370. assert_eq!(BigInt::from_f64(f64::MIN_POSITIVE), Some(BigInt::zero()));
  371. assert_eq!(BigInt::from_f64(0.5), Some(BigInt::zero()));
  372. assert_eq!(BigInt::from_f64(0.99999), Some(BigInt::zero()));
  373. assert_eq!(BigInt::from_f64(f64::consts::E), Some(BigInt::from(2u32)));
  374. assert_eq!(BigInt::from_f64(f64::consts::PI), Some(BigInt::from(3u32)));
  375. // special float values
  376. assert_eq!(BigInt::from_f64(f64::NAN), None);
  377. assert_eq!(BigInt::from_f64(f64::INFINITY), None);
  378. assert_eq!(BigInt::from_f64(f64::NEG_INFINITY), None);
  379. // largest BigInt that will round to a finite f64 value
  380. let big_num = (BigInt::one() << 1024) - BigInt::one() - (BigInt::one() << (1024 - 54));
  381. assert_eq!(big_num.to_f64(), Some(f64::MAX));
  382. assert_eq!((&big_num + BigInt::one()).to_f64(), None);
  383. assert_eq!((-&big_num).to_f64(), Some(f64::MIN));
  384. assert_eq!(((-&big_num) - BigInt::one()).to_f64(), None);
  385. assert_eq!(((BigInt::one() << 1024) - BigInt::one()).to_f64(), None);
  386. assert_eq!((BigInt::one() << 1024).to_f64(), None);
  387. assert_eq!((-((BigInt::one() << 1024) - BigInt::one())).to_f64(), None);
  388. assert_eq!((-(BigInt::one() << 1024)).to_f64(), None);
  389. }
  390. #[test]
  391. fn test_convert_to_biguint() {
  392. fn check(n: BigInt, ans_1: BigUint) {
  393. assert_eq!(n.to_biguint().unwrap(), ans_1);
  394. assert_eq!(n.to_biguint().unwrap().to_bigint().unwrap(), n);
  395. }
  396. let zero: BigInt = Zero::zero();
  397. let unsigned_zero: BigUint = Zero::zero();
  398. let positive = BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3]));
  399. let negative = -&positive;
  400. check(zero, unsigned_zero);
  401. check(positive, BigUint::new(vec![1, 2, 3]));
  402. assert_eq!(negative.to_biguint(), None);
  403. }
  404. #[test]
  405. fn test_convert_from_uint() {
  406. macro_rules! check {
  407. ($ty:ident, $max:expr) => {
  408. assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
  409. assert_eq!(BigInt::from($ty::one()), BigInt::one());
  410. assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
  411. assert_eq!(BigInt::from($ty::MAX), $max);
  412. }
  413. }
  414. check!(u8, BigInt::from_slice(Plus, &[u8::MAX as BigDigit]));
  415. check!(u16, BigInt::from_slice(Plus, &[u16::MAX as BigDigit]));
  416. check!(u32, BigInt::from_slice(Plus, &[u32::MAX as BigDigit]));
  417. check!(u64,
  418. BigInt::from_slice(Plus, &[u32::MAX as BigDigit, u32::MAX as BigDigit]));
  419. check!(usize, BigInt::from(usize::MAX as u64));
  420. }
  421. #[test]
  422. fn test_convert_from_int() {
  423. macro_rules! check {
  424. ($ty:ident, $min:expr, $max:expr) => {
  425. assert_eq!(BigInt::from($ty::MIN), $min);
  426. assert_eq!(BigInt::from($ty::MIN + $ty::one()), $min + BigInt::one());
  427. assert_eq!(BigInt::from(-$ty::one()), -BigInt::one());
  428. assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
  429. assert_eq!(BigInt::from($ty::one()), BigInt::one());
  430. assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
  431. assert_eq!(BigInt::from($ty::MAX), $max);
  432. }
  433. }
  434. check!(i8,
  435. BigInt::from_slice(Minus, &[1 << 7]),
  436. BigInt::from_slice(Plus, &[i8::MAX as BigDigit]));
  437. check!(i16,
  438. BigInt::from_slice(Minus, &[1 << 15]),
  439. BigInt::from_slice(Plus, &[i16::MAX as BigDigit]));
  440. check!(i32,
  441. BigInt::from_slice(Minus, &[1 << 31]),
  442. BigInt::from_slice(Plus, &[i32::MAX as BigDigit]));
  443. check!(i64,
  444. BigInt::from_slice(Minus, &[0, 1 << 31]),
  445. BigInt::from_slice(Plus, &[u32::MAX as BigDigit, i32::MAX as BigDigit]));
  446. check!(isize,
  447. BigInt::from(isize::MIN as i64),
  448. BigInt::from(isize::MAX as i64));
  449. }
  450. #[test]
  451. fn test_convert_from_biguint() {
  452. assert_eq!(BigInt::from(BigUint::zero()), BigInt::zero());
  453. assert_eq!(BigInt::from(BigUint::one()), BigInt::one());
  454. assert_eq!(BigInt::from(BigUint::from_slice(&[1, 2, 3])),
  455. BigInt::from_slice(Plus, &[1, 2, 3]));
  456. }
  457. const N1: BigDigit = -1i32 as BigDigit;
  458. const N2: BigDigit = -2i32 as BigDigit;
  459. const SUM_TRIPLES: &'static [(&'static [BigDigit],
  460. &'static [BigDigit],
  461. &'static [BigDigit])] = &[(&[], &[], &[]),
  462. (&[], &[1], &[1]),
  463. (&[1], &[1], &[2]),
  464. (&[1], &[1, 1], &[2, 1]),
  465. (&[1], &[N1], &[0, 1]),
  466. (&[1], &[N1, N1], &[0, 0, 1]),
  467. (&[N1, N1], &[N1, N1], &[N2, N1, 1]),
  468. (&[1, 1, 1], &[N1, N1], &[0, 1, 2]),
  469. (&[2, 2, 1], &[N1, N2], &[1, 1, 2])];
  470. #[test]
  471. fn test_add() {
  472. for elm in SUM_TRIPLES.iter() {
  473. let (a_vec, b_vec, c_vec) = *elm;
  474. let a = BigInt::from_slice(Plus, a_vec);
  475. let b = BigInt::from_slice(Plus, b_vec);
  476. let c = BigInt::from_slice(Plus, c_vec);
  477. let (na, nb, nc) = (-&a, -&b, -&c);
  478. assert_op!(a + b == c);
  479. assert_op!(b + a == c);
  480. assert_op!(c + na == b);
  481. assert_op!(c + nb == a);
  482. assert_op!(a + nc == nb);
  483. assert_op!(b + nc == na);
  484. assert_op!(na + nb == nc);
  485. assert_op!(a + na == Zero::zero());
  486. }
  487. }
  488. #[test]
  489. fn test_sub() {
  490. for elm in SUM_TRIPLES.iter() {
  491. let (a_vec, b_vec, c_vec) = *elm;
  492. let a = BigInt::from_slice(Plus, a_vec);
  493. let b = BigInt::from_slice(Plus, b_vec);
  494. let c = BigInt::from_slice(Plus, c_vec);
  495. let (na, nb, nc) = (-&a, -&b, -&c);
  496. assert_op!(c - a == b);
  497. assert_op!(c - b == a);
  498. assert_op!(nb - a == nc);
  499. assert_op!(na - b == nc);
  500. assert_op!(b - na == c);
  501. assert_op!(a - nb == c);
  502. assert_op!(nc - na == nb);
  503. assert_op!(a - a == Zero::zero());
  504. }
  505. }
  506. const M: u32 = ::std::u32::MAX;
  507. static MUL_TRIPLES: &'static [(&'static [BigDigit],
  508. &'static [BigDigit],
  509. &'static [BigDigit])] = &[(&[], &[], &[]),
  510. (&[], &[1], &[]),
  511. (&[2], &[], &[]),
  512. (&[1], &[1], &[1]),
  513. (&[2], &[3], &[6]),
  514. (&[1], &[1, 1, 1], &[1, 1, 1]),
  515. (&[1, 2, 3], &[3], &[3, 6, 9]),
  516. (&[1, 1, 1], &[N1], &[N1, N1, N1]),
  517. (&[1, 2, 3], &[N1], &[N1, N2, N2, 2]),
  518. (&[1, 2, 3, 4], &[N1], &[N1, N2, N2, N2, 3]),
  519. (&[N1], &[N1], &[1, N2]),
  520. (&[N1, N1], &[N1], &[1, N1, N2]),
  521. (&[N1, N1, N1], &[N1], &[1, N1, N1, N2]),
  522. (&[N1, N1, N1, N1], &[N1], &[1, N1, N1, N1, N2]),
  523. (&[M / 2 + 1], &[2], &[0, 1]),
  524. (&[0, M / 2 + 1], &[2], &[0, 0, 1]),
  525. (&[1, 2], &[1, 2, 3], &[1, 4, 7, 6]),
  526. (&[N1, N1], &[N1, N1, N1], &[1, 0, N1, N2, N1]),
  527. (&[N1, N1, N1],
  528. &[N1, N1, N1, N1],
  529. &[1, 0, 0, N1, N2, N1, N1]),
  530. (&[0, 0, 1], &[1, 2, 3], &[0, 0, 1, 2, 3]),
  531. (&[0, 0, 1], &[0, 0, 0, 1], &[0, 0, 0, 0, 0, 1])];
  532. static DIV_REM_QUADRUPLES: &'static [(&'static [BigDigit],
  533. &'static [BigDigit],
  534. &'static [BigDigit],
  535. &'static [BigDigit])] = &[(&[1], &[2], &[], &[1]),
  536. (&[1, 1], &[2], &[M / 2 + 1], &[1]),
  537. (&[1, 1, 1], &[2], &[M / 2 + 1, M / 2 + 1], &[1]),
  538. (&[0, 1], &[N1], &[1], &[1]),
  539. (&[N1, N1], &[N2], &[2, 1], &[3])];
  540. #[test]
  541. fn test_mul() {
  542. for elm in MUL_TRIPLES.iter() {
  543. let (a_vec, b_vec, c_vec) = *elm;
  544. let a = BigInt::from_slice(Plus, a_vec);
  545. let b = BigInt::from_slice(Plus, b_vec);
  546. let c = BigInt::from_slice(Plus, c_vec);
  547. let (na, nb, nc) = (-&a, -&b, -&c);
  548. assert_op!(a * b == c);
  549. assert_op!(b * a == c);
  550. assert_op!(na * nb == c);
  551. assert_op!(na * b == nc);
  552. assert_op!(nb * a == nc);
  553. }
  554. for elm in DIV_REM_QUADRUPLES.iter() {
  555. let (a_vec, b_vec, c_vec, d_vec) = *elm;
  556. let a = BigInt::from_slice(Plus, a_vec);
  557. let b = BigInt::from_slice(Plus, b_vec);
  558. let c = BigInt::from_slice(Plus, c_vec);
  559. let d = BigInt::from_slice(Plus, d_vec);
  560. assert!(a == &b * &c + &d);
  561. assert!(a == &c * &b + &d);
  562. }
  563. }
  564. #[test]
  565. fn test_div_mod_floor() {
  566. fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
  567. let (d, m) = a.div_mod_floor(b);
  568. if !m.is_zero() {
  569. assert_eq!(m.sign, b.sign);
  570. }
  571. assert!(m.abs() <= b.abs());
  572. assert!(*a == b * &d + &m);
  573. assert!(d == *ans_d);
  574. assert!(m == *ans_m);
  575. }
  576. fn check(a: &BigInt, b: &BigInt, d: &BigInt, m: &BigInt) {
  577. if m.is_zero() {
  578. check_sub(a, b, d, m);
  579. check_sub(a, &b.neg(), &d.neg(), m);
  580. check_sub(&a.neg(), b, &d.neg(), m);
  581. check_sub(&a.neg(), &b.neg(), d, m);
  582. } else {
  583. let one: BigInt = One::one();
  584. check_sub(a, b, d, m);
  585. check_sub(a, &b.neg(), &(d.neg() - &one), &(m - b));
  586. check_sub(&a.neg(), b, &(d.neg() - &one), &(b - m));
  587. check_sub(&a.neg(), &b.neg(), d, &m.neg());
  588. }
  589. }
  590. for elm in MUL_TRIPLES.iter() {
  591. let (a_vec, b_vec, c_vec) = *elm;
  592. let a = BigInt::from_slice(Plus, a_vec);
  593. let b = BigInt::from_slice(Plus, b_vec);
  594. let c = BigInt::from_slice(Plus, c_vec);
  595. if !a.is_zero() {
  596. check(&c, &a, &b, &Zero::zero());
  597. }
  598. if !b.is_zero() {
  599. check(&c, &b, &a, &Zero::zero());
  600. }
  601. }
  602. for elm in DIV_REM_QUADRUPLES.iter() {
  603. let (a_vec, b_vec, c_vec, d_vec) = *elm;
  604. let a = BigInt::from_slice(Plus, a_vec);
  605. let b = BigInt::from_slice(Plus, b_vec);
  606. let c = BigInt::from_slice(Plus, c_vec);
  607. let d = BigInt::from_slice(Plus, d_vec);
  608. if !b.is_zero() {
  609. check(&a, &b, &c, &d);
  610. }
  611. }
  612. }
  613. #[test]
  614. fn test_div_rem() {
  615. fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) {
  616. let (q, r) = a.div_rem(b);
  617. if !r.is_zero() {
  618. assert_eq!(r.sign, a.sign);
  619. }
  620. assert!(r.abs() <= b.abs());
  621. assert!(*a == b * &q + &r);
  622. assert!(q == *ans_q);
  623. assert!(r == *ans_r);
  624. let (a, b, ans_q, ans_r) = (a.clone(), b.clone(), ans_q.clone(), ans_r.clone());
  625. assert_op!(a / b == ans_q);
  626. assert_op!(a % b == ans_r);
  627. }
  628. fn check(a: &BigInt, b: &BigInt, q: &BigInt, r: &BigInt) {
  629. check_sub(a, b, q, r);
  630. check_sub(a, &b.neg(), &q.neg(), r);
  631. check_sub(&a.neg(), b, &q.neg(), &r.neg());
  632. check_sub(&a.neg(), &b.neg(), q, &r.neg());
  633. }
  634. for elm in MUL_TRIPLES.iter() {
  635. let (a_vec, b_vec, c_vec) = *elm;
  636. let a = BigInt::from_slice(Plus, a_vec);
  637. let b = BigInt::from_slice(Plus, b_vec);
  638. let c = BigInt::from_slice(Plus, c_vec);
  639. if !a.is_zero() {
  640. check(&c, &a, &b, &Zero::zero());
  641. }
  642. if !b.is_zero() {
  643. check(&c, &b, &a, &Zero::zero());
  644. }
  645. }
  646. for elm in DIV_REM_QUADRUPLES.iter() {
  647. let (a_vec, b_vec, c_vec, d_vec) = *elm;
  648. let a = BigInt::from_slice(Plus, a_vec);
  649. let b = BigInt::from_slice(Plus, b_vec);
  650. let c = BigInt::from_slice(Plus, c_vec);
  651. let d = BigInt::from_slice(Plus, d_vec);
  652. if !b.is_zero() {
  653. check(&a, &b, &c, &d);
  654. }
  655. }
  656. }
  657. #[test]
  658. fn test_checked_add() {
  659. for elm in SUM_TRIPLES.iter() {
  660. let (a_vec, b_vec, c_vec) = *elm;
  661. let a = BigInt::from_slice(Plus, a_vec);
  662. let b = BigInt::from_slice(Plus, b_vec);
  663. let c = BigInt::from_slice(Plus, c_vec);
  664. assert!(a.checked_add(&b).unwrap() == c);
  665. assert!(b.checked_add(&a).unwrap() == c);
  666. assert!(c.checked_add(&(-&a)).unwrap() == b);
  667. assert!(c.checked_add(&(-&b)).unwrap() == a);
  668. assert!(a.checked_add(&(-&c)).unwrap() == (-&b));
  669. assert!(b.checked_add(&(-&c)).unwrap() == (-&a));
  670. assert!((-&a).checked_add(&(-&b)).unwrap() == (-&c));
  671. assert!(a.checked_add(&(-&a)).unwrap() == Zero::zero());
  672. }
  673. }
  674. #[test]
  675. fn test_checked_sub() {
  676. for elm in SUM_TRIPLES.iter() {
  677. let (a_vec, b_vec, c_vec) = *elm;
  678. let a = BigInt::from_slice(Plus, a_vec);
  679. let b = BigInt::from_slice(Plus, b_vec);
  680. let c = BigInt::from_slice(Plus, c_vec);
  681. assert!(c.checked_sub(&a).unwrap() == b);
  682. assert!(c.checked_sub(&b).unwrap() == a);
  683. assert!((-&b).checked_sub(&a).unwrap() == (-&c));
  684. assert!((-&a).checked_sub(&b).unwrap() == (-&c));
  685. assert!(b.checked_sub(&(-&a)).unwrap() == c);
  686. assert!(a.checked_sub(&(-&b)).unwrap() == c);
  687. assert!((-&c).checked_sub(&(-&a)).unwrap() == (-&b));
  688. assert!(a.checked_sub(&a).unwrap() == Zero::zero());
  689. }
  690. }
  691. #[test]
  692. fn test_checked_mul() {
  693. for elm in MUL_TRIPLES.iter() {
  694. let (a_vec, b_vec, c_vec) = *elm;
  695. let a = BigInt::from_slice(Plus, a_vec);
  696. let b = BigInt::from_slice(Plus, b_vec);
  697. let c = BigInt::from_slice(Plus, c_vec);
  698. assert!(a.checked_mul(&b).unwrap() == c);
  699. assert!(b.checked_mul(&a).unwrap() == c);
  700. assert!((-&a).checked_mul(&b).unwrap() == -&c);
  701. assert!((-&b).checked_mul(&a).unwrap() == -&c);
  702. }
  703. for elm in DIV_REM_QUADRUPLES.iter() {
  704. let (a_vec, b_vec, c_vec, d_vec) = *elm;
  705. let a = BigInt::from_slice(Plus, a_vec);
  706. let b = BigInt::from_slice(Plus, b_vec);
  707. let c = BigInt::from_slice(Plus, c_vec);
  708. let d = BigInt::from_slice(Plus, d_vec);
  709. assert!(a == b.checked_mul(&c).unwrap() + &d);
  710. assert!(a == c.checked_mul(&b).unwrap() + &d);
  711. }
  712. }
  713. #[test]
  714. fn test_checked_div() {
  715. for elm in MUL_TRIPLES.iter() {
  716. let (a_vec, b_vec, c_vec) = *elm;
  717. let a = BigInt::from_slice(Plus, a_vec);
  718. let b = BigInt::from_slice(Plus, b_vec);
  719. let c = BigInt::from_slice(Plus, c_vec);
  720. if !a.is_zero() {
  721. assert!(c.checked_div(&a).unwrap() == b);
  722. assert!((-&c).checked_div(&(-&a)).unwrap() == b);
  723. assert!((-&c).checked_div(&a).unwrap() == -&b);
  724. }
  725. if !b.is_zero() {
  726. assert!(c.checked_div(&b).unwrap() == a);
  727. assert!((-&c).checked_div(&(-&b)).unwrap() == a);
  728. assert!((-&c).checked_div(&b).unwrap() == -&a);
  729. }
  730. assert!(c.checked_div(&Zero::zero()).is_none());
  731. assert!((-&c).checked_div(&Zero::zero()).is_none());
  732. }
  733. }
  734. #[test]
  735. fn test_gcd() {
  736. fn check(a: isize, b: isize, c: isize) {
  737. let big_a: BigInt = FromPrimitive::from_isize(a).unwrap();
  738. let big_b: BigInt = FromPrimitive::from_isize(b).unwrap();
  739. let big_c: BigInt = FromPrimitive::from_isize(c).unwrap();
  740. assert_eq!(big_a.gcd(&big_b), big_c);
  741. }
  742. check(10, 2, 2);
  743. check(10, 3, 1);
  744. check(0, 3, 3);
  745. check(3, 3, 3);
  746. check(56, 42, 14);
  747. check(3, -3, 3);
  748. check(-6, 3, 3);
  749. check(-4, -2, 2);
  750. }
  751. #[test]
  752. fn test_lcm() {
  753. fn check(a: isize, b: isize, c: isize) {
  754. let big_a: BigInt = FromPrimitive::from_isize(a).unwrap();
  755. let big_b: BigInt = FromPrimitive::from_isize(b).unwrap();
  756. let big_c: BigInt = FromPrimitive::from_isize(c).unwrap();
  757. assert_eq!(big_a.lcm(&big_b), big_c);
  758. }
  759. check(1, 0, 0);
  760. check(0, 1, 0);
  761. check(1, 1, 1);
  762. check(-1, 1, 1);
  763. check(1, -1, 1);
  764. check(-1, -1, 1);
  765. check(8, 9, 72);
  766. check(11, 5, 55);
  767. }
  768. #[test]
  769. fn test_abs_sub() {
  770. let zero: BigInt = Zero::zero();
  771. let one: BigInt = One::one();
  772. assert_eq!((-&one).abs_sub(&one), zero);
  773. let one: BigInt = One::one();
  774. let zero: BigInt = Zero::zero();
  775. assert_eq!(one.abs_sub(&one), zero);
  776. let one: BigInt = One::one();
  777. let zero: BigInt = Zero::zero();
  778. assert_eq!(one.abs_sub(&zero), one);
  779. let one: BigInt = One::one();
  780. let two: BigInt = FromPrimitive::from_isize(2).unwrap();
  781. assert_eq!(one.abs_sub(&-&one), two);
  782. }
  783. #[test]
  784. fn test_from_str_radix() {
  785. fn check(s: &str, ans: Option<isize>) {
  786. let ans = ans.map(|n| {
  787. let x: BigInt = FromPrimitive::from_isize(n).unwrap();
  788. x
  789. });
  790. assert_eq!(BigInt::from_str_radix(s, 10).ok(), ans);
  791. }
  792. check("10", Some(10));
  793. check("1", Some(1));
  794. check("0", Some(0));
  795. check("-1", Some(-1));
  796. check("-10", Some(-10));
  797. check("+10", Some(10));
  798. check("--7", None);
  799. check("++5", None);
  800. check("+-9", None);
  801. check("-+3", None);
  802. check("Z", None);
  803. check("_", None);
  804. // issue 10522, this hit an edge case that caused it to
  805. // attempt to allocate a vector of size (-1u) == huge.
  806. let x: BigInt = format!("1{}", repeat("0").take(36).collect::<String>()).parse().unwrap();
  807. let _y = x.to_string();
  808. }
  809. #[test]
  810. fn test_lower_hex() {
  811. let a = BigInt::parse_bytes(b"A", 16).unwrap();
  812. let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
  813. assert_eq!(format!("{:x}", a), "a");
  814. assert_eq!(format!("{:x}", hello), "-48656c6c6f20776f726c6421");
  815. assert_eq!(format!("{:♥>+#8x}", a), "♥♥♥♥+0xa");
  816. }
  817. #[test]
  818. fn test_upper_hex() {
  819. let a = BigInt::parse_bytes(b"A", 16).unwrap();
  820. let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
  821. assert_eq!(format!("{:X}", a), "A");
  822. assert_eq!(format!("{:X}", hello), "-48656C6C6F20776F726C6421");
  823. assert_eq!(format!("{:♥>+#8X}", a), "♥♥♥♥+0xA");
  824. }
  825. #[test]
  826. fn test_binary() {
  827. let a = BigInt::parse_bytes(b"A", 16).unwrap();
  828. let hello = BigInt::parse_bytes("-224055342307539".as_bytes(), 10).unwrap();
  829. assert_eq!(format!("{:b}", a), "1010");
  830. assert_eq!(format!("{:b}", hello),
  831. "-110010111100011011110011000101101001100011010011");
  832. assert_eq!(format!("{:♥>+#8b}", a), "♥+0b1010");
  833. }
  834. #[test]
  835. fn test_octal() {
  836. let a = BigInt::parse_bytes(b"A", 16).unwrap();
  837. let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
  838. assert_eq!(format!("{:o}", a), "12");
  839. assert_eq!(format!("{:o}", hello), "-22062554330674403566756233062041");
  840. assert_eq!(format!("{:♥>+#8o}", a), "♥♥♥+0o12");
  841. }
  842. #[test]
  843. fn test_display() {
  844. let a = BigInt::parse_bytes(b"A", 16).unwrap();
  845. let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
  846. assert_eq!(format!("{}", a), "10");
  847. assert_eq!(format!("{}", hello), "-22405534230753963835153736737");
  848. assert_eq!(format!("{:♥>+#8}", a), "♥♥♥♥♥+10");
  849. }
  850. #[test]
  851. fn test_neg() {
  852. assert!(-BigInt::new(Plus, vec![1, 1, 1]) == BigInt::new(Minus, vec![1, 1, 1]));
  853. assert!(-BigInt::new(Minus, vec![1, 1, 1]) == BigInt::new(Plus, vec![1, 1, 1]));
  854. let zero: BigInt = Zero::zero();
  855. assert_eq!(-&zero, zero);
  856. }
  857. #[test]
  858. fn test_rand() {
  859. let mut rng = thread_rng();
  860. let _n: BigInt = rng.gen_bigint(137);
  861. assert!(rng.gen_bigint(0).is_zero());
  862. }
  863. #[test]
  864. fn test_rand_range() {
  865. let mut rng = thread_rng();
  866. for _ in 0..10 {
  867. assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_usize(236).unwrap(),
  868. &FromPrimitive::from_usize(237).unwrap()),
  869. FromPrimitive::from_usize(236).unwrap());
  870. }
  871. fn check(l: BigInt, u: BigInt) {
  872. let mut rng = thread_rng();
  873. for _ in 0..1000 {
  874. let n: BigInt = rng.gen_bigint_range(&l, &u);
  875. assert!(n >= l);
  876. assert!(n < u);
  877. }
  878. }
  879. let l: BigInt = FromPrimitive::from_usize(403469000 + 2352).unwrap();
  880. let u: BigInt = FromPrimitive::from_usize(403469000 + 3513).unwrap();
  881. check(l.clone(), u.clone());
  882. check(-l.clone(), u.clone());
  883. check(-u.clone(), -l.clone());
  884. }
  885. #[test]
  886. #[should_panic]
  887. fn test_zero_rand_range() {
  888. thread_rng().gen_bigint_range(&FromPrimitive::from_isize(54).unwrap(),
  889. &FromPrimitive::from_isize(54).unwrap());
  890. }
  891. #[test]
  892. #[should_panic]
  893. fn test_negative_rand_range() {
  894. let mut rng = thread_rng();
  895. let l = FromPrimitive::from_usize(2352).unwrap();
  896. let u = FromPrimitive::from_usize(3513).unwrap();
  897. // Switching u and l should fail:
  898. let _n: BigInt = rng.gen_bigint_range(&u, &l);
  899. }