Skip to content

Commit 72cf7c5

Browse files
committed
[WIP] Add tests for floating point add
1 parent 4cf590f commit 72cf7c5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/float/add.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,59 @@ pub extern fn __aeabi_dadd(a: f64, b: f64) -> f64 {
191191
pub extern fn __aeabi_dadd(a: f32, b: f32) -> f32 {
192192
__addsf3(a, b)
193193
}
194+
195+
#[cfg(test)]
196+
mod test {
197+
use core::{f32, f64};
198+
use qc::{U32, U64};
199+
use float::Float;
200+
201+
// TODO: Add F32/F64 to qc so that they print the right values (at the very least)
202+
quickcheck! {
203+
fn addsf3(a: U32, b: U32) -> bool {
204+
let (a, b) = (a.0, b.0);
205+
let r = super::__addsf3(<f32 as Float>::from_repr(a), <f32 as Float>::from_repr(b));
206+
r == <f32 as Float>::from_repr(a) + <f32 as Float>::from_repr(b)
207+
}
208+
209+
fn adddf3(a: U64, b: U64) -> bool {
210+
let (a, b) = (a.0, b.0);
211+
let r = super::__adddf3(<f64 as Float>::from_repr(a), <f64 as Float>::from_repr(b));
212+
r == <f64 as Float>::from_repr(a) + <f64 as Float>::from_repr(b)
213+
}
214+
}
215+
216+
// More tests for special float values
217+
218+
#[test]
219+
fn test_float_one_plus_one() {
220+
let r = super::__addsf3(<f32 as Float>::from_repr(1), <f32 as Float>::from_repr(1));
221+
assert_eq!(r, <f32 as Float>::from_repr(1) + <f32 as Float>::from_repr(1));
222+
}
223+
224+
#[test]
225+
fn test_float_nan() {
226+
let r = super::__addsf3(f32::NAN, 1.23);
227+
// Need .repr() since NAN != NAN
228+
assert_eq!(r.repr(), f32::NAN.repr());
229+
}
230+
231+
#[test]
232+
fn test_double_nan() {
233+
let r = super::__adddf3(f64::NAN, 1.23);
234+
// Need .repr() since NAN != NAN
235+
assert_eq!(r.repr(), f64::NAN.repr());
236+
}
237+
238+
#[test]
239+
fn test_float_inf() {
240+
let r = super::__addsf3(f32::INFINITY, -123.4);
241+
assert_eq!(r, f32::INFINITY);
242+
}
243+
244+
#[test]
245+
fn test_double_inf() {
246+
let r = super::__adddf3(f64::INFINITY, -123.4);
247+
assert_eq!(r, f64::INFINITY);
248+
}
249+
}

0 commit comments

Comments
 (0)