Robbepop / apint

Arbitrary precision integers library.
Other
26 stars 4 forks source link

Improve fuzz.rs #47

Open AaronKutch opened 5 years ago

AaronKutch commented 5 years ago

There are two problems I have right now:

This is not urgent, but I would prefer to fix the invariant problem at least before a new release of apint.

I am thinking about making a free function that checks all invariants and unwraps the results at the same time. As an example, it would change this:

            assert_eq!(
                lhs.clone()
                    .into_zero_extend(BitWidth::new(size * 2).unwrap()).unwrap()
                    .into_wrapping_mul(
                        &rhs.clone()
                            .into_zero_extend(BitWidth::new(size * 2).unwrap()).unwrap()
                    ).unwrap()
                    .into_zero_resize(rand_width),
                lhs.clone()
                    .into_wrapping_mul(&rhs).unwrap()
                    .into_zero_resize(rand_width)
            );

into this:

            assert_eq!(
               f(f(lhs.clone()
                    .into_zero_extend(BitWidth::new(size * 2)?))
                    .into_wrapping_mul(
                        &f(rhs.clone()
                            .into_zero_extend(BitWidth::new(size * 2)?))
                    ))
                    .into_zero_resize(rand_width),
                f(lhs.clone()
                    .into_wrapping_mul(&rhs))
                    .into_zero_resize(rand_width)
            );

Not much of an improvement, but at least invariants are checked. The only way I can think of which improves it more is doing evil stuff with feature flags, such as changing the Result type in the crate into something else which implements the Try trait. Maybe we could have a the feature flag change have #[cfg(feature_flag)] on the mod fuzz; (removing fuzz.rs entirely during any other compilation, so that users of the crate do not experience weird stuff when running with test).

The less weird stuff we are doing with feature flags though, the better. fuzz.rs is not intended to be very friendly to the eyes anyway, so I believe the free function approach is best.