]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/minmax-stability-issue-23687.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / minmax-stability-issue-23687.rs
CommitLineData
c34b1796
AL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
c34b1796
AL
11use std::fmt::Debug;
12use std::cmp::{self, PartialOrd, Ordering};
c34b1796
AL
13
14#[derive(Debug, Copy, Clone, PartialEq, Eq)]
15struct Foo {
16 n: u8,
17 name: &'static str
18}
19
20impl PartialOrd for Foo {
21 fn partial_cmp(&self, other: &Foo) -> Option<Ordering> {
22 Some(self.cmp(other))
23 }
24}
25
26impl Ord for Foo {
27 fn cmp(&self, other: &Foo) -> Ordering {
28 self.n.cmp(&other.n)
29 }
30}
31
32fn main() {
33 let a = Foo { n: 4, name: "a" };
34 let b = Foo { n: 4, name: "b" };
35 let c = Foo { n: 8, name: "c" };
36 let d = Foo { n: 8, name: "d" };
37 let e = Foo { n: 22, name: "e" };
38 let f = Foo { n: 22, name: "f" };
39
40 let data = [a, b, c, d, e, f];
41
42 // `min` should return the left when the values are equal
43 assert_eq!(data.iter().min(), Some(&a));
54a0048b 44 assert_eq!(data.iter().min_by_key(|a| a.n), Some(&a));
c34b1796
AL
45 assert_eq!(cmp::min(a, b), a);
46 assert_eq!(cmp::min(b, a), b);
c34b1796
AL
47
48 // `max` should return the right when the values are equal
49 assert_eq!(data.iter().max(), Some(&f));
54a0048b 50 assert_eq!(data.iter().max_by_key(|a| a.n), Some(&f));
c34b1796
AL
51 assert_eq!(cmp::max(e, f), f);
52 assert_eq!(cmp::max(f, e), e);
c34b1796
AL
53
54 let mut presorted = data.to_vec();
55 presorted.sort();
56 assert_stable(&presorted);
57
58 let mut presorted = data.to_vec();
59 presorted.sort_by(|a, b| a.cmp(b));
60 assert_stable(&presorted);
61
62 // Assert that sorted and min/max are the same
63 fn assert_stable<T: Ord + Debug>(presorted: &[T]) {
64 for slice in presorted.windows(2) {
65 let a = &slice[0];
66 let b = &slice[1];
67
68 assert_eq!(a, cmp::min(a, b));
69 assert_eq!(b, cmp::max(a, b));
70 }
71 }
72}