about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-09-02 23:01:06 +0000
committerbors <bors@rust-lang.org>2014-09-02 23:01:06 +0000
commit4e5d5ba1ed7365f515b438b3ab4fcda31b182faf (patch)
tree7521b9e0178ae0b2669b0d2e339822797bdf25e7
parent0e7e107ad69cc6f7facdc9401456a333361b5555 (diff)
parent46e6e42d7a17d33da0fd13f2afd81a566ec08ceb (diff)
downloadrust-4e5d5ba1ed7365f515b438b3ab4fcda31b182faf.tar.gz
rust-4e5d5ba1ed7365f515b438b3ab4fcda31b182faf.zip
auto merge of #16850 : vks/rust/hash-num, r=alexcrichton
Updates #15294.
-rw-r--r--src/libnum/complex.rs23
-rw-r--r--src/libnum/rational.rs9
2 files changed, 24 insertions, 8 deletions
diff --git a/src/libnum/complex.rs b/src/libnum/complex.rs
index 66412eb678f..24c99a38bd9 100644
--- a/src/libnum/complex.rs
+++ b/src/libnum/complex.rs
@@ -12,13 +12,13 @@
 //! Complex numbers.
 
 use std::fmt;
-use std::num::{Zero,One,ToStrRadix};
+use std::num::{Zero, One, ToStrRadix};
 
 // FIXME #1284: handle complex NaN & infinity etc. This
 // probably doesn't map to C's _Complex correctly.
 
 /// A complex number in Cartesian form.
-#[deriving(PartialEq,Clone)]
+#[deriving(PartialEq, Clone, Hash)]
 pub struct Complex<T> {
     /// Real portion of the complex number
     pub re: T,
@@ -36,10 +36,8 @@ impl<T: Clone + Num> Complex<T> {
         Complex { re: re, im: im }
     }
 
-    /**
-    Returns the square of the norm (since `T` doesn't necessarily
-    have a sqrt function), i.e. `re^2 + im^2`.
-    */
+    /// Returns the square of the norm (since `T` doesn't necessarily
+    /// have a sqrt function), i.e. `re^2 + im^2`.
     #[inline]
     pub fn norm_sqr(&self) -> T {
         self.re * self.re + self.im * self.im
@@ -193,7 +191,8 @@ mod test {
     #![allow(non_uppercase_statics)]
 
     use super::{Complex64, Complex};
-    use std::num::{Zero,One,Float};
+    use std::num::{Zero, One, Float};
+    use std::hash::hash;
 
     pub static _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 };
     pub static _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 };
@@ -367,4 +366,14 @@ mod test {
         test(-_neg1_1i, "1-1i".to_string());
         test(_05_05i, "0.5+0.5i".to_string());
     }
+
+    #[test]
+    fn test_hash() {
+        let a = Complex::new(0i32, 0i32);
+        let b = Complex::new(1i32, 0i32);
+        let c = Complex::new(0i32, 1i32);
+        assert!(hash(&a) != hash(&b));
+        assert!(hash(&b) != hash(&c));
+        assert!(hash(&c) != hash(&a));
+    }
 }
diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs
index 6f85460ab95..56f3f532533 100644
--- a/src/libnum/rational.rs
+++ b/src/libnum/rational.rs
@@ -21,7 +21,7 @@ use std::num::{Zero, One, ToStrRadix, FromStrRadix};
 use bigint::{BigInt, BigUint, Sign, Plus, Minus};
 
 /// Represents the ratio between 2 numbers.
-#[deriving(Clone)]
+#[deriving(Clone, Hash)]
 #[allow(missing_doc)]
 pub struct Ratio<T> {
     numer: T,
@@ -380,6 +380,7 @@ mod test {
     use super::{Ratio, Rational, BigRational};
     use std::num::{Zero, One, FromStrRadix, FromPrimitive, ToStrRadix};
     use std::from_str::FromStr;
+    use std::hash::hash;
     use std::num;
 
     pub static _0 : Rational = Ratio { numer: 0, denom: 1};
@@ -751,4 +752,10 @@ mod test {
         assert!(! _neg1_2.is_positive());
         assert!(! _1_2.is_negative());
     }
+
+    #[test]
+    fn test_hash() {
+        assert!(hash(&_0) != hash(&_1));
+        assert!(hash(&_0) != hash(&_3_2));
+    }
 }