Reltester
Relation tester is a small testing utility for automatically checking the correctness of PartialEq
, PartialOrd
, Eq
, and Ord
implementations. It's most useful when used in conjuction with quickcheck
or some other property-based testing framework.
Go to the docs!
Rationale
Imagine a scenario where you have a type Foo
with a custom implementation of either PartialEq
, Eq
, PartialOrd
, or Ord
. By "custom" we mean hand-written as opposed to derived. The Rust compiler alone cannot verify the correctness of these implementations and thus it is up to you, the programmer, to uphold certain invariants about the specific binary relation that you're implementing. For example, if you implement PartialEq
for Foo
, you must guarantee that foo1 == foo2
implies foo2 == foo1
(symmetry).
This is what reltester
is for. Rather than learning all subtle details of PartialEq
, Eq
, PartialOrd
, and Ord
, you can write some tests that will automatically check these invariants for you.
How to use
-
Write some tests that generate random values of the type you wish to test. You can do this by hand or using crates such as
quickcheck
andproptest
. -
Based on the traits that your type implements, call the appropriate checker:
reltester::eq
forEq
;reltester::ord
forOrd
;reltester::partial_eq
forPartialEq
;reltester::partial_ord
forPartialOrd
.
All of these functions take three arguments of the same type:
a
,b
, andc
. This is because it takes up to three values to test some invariants.
Please refer to the documentation for more advanced use cases.
A small example
use reltester;
use quickcheck_macros::quickcheck;
#[quickcheck]
fn test_f32(a: f32, b: f32, c: f32) -> bool {
// Let's check if `f32` implements `PartialEq` and `PartialOrd` correctly
// (spoiler: it does)
reltester::partial_eq(&a, &b, &c).is_ok()
&& reltester::partial_ord(&a, &b, &c).is_ok()
}
Legal
Reltester is available under the terms of the MIT license.