Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Delmas <[email protected]>
  • Loading branch information
tdelmas committed Jul 7, 2023
1 parent e878a9e commit 99ec370
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ members = [
"typed_floats_macros",
]

[profile.dev]
incremental = false

[profile.test]
incremental = false

[profile.release]
panic = "abort"
10 changes: 2 additions & 8 deletions typed_floats/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
[package]
name = "typed_floats"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Types for handling floats with type checking at compile time."
repository = "https://github.com/tdelmas/floats"
readme = "./README.md"

[profile.dev]
incremental = false

[profile.test]
incremental = false

[dependencies]
serde_json = { version = "1.0"}
thiserror = "1.0"
typed_floats_macros = { version = "0.1.7" }
typed_floats_macros = { version = "0.1.8" }

22 changes: 19 additions & 3 deletions typed_floats/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use typed_floats::*;
typed_floats_macros::generate_tests!();

#[test]
fn test_others() {
let values = [
fn test_others_f64() {
let values_f64 = [
(f64::NAN, false),
(f64::INFINITY, true),
(f64::NEG_INFINITY, true),
Expand All @@ -14,7 +14,7 @@ fn test_others() {
(-1.0f64, true),
];

for (value, expected) in &values {
for (value, expected) in &values_f64 {
let num = NonNaN::try_from(*value);
let result = num.is_ok();
assert_eq!(result, *expected);
Expand All @@ -28,3 +28,19 @@ fn test_others() {
}
}
}

#[test]
fn test_others_i64() {
let values_i64 = [
(0_i64, true),
(1_i64, true),
(-1_i64, true),
(i64::MAX, true),
];

for (value, expected) in &values_i64 {
let num = NonNaN::<f64>::try_from(*value);
let result = num.is_ok();
assert_eq!(result, *expected);
}
}
10 changes: 2 additions & 8 deletions typed_floats_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
[package]
name = "typed_floats_macros"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Crate only used to generate the `typed_floats` crate."
repository = "https://github.com/tdelmas/floats"
readme = "./README.md"

[profile.dev]
incremental = false

[profile.test]
incremental = false

[lib]
proc-macro = true

[dependencies]
quote = "1.0"
syn = { version = "2.0", features = ["extra-traits"] }
syn = { version = "2.0" }
proc-macro2 = "1.0"
9 changes: 5 additions & 4 deletions typed_floats_macros/src/gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ fn test_op_checks(
res
}

pub(crate) fn generate_tests() -> proc_macro::TokenStream {
let floats_f64 = get_definitions("f64");
pub(crate) fn generate_tests(float_type: &'static str) -> proc_macro2::TokenStream {
let floats_f64 = get_definitions(float_type);

let mut output = proc_macro2::TokenStream::new();

let float_type = floats_f64[0].float_type_ident();

let test_fn_name = quote::format_ident!("test_{}", float_type);

for float in &floats_f64 {
let float_type = float.float_type_ident();
let full_type = float.full_type_ident();
Expand Down Expand Up @@ -261,7 +263,7 @@ pub(crate) fn generate_tests() -> proc_macro::TokenStream {

quote! {
#[test]
fn test_floats() {
fn #test_fn_name() {

let values = [
#float_type::NAN,
Expand All @@ -282,5 +284,4 @@ pub(crate) fn generate_tests() -> proc_macro::TokenStream {
#output
}
}
.into()
}
10 changes: 9 additions & 1 deletion typed_floats_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ use add_doc::*;

mod gen_tests;

static F32: &'static str = "f32";
static F64: &'static str = "f64";

#[proc_macro]
pub fn generate_tests(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
gen_tests::generate_tests()
let mut output = proc_macro2::TokenStream::new();

output.extend(gen_tests::generate_tests(F32));
output.extend(gen_tests::generate_tests(F64));

output.into()
}

fn get_specifications() -> Vec<(&'static str, FloatSpecifications)> {
Expand Down

0 comments on commit 99ec370

Please sign in to comment.