<commit_before>use paste;
use permutohedron;
use quickcheck as qc;
use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng};
use rand::{seq::SliceRandom, thread_rng};
use std::{cmp::min, fmt::Debug, marker::PhantomData};
use itertools as it;
use crate::it::Itertools;
use crate::it::ExactlyOneError;
use crate::it::multizip;
use crate::it::multipeek;
use crate::it::peek_nth;
use crate::it::free::rciter;
use crate::it::free::put_back_n;
use crate::it::FoldWhile;
use crate::it::cloned;
use crate::it::iproduct;
use crate::it::izip;

#[test]
fn product3() {
    let prod = iproduct!(0..3, 0..2, 0..2);
    assert_eq!(prod.size_hint(), (12, Some(12)));
    let v = prod.collect_vec();
    for i in 0..3 {
        for j in 0..2 {
            for k in 0..2 {
                assert!((i, j, k) == v[(i * 2 * 2 + j * 2 + k) as usize]);
            }
        }
    }
    for (_, _, _, _) in iproduct!(0..3, 0..2, 0..2, 0..3) {
        /* test compiles */
    }
}

#[test]
fn interleave_shortest() {
    let v0: Vec<i32> = vec![0, 2, 4];
    let v1: Vec<i32> = vec![1, 3, 5, 7];
    let it = v0.into_iter().interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (6, Some(6)));
    assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5]);

    let v0: Vec<i32> = vec![0, 2, 4, 6, 8];
    let v1: Vec<i32> = vec![1, 3, 5];
    let it = v0.into_iter().interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (7, Some(7)));
    assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5, 6]);

    let i0 = ::std::iter::repeat(0);
    let v1: Vec<_> = vec![1, 3, 5];
    let it = i0.interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (7, Some(7)));

    let v0: Vec<_> = vec![0, 2, 4];
    let i1 = ::std::iter::repeat(1);
    let it = v0.into_iter().interleave_shortest(i1);
    assert_eq!(it.size_hint(), (6, Some(6)));
}

#[test]
fn duplicates_by() {
    let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
    let ys = ["aa", "bbbb", "cccc"];
    it::assert_equal(ys.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()));
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates_by(|x| x[..2].to_string()).rev());
    let ys_rev = ["ccc", "aa", "bbbbb"];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()).rev());
}

#[test]
fn duplicates() {
    let xs = [0, 1, 2, 3, 2, 1, 3];
    let ys = [2, 1, 3];
    it::assert_equal(ys.iter(), xs.iter().duplicates());
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates().rev());
    let ys_rev = [3, 2, 1];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates().rev());

    let xs = [0, 1, 0, 1];
    let ys = [0, 1];
    it::assert_equal(ys.iter(), xs.iter().duplicates());
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates().rev());
    let ys_rev = [1, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates().rev());

    let xs = vec![0, 1, 2, 1, 2];
    let ys = vec![1, 2];
    assert_eq!(ys, xs.iter().duplicates().cloned().collect_vec());
    assert_eq!(ys, xs.iter().rev().duplicates().rev().cloned().collect_vec());
    let ys_rev = vec![2, 1];
    assert_eq!(ys_rev, xs.iter().duplicates().rev().cloned().collect_vec());
}

#[test]
fn unique_by() {
    let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
    let ys = ["aaa", "bbbbb", "ccc"];
    it::assert_equal(ys.iter(), xs.iter().unique_by(|x| x[..2].to_string()));
    it::assert_equal(ys.iter(), xs.iter().rev().unique_by(|x| x[..2].to_string()).rev());
    let ys_rev = ["cccc", "aaaaa", "bbbb"];
    it::assert_equal(ys_rev.iter(), xs.iter().unique_by(|x| x[..2].to_string()).rev());
}

#[test]
fn unique() {
    let xs = [0, 1, 2, 3, 2, 1, 3];
    let ys = [0, 1, 2, 3];
    it::assert_equal(ys.iter(), xs.iter().unique());
    it::assert_equal(ys.iter(), xs.iter().rev().unique().rev());
    let ys_rev = [3, 1, 2, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().unique().rev());

    let xs = [0, 1];
    let ys = [0, 1];
    it::assert_equal(ys.iter(), xs.iter().unique());
    it::assert_equal(ys.iter(), xs.iter().rev().unique().rev());
    let ys_rev = [1, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().unique().rev());
}

#[test]
fn intersperse() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().map(|x| x.clone()).intersperse(", ").collect();
    let text: String = v.concat();
    assert_eq!(text, "a, , b, c".to_string());

    let ys = [0, 1, 2, 3];
    let mut it = ys[..0].iter().map(|x| *x).intersperse(1);
    assert!(it.next() == None);
}

#[test]
fn dedup() {
    let xs = [0, 1, 1, 1, 2, 1, 3, 3];
    let ys = [0, 1, 2, 1, 3];
    it::assert_equal(ys.iter(), xs.iter().dedup());<commit_msg>fix clippy::clone_double_ref in tests<commit_after>use paste;
use permutohedron;
use quickcheck as qc;
use rand::{distributions::{Distribution, Standard}, Rng, SeedableRng, rngs::StdRng};
use rand::{seq::SliceRandom, thread_rng};
use std::{cmp::min, fmt::Debug, marker::PhantomData};
use itertools as it;
use crate::it::Itertools;
use crate::it::ExactlyOneError;
use crate::it::multizip;
use crate::it::multipeek;
use crate::it::peek_nth;
use crate::it::free::rciter;
use crate::it::free::put_back_n;
use crate::it::FoldWhile;
use crate::it::cloned;
use crate::it::iproduct;
use crate::it::izip;

#[test]
fn product3() {
    let prod = iproduct!(0..3, 0..2, 0..2);
    assert_eq!(prod.size_hint(), (12, Some(12)));
    let v = prod.collect_vec();
    for i in 0..3 {
        for j in 0..2 {
            for k in 0..2 {
                assert!((i, j, k) == v[(i * 2 * 2 + j * 2 + k) as usize]);
            }
        }
    }
    for (_, _, _, _) in iproduct!(0..3, 0..2, 0..2, 0..3) {
        /* test compiles */
    }
}

#[test]
fn interleave_shortest() {
    let v0: Vec<i32> = vec![0, 2, 4];
    let v1: Vec<i32> = vec![1, 3, 5, 7];
    let it = v0.into_iter().interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (6, Some(6)));
    assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5]);

    let v0: Vec<i32> = vec![0, 2, 4, 6, 8];
    let v1: Vec<i32> = vec![1, 3, 5];
    let it = v0.into_iter().interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (7, Some(7)));
    assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5, 6]);

    let i0 = ::std::iter::repeat(0);
    let v1: Vec<_> = vec![1, 3, 5];
    let it = i0.interleave_shortest(v1.into_iter());
    assert_eq!(it.size_hint(), (7, Some(7)));

    let v0: Vec<_> = vec![0, 2, 4];
    let i1 = ::std::iter::repeat(1);
    let it = v0.into_iter().interleave_shortest(i1);
    assert_eq!(it.size_hint(), (6, Some(6)));
}

#[test]
fn duplicates_by() {
    let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
    let ys = ["aa", "bbbb", "cccc"];
    it::assert_equal(ys.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()));
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates_by(|x| x[..2].to_string()).rev());
    let ys_rev = ["ccc", "aa", "bbbbb"];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates_by(|x| x[..2].to_string()).rev());
}

#[test]
fn duplicates() {
    let xs = [0, 1, 2, 3, 2, 1, 3];
    let ys = [2, 1, 3];
    it::assert_equal(ys.iter(), xs.iter().duplicates());
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates().rev());
    let ys_rev = [3, 2, 1];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates().rev());

    let xs = [0, 1, 0, 1];
    let ys = [0, 1];
    it::assert_equal(ys.iter(), xs.iter().duplicates());
    it::assert_equal(ys.iter(), xs.iter().rev().duplicates().rev());
    let ys_rev = [1, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().duplicates().rev());

    let xs = vec![0, 1, 2, 1, 2];
    let ys = vec![1, 2];
    assert_eq!(ys, xs.iter().duplicates().cloned().collect_vec());
    assert_eq!(ys, xs.iter().rev().duplicates().rev().cloned().collect_vec());
    let ys_rev = vec![2, 1];
    assert_eq!(ys_rev, xs.iter().duplicates().rev().cloned().collect_vec());
}

#[test]
fn unique_by() {
    let xs = ["aaa", "bbbbb", "aa", "ccc", "bbbb", "aaaaa", "cccc"];
    let ys = ["aaa", "bbbbb", "ccc"];
    it::assert_equal(ys.iter(), xs.iter().unique_by(|x| x[..2].to_string()));
    it::assert_equal(ys.iter(), xs.iter().rev().unique_by(|x| x[..2].to_string()).rev());
    let ys_rev = ["cccc", "aaaaa", "bbbb"];
    it::assert_equal(ys_rev.iter(), xs.iter().unique_by(|x| x[..2].to_string()).rev());
}

#[test]
fn unique() {
    let xs = [0, 1, 2, 3, 2, 1, 3];
    let ys = [0, 1, 2, 3];
    it::assert_equal(ys.iter(), xs.iter().unique());
    it::assert_equal(ys.iter(), xs.iter().rev().unique().rev());
    let ys_rev = [3, 1, 2, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().unique().rev());

    let xs = [0, 1];
    let ys = [0, 1];
    it::assert_equal(ys.iter(), xs.iter().unique());
    it::assert_equal(ys.iter(), xs.iter().rev().unique().rev());
    let ys_rev = [1, 0];
    it::assert_equal(ys_rev.iter(), xs.iter().unique().rev());
}

#[test]
fn intersperse() {
    let xs = ["a", "", "b", "c"];
    let v: Vec<&str> = xs.iter().cloned().intersperse(", ").collect();
    let text: String = v.concat();
    assert_eq!(text, "a, , b, c".to_string());

    let ys = [0, 1, 2, 3];
    let mut it = ys[..0].iter().map(|x| *x).intersperse(1);
    assert!(it.next() == None);
}

#[test]
fn dedup() {
    let xs = [0, 1, 1, 1, 2, 1, 3, 3];
    let ys = [0, 1, 2, 1, 3];
    it::assert_equal(ys.iter(), xs.iter().dedup());
{'fraction_non_alphanumeric': 0.25739519046605663, 'fraction_numerical': 0.036603532666524795, 'mean_word_length': 3.50575263662512, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1954101', 'n_tokens_mistral': 4154, 'n_tokens_neox': 3871, 'n_words': 831}