]> git.proxmox.com Git - rustc.git/blame - src/libcollectionstest/btree/set.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / libcollectionstest / btree / set.rs
CommitLineData
c34b1796
AL
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.
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
11use std::collections::BTreeSet;
12use std::hash::{SipHasher, self};
13
14#[test]
15fn test_clone_eq() {
16 let mut m = BTreeSet::new();
17
18 m.insert(1);
19 m.insert(2);
20
21 assert!(m.clone() == m);
22}
23
24#[test]
25fn test_hash() {
26 let mut x = BTreeSet::new();
27 let mut y = BTreeSet::new();
28
29 x.insert(1);
30 x.insert(2);
31 x.insert(3);
32
33 y.insert(3);
34 y.insert(2);
35 y.insert(1);
36
37 assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
38}
39
40struct Counter<'a, 'b> {
41 i: &'a mut usize,
42 expected: &'b [i32],
43}
44
45impl<'a, 'b, 'c> FnMut<(&'c i32,)> for Counter<'a, 'b> {
46 extern "rust-call" fn call_mut(&mut self, (&x,): (&'c i32,)) -> bool {
47 assert_eq!(x, self.expected[*self.i]);
48 *self.i += 1;
49 true
50 }
51}
52
53impl<'a, 'b, 'c> FnOnce<(&'c i32,)> for Counter<'a, 'b> {
54 type Output = bool;
55
56 extern "rust-call" fn call_once(mut self, args: (&'c i32,)) -> bool {
57 self.call_mut(args)
58 }
59}
60
61fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F) where
62 // FIXME Replace Counter with `Box<FnMut(_) -> _>`
63 F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, Counter) -> bool,
64{
65 let mut set_a = BTreeSet::new();
66 let mut set_b = BTreeSet::new();
67
68 for x in a { assert!(set_a.insert(*x)) }
69 for y in b { assert!(set_b.insert(*y)) }
70
71 let mut i = 0;
72 f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
73 assert_eq!(i, expected.len());
74}
75
76#[test]
77fn test_intersection() {
78 fn check_intersection(a: &[i32], b: &[i32], expected: &[i32]) {
79 check(a, b, expected, |x, y, f| x.intersection(y).all(f))
80 }
81
82 check_intersection(&[], &[], &[]);
83 check_intersection(&[1, 2, 3], &[], &[]);
84 check_intersection(&[], &[1, 2, 3], &[]);
85 check_intersection(&[2], &[1, 2, 3], &[2]);
86 check_intersection(&[1, 2, 3], &[2], &[2]);
87 check_intersection(&[11, 1, 3, 77, 103, 5, -5],
88 &[2, 11, 77, -9, -42, 5, 3],
89 &[3, 5, 11, 77]);
90}
91
92#[test]
93fn test_difference() {
94 fn check_difference(a: &[i32], b: &[i32], expected: &[i32]) {
95 check(a, b, expected, |x, y, f| x.difference(y).all(f))
96 }
97
98 check_difference(&[], &[], &[]);
99 check_difference(&[1, 12], &[], &[1, 12]);
100 check_difference(&[], &[1, 2, 3, 9], &[]);
101 check_difference(&[1, 3, 5, 9, 11],
102 &[3, 9],
103 &[1, 5, 11]);
104 check_difference(&[-5, 11, 22, 33, 40, 42],
105 &[-12, -5, 14, 23, 34, 38, 39, 50],
106 &[11, 22, 33, 40, 42]);
107}
108
109#[test]
110fn test_symmetric_difference() {
111 fn check_symmetric_difference(a: &[i32], b: &[i32], expected: &[i32]) {
112 check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
113 }
114
115 check_symmetric_difference(&[], &[], &[]);
116 check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
117 check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
118 check_symmetric_difference(&[1, 3, 5, 9, 11],
119 &[-2, 3, 9, 14, 22],
120 &[-2, 1, 5, 11, 14, 22]);
121}
122
123#[test]
124fn test_union() {
125 fn check_union(a: &[i32], b: &[i32], expected: &[i32]) {
126 check(a, b, expected, |x, y, f| x.union(y).all(f))
127 }
128
129 check_union(&[], &[], &[]);
130 check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
131 check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
132 check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
133 &[-2, 1, 5, 9, 13, 19],
134 &[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
135}
136
137#[test]
138fn test_zip() {
139 let mut x = BTreeSet::new();
140 x.insert(5);
141 x.insert(12);
142 x.insert(11);
143
144 let mut y = BTreeSet::new();
145 y.insert("foo");
146 y.insert("bar");
147
148 let x = x;
149 let y = y;
62682a34 150 let mut z = x.iter().zip(&y);
c34b1796
AL
151
152 // FIXME: #5801: this needs a type hint to compile...
153 let result: Option<(&usize, & &'static str)> = z.next();
154 assert_eq!(result.unwrap(), (&5, &("bar")));
155
156 let result: Option<(&usize, & &'static str)> = z.next();
157 assert_eq!(result.unwrap(), (&11, &("foo")));
158
159 let result: Option<(&usize, & &'static str)> = z.next();
160 assert!(result.is_none());
161}
162
163#[test]
164fn test_from_iter() {
165 let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
166
167 let set: BTreeSet<_> = xs.iter().cloned().collect();
168
169 for x in &xs {
170 assert!(set.contains(x));
171 }
172}
173
174#[test]
175fn test_show() {
176 let mut set = BTreeSet::new();
177 let empty = BTreeSet::<i32>::new();
178
179 set.insert(1);
180 set.insert(2);
181
182 let set_str = format!("{:?}", set);
183
184 assert_eq!(set_str, "{1, 2}");
185 assert_eq!(format!("{:?}", empty), "{}");
186}
62682a34
SL
187
188#[test]
189fn test_extend_ref() {
190 let mut a = BTreeSet::new();
191 a.insert(1);
192
193 a.extend(&[2, 3, 4]);
194
195 assert_eq!(a.len(), 4);
196 assert!(a.contains(&1));
197 assert!(a.contains(&2));
198 assert!(a.contains(&3));
199 assert!(a.contains(&4));
200
201 let mut b = BTreeSet::new();
202 b.insert(5);
203 b.insert(6);
204
205 a.extend(&b);
206
207 assert_eq!(a.len(), 6);
208 assert!(a.contains(&1));
209 assert!(a.contains(&2));
210 assert!(a.contains(&3));
211 assert!(a.contains(&4));
212 assert!(a.contains(&5));
213 assert!(a.contains(&6));
214}