From 73da122627d2081113e450202c39ad1ecc7a5157 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 5 Dec 2016 22:40:27 +0200 Subject: Playing with documentation tests and quickcheck --- Cargo.toml | 1 + src/complex.rs | 45 ++++++++++++++++++++++++++++++++------------- src/lib.rs | 4 ++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab5ca58..bb8b755 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" authors = ["Justin Worthe "] [dependencies] +quickcheck = "0.4.0" \ No newline at end of file diff --git a/src/complex.rs b/src/complex.rs index 228ba8f..1161fe5 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -45,6 +45,12 @@ impl Complex where T: Trig + Pow + ArithmeticOps + Copy { impl Add for Complex where T: ArithmeticOps + Copy { type Output = Complex; + /// ``` + /// use worthe_signals::complex::Complex; + /// let a = Complex::new(1, 5); + /// let b = Complex::new(-3, 2); + /// assert_eq!(a+b, Complex::new(-2, 7)); + /// ``` fn add(self, other: Self) -> Self { let real = self.real + other.real; let imag = self.imag + other.imag; @@ -55,6 +61,12 @@ impl Add for Complex where T: ArithmeticOps + Copy { impl Sub for Complex where T: ArithmeticOps + Copy { type Output = Complex; + /// ``` + /// use worthe_signals::complex::Complex; + /// let a = Complex::new(1, 5); + /// let b = Complex::new(-3, 2); + /// assert_eq!(a-b, Complex::new(4, 3)); + /// ``` fn sub(self, other: Self) -> Self { let real = self.real - other.real; let imag = self.imag - other.imag; @@ -97,22 +109,29 @@ impl Neg for Complex where T: SignedArithmeticOps + Copy { } #[cfg(test)] -mod tests { +mod tests { use super::*; use std::f32; - - #[test] - fn addition() { - let a = Complex::new(1, 5); - let b = Complex::new(-3, 2); - assert_eq!(a+b, Complex::new(-2, 7)); - } - #[test] - fn subtraction() { - let a = Complex::new(1, 5); - let b = Complex::new(-3, 2); - assert_eq!(a-b, Complex::new(4, 3)); + mod addition { + use super::super::*; + use std::i32; + + quickcheck! { + fn zero(real: i32, imag: i32) -> bool { + let com = Complex::new(real, imag); + let zero = Complex::new(0, 0); + com == com + zero + } + fn double(real: i32, imag: i32) -> bool { + let com = Complex::new(real, imag); + com+com == com*Complex::new(2, 0) + } + fn inverse(real: i32, imag: i32) -> bool { + let com = Complex::new(real, imag); + com == com+com-com + } + } } #[test] diff --git a/src/lib.rs b/src/lib.rs index 70b7829..623bd2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +#[cfg(test)] +#[macro_use] +extern crate quickcheck; + pub mod complex; pub mod num_traits; pub mod sinusoid; -- cgit v1.2.3