summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml1
-rw-r--r--src/complex.rs45
-rw-r--r--src/lib.rs4
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 <justin.worthe@gmail.com>"]
[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<T> Complex<T> where T: Trig + Pow + ArithmeticOps + Copy {
impl<T> Add for Complex<T> where T: ArithmeticOps + Copy {
type Output = Complex<T>;
+ /// ```
+ /// 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<T> Add for Complex<T> where T: ArithmeticOps + Copy {
impl<T> Sub for Complex<T> where T: ArithmeticOps + Copy {
type Output = Complex<T>;
+ /// ```
+ /// 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<T> Neg for Complex<T> 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;