|
| 1 | +#![feature(plugin)] |
| 2 | +#![plugin(stainless)] |
| 3 | +#![allow(deprecated)] |
| 4 | + |
| 5 | +extern crate ordered_float; |
| 6 | +extern crate num_traits; |
| 7 | + |
| 8 | +pub use ordered_float::*; |
| 9 | +pub use num_traits::Float; |
| 10 | +pub use std::cmp::Ordering::*; |
| 11 | +pub use std::{f32, f64, panic}; |
| 12 | + |
| 13 | +pub use std::collections::HashSet; |
| 14 | +pub use std::collections::hash_map::RandomState; |
| 15 | +pub use std::hash::*; |
| 16 | + |
| 17 | +describe! ordered_float32 { |
| 18 | + it "should compare regular floats" { |
| 19 | + assert_eq!(OrderedFloat(7.0f32).cmp(&OrderedFloat(7.0)), Equal); |
| 20 | + assert_eq!(OrderedFloat(8.0f32).cmp(&OrderedFloat(7.0)), Greater); |
| 21 | + assert_eq!(OrderedFloat(4.0f32).cmp(&OrderedFloat(7.0)), Less); |
| 22 | + } |
| 23 | + |
| 24 | + it "should compare NaN" { |
| 25 | + let f32_nan: f32 = Float::nan(); |
| 26 | + assert_eq!(OrderedFloat(f32_nan).cmp(&OrderedFloat(Float::nan())), Equal); |
| 27 | + assert_eq!(OrderedFloat(f32_nan).cmp(&OrderedFloat(-100000.0f32)), Greater); |
| 28 | + assert_eq!(OrderedFloat(-100.0f32).cmp(&OrderedFloat(Float::nan())), Less); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe! ordered_float64 { |
| 33 | + it "should compare regular floats" { |
| 34 | + assert_eq!(OrderedFloat(7.0f64).cmp(&OrderedFloat(7.0)), Equal); |
| 35 | + assert_eq!(OrderedFloat(8.0f64).cmp(&OrderedFloat(7.0)), Greater); |
| 36 | + assert_eq!(OrderedFloat(4.0f64).cmp(&OrderedFloat(7.0)), Less); |
| 37 | + } |
| 38 | + |
| 39 | + it "should compare NaN" { |
| 40 | + let f64_nan: f64 = Float::nan(); |
| 41 | + assert_eq!(OrderedFloat(f64_nan).cmp(&OrderedFloat(Float::nan())), Equal); |
| 42 | + assert_eq!(OrderedFloat(f64_nan).cmp(&OrderedFloat(-100000.0f64)), Greater); |
| 43 | + assert_eq!(OrderedFloat(-100.0f64).cmp(&OrderedFloat(Float::nan())), Less); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe! not_nan32 { |
| 48 | + it "should compare regular floats" { |
| 49 | + assert_eq!(NotNaN::from(7.0f32).cmp(&NotNaN::from(7.0)), Equal); |
| 50 | + assert_eq!(NotNaN::from(8.0f32).cmp(&NotNaN::from(7.0)), Greater); |
| 51 | + assert_eq!(NotNaN::from(4.0f32).cmp(&NotNaN::from(7.0)), Less); |
| 52 | + } |
| 53 | + |
| 54 | + it "should fail when constructing NotNaN with NaN" { |
| 55 | + let f32_nan: f32 = Float::nan(); |
| 56 | + assert!(NotNaN::new(f32_nan).is_err()); |
| 57 | + } |
| 58 | + |
| 59 | + it "should calculate correctly" { |
| 60 | + assert_eq!(*(NotNaN::from(5.0f32) + NotNaN::from(4.0f32)), 5.0f32 + 4.0f32); |
| 61 | + assert_eq!(*(NotNaN::from(5.0f32) + 4.0f32), 5.0f32 + 4.0f32); |
| 62 | + assert_eq!(*(NotNaN::from(5.0f32) - NotNaN::from(4.0f32)), 5.0f32 - 4.0f32); |
| 63 | + assert_eq!(*(NotNaN::from(5.0f32) - 4.0f32), 5.0f32 - 4.0f32); |
| 64 | + assert_eq!(*(NotNaN::from(5.0f32) * NotNaN::from(4.0f32)), 5.0f32 * 4.0f32); |
| 65 | + assert_eq!(*(NotNaN::from(5.0f32) * 4.0f32), 5.0f32 * 4.0f32); |
| 66 | + assert_eq!(*(NotNaN::from(8.0f32) / NotNaN::from(4.0f32)), 8.0f32 / 4.0f32); |
| 67 | + assert_eq!(*(NotNaN::from(8.0f32) / 4.0f32), 8.0f32 / 4.0f32); |
| 68 | + assert_eq!(*(NotNaN::from(8.0f32) % NotNaN::from(4.0f32)), 8.0f32 % 4.0f32); |
| 69 | + assert_eq!(*(NotNaN::from(8.0f32) % 4.0f32), 8.0f32 % 4.0f32); |
| 70 | + assert_eq!(*(-NotNaN::from(1.0f32)), -1.0f32); |
| 71 | + |
| 72 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f32) + f32::NAN}).is_err()); |
| 73 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f32) - f32::NAN}).is_err()); |
| 74 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f32) * f32::NAN}).is_err()); |
| 75 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f32) / f32::NAN}).is_err()); |
| 76 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f32) % f32::NAN}).is_err()); |
| 77 | + |
| 78 | + let mut number = NotNaN::from(5.0f32); |
| 79 | + number += NotNaN::from(4.0f32); |
| 80 | + assert_eq!(*number, 9.0f32); |
| 81 | + number -= NotNaN::from(4.0f32); |
| 82 | + assert_eq!(*number, 5.0f32); |
| 83 | + number *= NotNaN::from(4.0f32); |
| 84 | + assert_eq!(*number, 20.0f32); |
| 85 | + number /= NotNaN::from(4.0f32); |
| 86 | + assert_eq!(*number, 5.0f32); |
| 87 | + number %= NotNaN::from(4.0f32); |
| 88 | + assert_eq!(*number, 1.0f32); |
| 89 | + |
| 90 | + number = NotNaN::from(5.0f32); |
| 91 | + number += 4.0f32; |
| 92 | + assert_eq!(*number, 9.0f32); |
| 93 | + number -= 4.0f32; |
| 94 | + assert_eq!(*number, 5.0f32); |
| 95 | + number *= 4.0f32; |
| 96 | + assert_eq!(*number, 20.0f32); |
| 97 | + number /= 4.0f32; |
| 98 | + assert_eq!(*number, 5.0f32); |
| 99 | + number %= 4.0f32; |
| 100 | + assert_eq!(*number, 1.0f32); |
| 101 | + |
| 102 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f32); tmp += f32::NAN;}).is_err()); |
| 103 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f32); tmp -= f32::NAN;}).is_err()); |
| 104 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f32); tmp *= f32::NAN;}).is_err()); |
| 105 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f32); tmp /= f32::NAN;}).is_err()); |
| 106 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f32); tmp %= f32::NAN;}).is_err()); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +describe! not_nan64 { |
| 111 | + it "should compare regular floats" { |
| 112 | + assert_eq!(NotNaN::from(7.0f64).cmp(&NotNaN::from(7.0)), Equal); |
| 113 | + assert_eq!(NotNaN::from(8.0f64).cmp(&NotNaN::from(7.0)), Greater); |
| 114 | + assert_eq!(NotNaN::from(4.0f64).cmp(&NotNaN::from(7.0)), Less); |
| 115 | + } |
| 116 | + |
| 117 | + it "should fail when constructing NotNaN with NaN" { |
| 118 | + let f64_nan: f64 = Float::nan(); |
| 119 | + assert!(NotNaN::new(f64_nan).is_err()); |
| 120 | + } |
| 121 | + |
| 122 | + it "should calculate correctly" { |
| 123 | + assert_eq!(*(NotNaN::from(5.0f64) + NotNaN::from(4.0f64)), 5.0f64 + 4.0f64); |
| 124 | + assert_eq!(*(NotNaN::from(5.0f64) + 4.0f64), 5.0f64 + 4.0f64); |
| 125 | + assert_eq!(*(NotNaN::from(5.0f64) - NotNaN::from(4.0f64)), 5.0f64 - 4.0f64); |
| 126 | + assert_eq!(*(NotNaN::from(5.0f64) - 4.0f64), 5.0f64 - 4.0f64); |
| 127 | + assert_eq!(*(NotNaN::from(5.0f64) * NotNaN::from(4.0f64)), 5.0f64 * 4.0f64); |
| 128 | + assert_eq!(*(NotNaN::from(5.0f64) * 4.0f64), 5.0f64 * 4.0f64); |
| 129 | + assert_eq!(*(NotNaN::from(8.0f64) / NotNaN::from(4.0f64)), 8.0f64 / 4.0f64); |
| 130 | + assert_eq!(*(NotNaN::from(8.0f64) / 4.0f64), 8.0f64 / 4.0f64); |
| 131 | + assert_eq!(*(NotNaN::from(8.0f64) % NotNaN::from(4.0f64)), 8.0f64 % 4.0f64); |
| 132 | + assert_eq!(*(NotNaN::from(8.0f64) % 4.0f64), 8.0f64 % 4.0f64); |
| 133 | + assert_eq!(*(-NotNaN::from(1.0f64)), -1.0f64); |
| 134 | + |
| 135 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f64) + f64::NAN}).is_err()); |
| 136 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f64) - f64::NAN}).is_err()); |
| 137 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f64) * f64::NAN}).is_err()); |
| 138 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f64) / f64::NAN}).is_err()); |
| 139 | + assert!(panic::catch_unwind(|| {NotNaN::from(0.0f64) % f64::NAN}).is_err()); |
| 140 | + |
| 141 | + let mut number = NotNaN::from(5.0f64); |
| 142 | + number += NotNaN::from(4.0f64); |
| 143 | + assert_eq!(*number, 9.0f64); |
| 144 | + number -= NotNaN::from(4.0f64); |
| 145 | + assert_eq!(*number, 5.0f64); |
| 146 | + number *= NotNaN::from(4.0f64); |
| 147 | + assert_eq!(*number, 20.0f64); |
| 148 | + number /= NotNaN::from(4.0f64); |
| 149 | + assert_eq!(*number, 5.0f64); |
| 150 | + number %= NotNaN::from(4.0f64); |
| 151 | + assert_eq!(*number, 1.0f64); |
| 152 | + |
| 153 | + number = NotNaN::from(5.0f64); |
| 154 | + number += 4.0f64; |
| 155 | + assert_eq!(*number, 9.0f64); |
| 156 | + number -= 4.0f64; |
| 157 | + assert_eq!(*number, 5.0f64); |
| 158 | + number *= 4.0f64; |
| 159 | + assert_eq!(*number, 20.0f64); |
| 160 | + number /= 4.0f64; |
| 161 | + assert_eq!(*number, 5.0f64); |
| 162 | + number %= 4.0f64; |
| 163 | + assert_eq!(*number, 1.0f64); |
| 164 | + |
| 165 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f64); tmp += f64::NAN;}).is_err()); |
| 166 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f64); tmp -= f64::NAN;}).is_err()); |
| 167 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f64); tmp *= f64::NAN;}).is_err()); |
| 168 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f64); tmp /= f64::NAN;}).is_err()); |
| 169 | + assert!(panic::catch_unwind(|| {let mut tmp = NotNaN::from(0.0f64); tmp %= f64::NAN;}).is_err()); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +describe! hashing { |
| 174 | + it "should hash zero and neg-zero to the same hc" { |
| 175 | + let state = RandomState::new(); |
| 176 | + let mut h1 = state.build_hasher(); |
| 177 | + let mut h2 = state.build_hasher(); |
| 178 | + OrderedFloat::from(0f64).hash(&mut h1); |
| 179 | + OrderedFloat::from(-0f64).hash(&mut h2); |
| 180 | + assert_eq!(h1.finish(), h2.finish()); |
| 181 | + } |
| 182 | + |
| 183 | + it "should hash inf and neg-inf to different hcs" { |
| 184 | + let state = RandomState::new(); |
| 185 | + let mut h1 = state.build_hasher(); |
| 186 | + let mut h2 = state.build_hasher(); |
| 187 | + OrderedFloat::from(f64::INFINITY).hash(&mut h1); |
| 188 | + OrderedFloat::from(f64::NEG_INFINITY).hash(&mut h2); |
| 189 | + assert!(h1.finish() != h2.finish()); |
| 190 | + } |
| 191 | + |
| 192 | + it "should have a good hash function for whole numbers" { |
| 193 | + let state = RandomState::new(); |
| 194 | + let limit = 10000; |
| 195 | + |
| 196 | + let mut set = ::std::collections::HashSet::with_capacity(limit); |
| 197 | + for i in 0..limit { |
| 198 | + let mut h = state.build_hasher(); |
| 199 | + OrderedFloat::from(i as f64).hash(&mut h); |
| 200 | + set.insert(h.finish()); |
| 201 | + } |
| 202 | + |
| 203 | + // This allows 100 collisions, which is far too |
| 204 | + // many, but should guard against transient issues |
| 205 | + // that will result from using RandomState |
| 206 | + let pct_unique = set.len() as f64 / limit as f64; |
| 207 | + assert!(0.99f64 < pct_unique, "percent-unique={}", pct_unique); |
| 208 | + } |
| 209 | + |
| 210 | + it "should have a good hash function for fractional numbers" { |
| 211 | + let state = RandomState::new(); |
| 212 | + let limit = 10000; |
| 213 | + |
| 214 | + let mut set = ::std::collections::HashSet::with_capacity(limit); |
| 215 | + for i in 0..limit { |
| 216 | + let mut h = state.build_hasher(); |
| 217 | + OrderedFloat::from(i as f64 * (1f64 / limit as f64)).hash(&mut h); |
| 218 | + set.insert(h.finish()); |
| 219 | + } |
| 220 | + |
| 221 | + // This allows 100 collisions, which is far too |
| 222 | + // many, but should guard against transient issues |
| 223 | + // that will result from using RandomState |
| 224 | + let pct_unique = set.len() as f64 / limit as f64; |
| 225 | + assert!(0.99f64 < pct_unique, "percent-unique={}", pct_unique); |
| 226 | + } |
| 227 | +} |
0 commit comments