4 use std::collections::{HashSet, BTreeSet};
6 #[derive(EnumSetType, Debug)]
9 #[derive(EnumSetType, Debug)]
14 #[derive(EnumSetType, Debug)]
16 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
18 #[derive(EnumSetType, Debug)]
20 _00, _01, _02, _03, _04, _05, _06, _07,
21 _10, _11, _12, _13, _14, _15, _16, _17,
22 _20, _21, _22, _23, _24, _25, _26, _27,
23 _30, _31, _32, _33, _34, _35, _36, _37,
24 _40, _41, _42, _43, _44, _45, _46, _47,
25 _50, _51, _52, _53, _54, _55, _56, _57,
26 _60, _61, _62, _63, _64, _65, _66, _67,
27 _70, _71, _72, _73, _74, _75, _76, _77,
28 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
30 #[derive(EnumSetType, Debug)]
32 A, B, C, D, E, F, G, H,
34 #[derive(EnumSetType, Debug)]
36 A, B, C, D, E, F, G, H, _8, _9, _10, _11, _12, _13, _14, _15,
37 _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31,
38 _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47,
39 _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63,
40 _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79,
41 _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95,
42 _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109,
43 _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122,
44 _123, _124, _125, _126, _127,
46 #[derive(EnumSetType, Debug)]
48 A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80,
51 macro_rules! test_variants {
52 ($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => {
54 fn $all_empty_test() {
55 let all = EnumSet::<$enum_name>::all();
56 let empty = EnumSet::<$enum_name>::empty();
59 assert!(!empty.contains($enum_name::$variant));
60 assert!(all.contains($enum_name::$variant));
65 test_variants! { SmallEnum small_enum_all_empty
66 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
68 test_variants! { LargeEnum large_enum_all_empty
69 _00, _01, _02, _03, _04, _05, _06, _07,
70 _10, _11, _12, _13, _14, _15, _16, _17,
71 _20, _21, _22, _23, _24, _25, _26, _27,
72 _30, _31, _32, _33, _34, _35, _36, _37,
73 _40, _41, _42, _43, _44, _45, _46, _47,
74 _50, _51, _52, _53, _54, _55, _56, _57,
75 _60, _61, _62, _63, _64, _65, _66, _67,
76 _70, _71, _72, _73, _74, _75, _76, _77,
77 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
79 test_variants! { SparseEnum sparse_enum_all_empty
83 macro_rules! test_enum {
84 ($e:ident, $mem_size:expr) => {
85 const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C);
86 const EMPTY_SET: EnumSet<$e> = enum_set!();
89 assert_eq!(CONST_SET.len(), 2);
90 assert!(CONST_SET.contains($e::A));
91 assert!(CONST_SET.contains($e::C));
92 assert!(EMPTY_SET.is_empty());
96 fn basic_add_remove() {
97 let mut set = EnumSet::new();
101 assert_eq!(set, $e::A | $e::B | $e::C);
103 assert_eq!(set, $e::A | $e::C);
105 assert_eq!(set, $e::A | $e::C | $e::D);
106 set.insert_all($e::F | $e::E | $e::G);
107 assert_eq!(set, $e::A | $e::C | $e::D | $e::F | $e::E | $e::G);
108 set.remove_all($e::A | $e::D | $e::G);
109 assert_eq!(set, $e::C | $e::F | $e::E);
110 assert!(!set.is_empty());
112 assert!(set.is_empty());
116 fn already_present_element() {
117 let mut set = EnumSet::new();
118 assert!(set.insert($e::A));
119 assert!(!set.insert($e::A));
121 assert!(set.insert($e::A));
125 fn empty_is_empty() {
126 assert_eq!(EnumSet::<$e>::empty().len(), 0)
131 assert_eq!(EnumSet::<$e>::all().len(), EnumSet::<$e>::variant_count() as usize)
135 fn basic_iter_test() {
136 let mut set = EnumSet::new();
142 let mut set_2 = EnumSet::new();
143 let vec: Vec<$e> = set.iter().collect();
145 assert!(!set_2.contains(val));
148 assert_eq!(set, set_2);
150 let mut set_3 = EnumSet::new();
152 assert!(!set_3.contains(val));
155 assert_eq!(set, set_3);
158 fn check_iter_size_hint(set: EnumSet<$e>) {
159 let count = set.len();
160 let mut itr = set.iter();
161 for idx in 0 .. count {
162 assert_eq!(itr.size_hint(), (count-idx, Some(count-idx)));
163 assert!(itr.next().is_some());
165 assert_eq!(itr.size_hint(), (0, Some(0)));
168 fn test_iter_size_hint() {
169 check_iter_size_hint(EnumSet::<$e>::all());
170 let mut set = EnumSet::new();
174 check_iter_size_hint(set);
179 let set = $e::A | $e::B | $e::C | $e::E;
180 let set2 = set.iter().filter(|&v| v != $e::B).collect::<EnumSet<_>>();
181 assert_eq!(set2, $e::A | $e::C | $e::E);
185 fn basic_ops_test() {
186 assert_eq!(($e::A | $e::B) | ($e::B | $e::C), $e::A | $e::B | $e::C);
187 assert_eq!(($e::A | $e::B) & ($e::B | $e::C), $e::B);
188 assert_eq!(($e::A | $e::B) ^ ($e::B | $e::C), $e::A | $e::C);
189 assert_eq!(($e::A | $e::B) - ($e::B | $e::C), $e::A);
190 assert_eq!($e::A | !$e::A, EnumSet::<$e>::all());
194 fn mutable_ops_test() {
195 let mut set = $e::A | $e::B;
196 assert_eq!(set, $e::A | $e::B);
197 set |= $e::C | $e::D;
198 assert_eq!(set, $e::A | $e::B | $e::C | $e::D);
200 assert_eq!(set, $e::A | $e::B | $e::D);
201 set ^= $e::B | $e::E;
202 assert_eq!(set, $e::A | $e::D | $e::E);
203 set &= $e::A | $e::E | $e::F;
204 assert_eq!(set, $e::A | $e::E);
208 fn basic_set_status() {
209 assert!(($e::A | $e::B | $e::C).is_disjoint($e::D | $e::E | $e::F));
210 assert!(!($e::A | $e::B | $e::C | $e::D).is_disjoint($e::D | $e::E | $e::F));
211 assert!(($e::A | $e::B).is_subset($e::A | $e::B | $e::C));
212 assert!(!($e::A | $e::D).is_subset($e::A | $e::B | $e::C));
217 assert_eq!(format!("{:?}", $e::A | $e::B | $e::D), "EnumSet(A | B | D)");
222 let value = $e::A | $e::C | $e::D | $e::F | $e::E | $e::G;
223 assert_eq!(EnumSet::from_u128(value.to_u128()), value);
229 if EnumSet::<$e>::variant_count() == 128 {
230 panic!("(test skipped)")
232 EnumSet::<$e>::from_u128(!0);
236 fn match_const_test() {
238 CONST_SET => { /* ok */ }
239 _ => panic!("match fell through?"),
245 const SET_TEST_A: EnumSet<$e> = enum_set!($e::A | $e::B | $e::C);
246 const SET_TEST_B: EnumSet<$e> = enum_set!($e::A | $e::B | $e::D);
247 const SET_TEST_C: EnumSet<$e> = enum_set!($e::A | $e::B | $e::E);
248 const SET_TEST_D: EnumSet<$e> = enum_set!($e::A | $e::B | $e::F);
249 const SET_TEST_E: EnumSet<$e> = enum_set!($e::A | $e::B | $e::G);
250 macro_rules! test_set {
252 assert!(!$set.contains(&SET_TEST_A));
253 assert!(!$set.contains(&SET_TEST_B));
254 assert!(!$set.contains(&SET_TEST_C));
255 assert!(!$set.contains(&SET_TEST_D));
256 assert!(!$set.contains(&SET_TEST_E));
257 $set.insert(SET_TEST_A);
258 $set.insert(SET_TEST_C);
259 assert!($set.contains(&SET_TEST_A));
260 assert!(!$set.contains(&SET_TEST_B));
261 assert!($set.contains(&SET_TEST_C));
262 assert!(!$set.contains(&SET_TEST_D));
263 assert!(!$set.contains(&SET_TEST_E));
264 $set.remove(&SET_TEST_C);
265 $set.remove(&SET_TEST_D);
266 assert!($set.contains(&SET_TEST_A));
267 assert!(!$set.contains(&SET_TEST_B));
268 assert!(!$set.contains(&SET_TEST_C));
269 assert!(!$set.contains(&SET_TEST_D));
270 assert!(!$set.contains(&SET_TEST_E));
271 $set.insert(SET_TEST_A);
272 $set.insert(SET_TEST_D);
273 assert!($set.contains(&SET_TEST_A));
274 assert!(!$set.contains(&SET_TEST_B));
275 assert!(!$set.contains(&SET_TEST_C));
276 assert!($set.contains(&SET_TEST_D));
277 assert!(!$set.contains(&SET_TEST_E));
281 let mut hash_set = HashSet::new();
284 let mut tree_set = BTreeSet::new();
290 assert_eq!(::std::mem::size_of::<EnumSet<$e>>(), $mem_size);
295 ($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } }
298 tests!(small_enum, test_enum!(SmallEnum, 4));
299 tests!(large_enum, test_enum!(LargeEnum, 16));
300 tests!(enum8, test_enum!(Enum8, 1));
301 tests!(enum128, test_enum!(Enum128, 16));
302 tests!(sparse_enum, test_enum!(SparseEnum, 16));
304 #[derive(EnumSetType, Debug)]
305 pub enum ThresholdEnum {
307 U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64,
309 macro_rules! bits_tests {
311 $mod_name:ident, $threshold_expr:expr, ($($too_big_expr:expr)?), $ty:ty,
312 $to:ident $try_to:ident $to_truncated:ident
313 $from:ident $try_from:ident $from_truncated:ident
317 use crate::ThresholdEnum::*;
322 $threshold_expr | B | C | D,
323 $threshold_expr | A | D,
324 $threshold_expr | B | C,
326 assert_eq!(mask, EnumSet::<ThresholdEnum>::$from(mask.$to()));
327 assert_eq!(mask.$to_truncated(), mask.$to());
328 assert_eq!(Some(mask.$to()), mask.$try_to())
335 let invalid_mask: $ty = 0x80;
336 EnumSet::<ThresholdEnum>::$from(invalid_mask);
340 fn try_from_invalid() {
341 assert!(EnumSet::<ThresholdEnum>::$try_from(0xFF).is_none());
346 fn try_to_overflow() {
347 let set: EnumSet<ThresholdEnum> = $too_big_expr.into();
348 assert!(set.$try_to().is_none());
353 fn truncated_overflow() {
354 let trunc_invalid = EnumSet::<ThresholdEnum>::$from_truncated(0xFE);
355 assert_eq!(A | B | C | D, trunc_invalid);
357 let set: EnumSet<ThresholdEnum> = $too_big_expr | A;
358 assert_eq!(2, set.$to_truncated());
365 bits_tests!(test_u8_bits, U8, (U16), u8,
366 to_u8 to_u8_checked to_u8_truncated from_u8 try_from_u8 from_u8_truncated);
367 bits_tests!(test_u16_bits, U16, (U32), u16,
368 to_u16 to_u16_checked to_u16_truncated from_u16 try_from_u16 from_u16_truncated);
369 bits_tests!(test_u32_bits, U32, (U64), u32,
370 to_u32 to_u32_checked to_u32_truncated from_u32 try_from_u32 from_u32_truncated);
371 bits_tests!(test_u64_bits, U64, (U128), u64,
372 to_u64 to_u64_checked to_u64_truncated from_u64 try_from_u64 from_u64_truncated);
373 bits_tests!(test_u128_bits, U128, (), u128,
374 to_u128 to_u128_checked to_u128_truncated from_u128 try_from_u128 from_u128_truncated);