diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/derives/derive-partial-ord.rs | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/derives/derive-partial-ord.rs')
-rw-r--r-- | tests/ui/derives/derive-partial-ord.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/ui/derives/derive-partial-ord.rs b/tests/ui/derives/derive-partial-ord.rs new file mode 100644 index 000000000..9078a7ffa --- /dev/null +++ b/tests/ui/derives/derive-partial-ord.rs @@ -0,0 +1,60 @@ +// Checks that in a derived implementation of PartialOrd the lt, le, ge, gt methods are consistent +// with partial_cmp. Also verifies that implementation is consistent with that for tuples. +// +// run-pass + +#[derive(PartialEq, PartialOrd)] +struct P(f64, f64); + +fn main() { + let values: &[f64] = &[1.0, 2.0, f64::NAN]; + for a in values { + for b in values { + for c in values { + for d in values { + // Check impl for a tuple. + check(&(*a, *b), &(*c, *d)); + + // Check derived impl. + check(&P(*a, *b), &P(*c, *d)); + + // Check that impls agree with each other. + assert_eq!( + PartialOrd::partial_cmp(&(*a, *b), &(*c, *d)), + PartialOrd::partial_cmp(&P(*a, *b), &P(*c, *d)), + ); + } + } + } + } +} + +fn check<T: PartialOrd>(a: &T, b: &T) { + use std::cmp::Ordering::*; + match PartialOrd::partial_cmp(a, b) { + None => { + assert!(!(a < b)); + assert!(!(a <= b)); + assert!(!(a > b)); + assert!(!(a >= b)); + } + Some(Equal) => { + assert!(!(a < b)); + assert!(a <= b); + assert!(!(a > b)); + assert!(a >= b); + } + Some(Less) => { + assert!(a < b); + assert!(a <= b); + assert!(!(a > b)); + assert!(!(a >= b)); + } + Some(Greater) => { + assert!(!(a < b)); + assert!(!(a <= b)); + assert!(a > b); + assert!(a >= b); + } + } +} |