|
| 1 | +//! Exhaustive testing utilities. |
| 2 | +//! (These are used in Tree Borrows `#[test]`s for thorough verification |
| 3 | +//! of the behavior of the state machine of permissions, |
| 4 | +//! but the contents of this file are extremely generic) |
| 5 | +#![cfg(test)] |
| 6 | + |
| 7 | +pub trait Exhaustive: Sized { |
| 8 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>>; |
| 9 | +} |
| 10 | + |
| 11 | +macro_rules! precondition { |
| 12 | + ($cond:expr) => { |
| 13 | + if !$cond { |
| 14 | + continue; |
| 15 | + } |
| 16 | + }; |
| 17 | +} |
| 18 | +pub(crate) use precondition; |
| 19 | + |
| 20 | +// Trivial impls of `Exhaustive` for the standard types with 0, 1 and 2 elements respectively. |
| 21 | + |
| 22 | +impl Exhaustive for ! { |
| 23 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 24 | + Box::new(std::iter::empty()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl Exhaustive for () { |
| 29 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 30 | + Box::new(std::iter::once(())) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Exhaustive for bool { |
| 35 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 36 | + Box::new(vec![true, false].into_iter()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Some container impls for `Exhaustive`. |
| 41 | + |
| 42 | +impl<T> Exhaustive for Option<T> |
| 43 | +where |
| 44 | + T: Exhaustive + 'static, |
| 45 | +{ |
| 46 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 47 | + Box::new(std::iter::once(None).chain(T::exhaustive().map(Some))) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<T1, T2> Exhaustive for (T1, T2) |
| 52 | +where |
| 53 | + T1: Exhaustive + Clone + 'static, |
| 54 | + T2: Exhaustive + 'static, |
| 55 | +{ |
| 56 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 57 | + Box::new(T1::exhaustive().flat_map(|t1| T2::exhaustive().map(move |t2| (t1.clone(), t2)))) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<T> Exhaustive for [T; 1] |
| 62 | +where |
| 63 | + T: Exhaustive + 'static, |
| 64 | +{ |
| 65 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 66 | + Box::new(T::exhaustive().map(|t| [t])) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<T> Exhaustive for [T; 2] |
| 71 | +where |
| 72 | + T: Exhaustive + Clone + 'static, |
| 73 | +{ |
| 74 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 75 | + Box::new(T::exhaustive().flat_map(|t1| T::exhaustive().map(move |t2| [t1.clone(), t2]))) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<T> Exhaustive for [T; 3] |
| 80 | +where |
| 81 | + T: Exhaustive + Clone + 'static, |
| 82 | +{ |
| 83 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 84 | + Box::new( |
| 85 | + <[T; 2]>::exhaustive() |
| 86 | + .flat_map(|[t1, t2]| T::exhaustive().map(move |t3| [t1.clone(), t2.clone(), t3])), |
| 87 | + ) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<T> Exhaustive for [T; 4] |
| 92 | +where |
| 93 | + T: Exhaustive + Clone + 'static, |
| 94 | +{ |
| 95 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 96 | + Box::new(<[T; 2]>::exhaustive().flat_map(|[t1, t2]| { |
| 97 | + <[T; 2]>::exhaustive().map(move |[t3, t4]| [t1.clone(), t2.clone(), t3, t4]) |
| 98 | + })) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<T> Exhaustive for [T; 5] |
| 103 | +where |
| 104 | + T: Exhaustive + Clone + 'static, |
| 105 | +{ |
| 106 | + fn exhaustive() -> Box<dyn Iterator<Item = Self>> { |
| 107 | + Box::new(<[T; 2]>::exhaustive().flat_map(|[t1, t2]| { |
| 108 | + <[T; 3]>::exhaustive().map(move |[t3, t4, t5]| [t1.clone(), t2.clone(), t3, t4, t5]) |
| 109 | + })) |
| 110 | + } |
| 111 | +} |
0 commit comments