test_core.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //! Licensed under the Apache License, Version 2.0
  2. //! https://www.apache.org/licenses/LICENSE-2.0 or the MIT license
  3. //! https://opensource.org/licenses/MIT, at your
  4. //! option. This file may not be copied, modified, or distributed
  5. //! except according to those terms.
  6. #![no_std]
  7. use core::iter;
  8. use itertools as it;
  9. use crate::it::Itertools;
  10. use crate::it::interleave;
  11. use crate::it::intersperse;
  12. use crate::it::intersperse_with;
  13. use crate::it::multizip;
  14. use crate::it::free::put_back;
  15. use crate::it::iproduct;
  16. use crate::it::izip;
  17. use crate::it::chain;
  18. #[test]
  19. fn product2() {
  20. let s = "αβ";
  21. let mut prod = iproduct!(s.chars(), 0..2);
  22. assert!(prod.next() == Some(('α', 0)));
  23. assert!(prod.next() == Some(('α', 1)));
  24. assert!(prod.next() == Some(('β', 0)));
  25. assert!(prod.next() == Some(('β', 1)));
  26. assert!(prod.next() == None);
  27. }
  28. #[test]
  29. fn product_temporary() {
  30. for (_x, _y, _z) in iproduct!(
  31. [0, 1, 2].iter().cloned(),
  32. [0, 1, 2].iter().cloned(),
  33. [0, 1, 2].iter().cloned())
  34. {
  35. // ok
  36. }
  37. }
  38. #[test]
  39. fn izip_macro() {
  40. let mut zip = izip!(2..3);
  41. assert!(zip.next() == Some(2));
  42. assert!(zip.next().is_none());
  43. let mut zip = izip!(0..3, 0..2, 0..2i8);
  44. for i in 0..2 {
  45. assert!((i as usize, i, i as i8) == zip.next().unwrap());
  46. }
  47. assert!(zip.next().is_none());
  48. let xs: [isize; 0] = [];
  49. let mut zip = izip!(0..3, 0..2, 0..2i8, &xs);
  50. assert!(zip.next().is_none());
  51. }
  52. #[test]
  53. fn izip2() {
  54. let _zip1: iter::Zip<_, _> = izip!(1.., 2..);
  55. let _zip2: iter::Zip<_, _> = izip!(1.., 2.., );
  56. }
  57. #[test]
  58. fn izip3() {
  59. let mut zip: iter::Map<iter::Zip<_, _>, _> = izip!(0..3, 0..2, 0..2i8);
  60. for i in 0..2 {
  61. assert!((i as usize, i, i as i8) == zip.next().unwrap());
  62. }
  63. assert!(zip.next().is_none());
  64. }
  65. #[test]
  66. fn multizip3() {
  67. let mut zip = multizip((0..3, 0..2, 0..2i8));
  68. for i in 0..2 {
  69. assert!((i as usize, i, i as i8) == zip.next().unwrap());
  70. }
  71. assert!(zip.next().is_none());
  72. let xs: [isize; 0] = [];
  73. let mut zip = multizip((0..3, 0..2, 0..2i8, xs.iter()));
  74. assert!(zip.next().is_none());
  75. for (_, _, _, _, _) in multizip((0..3, 0..2, xs.iter(), &xs, xs.to_vec())) {
  76. /* test compiles */
  77. }
  78. }
  79. #[test]
  80. fn chain_macro() {
  81. let mut chain = chain!(2..3);
  82. assert!(chain.next() == Some(2));
  83. assert!(chain.next().is_none());
  84. let mut chain = chain!(0..2, 2..3, 3..5i8);
  85. for i in 0..5i8 {
  86. assert_eq!(Some(i), chain.next());
  87. }
  88. assert!(chain.next().is_none());
  89. let mut chain = chain!();
  90. assert_eq!(chain.next(), Option::<()>::None);
  91. }
  92. #[test]
  93. fn chain2() {
  94. let _ = chain!(1.., 2..);
  95. let _ = chain!(1.., 2.., );
  96. }
  97. #[test]
  98. fn write_to() {
  99. let xs = [7, 9, 8];
  100. let mut ys = [0; 5];
  101. let cnt = ys.iter_mut().set_from(xs.iter().map(|x| *x));
  102. assert!(cnt == xs.len());
  103. assert!(ys == [7, 9, 8, 0, 0]);
  104. let cnt = ys.iter_mut().set_from(0..10);
  105. assert!(cnt == ys.len());
  106. assert!(ys == [0, 1, 2, 3, 4]);
  107. }
  108. #[test]
  109. fn test_interleave() {
  110. let xs: [u8; 0] = [];
  111. let ys = [7u8, 9, 8, 10];
  112. let zs = [2u8, 77];
  113. let it = interleave(xs.iter(), ys.iter());
  114. it::assert_equal(it, ys.iter());
  115. let rs = [7u8, 2, 9, 77, 8, 10];
  116. let it = interleave(ys.iter(), zs.iter());
  117. it::assert_equal(it, rs.iter());
  118. }
  119. #[test]
  120. fn test_intersperse() {
  121. let xs = [1u8, 2, 3];
  122. let ys = [1u8, 0, 2, 0, 3];
  123. let it = intersperse(&xs, &0);
  124. it::assert_equal(it, ys.iter());
  125. }
  126. #[test]
  127. fn test_intersperse_with() {
  128. let xs = [1u8, 2, 3];
  129. let ys = [1u8, 10, 2, 10, 3];
  130. let i = 10;
  131. let it = intersperse_with(&xs, || &i);
  132. it::assert_equal(it, ys.iter());
  133. }
  134. #[allow(deprecated)]
  135. #[test]
  136. fn foreach() {
  137. let xs = [1i32, 2, 3];
  138. let mut sum = 0;
  139. xs.iter().foreach(|elt| sum += *elt);
  140. assert!(sum == 6);
  141. }
  142. #[test]
  143. fn dropping() {
  144. let xs = [1, 2, 3];
  145. let mut it = xs.iter().dropping(2);
  146. assert_eq!(it.next(), Some(&3));
  147. assert!(it.next().is_none());
  148. let mut it = xs.iter().dropping(5);
  149. assert!(it.next().is_none());
  150. }
  151. #[test]
  152. fn batching() {
  153. let xs = [0, 1, 2, 1, 3];
  154. let ys = [(0, 1), (2, 1)];
  155. // An iterator that gathers elements up in pairs
  156. let pit = xs.iter().cloned().batching(|it| {
  157. match it.next() {
  158. None => None,
  159. Some(x) => match it.next() {
  160. None => None,
  161. Some(y) => Some((x, y)),
  162. }
  163. }
  164. });
  165. it::assert_equal(pit, ys.iter().cloned());
  166. }
  167. #[test]
  168. fn test_put_back() {
  169. let xs = [0, 1, 1, 1, 2, 1, 3, 3];
  170. let mut pb = put_back(xs.iter().cloned());
  171. pb.next();
  172. pb.put_back(1);
  173. pb.put_back(0);
  174. it::assert_equal(pb, xs.iter().cloned());
  175. }
  176. #[allow(deprecated)]
  177. #[test]
  178. fn step() {
  179. it::assert_equal((0..10).step(1), 0..10);
  180. it::assert_equal((0..10).step(2), (0..10).filter(|x: &i32| *x % 2 == 0));
  181. it::assert_equal((0..10).step(10), 0..1);
  182. }
  183. #[allow(deprecated)]
  184. #[test]
  185. fn merge() {
  186. it::assert_equal((0..10).step(2).merge((1..10).step(2)), 0..10);
  187. }
  188. #[test]
  189. fn repeatn() {
  190. let s = "α";
  191. let mut it = it::repeat_n(s, 3);
  192. assert_eq!(it.len(), 3);
  193. assert_eq!(it.next(), Some(s));
  194. assert_eq!(it.next(), Some(s));
  195. assert_eq!(it.next(), Some(s));
  196. assert_eq!(it.next(), None);
  197. assert_eq!(it.next(), None);
  198. }
  199. #[test]
  200. fn count_clones() {
  201. // Check that RepeatN only clones N - 1 times.
  202. use core::cell::Cell;
  203. #[derive(PartialEq, Debug)]
  204. struct Foo {
  205. n: Cell<usize>
  206. }
  207. impl Clone for Foo
  208. {
  209. fn clone(&self) -> Self
  210. {
  211. let n = self.n.get();
  212. self.n.set(n + 1);
  213. Foo { n: Cell::new(n + 1) }
  214. }
  215. }
  216. for n in 0..10 {
  217. let f = Foo{n: Cell::new(0)};
  218. let it = it::repeat_n(f, n);
  219. // drain it
  220. let last = it.last();
  221. if n == 0 {
  222. assert_eq!(last, None);
  223. } else {
  224. assert_eq!(last, Some(Foo{n: Cell::new(n - 1)}));
  225. }
  226. }
  227. }
  228. #[test]
  229. fn part() {
  230. let mut data = [7, 1, 1, 9, 1, 1, 3];
  231. let i = it::partition(&mut data, |elt| *elt >= 3);
  232. assert_eq!(i, 3);
  233. assert_eq!(data, [7, 3, 9, 1, 1, 1, 1]);
  234. let i = it::partition(&mut data, |elt| *elt == 1);
  235. assert_eq!(i, 4);
  236. assert_eq!(data, [1, 1, 1, 1, 9, 3, 7]);
  237. let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  238. let i = it::partition(&mut data, |elt| *elt % 3 == 0);
  239. assert_eq!(i, 3);
  240. assert_eq!(data, [9, 6, 3, 4, 5, 2, 7, 8, 1]);
  241. }
  242. #[test]
  243. fn tree_fold1() {
  244. for i in 0..100 {
  245. assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y));
  246. }
  247. }
  248. #[test]
  249. fn exactly_one() {
  250. assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
  251. assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
  252. assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
  253. assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
  254. }
  255. #[test]
  256. fn at_most_one() {
  257. assert_eq!((0..10).filter(|&x| x == 2).at_most_one().unwrap(), Some(2));
  258. assert!((0..10).filter(|&x| x > 1 && x < 4).at_most_one().unwrap_err().eq(2..4));
  259. assert!((0..10).filter(|&x| x > 1 && x < 5).at_most_one().unwrap_err().eq(2..5));
  260. assert_eq!((0..10).filter(|&_| false).at_most_one().unwrap(), None);
  261. }
  262. #[test]
  263. fn sum1() {
  264. let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  265. assert_eq!(v[..0].iter().cloned().sum1::<i32>(), None);
  266. assert_eq!(v[1..2].iter().cloned().sum1::<i32>(), Some(1));
  267. assert_eq!(v[1..3].iter().cloned().sum1::<i32>(), Some(3));
  268. assert_eq!(v.iter().cloned().sum1::<i32>(), Some(55));
  269. }
  270. #[test]
  271. fn product1() {
  272. let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  273. assert_eq!(v[..0].iter().cloned().product1::<i32>(), None);
  274. assert_eq!(v[..1].iter().cloned().product1::<i32>(), Some(0));
  275. assert_eq!(v[1..3].iter().cloned().product1::<i32>(), Some(2));
  276. assert_eq!(v[1..5].iter().cloned().product1::<i32>(), Some(24));
  277. }