Skip to content

Commit

Permalink
Reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nordzilla committed Jan 25, 2024
1 parent 3c07703 commit b76108f
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,23 +1003,6 @@ mod count_digits {
};
}

macro_rules! min_and_max {
($type:ty, $non_zero_type:ty) => {
paste! {
#[test]
fn [<min_and_max_ $type>]() {
assert_min_and_max!($type);
}

#[test]
#[allow(non_snake_case)]
fn [<min_and_max_ $non_zero_type>]() {
assert_min_and_max!($type);
}
}
};
}

macro_rules! lower_bound {
($type:ty) => {
-100_000 as $type
Expand Down Expand Up @@ -1095,12 +1078,50 @@ mod count_digits {
};
}

/// Returns an iterator over the pairs boundaries (n, m) for a given radix
/// where n is the largest number of its digit-size group and m is the smallest
/// number of its digit-size group.
///
/// Using radix 2 would give (001, 002) -> (003, 004) -> (007, 008) -> etc.
/// Using radix 8 would give (007, 008) -> (063, 064) -> (511, 512) -> etc.
macro_rules! radix_boundaries {
($type:ty, $radix:expr) => {
std::iter::successors(Some(1 as $type), move |n| n.checked_mul($radix))
.skip(1)
.map(|n| (n - 1, n))
};
}

/// Returns an iterator of increasing pairs staring with (1, 2).
/// e.g. (1, 2) -> (2, 3) -> (3, 4) -> (4, 5) -> (5, 6) -> etc.
pub fn increasing_pairs() -> impl Iterator<Item = (u32, u32)> {
std::iter::successors(Some(1u32), move |n| n.checked_add(1))
.zip(std::iter::successors(Some(2u32), move |n| n.checked_add(1)))
}

macro_rules! min_and_max {
($type:ty, $non_zero_type:ty) => {
paste! {
#[test]
fn [<$type _min_and_max>]() {
assert_min_and_max!($type);
}

#[test]
#[allow(non_snake_case)]
fn [<$non_zero_type _min_and_max>]() {
assert_min_and_max!($type);
}
}
};
}

macro_rules! iteration {
(signed, $type:ty, $non_zero_type:ty) => {
paste! {
#[test]
#[allow(overflowing_literals)]
fn [<iteration_ $type>]() {
fn [<$type _iteration>]() {
let max = max_or_upper_bound!($type);
let min = min_or_lower_bound!($type);
for n in min..=max {
Expand All @@ -1111,7 +1132,7 @@ mod count_digits {
#[test]
#[allow(non_snake_case)]
#[allow(overflowing_literals)]
fn [<iteration_ $non_zero_type>]() {
fn [<$non_zero_type _iteration>]() {
let max = max_or_upper_bound!($type);
let min = min_or_lower_bound!($type);
for n in min..=max {
Expand All @@ -1126,7 +1147,7 @@ mod count_digits {
paste! {
#[test]
#[allow(overflowing_literals)]
fn [<iteration_ $type>]() {
fn [<$type _iteration>]() {
let max = max_or_upper_bound!($type);
for n in $type::MIN..=max {
assert_representations!(n);
Expand All @@ -1136,7 +1157,7 @@ mod count_digits {
#[test]
#[allow(non_snake_case)]
#[allow(overflowing_literals)]
fn [<iteration_ $non_zero_type>]() {
fn [<$non_zero_type _iteration>]() {
let max = max_or_upper_bound!($type);
for n in $non_zero_type::MIN.get()..=max {
let n = $non_zero_type::new(n).unwrap();
Expand All @@ -1147,27 +1168,6 @@ mod count_digits {
};
}

/// Returns an iterator over the pairs boundaries (n, m) for a given radix
/// where n is the largest number of its digit-size group and m is the smallest
/// number of its digit-size group.
///
/// Using radix 2 would give (001, 002) -> (003, 004) -> (007, 008) -> etc.
/// Using radix 8 would give (007, 008) -> (063, 064) -> (511, 512) -> etc.
macro_rules! radix_boundaries {
($type:ty, $radix:expr) => {
std::iter::successors(Some(1 as $type), move |n| n.checked_mul($radix))
.skip(1)
.map(|n| (n - 1, n))
};
}

/// Returns an iterator of increasing pairs staring with (1, 2).
/// e.g. (1, 2) -> (2, 3) -> (3, 4) -> (4, 5) -> (5, 6) -> etc.
pub fn increasing_pairs() -> impl Iterator<Item = (u32, u32)> {
std::iter::successors(Some(1u32), move |n| n.checked_add(1))
.zip(std::iter::successors(Some(2u32), move |n| n.checked_add(1)))
}

macro_rules! boundaries_for_radix {
($type:ty, $non_zero_type:ty) => {
boundaries_for_radix!($type, $non_zero_type, 2);
Expand All @@ -1193,7 +1193,7 @@ mod count_digits {
($type:ty, $non_zero_type:ty, $radix:expr) => {
paste! {
#[test]
fn [<boundaries_for_radix_ $radix _ $type>]() {
fn [<$type _boundaries_for_radix_ $radix>]() {
assert!(
radix_boundaries!($type, $radix)
.map(|(n, m)| (n.count_digits_radix($radix), m.count_digits_radix($radix)))
Expand All @@ -1205,7 +1205,7 @@ mod count_digits {
}
#[test]
#[allow(non_snake_case)]
fn [<boundaries_for_radix_ $radix _ $non_zero_type>]() {
fn [<$non_zero_type _boundaries_for_radix_ $radix>]() {
assert!(
radix_boundaries!($type, $radix)
.map(|(n, m)| (<$non_zero_type>::new(n).unwrap(), <$non_zero_type>::new(m).unwrap()))
Expand Down

0 comments on commit b76108f

Please sign in to comment.