Skip to content

Commit

Permalink
refactor(test): reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Oct 8, 2023
1 parent 22696f8 commit c33b59d
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 43 deletions.
22 changes: 11 additions & 11 deletions src/i256/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/i256/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/it/const_ctor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use fixnum::{fixnum_const, FixedPoint};

#[test]
fn valid() {
type F64p9 = FixedPoint<i64, typenum::U9>;

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");
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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<i64, U9> = fixnum_const!(0.1234567891, 9);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[deny(long_running_const_eval)]` on by default
= 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<i64, U9> = fixnum_const!(0.1234567891, 9);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/test_convert.rs → tests/it/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use proptest::prelude::*;

use fixnum::ops::Bounded;

mod macros;

#[test]
fn from_decimal() -> Result<()> {
test_fixed_point! {
Expand Down
2 changes: 0 additions & 2 deletions tests/test_convert_f64.rs → tests/it/convert_f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use proptest::prelude::*;

use fixnum::*;

mod macros;

#[test]
#[allow(clippy::float_cmp)]
fn to_f64() -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions tests/test_convert_string.rs → tests/it/convert_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
29 changes: 17 additions & 12 deletions tests/macros.rs → tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
})?;
)*
Expand All @@ -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<dyn Display>);
type TestCaseResult = Result<(), TestCaseError>;
struct TestCaseError(Box<dyn Display>);

impl<E: Display + 'static> From<E> for TestCaseError {
fn from(error: E) -> Self {
Expand All @@ -125,7 +122,7 @@ impl From<TestCaseError> for anyhow::Error {
}

#[cfg(not(feature = "std"))]
pub(crate) mod r#impl {
mod r#impl {
use anyhow::Result;

use super::TestCaseResult;
Expand All @@ -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};
Expand Down Expand Up @@ -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;
2 changes: 0 additions & 2 deletions tests/test_ops.rs → tests/it/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use fixnum::{
*,
};

mod macros;

#[test]
fn cmul_overflow() -> Result<()> {
test_fixed_point! {
Expand Down
2 changes: 0 additions & 2 deletions tests/test_serde.rs → tests/it/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
6 changes: 0 additions & 6 deletions tests/test_const_fn.rs

This file was deleted.

0 comments on commit c33b59d

Please sign in to comment.