Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arrays powered by const generics without unsafe or libs #311

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ impl_arb_for_tuples! {
(H, 7),
}

impl<const N: usize, A: Arbitrary> Arbitrary for [A; N] {
fn arbitrary(g: &mut Gen) -> [A; N] {
[(); N].map(|_| A::arbitrary(g))
}

fn shrink(&self) -> Box<dyn Iterator<Item = [A; N]>> {
let cloned = self.clone();
let iter = (0..N).flat_map(move |n| {
let cloned = cloned.clone();
cloned[n].shrink().map(move |shr_value| {
let mut result = cloned.clone();
result[n] = shr_value;
result
})
});

Box::new(iter)
}
}

impl<A: Arbitrary> Arbitrary for Vec<A> {
fn arbitrary(g: &mut Gen) -> Vec<A> {
let size = {
Expand Down Expand Up @@ -1410,6 +1430,45 @@ mod test {
eq(Wrapping(0i32), vec![]);
}

#[test]
fn arrays() {
eq([false, false], vec![]);
eq([true, false], vec![[false, false]]);
eq([true, true], vec![[false, true], [true, false]]);

eq([false, false, false], vec![]);
eq([true, false, false], vec![[false, false, false]]);
eq(
[true, true, false],
vec![[false, true, false], [true, false, false]],
);

eq([false, false, false, false], vec![]);
eq([true, false, false, false], vec![[false, false, false, false]]);
eq(
[true, true, false, false],
vec![[false, true, false, false], [true, false, false, false]],
);

eq(
{
let it: [isize; 0] = [];
it
},
vec![],
);
eq(
{
let it: [[isize; 0]; 1] = [[]];
it
},
vec![],
);
eq([1isize; 1], vec![[0; 1]]);
eq([11isize; 1], vec![[0; 1], [6; 1], [9; 1], [10; 1]]);
eq([3isize, 5], vec![[0, 5], [2, 5], [3, 0], [3, 3], [3, 4]]);
}

#[test]
fn vecs() {
eq(
Expand Down