Skip to content

Commit 591e3d1

Browse files
authored
Rollup merge of #154761 - Vastargazing:add-regression-tests-cmp-argument-order, r=jhpratt
coretests: add argument order regression tests for min_by/max_by/minmax_by PR #136307 introduced a regression in min_by, max_by, and minmax_by: the compare closure received arguments as (v2, v1) instead of (v1, v2), contrary to the documented contract. Although this was fixed in #139357, no regression tests were added. This PR adds regression tests for all three functions, verifying that compare always receives arguments in the documented order (v1, v2). As a bonus: first coretests coverage for minmax_by.
2 parents b7b1632 + 51c4299 commit 591e3d1

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

library/coretests/tests/cmp.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,37 @@ fn test_ord_min_max_by() {
4848
assert_eq!(cmp::max_by(2, -1, f), 2);
4949
}
5050

51+
// Regression test for #136307 / #139357: ensure compare() receives (v1, v2), not (v2, v1).
52+
#[test]
53+
fn min_by_compare_argument_order() {
54+
let mut order = vec![];
55+
let _ = cmp::min_by(1i32, 2, |a, b| {
56+
order.push((*a, *b));
57+
a.cmp(b)
58+
});
59+
assert_eq!(order, [(1, 2)]);
60+
}
61+
62+
#[test]
63+
fn max_by_compare_argument_order() {
64+
let mut order = vec![];
65+
let _ = cmp::max_by(1i32, 2, |a, b| {
66+
order.push((*a, *b));
67+
a.cmp(b)
68+
});
69+
assert_eq!(order, [(1, 2)]);
70+
}
71+
72+
#[test]
73+
fn minmax_by_compare_argument_order() {
74+
let mut order = vec![];
75+
let _ = cmp::minmax_by(1i32, 2, |a, b| {
76+
order.push((*a, *b));
77+
a.cmp(b)
78+
});
79+
assert_eq!(order, [(1, 2)]);
80+
}
81+
5182
#[test]
5283
fn test_ord_min_max_by_key() {
5384
let f = |x: &i32| x.abs();

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(char_internals)]
1515
#![feature(char_max_len)]
1616
#![feature(clone_to_uninit)]
17+
#![feature(cmp_minmax)]
1718
#![feature(const_array)]
1819
#![feature(const_bool)]
1920
#![feature(const_cell_traits)]

0 commit comments

Comments
 (0)