zip.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. use itertools::Itertools;
  2. use itertools::EitherOrBoth::{Both, Left, Right};
  3. use itertools::free::zip_eq;
  4. use itertools::multizip;
  5. #[test]
  6. fn zip_longest_fused() {
  7. let a = [Some(1), None, Some(3), Some(4)];
  8. let b = [1, 2, 3];
  9. let unfused = a.iter().batching(|it| *it.next().unwrap())
  10. .zip_longest(b.iter().cloned());
  11. itertools::assert_equal(unfused,
  12. vec![Both(1, 1), Right(2), Right(3)]);
  13. }
  14. #[test]
  15. fn test_zip_longest_size_hint() {
  16. let c = (1..10).cycle();
  17. let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  18. let v2 = &[10, 11, 12];
  19. assert_eq!(c.zip_longest(v.iter()).size_hint(), (std::usize::MAX, None));
  20. assert_eq!(v.iter().zip_longest(v2.iter()).size_hint(), (10, Some(10)));
  21. }
  22. #[test]
  23. fn test_double_ended_zip_longest() {
  24. let xs = [1, 2, 3, 4, 5, 6];
  25. let ys = [1, 2, 3, 7];
  26. let a = xs.iter().map(|&x| x);
  27. let b = ys.iter().map(|&x| x);
  28. let mut it = a.zip_longest(b);
  29. assert_eq!(it.next(), Some(Both(1, 1)));
  30. assert_eq!(it.next(), Some(Both(2, 2)));
  31. assert_eq!(it.next_back(), Some(Left(6)));
  32. assert_eq!(it.next_back(), Some(Left(5)));
  33. assert_eq!(it.next_back(), Some(Both(4, 7)));
  34. assert_eq!(it.next(), Some(Both(3, 3)));
  35. assert_eq!(it.next(), None);
  36. }
  37. #[test]
  38. fn test_double_ended_zip() {
  39. let xs = [1, 2, 3, 4, 5, 6];
  40. let ys = [1, 2, 3, 7];
  41. let a = xs.iter().map(|&x| x);
  42. let b = ys.iter().map(|&x| x);
  43. let mut it = multizip((a, b));
  44. assert_eq!(it.next_back(), Some((4, 7)));
  45. assert_eq!(it.next_back(), Some((3, 3)));
  46. assert_eq!(it.next_back(), Some((2, 2)));
  47. assert_eq!(it.next_back(), Some((1, 1)));
  48. assert_eq!(it.next_back(), None);
  49. }
  50. #[should_panic]
  51. #[test]
  52. fn zip_eq_panic1()
  53. {
  54. let a = [1, 2];
  55. let b = [1, 2, 3];
  56. zip_eq(&a, &b).count();
  57. }
  58. #[should_panic]
  59. #[test]
  60. fn zip_eq_panic2()
  61. {
  62. let a: [i32; 0] = [];
  63. let b = [1, 2, 3];
  64. zip_eq(&a, &b).count();
  65. }