Skip to content

Commit 21661aa

Browse files
WaffleLapkingitbot
authored and
gitbot
committed
improve doc tests for (min/max/minmax).* functions
- add tests for `a == b` where missing - try to make all the tests more similar - try to use more illustrative test values
1 parent 8f8aef5 commit 21661aa

File tree

1 file changed

+128
-18
lines changed

1 file changed

+128
-18
lines changed

core/src/cmp.rs

+128-18
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,24 @@ pub trait Ord: Eq + PartialOrd<Self> {
973973
/// assert_eq!(1.max(2), 2);
974974
/// assert_eq!(2.max(2), 2);
975975
/// ```
976+
/// ```
977+
/// use std::cmp::Ordering;
978+
///
979+
/// #[derive(Eq)]
980+
/// struct Equal(&'static str);
981+
///
982+
/// impl PartialEq for Equal {
983+
/// fn eq(&self, other: &Self) -> bool { true }
984+
/// }
985+
/// impl PartialOrd for Equal {
986+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
987+
/// }
988+
/// impl Ord for Equal {
989+
/// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
990+
/// }
991+
///
992+
/// assert_eq!(Equal("self").max(Equal("other")).0, "other");
993+
/// ```
976994
#[stable(feature = "ord_max_min", since = "1.21.0")]
977995
#[inline]
978996
#[must_use]
@@ -994,6 +1012,24 @@ pub trait Ord: Eq + PartialOrd<Self> {
9941012
/// assert_eq!(1.min(2), 1);
9951013
/// assert_eq!(2.min(2), 2);
9961014
/// ```
1015+
/// ```
1016+
/// use std::cmp::Ordering;
1017+
///
1018+
/// #[derive(Eq)]
1019+
/// struct Equal(&'static str);
1020+
///
1021+
/// impl PartialEq for Equal {
1022+
/// fn eq(&self, other: &Self) -> bool { true }
1023+
/// }
1024+
/// impl PartialOrd for Equal {
1025+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1026+
/// }
1027+
/// impl Ord for Equal {
1028+
/// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1029+
/// }
1030+
///
1031+
/// assert_eq!(Equal("self").min(Equal("other")).0, "self");
1032+
/// ```
9971033
#[stable(feature = "ord_max_min", since = "1.21.0")]
9981034
#[inline]
9991035
#[must_use]
@@ -1414,6 +1450,24 @@ pub macro PartialOrd($item:item) {
14141450
/// assert_eq!(cmp::min(1, 2), 1);
14151451
/// assert_eq!(cmp::min(2, 2), 2);
14161452
/// ```
1453+
/// ```
1454+
/// use std::cmp::{self, Ordering};
1455+
///
1456+
/// #[derive(Eq)]
1457+
/// struct Equal(&'static str);
1458+
///
1459+
/// impl PartialEq for Equal {
1460+
/// fn eq(&self, other: &Self) -> bool { true }
1461+
/// }
1462+
/// impl PartialOrd for Equal {
1463+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1464+
/// }
1465+
/// impl Ord for Equal {
1466+
/// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1467+
/// }
1468+
///
1469+
/// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1");
1470+
/// ```
14171471
#[inline]
14181472
#[must_use]
14191473
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1431,11 +1485,16 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
14311485
/// ```
14321486
/// use std::cmp;
14331487
///
1434-
/// let result = cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1435-
/// assert_eq!(result, 1);
1488+
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
14361489
///
1437-
/// let result = cmp::min_by(-2, 3, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1438-
/// assert_eq!(result, -2);
1490+
/// let result = cmp::min_by(2, -1, abs_cmp);
1491+
/// assert_eq!(result, -1);
1492+
///
1493+
/// let result = cmp::min_by(2, -3, abs_cmp);
1494+
/// assert_eq!(result, 2);
1495+
///
1496+
/// let result = cmp::min_by(1, -1, abs_cmp);
1497+
/// assert_eq!(result, 1);
14391498
/// ```
14401499
#[inline]
14411500
#[must_use]
@@ -1456,11 +1515,14 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
14561515
/// ```
14571516
/// use std::cmp;
14581517
///
1459-
/// let result = cmp::min_by_key(-2, 1, |x: &i32| x.abs());
1460-
/// assert_eq!(result, 1);
1518+
/// let result = cmp::min_by_key(2, -1, |x: &i32| x.abs());
1519+
/// assert_eq!(result, -1);
14611520
///
1462-
/// let result = cmp::min_by_key(-2, 2, |x: &i32| x.abs());
1463-
/// assert_eq!(result, -2);
1521+
/// let result = cmp::min_by_key(2, -3, |x: &i32| x.abs());
1522+
/// assert_eq!(result, 2);
1523+
///
1524+
/// let result = cmp::min_by_key(1, -1, |x: &i32| x.abs());
1525+
/// assert_eq!(result, 1);
14641526
/// ```
14651527
#[inline]
14661528
#[must_use]
@@ -1483,6 +1545,24 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
14831545
/// assert_eq!(cmp::max(1, 2), 2);
14841546
/// assert_eq!(cmp::max(2, 2), 2);
14851547
/// ```
1548+
/// ```
1549+
/// use std::cmp::{self, Ordering};
1550+
///
1551+
/// #[derive(Eq)]
1552+
/// struct Equal(&'static str);
1553+
///
1554+
/// impl PartialEq for Equal {
1555+
/// fn eq(&self, other: &Self) -> bool { true }
1556+
/// }
1557+
/// impl PartialOrd for Equal {
1558+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1559+
/// }
1560+
/// impl Ord for Equal {
1561+
/// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1562+
/// }
1563+
///
1564+
/// assert_eq!(cmp::max(Equal("v1"), Equal("v2")).0, "v2");
1565+
/// ```
14861566
#[inline]
14871567
#[must_use]
14881568
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1500,11 +1580,16 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
15001580
/// ```
15011581
/// use std::cmp;
15021582
///
1503-
/// let result = cmp::max_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1583+
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
1584+
///
1585+
/// let result = cmp::max_by(3, -2, abs_cmp) ;
1586+
/// assert_eq!(result, 3);
1587+
///
1588+
/// let result = cmp::max_by(1, -2, abs_cmp);
15041589
/// assert_eq!(result, -2);
15051590
///
1506-
/// let result = cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())) ;
1507-
/// assert_eq!(result, 2);
1591+
/// let result = cmp::max_by(1, -1, abs_cmp);
1592+
/// assert_eq!(result, -1);
15081593
/// ```
15091594
#[inline]
15101595
#[must_use]
@@ -1525,11 +1610,14 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
15251610
/// ```
15261611
/// use std::cmp;
15271612
///
1528-
/// let result = cmp::max_by_key(-2, 1, |x: &i32| x.abs());
1613+
/// let result = cmp::max_by_key(3, -2, |x: &i32| x.abs());
1614+
/// assert_eq!(result, 3);
1615+
///
1616+
/// let result = cmp::max_by_key(1, -2, |x: &i32| x.abs());
15291617
/// assert_eq!(result, -2);
15301618
///
1531-
/// let result = cmp::max_by_key(-2, 2, |x: &i32| x.abs());
1532-
/// assert_eq!(result, 2);
1619+
/// let result = cmp::max_by_key(1, -1, |x: &i32| x.abs());
1620+
/// assert_eq!(result, -1);
15331621
/// ```
15341622
#[inline]
15351623
#[must_use]
@@ -1549,13 +1637,32 @@ pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
15491637
/// use std::cmp;
15501638
///
15511639
/// assert_eq!(cmp::minmax(1, 2), [1, 2]);
1552-
/// assert_eq!(cmp::minmax(2, 2), [2, 2]);
1640+
/// assert_eq!(cmp::minmax(2, 1), [1, 2]);
15531641
///
15541642
/// // You can destructure the result using array patterns
15551643
/// let [min, max] = cmp::minmax(42, 17);
15561644
/// assert_eq!(min, 17);
15571645
/// assert_eq!(max, 42);
15581646
/// ```
1647+
/// ```
1648+
/// #![feature(cmp_minmax)]
1649+
/// use std::cmp::{self, Ordering};
1650+
///
1651+
/// #[derive(Eq)]
1652+
/// struct Equal(&'static str);
1653+
///
1654+
/// impl PartialEq for Equal {
1655+
/// fn eq(&self, other: &Self) -> bool { true }
1656+
/// }
1657+
/// impl PartialOrd for Equal {
1658+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
1659+
/// }
1660+
/// impl Ord for Equal {
1661+
/// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
1662+
/// }
1663+
///
1664+
/// assert_eq!(cmp::minmax(Equal("v1"), Equal("v2")).map(|v| v.0), ["v1", "v2"]);
1665+
/// ```
15591666
#[inline]
15601667
#[must_use]
15611668
#[unstable(feature = "cmp_minmax", issue = "115939")]
@@ -1576,11 +1683,14 @@ where
15761683
/// #![feature(cmp_minmax)]
15771684
/// use std::cmp;
15781685
///
1579-
/// assert_eq!(cmp::minmax_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [1, -2]);
1580-
/// assert_eq!(cmp::minmax_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), [-2, 2]);
1686+
/// let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
1687+
///
1688+
/// assert_eq!(cmp::minmax_by(-2, 1, abs_cmp), [1, -2]);
1689+
/// assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
1690+
/// assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
15811691
///
15821692
/// // You can destructure the result using array patterns
1583-
/// let [min, max] = cmp::minmax_by(-42, 17, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
1693+
/// let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
15841694
/// assert_eq!(min, 17);
15851695
/// assert_eq!(max, -42);
15861696
/// ```

0 commit comments

Comments
 (0)