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.
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.
11 #![feature(std_misc, binary_heap_extras, catch_panic, rand, sync_poison)]
13 use std::__rand::{thread_rng, Rng};
16 use std::collections::BinaryHeap;
20 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
22 static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
24 // old binaryheap failed this test
26 // Integrity means that all elements are present after a comparison panics,
27 // even if the order may not be correct.
29 // Destructors must be called exactly once per element.
31 #[derive(Eq, PartialEq, Ord, Clone, Debug)]
32 struct PanicOrd<T>(T, bool);
34 impl<T> Drop for PanicOrd<T> {
36 // update global drop count
37 DROP_COUNTER.fetch_add(1, Ordering::SeqCst);
41 impl<T: PartialOrd> PartialOrd for PanicOrd<T> {
42 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
43 if self.1 || other.1 {
44 panic!("Panicking comparison");
46 self.0.partial_cmp(&other.0)
49 let mut rng = thread_rng();
50 const DATASZ: usize = 32;
51 const NTEST: usize = 10;
53 // don't use 0 in the data -- we want to catch the zeroed-out case.
54 let data = (1..DATASZ + 1).collect::<Vec<_>>();
56 // since it's a fuzzy test, run several tries.
58 for i in 1..DATASZ + 1 {
59 DROP_COUNTER.store(0, Ordering::SeqCst);
61 let mut panic_ords: Vec<_> = data.iter()
63 .map(|&x| PanicOrd(x, false))
65 let panic_item = PanicOrd(i, true);
67 // heapify the sane items
68 rng.shuffle(&mut panic_ords);
69 let heap = Arc::new(Mutex::new(BinaryHeap::from_vec(panic_ords)));
73 let heap_ref = heap.clone();
76 // push the panicking item to the heap and catch the panic
77 let thread_result = thread::catch_panic(move || {
78 heap.lock().unwrap().push(panic_item);
80 assert!(thread_result.is_err());
82 // Assert no elements were dropped
83 let drops = DROP_COUNTER.load(Ordering::SeqCst);
84 //assert!(drops == 0, "Must not drop items. drops={}", drops);
87 // now fetch the binary heap's data vector
88 let mutex_guard = match heap_ref.lock() {
90 Err(poison) => poison.into_inner(),
92 inner_data = mutex_guard.clone().into_vec();
95 let drops = DROP_COUNTER.load(Ordering::SeqCst);
96 assert_eq!(drops, DATASZ);
98 let mut data_sorted = inner_data.into_iter().map(|p| p.0).collect::<Vec<_>>();
100 assert_eq!(data_sorted, data);