Skip to content

Commit

Permalink
common: cope with i64::MIN in PartialOrd
Browse files Browse the repository at this point in the history
Negating i64::MIN gives a positive value that is > i64::MAX.
  • Loading branch information
daviddrysdale committed Apr 4, 2024
1 parent a6e7fc3 commit 0765ac3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Ord for Label {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Label::Int(i1), Label::Int(i2)) => match (i1.signum(), i2.signum()) {
(-1, -1) => (-i1).cmp(&(-i2)),
(-1, -1) => i2.cmp(i1),
(-1, 0) => Ordering::Greater,
(-1, 1) => Ordering::Greater,
(0, -1) => Ordering::Less,
Expand Down
4 changes: 4 additions & 0 deletions src/common/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ fn test_label_sort() {
(Label::Int(0x1234), Label::Int(0x1235)),
(Label::Text("a".to_owned()), Label::Text("ab".to_owned())),
(Label::Text("aa".to_owned()), Label::Text("ab".to_owned())),
(Label::Int(i64::MAX - 2), Label::Int(i64::MAX - 1)),
(Label::Int(i64::MAX - 1), Label::Int(i64::MAX)),
(Label::Int(i64::MIN + 2), Label::Int(i64::MIN + 1)),
(Label::Int(i64::MIN + 1), Label::Int(i64::MIN)),
];
for (left, right) in pairs.into_iter() {
let value_cmp = left.cmp(&right);
Expand Down

0 comments on commit 0765ac3

Please sign in to comment.