Skip to content

Commit

Permalink
Add test suite
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Schultzer <[email protected]>
  • Loading branch information
Schultzer committed Jun 24, 2019
1 parent 2f69e15 commit e9e3c5f
Show file tree
Hide file tree
Showing 51 changed files with 570 additions and 37 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ checked = []
[workspace]
members = [
"crates/compiler-builtins-smoke-test",
"crates/libm-test",
]

[dependencies]
libm-test = { version = "0.*", path = "./crates/libm-test" }

[dev-dependencies]
no-panic = "0.1.8"
libm-test = { version = "0.*", path = "./crates/libm-test" }

[build-dependencies]
rand = { version = "0.6.5", optional = true }
4 changes: 2 additions & 2 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
set -ex
TARGET=$1

cargo test --target $TARGET
# cargo test --target $TARGET
cargo test --target $TARGET --release

cargo test --features 'checked musl-reference-tests' --target $TARGET
# cargo test --features 'checked musl-reference-tests' --target $TARGET

cargo test --features 'checked musl-reference-tests' --target $TARGET --release
13 changes: 13 additions & 0 deletions crates/libm-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "libm-test"
version = "0.1.0"
authors = ["Benjamin Schultzer <[email protected]>"]

[lib]
proc-macro = true
test = false

[dependencies]
proc-macro2 = { version = "0.4", features = ["nightly"] }
quote = "0.6"
syn = { version = "0.15", features = ["full", "extra-traits"] }
49 changes: 49 additions & 0 deletions crates/libm-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![recursion_limit = "256"]
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
extern crate core;
extern crate syn;

use proc_macro::TokenStream as TS;
use proc_macro2::TokenStream;
mod nearest;

#[proc_macro_attribute]
pub fn nearest(_attr: TS, item: TS) -> TS {
let item = match syn::parse::<syn::Item>(item) {
Ok(s) => s,
Err(e) => return e.to_compile_error().into(),
};
let func = match item {
syn::Item::Fn(ref f) => f,
_ => panic!("must be attached to a function"),
};
let name = &func.ident;
let mut tests = TokenStream::new();
match format!("{}", &name).as_ref() {
"ceil" => tests.extend(nearest::ceil(&name)),

"ceilf" => tests.extend(nearest::ceilf(&name)),

"floor" => tests.extend(nearest::floor(&name)),

"floorf" => tests.extend(nearest::floorf(&name)),

"round" => tests.extend(nearest::round(&name)),

"roundf" => tests.extend(nearest::roundf(&name)),

"trunc" => tests.extend(nearest::trunc(&name)),

"truncf" => tests.extend(nearest::truncf(&name)),

_ => {}
}
let tts: TokenStream = quote! {
#item
#tests
};
tts.into()
}
109 changes: 109 additions & 0 deletions crates/libm-test/src/nearest/b32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use proc_macro2::TokenStream;

pub fn nearest(func: &syn::Ident) -> TokenStream {
quote! {
#[test]
pub fn infinity_eq_infinity() {
assert_eq!(#func(::core::f32::INFINITY), ::core::f32::INFINITY);
}
#[test]
pub fn neg_infinity_eq_neg_infinity() {
assert_eq!(#func(::core::f32::NEG_INFINITY), ::core::f32::NEG_INFINITY);
}
#[test]
pub fn max_eq_max() {
assert_eq!(#func(::core::f32::MAX), ::core::f32::MAX);
}
#[test]
pub fn neg_max_eq_neg_max() {
assert_eq!(#func(-::core::f32::MAX), -::core::f32::MAX);
}
#[test]
pub fn nan_eq_nan() {
assert!(#func(::core::f32::NAN).is_nan());
}
#[test]
pub fn neg_nan_eq_nan() {
assert!(#func(-::core::f32::NAN).is_nan());
}
}
}
pub fn ceilf(func: &syn::Ident) -> TokenStream {
let nearest = nearest(&func);
let neg_min_subnorm_eq_neg_zero = neg_min_subnorm_eq_neg_zero(&func);
let neg_min_positive_eq_neg_zero = neg_min_positive_eq_neg_zero(&func);
quote! {
#neg_min_positive_eq_neg_zero
#[test]
pub fn min_positive_eq_one() {
assert_eq!(#func(::core::f32::MIN_POSITIVE), 1.);
}
#neg_min_subnorm_eq_neg_zero
#[test]
pub fn min_subnorm_eq_one() {
assert_eq!(#func(crate::F32_MIN_SUBNORM), 1.);
}
#[test]
pub fn neg_pi_eq_neg_three() {
assert_eq!(#func(-::core::f32::consts::PI), -3.);
}
#[test]
pub fn pi_eq_four() {
assert_eq!(#func(::core::f32::consts::PI), 4.);
}
#nearest
}
}

pub fn floorf(func: &syn::Ident) -> TokenStream {
quote! {
#[test]
pub fn neg_min_positive_eq_neg_one() {
assert_eq!(#func(-::core::f32::MIN_POSITIVE), -1.);
}
#[test]
pub fn pi_eq_neg_four() {
assert_eq!(#func(::core::f32::consts::PI), 3.);
}
#[test]
pub fn neg_pi_eq_three() {
assert_eq!(#func(-::core::f32::consts::PI), -4.);
}
#[test]
pub fn min_positive_eq_zero() {
assert_eq!(#func(::core::f32::MIN_POSITIVE), 0.);
}
#[test]
pub fn neg_min_subnorm_eq_one() {
assert_eq!(#func(-crate::F32_MIN_SUBNORM), -1.);
}
}
}

pub fn neg_min_positive_eq_neg_zero(func: &syn::Ident) -> TokenStream {
// round trunc ceil
quote! {
#[test]
pub fn neg_min_positive_eq_neg_zero() {
assert_eq!(#func(-::core::f32::MIN_POSITIVE), -0.);
}
}
}
pub fn neg_min_subnorm_eq_neg_zero(func: &syn::Ident) -> TokenStream {
// round trunc ceil
quote! {
#[test]
pub fn neg_min_subnorm_eq_neg_zero() {
assert_eq!(#func(-crate::F32_MIN_SUBNORM), -0.);
}
}
}
pub fn min_subnorm_eq_zero(func: &syn::Ident) -> TokenStream {
// round trunc floor
quote! {
#[test]
pub fn min_subnorm_eq_zero() {
assert_eq!(#func(crate::F32_MIN_SUBNORM), 0.);
}
}
}
109 changes: 109 additions & 0 deletions crates/libm-test/src/nearest/b64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use proc_macro2::TokenStream;

pub fn nearest(func: &syn::Ident) -> TokenStream {
quote! {
#[test]
pub fn infinity_eq_infinity() {
assert_eq!(#func(::core::f64::INFINITY), ::core::f64::INFINITY);
}
#[test]
pub fn neg_infinity_eq_neg_infinity() {
assert_eq!(#func(::core::f64::NEG_INFINITY), ::core::f64::NEG_INFINITY);
}
#[test]
pub fn max_eq_max() {
assert_eq!(#func(::core::f64::MAX), ::core::f64::MAX);
}
#[test]
pub fn neg_max_eq_neg_max() {
assert_eq!(#func(-::core::f64::MAX), -::core::f64::MAX);
}
#[test]
pub fn nan_eq_nan() {
assert!(#func(::core::f64::NAN).is_nan());
}
#[test]
pub fn neg_nan_eq_nan() {
assert!(#func(-::core::f64::NAN).is_nan());
}
}
}
pub fn ceil(func: &syn::Ident) -> TokenStream {
let nearest = nearest(&func);
let neg_min_subnorm_eq_neg_zero = neg_min_subnorm_eq_neg_zero(&func);
let neg_min_positive_eq_neg_zero = neg_min_positive_eq_neg_zero(&func);
quote! {
#neg_min_positive_eq_neg_zero
#[test]
pub fn min_positive_eq_one() {
assert_eq!(#func(::core::f64::MIN_POSITIVE), 1.);
}
#neg_min_subnorm_eq_neg_zero
#[test]
pub fn min_subnorm_eq_one() {
assert_eq!(#func(crate::F64_MIN_SUBNORM), 1.);
}
#[test]
pub fn neg_pi_eq_neg_three() {
assert_eq!(#func(-::core::f64::consts::PI), -3.);
}
#[test]
pub fn pi_eq_four() {
assert_eq!(#func(::core::f64::consts::PI), 4.);
}
#nearest
}
}

pub fn floor(func: &syn::Ident) -> TokenStream {
quote! {
#[test]
pub fn neg_min_positive_eq_neg_one() {
assert_eq!(#func(-::core::f64::MIN_POSITIVE), -1.);
}
#[test]
pub fn pi_eq_neg_four() {
assert_eq!(#func(::core::f64::consts::PI), 3.);
}
#[test]
pub fn neg_pi_eq_three() {
assert_eq!(#func(-::core::f64::consts::PI), -4.);
}
#[test]
pub fn min_positive_eq_zero() {
assert_eq!(#func(::core::f64::MIN_POSITIVE), 0.);
}
#[test]
pub fn neg_min_subnorm_eq_one() {
assert_eq!(#func(-crate::F64_MIN_SUBNORM), -1.);
}
}
}

pub fn neg_min_positive_eq_neg_zero(func: &syn::Ident) -> TokenStream {
// round trunc ceil
quote! {
#[test]
pub fn neg_min_positive_eq_neg_zero() {
assert_eq!(#func(-::core::f64::MIN_POSITIVE), -0.);
}
}
}
pub fn neg_min_subnorm_eq_neg_zero(func: &syn::Ident) -> TokenStream {
// round trunc ceil
quote! {
#[test]
pub fn neg_min_subnorm_eq_neg_zero() {
assert_eq!(#func(-crate::F64_MIN_SUBNORM), -0.);
}
}
}
pub fn min_subnorm_eq_zero(func: &syn::Ident) -> TokenStream {
// round trunc floor
quote! {
#[test]
pub fn min_subnorm_eq_zero() {
assert_eq!(#func(crate::F64_MIN_SUBNORM), 0.);
}
}
}
Loading

0 comments on commit e9e3c5f

Please sign in to comment.