btree_set_range/
btree_set_range.rs1use std::collections::BTreeSet;
2use std::ops::Bound::{self, *};
3
4use quickcheck::{quickcheck, TestResult};
5
6type RangeAny<T> = (Bound<T>, Bound<T>);
8
9trait RangeBounds<T> {
11 fn contains(&self, _: &T) -> bool;
12}
13
14impl<T: PartialOrd> RangeBounds<T> for RangeAny<T> {
15 fn contains(&self, item: &T) -> bool {
16 (match &self.0 {
17 Included(start) => start <= item,
18 Excluded(start) => start < item,
19 Unbounded => true,
20 }) && (match &self.1 {
21 Included(end) => item <= end,
22 Excluded(end) => item < end,
23 Unbounded => true,
24 })
25 }
26}
27
28fn panics<T: PartialOrd>(range: RangeAny<T>) -> bool {
32 match (&range.0, &range.1) {
33 (Excluded(start), Excluded(end)) => start >= end,
34 (Included(start), Excluded(end))
35 | (Excluded(start), Included(end))
36 | (Included(start), Included(end)) => start > end,
37 (Unbounded, _) | (_, Unbounded) => false,
38 }
39}
40
41fn check_range(set: BTreeSet<i32>, range: RangeAny<i32>) -> TestResult {
43 if panics(range) {
44 TestResult::discard()
45 } else {
46 let xs: BTreeSet<_> = set.range(range).cloned().collect();
47 TestResult::from_bool(
48 set.iter().all(|x| range.contains(x) == xs.contains(x)),
49 )
50 }
51}
52
53fn main() {
54 quickcheck(check_range as fn(_, _) -> TestResult);
55}