File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,15 @@ impl Integer {
85
85
( true , false ) => {
86
86
Ordering :: Greater
87
87
}
88
+ ( true , true ) => {
89
+ // For negative numbers the byte order puts numbers closer to 0 which
90
+ // are lexically higher, lower. So -1 < -2 when sorting by be_bytes().
91
+ match self . 0 . cmp ( & other. 0 ) {
92
+ Ordering :: Less => Ordering :: Greater ,
93
+ Ordering :: Equal => Ordering :: Equal ,
94
+ Ordering :: Greater => Ordering :: Less ,
95
+ }
96
+ }
88
97
( _, _) => {
89
98
self . 0 . cmp ( & other. 0 )
90
99
}
Original file line number Diff line number Diff line change @@ -58,3 +58,31 @@ fn map() {
58
58
59
59
assert_eq ! ( hex:: encode( & bytes1) , "a80a002001f402186403617a048120056261610681186407" ) ;
60
60
}
61
+
62
+ #[ test]
63
+ fn negative_numbers ( ) {
64
+ let mut array: Vec < CanonicalValue > = vec ! [
65
+ cval!( 10 ) ,
66
+ cval!( -1 ) ,
67
+ cval!( -2 ) ,
68
+ cval!( -3 ) ,
69
+ cval!( -4 ) ,
70
+ cval!( false ) ,
71
+ cval!( 100 ) ,
72
+ cval!( -100 ) ,
73
+ cval!( -200 ) ,
74
+ cval!( "z" ) ,
75
+ cval!( [ -1 ] ) ,
76
+ cval!( -300 ) ,
77
+ cval!( "aa" ) ,
78
+ cval!( [ 100 ] ) ,
79
+ ] ;
80
+ let golden = array. clone ( ) ;
81
+
82
+ // Shuffle the array.
83
+ array. shuffle ( & mut rand:: thread_rng ( ) ) ;
84
+
85
+ array. sort ( ) ;
86
+
87
+ assert_eq ! ( array, golden) ;
88
+ }
You can’t perform that action at this time.
0 commit comments