]>
git.proxmox.com Git - rustc.git/blob - src/libcollectionstest/binary_heap.rs
1 // Copyright 2013-2014 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 use std
::collections
::BinaryHeap
;
12 use std
::collections
::binary_heap
::Drain
;
16 let data
= vec
![5, 9, 3];
17 let iterout
= [9, 5, 3];
18 let heap
= BinaryHeap
::from(data
);
21 assert_eq
!(*el
, iterout
[i
]);
27 fn test_iterator_reverse() {
28 let data
= vec
![5, 9, 3];
29 let iterout
= vec
![3, 5, 9];
30 let pq
= BinaryHeap
::from(data
);
32 let v
: Vec
<_
> = pq
.iter().rev().cloned().collect();
33 assert_eq
!(v
, iterout
);
38 let data
= vec
![5, 9, 3];
39 let iterout
= vec
![9, 5, 3];
40 let pq
= BinaryHeap
::from(data
);
42 let v
: Vec
<_
> = pq
.into_iter().collect();
43 assert_eq
!(v
, iterout
);
47 fn test_move_iter_size_hint() {
48 let data
= vec
![5, 9];
49 let pq
= BinaryHeap
::from(data
);
51 let mut it
= pq
.into_iter();
53 assert_eq
!(it
.size_hint(), (2, Some(2)));
54 assert_eq
!(it
.next(), Some(9));
56 assert_eq
!(it
.size_hint(), (1, Some(1)));
57 assert_eq
!(it
.next(), Some(5));
59 assert_eq
!(it
.size_hint(), (0, Some(0)));
60 assert_eq
!(it
.next(), None
);
64 fn test_move_iter_reverse() {
65 let data
= vec
![5, 9, 3];
66 let iterout
= vec
![3, 5, 9];
67 let pq
= BinaryHeap
::from(data
);
69 let v
: Vec
<_
> = pq
.into_iter().rev().collect();
70 assert_eq
!(v
, iterout
);
74 fn test_peek_and_pop() {
75 let data
= vec
![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
76 let mut sorted
= data
.clone();
78 let mut heap
= BinaryHeap
::from(data
);
79 while !heap
.is_empty() {
80 assert_eq
!(heap
.peek().unwrap(), sorted
.last().unwrap());
81 assert_eq
!(heap
.pop().unwrap(), sorted
.pop().unwrap());
87 let data
= vec
![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
88 let mut heap
= BinaryHeap
::from(data
);
89 assert_eq
!(heap
.peek(), Some(&10));
91 let mut top
= heap
.peek_mut().unwrap();
94 assert_eq
!(heap
.peek(), Some(&9));
99 let mut heap
= BinaryHeap
::from(vec
![2, 4, 9]);
100 assert_eq
!(heap
.len(), 3);
101 assert
!(*heap
.peek().unwrap() == 9);
103 assert_eq
!(heap
.len(), 4);
104 assert
!(*heap
.peek().unwrap() == 11);
106 assert_eq
!(heap
.len(), 5);
107 assert
!(*heap
.peek().unwrap() == 11);
109 assert_eq
!(heap
.len(), 6);
110 assert
!(*heap
.peek().unwrap() == 27);
112 assert_eq
!(heap
.len(), 7);
113 assert
!(*heap
.peek().unwrap() == 27);
115 assert_eq
!(heap
.len(), 8);
116 assert
!(*heap
.peek().unwrap() == 103);
120 fn test_push_unique() {
121 let mut heap
= BinaryHeap
::<Box
<_
>>::from(vec
![box 2, box 4, box 9]);
122 assert_eq
!(heap
.len(), 3);
123 assert
!(*heap
.peek().unwrap() == box 9);
125 assert_eq
!(heap
.len(), 4);
126 assert
!(*heap
.peek().unwrap() == box 11);
128 assert_eq
!(heap
.len(), 5);
129 assert
!(*heap
.peek().unwrap() == box 11);
131 assert_eq
!(heap
.len(), 6);
132 assert
!(*heap
.peek().unwrap() == box 27);
134 assert_eq
!(heap
.len(), 7);
135 assert
!(*heap
.peek().unwrap() == box 27);
137 assert_eq
!(heap
.len(), 8);
138 assert
!(*heap
.peek().unwrap() == box 103);
144 let mut heap
= BinaryHeap
::from(vec
![5, 5, 2, 1, 3]);
145 assert_eq
!(heap
.len(), 5);
146 assert_eq
!(heap
.push_pop(6), 6);
147 assert_eq
!(heap
.len(), 5);
148 assert_eq
!(heap
.push_pop(0), 5);
149 assert_eq
!(heap
.len(), 5);
150 assert_eq
!(heap
.push_pop(4), 5);
151 assert_eq
!(heap
.len(), 5);
152 assert_eq
!(heap
.push_pop(1), 4);
153 assert_eq
!(heap
.len(), 5);
159 let mut heap
= BinaryHeap
::from(vec
![5, 5, 2, 1, 3]);
160 assert_eq
!(heap
.len(), 5);
161 assert_eq
!(heap
.replace(6).unwrap(), 5);
162 assert_eq
!(heap
.len(), 5);
163 assert_eq
!(heap
.replace(0).unwrap(), 6);
164 assert_eq
!(heap
.len(), 5);
165 assert_eq
!(heap
.replace(4).unwrap(), 5);
166 assert_eq
!(heap
.len(), 5);
167 assert_eq
!(heap
.replace(1).unwrap(), 4);
168 assert_eq
!(heap
.len(), 5);
171 fn check_to_vec(mut data
: Vec
<i32>) {
172 let heap
= BinaryHeap
::from(data
.clone());
173 let mut v
= heap
.clone().into_vec();
178 assert_eq
!(heap
.into_sorted_vec(), data
);
183 check_to_vec(vec
![]);
184 check_to_vec(vec
![5]);
185 check_to_vec(vec
![3, 2]);
186 check_to_vec(vec
![2, 3]);
187 check_to_vec(vec
![5, 1, 2]);
188 check_to_vec(vec
![1, 100, 2, 3]);
189 check_to_vec(vec
![1, 3, 5, 7, 9, 2, 4, 6, 8, 0]);
190 check_to_vec(vec
![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]);
191 check_to_vec(vec
![9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0]);
192 check_to_vec(vec
![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
193 check_to_vec(vec
![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
194 check_to_vec(vec
![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2]);
195 check_to_vec(vec
![5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]);
199 fn test_empty_pop() {
200 let mut heap
= BinaryHeap
::<i32>::new();
201 assert
!(heap
.pop().is_none());
205 fn test_empty_peek() {
206 let empty
= BinaryHeap
::<i32>::new();
207 assert
!(empty
.peek().is_none());
211 fn test_empty_peek_mut() {
212 let mut empty
= BinaryHeap
::<i32>::new();
213 assert
!(empty
.peek_mut().is_none());
218 fn test_empty_replace() {
219 let mut heap
= BinaryHeap
::new();
220 assert
!(heap
.replace(5).is_none());
224 fn test_from_iter() {
225 let xs
= vec
![9, 8, 7, 6, 5, 4, 3, 2, 1];
227 let mut q
: BinaryHeap
<_
> = xs
.iter().rev().cloned().collect();
230 assert_eq
!(q
.pop().unwrap(), x
);
236 let mut q
: BinaryHeap
<_
> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect();
238 assert_eq
!(q
.drain().take(5).count(), 5);
240 assert
!(q
.is_empty());
244 fn test_extend_ref() {
245 let mut a
= BinaryHeap
::new();
249 a
.extend(&[3, 4, 5]);
251 assert_eq
!(a
.len(), 5);
252 assert_eq
!(a
.into_sorted_vec(), [1, 2, 3, 4, 5]);
254 let mut a
= BinaryHeap
::new();
257 let mut b
= BinaryHeap
::new();
264 assert_eq
!(a
.len(), 5);
265 assert_eq
!(a
.into_sorted_vec(), [1, 2, 3, 4, 5]);
270 let mut a
= BinaryHeap
::from(vec
![-10, 1, 2, 3, 3]);
271 let mut b
= BinaryHeap
::from(vec
![-20, 5, 43]);
275 assert_eq
!(a
.into_sorted_vec(), [-20, -10, 1, 2, 3, 3, 5, 43]);
276 assert
!(b
.is_empty());
280 fn test_append_to_empty() {
281 let mut a
= BinaryHeap
::new();
282 let mut b
= BinaryHeap
::from(vec
![-20, 5, 43]);
286 assert_eq
!(a
.into_sorted_vec(), [-20, 5, 43]);
287 assert
!(b
.is_empty());
291 fn test_extend_specialization() {
292 let mut a
= BinaryHeap
::from(vec
![-10, 1, 2, 3, 3]);
293 let b
= BinaryHeap
::from(vec
![-20, 5, 43]);
297 assert_eq
!(a
.into_sorted_vec(), [-20, -10, 1, 2, 3, 3, 5, 43]);
301 fn assert_covariance() {
302 fn drain
<'new
>(d
: Drain
<'
static, &'
static str>) -> Drain
<'new
, &'new
str> { d }