@@ -7,13 +7,13 @@ use crate::{
7
7
} ;
8
8
9
9
macro_rules! impl_ord {
10
- ( $type: ident) => {
10
+ ( $test : ident , $ type: ident) => {
11
11
impl Ord for $type<f32 > {
12
12
#[ inline]
13
13
fn cmp( & self , other: & Self ) -> core:: cmp:: Ordering {
14
- if * self < * other {
14
+ if self . get ( ) < other. get ( ) {
15
15
core:: cmp:: Ordering :: Less
16
- } else if * self == * other {
16
+ } else if self . get ( ) == other. get ( ) {
17
17
core:: cmp:: Ordering :: Equal
18
18
} else {
19
19
core:: cmp:: Ordering :: Greater
@@ -24,9 +24,9 @@ macro_rules! impl_ord {
24
24
impl Ord for $type<f64 > {
25
25
#[ inline]
26
26
fn cmp( & self , other: & Self ) -> core:: cmp:: Ordering {
27
- if * self < * other {
27
+ if self . get ( ) < other. get ( ) {
28
28
core:: cmp:: Ordering :: Less
29
- } else if * self == * other {
29
+ } else if self . get ( ) == other. get ( ) {
30
30
core:: cmp:: Ordering :: Equal
31
31
} else {
32
32
core:: cmp:: Ordering :: Greater
@@ -47,18 +47,69 @@ macro_rules! impl_ord {
47
47
Some ( self . cmp( other) )
48
48
}
49
49
}
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
+ }
50
101
} ;
51
102
}
52
103
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