Skip to content

Commit 0180f60

Browse files
committed
Add Quickcheck types for float tests
1 parent 8a98e06 commit 0180f60

File tree

3 files changed

+118
-114
lines changed

3 files changed

+118
-114
lines changed

src/float/add.rs

+7-97
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ mod tests {
188188
use core::{f32, f64};
189189

190190
use float::Float;
191-
use qc::{U32, U64};
191+
use qc::{F32, F64};
192192

193193
// NOTE The tests below have special handing for NaN values.
194194
// Because NaN != NaN, the floating-point representations must be used
@@ -212,108 +212,18 @@ mod tests {
212212
}
213213
}
214214

215-
// TODO: Add F32/F64 to qc so that they print the right values (at the very least)
216215
check! {
217216
fn __addsf3(f: extern fn(f32, f32) -> f32,
218-
a: U32,
219-
b: U32)
217+
a: F32,
218+
b: F32)
220219
-> Option<FRepr<f32> > {
221-
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
222-
Some(FRepr(f(a, b)))
220+
Some(FRepr(f(a.0, b.0)))
223221
}
224222

225223
fn __adddf3(f: extern fn(f64, f64) -> f64,
226-
a: U64,
227-
b: U64) -> Option<FRepr<f64> > {
228-
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
229-
Some(FRepr(f(a, b)))
224+
a: F64,
225+
b: F64) -> Option<FRepr<f64> > {
226+
Some(FRepr(f(a.0, b.0)))
230227
}
231228
}
232-
233-
// More tests for special float values
234-
235-
#[test]
236-
fn test_float_tiny_plus_tiny() {
237-
let tiny = f32::from_repr(1);
238-
let r = super::__addsf3(tiny, tiny);
239-
assert!(r.eq_repr(tiny + tiny));
240-
}
241-
242-
#[test]
243-
fn test_double_tiny_plus_tiny() {
244-
let tiny = f64::from_repr(1);
245-
let r = super::__adddf3(tiny, tiny);
246-
assert!(r.eq_repr(tiny + tiny));
247-
}
248-
249-
#[test]
250-
fn test_float_small_plus_small() {
251-
let a = f32::from_repr(327);
252-
let b = f32::from_repr(256);
253-
let r = super::__addsf3(a, b);
254-
assert!(r.eq_repr(a + b));
255-
}
256-
257-
#[test]
258-
fn test_double_small_plus_small() {
259-
let a = f64::from_repr(327);
260-
let b = f64::from_repr(256);
261-
let r = super::__adddf3(a, b);
262-
assert!(r.eq_repr(a + b));
263-
}
264-
265-
#[test]
266-
fn test_float_one_plus_one() {
267-
let r = super::__addsf3(1f32, 1f32);
268-
assert!(r.eq_repr(1f32 + 1f32));
269-
}
270-
271-
#[test]
272-
fn test_double_one_plus_one() {
273-
let r = super::__adddf3(1f64, 1f64);
274-
assert!(r.eq_repr(1f64 + 1f64));
275-
}
276-
277-
#[test]
278-
fn test_float_different_nan() {
279-
let a = f32::from_repr(1);
280-
let b = f32::from_repr(0b11111111100100010001001010101010);
281-
let x = super::__addsf3(a, b);
282-
let y = a + b;
283-
assert!(x.eq_repr(y));
284-
}
285-
286-
#[test]
287-
fn test_double_different_nan() {
288-
let a = f64::from_repr(1);
289-
let b = f64::from_repr(
290-
0b1111111111110010001000100101010101001000101010000110100011101011);
291-
let x = super::__adddf3(a, b);
292-
let y = a + b;
293-
assert!(x.eq_repr(y));
294-
}
295-
296-
#[test]
297-
fn test_float_nan() {
298-
let r = super::__addsf3(f32::NAN, 1.23);
299-
assert_eq!(r.repr(), f32::NAN.repr());
300-
}
301-
302-
#[test]
303-
fn test_double_nan() {
304-
let r = super::__adddf3(f64::NAN, 1.23);
305-
assert_eq!(r.repr(), f64::NAN.repr());
306-
}
307-
308-
#[test]
309-
fn test_float_inf() {
310-
let r = super::__addsf3(f32::INFINITY, -123.4);
311-
assert_eq!(r, f32::INFINITY);
312-
}
313-
314-
#[test]
315-
fn test_double_inf() {
316-
let r = super::__adddf3(f64::INFINITY, -123.4);
317-
assert_eq!(r, f64::INFINITY);
318-
}
319229
}

src/float/mod.rs

+56-17
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ pub trait Float: Sized + Copy {
1616
/// Returns the bitwidth of the significand
1717
fn significand_bits() -> u32;
1818

19-
/// Returns `self` transmuted to `Self::Int`
20-
fn repr(self) -> Self::Int;
19+
/// Returns a mask for the sign bit of `self`
20+
fn sign_mask() -> Self::Int;
2121

22-
#[cfg(test)]
23-
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
24-
/// represented in multiple different ways. This methods returns `true` if two NaNs are
25-
/// compared.
26-
fn eq_repr(self, rhs: Self) -> bool;
22+
/// Returns a mask for the exponent portion of `self`
23+
fn exponent_mask() -> Self::Int;
2724

28-
/// Returns a `Self::Int` transmuted back to `Self`
29-
fn from_repr(a: Self::Int) -> Self;
25+
/// Returns a mask for the significand portion of `self`
26+
fn significand_mask() -> Self::Int;
3027

3128
/// Returns the sign bit of `self`
3229
fn sign(self) -> bool;
@@ -37,6 +34,21 @@ pub trait Float: Sized + Copy {
3734
/// Returns the significand portion of `self`
3835
fn significand(self) -> Self::Int;
3936

37+
/// Returns `self` transmuted to `Self::Int`
38+
fn repr(self) -> Self::Int;
39+
40+
#[cfg(test)]
41+
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
42+
/// represented in multiple different ways. This method returns `true` if two NaNs are
43+
/// compared.
44+
fn eq_repr(self, rhs: Self) -> bool;
45+
46+
/// Returns a `Self::Int` transmuted back to `Self`
47+
fn from_repr(a: Self::Int) -> Self;
48+
49+
/// Constructs a `Self` from its parts
50+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
51+
4052
/// Returns (normalized exponent, normalized significand)
4153
fn normalize(significand: Self::Int) -> (i32, Self::Int);
4254
}
@@ -52,6 +64,15 @@ impl Float for f32 {
5264
fn significand_bits() -> u32 {
5365
23
5466
}
67+
fn sign_mask() -> Self::Int {
68+
1 << (Self::bits() - 1)
69+
}
70+
fn exponent_mask() -> Self::Int {
71+
((1 << Self::exponent_bits()) - 1) << Self::significand_bits()
72+
}
73+
fn significand_mask() -> Self::Int {
74+
(1 << Self::significand_bits()) - 1
75+
}
5576
fn repr(self) -> Self::Int {
5677
unsafe { mem::transmute(self) }
5778
}
@@ -66,15 +87,20 @@ impl Float for f32 {
6687
fn from_repr(a: Self::Int) -> Self {
6788
unsafe { mem::transmute(a) }
6889
}
90+
91+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
92+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
93+
exponent & Self::exponent_mask() |
94+
significand & Self::significand_mask())
95+
}
6996
fn sign(self) -> bool {
70-
(self.repr() & 1 << Self::bits()) != 0
97+
(self.repr() & Self::sign_mask()) != 0
7198
}
7299
fn exponent(self) -> Self::Int {
73-
self.repr() >> Self::significand_bits()
74-
& ((1 << Self::exponent_bits()) - 1)
100+
self.repr() >> Self::significand_bits() & Self::exponent_mask()
75101
}
76102
fn significand(self) -> Self::Int {
77-
self.repr() & ((1 << Self::significand_bits()) - 1)
103+
self.repr() & Self::significand_mask()
78104
}
79105
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
80106
let shift = significand.leading_zeros()
@@ -93,6 +119,15 @@ impl Float for f64 {
93119
fn significand_bits() -> u32 {
94120
52
95121
}
122+
fn sign_mask() -> Self::Int {
123+
1 << (Self::bits() - 1)
124+
}
125+
fn exponent_mask() -> Self::Int {
126+
((1 << Self::exponent_bits()) - 1) << Self::significand_bits()
127+
}
128+
fn significand_mask() -> Self::Int {
129+
(1 << Self::significand_bits()) - 1
130+
}
96131
fn repr(self) -> Self::Int {
97132
unsafe { mem::transmute(self) }
98133
}
@@ -107,15 +142,19 @@ impl Float for f64 {
107142
fn from_repr(a: Self::Int) -> Self {
108143
unsafe { mem::transmute(a) }
109144
}
145+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
146+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
147+
exponent & Self::exponent_mask() |
148+
significand & Self::significand_mask())
149+
}
110150
fn sign(self) -> bool {
111-
(self.repr() & 1 << Self::bits()) != 0
151+
(self.repr() & Self::sign_mask()) != 0
112152
}
113153
fn exponent(self) -> Self::Int {
114-
self.repr() >> Self::significand_bits()
115-
& ((1 << Self::exponent_bits()) - 1)
154+
self.repr() >> Self::significand_bits() & Self::exponent_mask()
116155
}
117156
fn significand(self) -> Self::Int {
118-
self.repr() & ((1 << Self::significand_bits()) - 1)
157+
self.repr() & Self::significand_mask()
119158
}
120159
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
121160
let shift = significand.leading_zeros()

src/qc.rs

+55
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
use std::boxed::Box;
77
use std::fmt;
8+
use core::{f32, f64};
89

910
use quickcheck::{Arbitrary, Gen};
1011

1112
use int::LargeInt;
13+
use float::Float;
1214

1315
// Generates values in the full range of the integer type
1416
macro_rules! arbitrary {
@@ -71,6 +73,7 @@ macro_rules! arbitrary {
7173
arbitrary!(I32: i32);
7274
arbitrary!(U32: u32);
7375

76+
7477
// These integers are "too large". If we generate e.g. `u64` values in the full range then there's
7578
// only `1 / 2^32` chance of seeing a value smaller than `2^32` (i.e. whose higher "word" (32-bits)
7679
// is `0`)! But this is an important group of values to tests because we have special code paths for
@@ -143,6 +146,57 @@ macro_rules! arbitrary_large {
143146
arbitrary_large!(I64: i64);
144147
arbitrary_large!(U64: u64);
145148

149+
150+
macro_rules! arbitrary_float {
151+
($TY:ident : $ty:ident) => {
152+
#[derive(Clone, Copy)]
153+
pub struct $TY(pub $ty);
154+
155+
impl Arbitrary for $TY {
156+
fn arbitrary<G>(g: &mut G) -> $TY
157+
where G: Gen
158+
{
159+
let special = [
160+
-0.0, 0.0, $ty::NAN, $ty::INFINITY, -$ty::INFINITY
161+
];
162+
163+
if g.gen_weighted_bool(10) { // Random special case
164+
$TY(*g.choose(&special).unwrap())
165+
} else if g.gen_weighted_bool(10) { // NaN variants
166+
let sign: bool = g.gen();
167+
let exponent: <$ty as Float>::Int = g.gen();
168+
let significand: <$ty as Float>::Int = 0;
169+
$TY($ty::from_parts(sign, exponent, significand))
170+
} else if g.gen() { // Denormalized
171+
let sign: bool = g.gen();
172+
let exponent: <$ty as Float>::Int = 0;
173+
let significand: <$ty as Float>::Int = g.gen();
174+
$TY($ty::from_parts(sign, exponent, significand))
175+
} else { // Random anything
176+
let sign: bool = g.gen();
177+
let exponent: <$ty as Float>::Int = g.gen();
178+
let significand: <$ty as Float>::Int = g.gen();
179+
$TY($ty::from_parts(sign, exponent, significand))
180+
}
181+
}
182+
183+
fn shrink(&self) -> Box<Iterator<Item=$TY>> {
184+
::quickcheck::empty_shrinker()
185+
}
186+
}
187+
188+
impl fmt::Debug for $TY {
189+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190+
fmt::Debug::fmt(&self.0, f)
191+
}
192+
}
193+
}
194+
}
195+
196+
arbitrary_float!(F32: f32);
197+
arbitrary_float!(F64: f64);
198+
199+
146200
// Convenience macro to test intrinsics against their reference implementations.
147201
//
148202
// Each intrinsic is tested against both the `gcc_s` library as well as
@@ -263,3 +317,4 @@ macro_rules! check {
263317
}
264318
)
265319
}
320+

0 commit comments

Comments
 (0)