Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor iteration tests #6

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 133 additions & 85 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of bits in an integer starting with the first non-zero bit.
fn count_bits(self) -> u32 {
if self >= 0 {
1 + self.abs_diff(0).checked_ilog2().unwrap_or_default()
} else {
if self.is_negative() {
$min_value_bits
} else {
1 + self.abs_diff(0).checked_ilog2().unwrap_or_default()
}
}

Expand All @@ -564,20 +564,20 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of octal digits in an integer starting with the first non-zero digit.
fn count_octal_digits(self) -> u32 {
if self >= 0 {
1 + self.abs_diff(0).checked_ilog(8).unwrap_or_default()
} else {
if self.is_negative() {
$min_value_octal_digits
} else {
1 + self.abs_diff(0).checked_ilog(8).unwrap_or_default()
}
}

#[inline(always)]
/// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit.
fn count_hex_digits(self) -> u32 {
if self >= 0 {
1 + self.abs_diff(0).checked_ilog(16).unwrap_or_default()
} else {
if self.is_negative() {
$min_value_hex_digits
} else {
1 + self.abs_diff(0).checked_ilog(16).unwrap_or_default()
}
}

Expand All @@ -590,13 +590,13 @@ macro_rules! impl_count_digits {
10 => self.count_digits(),
16 => self.count_hex_digits(),
__ => {
if self >= 0 {
1 + self.abs_diff(0).checked_ilog(radix).unwrap_or_default()
} else {
if self.is_negative() {
1 + <$primitive_type>::MIN
.abs_diff(0)
.checked_ilog(radix)
.unwrap_or_default()
} else {
1 + self.abs_diff(0).checked_ilog(radix).unwrap_or_default()
}
}
}
Expand All @@ -609,10 +609,10 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of bits in an integer starting with the first non-zero bit.
fn count_bits(self) -> u32 {
if self.is_positive() {
1 + self.get().abs_diff(0).ilog2()
} else {
if self.is_negative() {
$min_value_bits
} else {
1 + self.get().abs_diff(0).ilog2()
}
}

Expand All @@ -625,20 +625,20 @@ macro_rules! impl_count_digits {
#[inline(always)]
/// Returns the count of octal digits in an integer starting with the first non-zero digit.
fn count_octal_digits(self) -> u32 {
if self.is_positive() {
1 + self.get().abs_diff(0).ilog(8)
} else {
if self.is_negative() {
$min_value_octal_digits
} else {
1 + self.get().abs_diff(0).ilog(8)
}
}

#[inline(always)]
/// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit.
fn count_hex_digits(self) -> u32 {
if self.is_positive() {
1 + self.get().abs_diff(0).ilog(16)
} else {
if self.is_negative() {
$min_value_hex_digits
} else {
1 + self.get().abs_diff(0).ilog(16)
}
}

Expand All @@ -651,10 +651,10 @@ macro_rules! impl_count_digits {
10 => self.count_digits(),
16 => self.count_hex_digits(),
__ => {
if self.is_positive() {
1 + self.get().abs_diff(0).ilog(radix)
} else {
if self.is_negative() {
1 + <$non_zero_type>::MIN.get().abs_diff(0).ilog(radix)
} else {
1 + self.get().abs_diff(0).ilog(radix)
}
}
}
Expand Down Expand Up @@ -888,7 +888,7 @@ mod count_digits {

macro_rules! hex_string_count {
($n:expr) => {
format!("{:X}", $n).len() as u32
format!("{:x}", $n).len() as u32
};
}

Expand Down Expand Up @@ -1020,53 +1020,125 @@ mod count_digits {
};
}

macro_rules! min_to_max_or_one_million {
($type:ty, $non_zero_type:ty) => {
macro_rules! lower_bound {
($type:ty) => {
-100_000 as $type
};
}

macro_rules! upper_bound {
($type:ty) => {
100_000 as $type
};
}

macro_rules! min_or_lower_bound {
(i8) => {
i8::MIN
};
(i16) => {
i16::MIN
};
(isize) => {{
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
const fn min_or_lower_bound() -> isize {
lower_bound!(isize)
}
#[cfg(any(target_pointer_width = "16", target_pointer_width = "8"))]
const fn min_or_lower_bound() -> isize {
isize::MIN
}
min_or_lower_bound()
}};
($type:ty) => {
lower_bound!($type)
};
}

macro_rules! max_or_upper_bound {
(i8) => {
i8::MAX
};
(i16) => {
i16::MAX
};
(u8) => {
u8::MAX
};
(u16) => {
u16::MAX
};
(isize) => {{
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
const fn max_or_upper_bound() -> isize {
upper_bound!(isize)
}
#[cfg(any(target_pointer_width = "16", target_pointer_width = "8"))]
const fn max_or_upper_bound() -> isize {
isize::MAX
}
max_or_upper_bound()
}};
(usize) => {{
#[cfg(any(target_pointer_width = "64", target_pointer_width = "32"))]
const fn max_or_upper_bound() -> usize {
upper_bound!(usize)
}
#[cfg(any(target_pointer_width = "16", target_pointer_width = "8"))]
const fn max_or_upper_bound() -> usize {
isize::MAX
}
max_or_upper_bound()
}};
($type:ty) => {
upper_bound!($type)
};
}

macro_rules! iteration {
(signed, $type:ty, $non_zero_type:ty) => {
paste! {
#[test]
#[allow(overflowing_literals)]
fn [<min_to_max_or_one_million_ $type>]() {
let max = if ($type::MAX as u128) < 1_000_000 { $type::MAX } else { 1_000_000 };
for n in $type::MIN..=max {
fn [<iteration_ $type>]() {
let max = max_or_upper_bound!($type);
let min = min_or_lower_bound!($type);
for n in min..=max {
assert_representations!(n);
}
}

#[test]
#[allow(non_snake_case)]
#[allow(overflowing_literals)]
fn [<min_to_max_or_one_million_ $non_zero_type>]() {
let max = if ($type::MAX as u128) < 1_000_000 { $type::MAX } else { 1_000_000 };
for n in $non_zero_type::MIN.get()..=max {
fn [<iteration_ $non_zero_type>]() {
let max = max_or_upper_bound!($type);
let min = min_or_lower_bound!($type);
for n in min..=max {
if n == 0 { continue; }
let n = $non_zero_type::new(n).unwrap();
assert_representations!(n);
}
}
}
};
}

macro_rules! min_or_negative_one_million_to_max_or_one_million {
($type:ty, $non_zero_type:ty) => {
(unsigned, $type:ty, $non_zero_type:ty) => {
paste! {
#[test]
#[allow(overflowing_literals)]
fn [<min_or_negative_one_million_to_max_or_one_million_ $type>]() {
let max = if ($type::MAX as u128) < 1_000_000 { $type::MAX } else { 1_000_000 };
let min = if ($type::MIN as i128) > -1_000_000 { $type::MIN } else { -1_000_000 };
for n in min..=max {
fn [<iteration_ $type>]() {
let max = max_or_upper_bound!($type);
for n in $type::MIN..=max {
assert_representations!(n);
}
}

#[test]
#[allow(non_snake_case)]
#[allow(overflowing_literals)]
fn [<min_or_negative_one_million_to_max_or_one_million_ $non_zero_type>]() {
let max = if ($type::MAX as u128) < 1_000_000 { $type::MAX } else { 1_000_000 };
let min = if ($type::MIN as i128) > -1_000_000 { $type::MIN } else { -1_000_000 };
for n in min..=max {
if n == 0 { continue; }
fn [<iteration_ $non_zero_type>]() {
let max = max_or_upper_bound!($type);
for n in $non_zero_type::MIN.get()..=max {
let n = $non_zero_type::new(n).unwrap();
assert_representations!(n);
}
Expand All @@ -1076,8 +1148,8 @@ mod count_digits {
}

macro_rules! add_test {
($name:ident, $type:ty, $non_zero_type:ty) => {
$name!($type, $non_zero_type);
($name:ident, $($args:tt)+) => {
$name!($($args)*);
};
}

Expand All @@ -1095,41 +1167,17 @@ mod count_digits {
add_test!(min_and_max, u128, NonZeroU128);
add_test!(min_and_max, usize, NonZeroUsize);

add_test!(min_to_max_or_one_million, u8, NonZeroU8);
add_test!(min_to_max_or_one_million, u16, NonZeroU16);
add_test!(min_to_max_or_one_million, u32, NonZeroU32);
add_test!(min_to_max_or_one_million, u64, NonZeroU64);
add_test!(min_to_max_or_one_million, u128, NonZeroU128);
add_test!(min_to_max_or_one_million, usize, NonZeroUsize);

add_test!(
min_or_negative_one_million_to_max_or_one_million,
i8,
NonZeroI8
);
add_test!(
min_or_negative_one_million_to_max_or_one_million,
i16,
NonZeroI16
);
add_test!(
min_or_negative_one_million_to_max_or_one_million,
i32,
NonZeroI32
);
add_test!(
min_or_negative_one_million_to_max_or_one_million,
i64,
NonZeroI64
);
add_test!(
min_or_negative_one_million_to_max_or_one_million,
i128,
NonZeroI128
);
add_test!(
min_or_negative_one_million_to_max_or_one_million,
isize,
NonZeroIsize
);
add_test!(iteration, signed, i8, NonZeroI8);
add_test!(iteration, signed, i16, NonZeroI16);
add_test!(iteration, signed, i32, NonZeroI32);
add_test!(iteration, signed, i64, NonZeroI64);
add_test!(iteration, signed, i128, NonZeroI128);
add_test!(iteration, signed, isize, NonZeroIsize);

add_test!(iteration, unsigned, u8, NonZeroU8);
add_test!(iteration, unsigned, u16, NonZeroU16);
add_test!(iteration, unsigned, u32, NonZeroU32);
add_test!(iteration, unsigned, u64, NonZeroU64);
add_test!(iteration, unsigned, u128, NonZeroU128);
add_test!(iteration, unsigned, usize, NonZeroUsize);
}
Loading