1 // Copyright 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::BTreeSet;
13 use std::iter::FromIterator;
14 use super::DeterministicRng;
18 let mut m = BTreeSet::new();
23 assert!(m.clone() == m);
28 let mut x = BTreeSet::new();
29 let mut y = BTreeSet::new();
39 assert!(::hash(&x) == ::hash(&y));
42 fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F)
43 where F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, &mut dyn FnMut(&i32) -> bool) -> bool
45 let mut set_a = BTreeSet::new();
46 let mut set_b = BTreeSet::new();
49 assert!(set_a.insert(*x))
52 assert!(set_b.insert(*y))
59 assert_eq!(x, expected[i]);
63 assert_eq!(i, expected.len());
67 fn test_intersection() {
68 fn check_intersection(a: &[i32], b: &[i32], expected: &[i32]) {
69 check(a, b, expected, |x, y, f| x.intersection(y).all(f))
72 check_intersection(&[], &[], &[]);
73 check_intersection(&[1, 2, 3], &[], &[]);
74 check_intersection(&[], &[1, 2, 3], &[]);
75 check_intersection(&[2], &[1, 2, 3], &[2]);
76 check_intersection(&[1, 2, 3], &[2], &[2]);
77 check_intersection(&[11, 1, 3, 77, 103, 5, -5],
78 &[2, 11, 77, -9, -42, 5, 3],
83 fn test_difference() {
84 fn check_difference(a: &[i32], b: &[i32], expected: &[i32]) {
85 check(a, b, expected, |x, y, f| x.difference(y).all(f))
88 check_difference(&[], &[], &[]);
89 check_difference(&[1, 12], &[], &[1, 12]);
90 check_difference(&[], &[1, 2, 3, 9], &[]);
91 check_difference(&[1, 3, 5, 9, 11], &[3, 9], &[1, 5, 11]);
92 check_difference(&[-5, 11, 22, 33, 40, 42],
93 &[-12, -5, 14, 23, 34, 38, 39, 50],
94 &[11, 22, 33, 40, 42]);
98 fn test_symmetric_difference() {
99 fn check_symmetric_difference(a: &[i32], b: &[i32], expected: &[i32]) {
100 check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
103 check_symmetric_difference(&[], &[], &[]);
104 check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
105 check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
106 check_symmetric_difference(&[1, 3, 5, 9, 11],
108 &[-2, 1, 5, 11, 14, 22]);
113 fn check_union(a: &[i32], b: &[i32], expected: &[i32]) {
114 check(a, b, expected, |x, y, f| x.union(y).all(f))
117 check_union(&[], &[], &[]);
118 check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
119 check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
120 check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
121 &[-2, 1, 5, 9, 13, 19],
122 &[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
127 let mut x = BTreeSet::new();
132 let mut y = BTreeSet::new();
138 let mut z = x.iter().zip(&y);
140 assert_eq!(z.next().unwrap(), (&5, &("bar")));
141 assert_eq!(z.next().unwrap(), (&11, &("foo")));
142 assert!(z.next().is_none());
146 fn test_from_iter() {
147 let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
149 let set: BTreeSet<_> = xs.iter().cloned().collect();
152 assert!(set.contains(x));
158 let mut set = BTreeSet::new();
159 let empty = BTreeSet::<i32>::new();
164 let set_str = format!("{:?}", set);
166 assert_eq!(set_str, "{1, 2}");
167 assert_eq!(format!("{:?}", empty), "{}");
171 fn test_extend_ref() {
172 let mut a = BTreeSet::new();
175 a.extend(&[2, 3, 4]);
177 assert_eq!(a.len(), 4);
178 assert!(a.contains(&1));
179 assert!(a.contains(&2));
180 assert!(a.contains(&3));
181 assert!(a.contains(&4));
183 let mut b = BTreeSet::new();
189 assert_eq!(a.len(), 6);
190 assert!(a.contains(&1));
191 assert!(a.contains(&2));
192 assert!(a.contains(&3));
193 assert!(a.contains(&4));
194 assert!(a.contains(&5));
195 assert!(a.contains(&6));
200 use std::cmp::Ordering;
203 struct Foo(&'static str, i32);
205 impl PartialEq for Foo {
206 fn eq(&self, other: &Self) -> bool {
213 impl PartialOrd for Foo {
214 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
215 self.0.partial_cmp(&other.0)
220 fn cmp(&self, other: &Self) -> Ordering {
225 let mut s = BTreeSet::new();
226 assert_eq!(s.replace(Foo("a", 1)), None);
227 assert_eq!(s.len(), 1);
228 assert_eq!(s.replace(Foo("a", 2)), Some(Foo("a", 1)));
229 assert_eq!(s.len(), 1);
232 let mut it = s.iter();
233 assert_eq!(it.next(), Some(&Foo("a", 2)));
234 assert_eq!(it.next(), None);
237 assert_eq!(s.get(&Foo("a", 1)), Some(&Foo("a", 2)));
238 assert_eq!(s.take(&Foo("a", 1)), Some(Foo("a", 2)));
239 assert_eq!(s.len(), 0);
241 assert_eq!(s.get(&Foo("a", 1)), None);
242 assert_eq!(s.take(&Foo("a", 1)), None);
244 assert_eq!(s.iter().next(), None);
250 use std::collections::btree_set::{IntoIter, Iter, Range};
252 fn set<'new>(v: BTreeSet<&'static str>) -> BTreeSet<&'new str> {
255 fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> {
258 fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> {
261 fn range<'a, 'new>(v: Range<'a, &'static str>) -> Range<'a, &'new str> {
268 let mut a = BTreeSet::new();
273 let mut b = BTreeSet::new();
280 assert_eq!(a.len(), 5);
281 assert_eq!(b.len(), 0);
283 assert_eq!(a.contains(&1), true);
284 assert_eq!(a.contains(&2), true);
285 assert_eq!(a.contains(&3), true);
286 assert_eq!(a.contains(&4), true);
287 assert_eq!(a.contains(&5), true);
290 fn rand_data(len: usize) -> Vec<u32> {
291 let mut rng = DeterministicRng::new();
292 Vec::from_iter((0..len).map(|_| rng.next()))
296 fn test_split_off_empty_right() {
297 let mut data = rand_data(173);
299 let mut set = BTreeSet::from_iter(data.clone());
300 let right = set.split_off(&(data.iter().max().unwrap() + 1));
303 assert!(set.into_iter().eq(data));
304 assert!(right.into_iter().eq(None));
308 fn test_split_off_empty_left() {
309 let mut data = rand_data(314);
311 let mut set = BTreeSet::from_iter(data.clone());
312 let right = set.split_off(data.iter().min().unwrap());
315 assert!(set.into_iter().eq(None));
316 assert!(right.into_iter().eq(data));
320 fn test_split_off_large_random_sorted() {
321 let mut data = rand_data(1529);
322 // special case with maximum height.
325 let mut set = BTreeSet::from_iter(data.clone());
326 let key = data[data.len() / 2];
327 let right = set.split_off(&key);
329 assert!(set.into_iter().eq(data.clone().into_iter().filter(|x| *x < key)));
330 assert!(right.into_iter().eq(data.into_iter().filter(|x| *x >= key)));