From c33b59d8f872a859d61abeefaac7312683ad817a Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Sun, 8 Oct 2023 14:05:12 +0400 Subject: [PATCH] refactor(test): reorganize tests --- src/i256/mod.rs | 22 +++++++------- src/i256/u256.rs | 2 +- tests/it/const_ctor/mod.rs | 21 ++++++++++++++ .../const_ctor/too_long_fractional.rs} | 0 .../const_ctor/too_long_fractional.stderr} | 4 +-- tests/{test_convert.rs => it/convert.rs} | 2 -- .../convert_f64.rs} | 2 -- .../convert_str.rs} | 4 +-- tests/{macros.rs => it/main.rs} | 29 +++++++++++-------- tests/{test_ops.rs => it/ops.rs} | 2 -- tests/{test_serde.rs => it/serde.rs} | 2 -- tests/test_const_fn.rs | 6 ---- 12 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 tests/it/const_ctor/mod.rs rename tests/{const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs => it/const_ctor/too_long_fractional.rs} (100%) rename tests/{const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.stderr => it/const_ctor/too_long_fractional.stderr} (86%) rename tests/{test_convert.rs => it/convert.rs} (99%) rename tests/{test_convert_f64.rs => it/convert_f64.rs} (99%) rename tests/{test_convert_string.rs => it/convert_str.rs} (99%) rename tests/{macros.rs => it/main.rs} (89%) rename tests/{test_ops.rs => it/ops.rs} (99%) rename tests/{test_serde.rs => it/serde.rs} (99%) delete mode 100644 tests/test_const_fn.rs diff --git a/src/i256/mod.rs b/src/i256/mod.rs index e8597a7..cf55741 100644 --- a/src/i256/mod.rs +++ b/src/i256/mod.rs @@ -195,17 +195,17 @@ mod tests { use super::*; #[test] - fn it_calculates_min() { + fn min() { assert_eq!(i128::try_from(I256::I128_MIN).unwrap(), i128::MIN); } #[test] - fn it_calculates_max() { + fn max() { assert_eq!(i128::try_from(I256::I128_MAX).unwrap(), i128::MAX); } #[test] - fn it_compares() { + fn cmp() { use core::cmp::Ordering::{self, *}; fn t(a: i128, b: i128, ord: Ordering) { let a = I256::from(a); @@ -219,7 +219,7 @@ mod tests { } #[test] - fn it_converts_i256_from_i128() { + fn from_i128() { fn t(x: i128) { assert_eq!(i128::try_from(I256::from(x)).unwrap(), x); } @@ -233,7 +233,7 @@ mod tests { } #[test] - fn it_negates_i128() { + fn neg_i128() { fn t(x: i128) { assert_eq!(i128::try_from(-I256::from(x)).unwrap(), -x); assert_eq!(i128::try_from(-I256::from(-x)).unwrap(), x); @@ -245,7 +245,7 @@ mod tests { } #[test] - fn it_negates_i256() { + fn neg_i256() { fn t(value: I256, expected: I256) { let actual: I256 = -value; assert_eq!(actual, expected); @@ -270,12 +270,12 @@ mod tests { #[test] #[should_panic] - fn it_doesnt_negate_i256_min() { + fn neg_i256_min() { let _x = -I256::MIN; } #[test] - fn it_adds() { + fn add() { fn t(a: i128, b: i128, expected: i128) { let a = I256::from(a); let b = I256::from(b); @@ -291,7 +291,7 @@ mod tests { } #[test] - fn it_subtracts() { + fn sub() { fn t(a: i128, b: i128, expected: i128) { let a = I256::from(a); let b = I256::from(b); @@ -307,7 +307,7 @@ mod tests { } #[test] - fn it_multiplies() { + fn mul() { fn t(a: i128, b: i128, expected: i128) { let a = I256::from(a); let b = I256::from(b); @@ -322,7 +322,7 @@ mod tests { } #[test] - fn it_divides() { + fn div() { fn t(a: i128, b: i128, expected: i128) { let a = I256::from(a); let b = I256::from(b); diff --git a/src/i256/u256.rs b/src/i256/u256.rs index 5765698..9b30cf5 100644 --- a/src/i256/u256.rs +++ b/src/i256/u256.rs @@ -853,7 +853,7 @@ mod tests { use super::*; #[test] - fn it_counts_leading_zeros() { + fn leading_zeros() { fn t(x: U256, expected: u32) { assert_eq!(x.leading_zeros(), expected); } diff --git a/tests/it/const_ctor/mod.rs b/tests/it/const_ctor/mod.rs new file mode 100644 index 0000000..9b30c37 --- /dev/null +++ b/tests/it/const_ctor/mod.rs @@ -0,0 +1,21 @@ +use fixnum::{fixnum_const, FixedPoint}; + +#[test] +fn valid() { + type F64p9 = FixedPoint; + + const SAMPLE0: F64p9 = fixnum_const!(42.42, 9); + assert_eq!(SAMPLE0, F64p9::from_decimal(4242, -2).unwrap()); + + const SAMPLE1: F64p9 = fixnum_const!(42, 9); + assert_eq!(SAMPLE1, F64p9::from_decimal(42, 0).unwrap()); + + const SAMPLE2: F64p9 = fixnum_const!(42., 9); + assert_eq!(SAMPLE2, F64p9::from_decimal(42, 0).unwrap()); +} + +#[test] +fn too_long_fractional() { + let test_cases = trybuild::TestCases::new(); + test_cases.compile_fail("tests/it/const_ctor/too_long_fractional.rs"); +} diff --git a/tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs b/tests/it/const_ctor/too_long_fractional.rs similarity index 100% rename from tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs rename to tests/it/const_ctor/too_long_fractional.rs diff --git a/tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.stderr b/tests/it/const_ctor/too_long_fractional.stderr similarity index 86% rename from tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.stderr rename to tests/it/const_ctor/too_long_fractional.stderr index 753ccef..50912d3 100644 --- a/tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.stderr +++ b/tests/it/const_ctor/too_long_fractional.stderr @@ -7,7 +7,7 @@ error: constant evaluation is taking a long time = note: this lint makes sure the compiler doesn't get stuck due to infinite loops in const eval. If your compilation actually takes a long time, you can safely allow the lint. help: the constant being evaluated - --> tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs:7:36 + --> tests/it/const_ctor/too_long_fractional.rs:7:36 | 7 | const VALUE: FixedPoint = fixnum_const!(0.1234567891, 9); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -15,7 +15,7 @@ help: the constant being evaluated = note: this error originates in the macro `const_assert` which comes from the expansion of the macro `fixnum_const` (in Nightly builds, run with -Z macro-backtrace for more info) note: erroneous constant used - --> tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs:7:36 + --> tests/it/const_ctor/too_long_fractional.rs:7:36 | 7 | const VALUE: FixedPoint = fixnum_const!(0.1234567891, 9); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/test_convert.rs b/tests/it/convert.rs similarity index 99% rename from tests/test_convert.rs rename to tests/it/convert.rs index 0bf237f..60c99b7 100644 --- a/tests/test_convert.rs +++ b/tests/it/convert.rs @@ -4,8 +4,6 @@ use proptest::prelude::*; use fixnum::ops::Bounded; -mod macros; - #[test] fn from_decimal() -> Result<()> { test_fixed_point! { diff --git a/tests/test_convert_f64.rs b/tests/it/convert_f64.rs similarity index 99% rename from tests/test_convert_f64.rs rename to tests/it/convert_f64.rs index d7438c2..aadb5a4 100644 --- a/tests/test_convert_f64.rs +++ b/tests/it/convert_f64.rs @@ -4,8 +4,6 @@ use proptest::prelude::*; use fixnum::*; -mod macros; - #[test] #[allow(clippy::float_cmp)] fn to_f64() -> Result<()> { diff --git a/tests/test_convert_string.rs b/tests/it/convert_str.rs similarity index 99% rename from tests/test_convert_string.rs rename to tests/it/convert_str.rs index 2e77968..df0e47e 100644 --- a/tests/test_convert_string.rs +++ b/tests/it/convert_str.rs @@ -7,9 +7,7 @@ use anyhow::Result; #[cfg(feature = "i128")] use proptest::prelude::*; -use self::macros::TestCaseResult; - -mod macros; +use crate::TestCaseResult; #[test] #[allow(overflowing_literals)] diff --git a/tests/macros.rs b/tests/it/main.rs similarity index 89% rename from tests/macros.rs rename to tests/it/main.rs index a9f5628..6bad759 100644 --- a/tests/macros.rs +++ b/tests/it/main.rs @@ -1,7 +1,4 @@ -#![allow(dead_code)] - -/// Testing helper macro. Allows to use generalized `FixedPoint` and `Layout` types in the test cases. -#[macro_export] +/// Allows to use generalized `FixedPoint` and `Layout` types in the test cases. macro_rules! test_fixed_point { ( case ($( $case_pattern:ident: $case_type:ty ),* $( , )?) => $case:block, @@ -11,7 +8,7 @@ macro_rules! test_fixed_point { ) => {{ macro_rules! impl_test_case { () => { - fn test_case($( $case_pattern: $case_type ),*) -> $crate::macros::TestCaseResult { + fn test_case($( $case_pattern: $case_type ),*) -> $crate::TestCaseResult { $case Ok(()) } @@ -90,15 +87,15 @@ macro_rules! test_fixed_point { }; (@suite_passes {$( ($( $args:expr )*) )*}) => { $( - $crate::macros::r#impl::catch_and_augment(stringify!($( $args ),*), || { + $crate::r#impl::catch_and_augment(stringify!($( $args ),*), || { test_case($( $args ),*) })?; )* }; (@suite_fails {$( ($( $args:expr )*) )*}) => { $( - $crate::macros::r#impl::catch_and_augment(stringify!($( $args ),*), || { - $crate::macros::r#impl::assert_fails(|| test_case($( $args ),*)); + $crate::r#impl::catch_and_augment(stringify!($( $args ),*), || { + $crate::r#impl::assert_fails(|| test_case($( $args ),*)); Ok(()) })?; )* @@ -108,8 +105,8 @@ macro_rules! test_fixed_point { use std::fmt::Display; // Use a special error based on `Display` in order to support `nostd`. -pub(crate) type TestCaseResult = Result<(), TestCaseError>; -pub(crate) struct TestCaseError(Box); +type TestCaseResult = Result<(), TestCaseError>; +struct TestCaseError(Box); impl From for TestCaseError { fn from(error: E) -> Self { @@ -125,7 +122,7 @@ impl From for anyhow::Error { } #[cfg(not(feature = "std"))] -pub(crate) mod r#impl { +mod r#impl { use anyhow::Result; use super::TestCaseResult; @@ -141,7 +138,7 @@ pub(crate) mod r#impl { } #[cfg(feature = "std")] -pub(crate) mod r#impl { +mod r#impl { use std::panic::{catch_unwind, AssertUnwindSafe}; use anyhow::{anyhow, Context, Result}; @@ -180,3 +177,11 @@ pub(crate) mod r#impl { } } } + +// Tests +mod const_ctor; +mod convert; +mod convert_f64; +mod convert_str; +mod ops; +mod serde; diff --git a/tests/test_ops.rs b/tests/it/ops.rs similarity index 99% rename from tests/test_ops.rs rename to tests/it/ops.rs index 6dd60e8..05b675b 100644 --- a/tests/test_ops.rs +++ b/tests/it/ops.rs @@ -7,8 +7,6 @@ use fixnum::{ *, }; -mod macros; - #[test] fn cmul_overflow() -> Result<()> { test_fixed_point! { diff --git a/tests/test_serde.rs b/tests/it/serde.rs similarity index 99% rename from tests/test_serde.rs rename to tests/it/serde.rs index 02c1942..40cca8c 100644 --- a/tests/test_serde.rs +++ b/tests/it/serde.rs @@ -4,8 +4,6 @@ use anyhow::Result; use derive_more::{From, Into}; use serde::{Deserialize, Serialize}; -mod macros; - #[test] fn display_and_serde() -> Result<()> { test_fixed_point! { diff --git a/tests/test_const_fn.rs b/tests/test_const_fn.rs deleted file mode 100644 index 81d2be3..0000000 --- a/tests/test_const_fn.rs +++ /dev/null @@ -1,6 +0,0 @@ -#[test] -fn const_fn() { - let test_cases = trybuild::TestCases::new(); - test_cases - .compile_fail("tests/const_fn/01_fixnum_const_bad_str_with_too_long_fractional_part.rs"); -}