lundi 25 décembre 2017

Using QuickCheck with structures in Rust

I want to use QuickCheck to test a function that makes use of structs that I have created. I see I need to implement the Arbitrary trait for these types. I have something like this:

#[cfg(test)]
use quickcheck::{Arbitrary, Gen, QuickCheck};

const NUM_WORDS: usize = 12;

#[derive(Copy, Clone)]
pub struct Element(pub(crate) [u64; NUM_WORDS]);

#[cfg(test)]
impl Arbitrary for Element {
    fn arbitrary<G: Gen>(g: &mut G) -> Element {
        let e: [u64; NUM_WORDS] = g.gen::<[u64; NUM_WORDS]>();
        Element(e)
    }
}

#[derive(Clone, Debug)]
struct ExtensionElement {
    A: Element,
    B: Element,
}

#[cfg(test)]
impl Arbitrary for ExtensionElement {
    fn arbitrary<G: Gen>(g: &mut G) -> ExtensionElement {
        let a = g.gen::<Element>();
        let b = g.gen::<Element>();
        ExtensionElement { A: a, B: b }
    }
}

When I try to use ExtensionElement inside a QuickCheck test, I get the following error message inside the arbitrary function for ExtensionElement:

the trait 'rand::Rand' is not implemented for 'Element'

How I can implement the Arbitrary trait so that I can use my structs with QuickCheck?

Aucun commentaire:

Enregistrer un commentaire