@@ -7,13 +7,13 @@ use crate::{
77} ;
88
99macro_rules! impl_ord {
10- ( $type: ident) => {
10+ ( $test : ident , $ type: ident) => {
1111 impl Ord for $type<f32 > {
1212 #[ inline]
1313 fn cmp( & self , other: & Self ) -> core:: cmp:: Ordering {
14- if * self < * other {
14+ if self . get ( ) < other. get ( ) {
1515 core:: cmp:: Ordering :: Less
16- } else if * self == * other {
16+ } else if self . get ( ) == other. get ( ) {
1717 core:: cmp:: Ordering :: Equal
1818 } else {
1919 core:: cmp:: Ordering :: Greater
@@ -24,9 +24,9 @@ macro_rules! impl_ord {
2424 impl Ord for $type<f64 > {
2525 #[ inline]
2626 fn cmp( & self , other: & Self ) -> core:: cmp:: Ordering {
27- if * self < * other {
27+ if self . get ( ) < other. get ( ) {
2828 core:: cmp:: Ordering :: Less
29- } else if * self == * other {
29+ } else if self . get ( ) == other. get ( ) {
3030 core:: cmp:: Ordering :: Equal
3131 } else {
3232 core:: cmp:: Ordering :: Greater
@@ -47,18 +47,69 @@ macro_rules! impl_ord {
4747 Some ( self . cmp( other) )
4848 }
4949 }
50+
51+ #[ cfg( test) ]
52+ mod $test {
53+ extern crate std;
54+ use crate :: * ;
55+ use std:: vec:: Vec ; // Required for the tests to compile in no_std mode
56+
57+ fn is_sorted<T : Ord >( slice: & [ T ] ) -> bool {
58+ slice. windows( 2 ) . all( |w| w[ 0 ] <= w[ 1 ] )
59+ }
60+
61+ #[ test]
62+ fn f32 ( ) {
63+ let values = tf32:: TEST_VALUES
64+ . iter( )
65+ . map( |& x| tf32:: $type:: new( x) )
66+ . filter_map( |x| x. ok( ) )
67+ . collect:: <Vec <_>>( ) ;
68+
69+ assert!( is_sorted( & values) ) ;
70+
71+ let reversed = values. iter( ) . rev( ) . collect:: <Vec <_>>( ) ;
72+
73+ assert!( !is_sorted( & reversed) ) ;
74+
75+ let mut sorted = reversed. clone( ) ;
76+ sorted. sort( ) ;
77+
78+ assert!( is_sorted( & sorted) ) ;
79+ }
80+
81+ #[ test]
82+ fn f64 ( ) {
83+ let values = tf64:: TEST_VALUES
84+ . iter( )
85+ . map( |& x| tf64:: $type:: new( x) )
86+ . filter_map( |x| x. ok( ) )
87+ . collect:: <Vec <_>>( ) ;
88+
89+ assert!( is_sorted( & values) ) ;
90+
91+ let reversed = values. iter( ) . rev( ) . collect:: <Vec <_>>( ) ;
92+
93+ assert!( !is_sorted( & reversed) ) ;
94+
95+ let mut sorted = reversed. clone( ) ;
96+ sorted. sort( ) ;
97+
98+ assert!( is_sorted( & sorted) ) ;
99+ }
100+ }
50101 } ;
51102}
52103
53- impl_ord ! ( NonNaN ) ;
54- impl_ord ! ( NonZeroNonNaN ) ;
55- impl_ord ! ( NonNaNFinite ) ;
56- impl_ord ! ( NonZeroNonNaNFinite ) ;
57- impl_ord ! ( Positive ) ;
58- impl_ord ! ( Negative ) ;
59- impl_ord ! ( PositiveFinite ) ;
60- impl_ord ! ( NegativeFinite ) ;
61- impl_ord ! ( StrictlyPositive ) ;
62- impl_ord ! ( StrictlyNegative ) ;
63- impl_ord ! ( StrictlyPositiveFinite ) ;
64- impl_ord ! ( StrictlyNegativeFinite ) ;
104+ impl_ord ! ( non_nan , NonNaN ) ;
105+ impl_ord ! ( non_zero_non_nan , NonZeroNonNaN ) ;
106+ impl_ord ! ( non_nan_finite , NonNaNFinite ) ;
107+ impl_ord ! ( non_zero_non_nan_finite , NonZeroNonNaNFinite ) ;
108+ impl_ord ! ( positive , Positive ) ;
109+ impl_ord ! ( negative , Negative ) ;
110+ impl_ord ! ( positive_finite , PositiveFinite ) ;
111+ impl_ord ! ( negative_finite , NegativeFinite ) ;
112+ impl_ord ! ( strictly_positive , StrictlyPositive ) ;
113+ impl_ord ! ( strictly_negative , StrictlyNegative ) ;
114+ impl_ord ! ( strictly_positive_finite , StrictlyPositiveFinite ) ;
115+ impl_ord ! ( strictly_negative_finite , StrictlyNegativeFinite ) ;
0 commit comments