Skip to content

Commit

Permalink
Fix #84 core::cmp::Ordering impl and add tests (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelmas authored Feb 18, 2024
1 parent 03859f0 commit c3208f7
Showing 1 changed file with 68 additions and 17 deletions.
85 changes: 68 additions & 17 deletions typed_floats/src/types/impls/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::{
};

macro_rules! impl_ord {
($type:ident) => {
($test:ident, $type:ident) => {
impl Ord for $type<f32> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
if *self < *other {
if self.get() < other.get() {
core::cmp::Ordering::Less
} else if *self == *other {
} else if self.get() == other.get() {
core::cmp::Ordering::Equal
} else {
core::cmp::Ordering::Greater
Expand All @@ -24,9 +24,9 @@ macro_rules! impl_ord {
impl Ord for $type<f64> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
if *self < *other {
if self.get() < other.get() {
core::cmp::Ordering::Less
} else if *self == *other {
} else if self.get() == other.get() {
core::cmp::Ordering::Equal
} else {
core::cmp::Ordering::Greater
Expand All @@ -47,18 +47,69 @@ macro_rules! impl_ord {
Some(self.cmp(other))
}
}

#[cfg(test)]
mod $test {
extern crate std;
use crate::*;
use std::vec::Vec; // Required for the tests to compile in no_std mode

fn is_sorted<T: Ord>(slice: &[T]) -> bool {
slice.windows(2).all(|w| w[0] <= w[1])
}

#[test]
fn f32() {
let values = tf32::TEST_VALUES
.iter()
.map(|&x| tf32::$type::new(x))
.filter_map(|x| x.ok())
.collect::<Vec<_>>();

assert!(is_sorted(&values));

let reversed = values.iter().rev().collect::<Vec<_>>();

assert!(!is_sorted(&reversed));

let mut sorted = reversed.clone();
sorted.sort();

assert!(is_sorted(&sorted));
}

#[test]
fn f64() {
let values = tf64::TEST_VALUES
.iter()
.map(|&x| tf64::$type::new(x))
.filter_map(|x| x.ok())
.collect::<Vec<_>>();

assert!(is_sorted(&values));

let reversed = values.iter().rev().collect::<Vec<_>>();

assert!(!is_sorted(&reversed));

let mut sorted = reversed.clone();
sorted.sort();

assert!(is_sorted(&sorted));
}
}
};
}

impl_ord!(NonNaN);
impl_ord!(NonZeroNonNaN);
impl_ord!(NonNaNFinite);
impl_ord!(NonZeroNonNaNFinite);
impl_ord!(Positive);
impl_ord!(Negative);
impl_ord!(PositiveFinite);
impl_ord!(NegativeFinite);
impl_ord!(StrictlyPositive);
impl_ord!(StrictlyNegative);
impl_ord!(StrictlyPositiveFinite);
impl_ord!(StrictlyNegativeFinite);
impl_ord!(non_nan, NonNaN);
impl_ord!(non_zero_non_nan, NonZeroNonNaN);
impl_ord!(non_nan_finite, NonNaNFinite);
impl_ord!(non_zero_non_nan_finite, NonZeroNonNaNFinite);
impl_ord!(positive, Positive);
impl_ord!(negative, Negative);
impl_ord!(positive_finite, PositiveFinite);
impl_ord!(negative_finite, NegativeFinite);
impl_ord!(strictly_positive, StrictlyPositive);
impl_ord!(strictly_negative, StrictlyNegative);
impl_ord!(strictly_positive_finite, StrictlyPositiveFinite);
impl_ord!(strictly_negative_finite, StrictlyNegativeFinite);

0 comments on commit c3208f7

Please sign in to comment.