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,
52 #[derive(EnumSetType, Debug)]
54 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,
57 #[derive(EnumSetType, Debug)]
59 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,
62 #[derive(EnumSetType, Debug)]
64 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,
67 macro_rules! test_variants {
68 ($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => {
70 fn $all_empty_test() {
71 let all = EnumSet::<$enum_name>::all();
72 let empty = EnumSet::<$enum_name>::empty();
75 assert!(!empty.contains($enum_name::$variant));
76 assert!(all.contains($enum_name::$variant));
81 test_variants! { SmallEnum small_enum_all_empty
82 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,
84 test_variants! { LargeEnum large_enum_all_empty
85 _00, _01, _02, _03, _04, _05, _06, _07,
86 _10, _11, _12, _13, _14, _15, _16, _17,
87 _20, _21, _22, _23, _24, _25, _26, _27,
88 _30, _31, _32, _33, _34, _35, _36, _37,
89 _40, _41, _42, _43, _44, _45, _46, _47,
90 _50, _51, _52, _53, _54, _55, _56, _57,
91 _60, _61, _62, _63, _64, _65, _66, _67,
92 _70, _71, _72, _73, _74, _75, _76, _77,
93 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,
95 test_variants! { SparseEnum sparse_enum_all_empty
99 macro_rules! test_enum {
100 ($e:ident, $mem_size:expr) => {
101 const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C);
102 const EMPTY_SET: EnumSet<$e> = enum_set!();
105 assert_eq!(CONST_SET.len(), 2);
106 assert!(CONST_SET.contains($e::A));
107 assert!(CONST_SET.contains($e::C));
108 assert!(EMPTY_SET.is_empty());
112 fn basic_add_remove() {
113 let mut set = EnumSet::new();
117 assert_eq!(set, $e::A | $e::B | $e::C);
119 assert_eq!(set, $e::A | $e::C);
121 assert_eq!(set, $e::A | $e::C | $e::D);
122 set.insert_all($e::F | $e::E | $e::G);
123 assert_eq!(set, $e::A | $e::C | $e::D | $e::F | $e::E | $e::G);
124 set.remove_all($e::A | $e::D | $e::G);
125 assert_eq!(set, $e::C | $e::F | $e::E);
126 assert!(!set.is_empty());
128 assert!(set.is_empty());
132 fn already_present_element() {
133 let mut set = EnumSet::new();
134 assert!(set.insert($e::A));
135 assert!(!set.insert($e::A));
137 assert!(set.insert($e::A));
141 fn empty_is_empty() {
142 assert_eq!(EnumSet::<$e>::empty().len(), 0)
147 assert_eq!(EnumSet::<$e>::all().len(), EnumSet::<$e>::variant_count() as usize)
151 fn basic_iter_test() {
152 let mut set = EnumSet::new();
158 let mut set_2 = EnumSet::new();
159 let vec: Vec<$e> = set.iter().collect();
161 assert!(!set_2.contains(val));
164 assert_eq!(set, set_2);
166 let mut set_3 = EnumSet::new();
168 assert!(!set_3.contains(val));
171 assert_eq!(set, set_3);
174 fn check_iter_size_hint(set: EnumSet<$e>) {
175 let count = set.len();
176 let mut itr = set.iter();
177 for idx in 0 .. count {
178 assert_eq!(itr.size_hint(), (count-idx, Some(count-idx)));
179 assert!(itr.next().is_some());
181 assert_eq!(itr.size_hint(), (0, Some(0)));
184 fn test_iter_size_hint() {
185 check_iter_size_hint(EnumSet::<$e>::all());
186 let mut set = EnumSet::new();
190 check_iter_size_hint(set);
195 let set = $e::A | $e::B | $e::C | $e::E;
196 let set2 = set.iter().filter(|&v| v != $e::B).collect::<EnumSet<_>>();
197 assert_eq!(set2, $e::A | $e::C | $e::E);
201 fn basic_ops_test() {
202 assert_eq!(($e::A | $e::B) | ($e::B | $e::C), $e::A | $e::B | $e::C);
203 assert_eq!(($e::A | $e::B) & ($e::B | $e::C), $e::B);
204 assert_eq!(($e::A | $e::B) ^ ($e::B | $e::C), $e::A | $e::C);
205 assert_eq!(($e::A | $e::B) - ($e::B | $e::C), $e::A);
206 assert_eq!($e::A | !$e::A, EnumSet::<$e>::all());
210 fn mutable_ops_test() {
211 let mut set = $e::A | $e::B;
212 assert_eq!(set, $e::A | $e::B);
213 set |= $e::C | $e::D;
214 assert_eq!(set, $e::A | $e::B | $e::C | $e::D);
216 assert_eq!(set, $e::A | $e::B | $e::D);
217 set ^= $e::B | $e::E;
218 assert_eq!(set, $e::A | $e::D | $e::E);
219 set &= $e::A | $e::E | $e::F;
220 assert_eq!(set, $e::A | $e::E);
224 fn basic_set_status() {
225 assert!(($e::A | $e::B | $e::C).is_disjoint($e::D | $e::E | $e::F));
226 assert!(!($e::A | $e::B | $e::C | $e::D).is_disjoint($e::D | $e::E | $e::F));
227 assert!(($e::A | $e::B).is_subset($e::A | $e::B | $e::C));
228 assert!(!($e::A | $e::D).is_subset($e::A | $e::B | $e::C));
233 assert_eq!(format!("{:?}", $e::A | $e::B | $e::D), "EnumSet(A | B | D)");
238 let value = $e::A | $e::C | $e::D | $e::F | $e::E | $e::G;
239 assert_eq!(EnumSet::from_u128(value.to_u128()), value);
245 if EnumSet::<$e>::variant_count() == 128 {
246 panic!("(test skipped)")
248 EnumSet::<$e>::from_u128(!0);
252 fn match_const_test() {
254 CONST_SET => { /* ok */ }
255 _ => panic!("match fell through?"),
261 const SET_TEST_A: EnumSet<$e> = enum_set!($e::A | $e::B | $e::C);
262 const SET_TEST_B: EnumSet<$e> = enum_set!($e::A | $e::B | $e::D);
263 const SET_TEST_C: EnumSet<$e> = enum_set!($e::A | $e::B | $e::E);
264 const SET_TEST_D: EnumSet<$e> = enum_set!($e::A | $e::B | $e::F);
265 const SET_TEST_E: EnumSet<$e> = enum_set!($e::A | $e::B | $e::G);
266 macro_rules! test_set {
268 assert!(!$set.contains(&SET_TEST_A));
269 assert!(!$set.contains(&SET_TEST_B));
270 assert!(!$set.contains(&SET_TEST_C));
271 assert!(!$set.contains(&SET_TEST_D));
272 assert!(!$set.contains(&SET_TEST_E));
273 $set.insert(SET_TEST_A);
274 $set.insert(SET_TEST_C);
275 assert!($set.contains(&SET_TEST_A));
276 assert!(!$set.contains(&SET_TEST_B));
277 assert!($set.contains(&SET_TEST_C));
278 assert!(!$set.contains(&SET_TEST_D));
279 assert!(!$set.contains(&SET_TEST_E));
280 $set.remove(&SET_TEST_C);
281 $set.remove(&SET_TEST_D);
282 assert!($set.contains(&SET_TEST_A));
283 assert!(!$set.contains(&SET_TEST_B));
284 assert!(!$set.contains(&SET_TEST_C));
285 assert!(!$set.contains(&SET_TEST_D));
286 assert!(!$set.contains(&SET_TEST_E));
287 $set.insert(SET_TEST_A);
288 $set.insert(SET_TEST_D);
289 assert!($set.contains(&SET_TEST_A));
290 assert!(!$set.contains(&SET_TEST_B));
291 assert!(!$set.contains(&SET_TEST_C));
292 assert!($set.contains(&SET_TEST_D));
293 assert!(!$set.contains(&SET_TEST_E));
297 let mut hash_set = HashSet::new();
300 let mut tree_set = BTreeSet::new();
306 assert_eq!(::std::mem::size_of::<EnumSet<$e>>(), $mem_size);
311 ($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } }
314 tests!(small_enum, test_enum!(SmallEnum, 4));
315 tests!(large_enum, test_enum!(LargeEnum, 16));
316 tests!(enum8, test_enum!(Enum8, 1));
317 tests!(enum128, test_enum!(Enum128, 16));
318 tests!(sparse_enum, test_enum!(SparseEnum, 16));
319 tests!(repr_enum, test_enum!(ReprEnum, 4));
320 tests!(repr_enum2, test_enum!(ReprEnum2, 4));
321 tests!(repr_enum3, test_enum!(ReprEnum3, 4));
323 #[derive(EnumSetType, Debug)]
324 pub enum ThresholdEnum {
326 U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64,
328 macro_rules! bits_tests {
330 $mod_name:ident, $threshold_expr:expr, ($($too_big_expr:expr),*), $ty:ty,
331 $to:ident $try_to:ident $to_truncated:ident
332 $from:ident $try_from:ident $from_truncated:ident
336 use crate::ThresholdEnum::*;
341 $threshold_expr | B | C | D,
342 $threshold_expr | A | D,
343 $threshold_expr | B | C,
345 assert_eq!(mask, EnumSet::<ThresholdEnum>::$from(mask.$to()));
346 assert_eq!(mask.$to_truncated(), mask.$to());
347 assert_eq!(Some(mask.$to()), mask.$try_to())
354 let invalid_mask: $ty = 0x80;
355 EnumSet::<ThresholdEnum>::$from(invalid_mask);
359 fn try_from_invalid() {
360 assert!(EnumSet::<ThresholdEnum>::$try_from(0xFF).is_none());
365 fn try_to_overflow() {
366 let set: EnumSet<ThresholdEnum> = $too_big_expr.into();
367 assert!(set.$try_to().is_none());
372 fn truncated_overflow() {
373 let trunc_invalid = EnumSet::<ThresholdEnum>::$from_truncated(0xFE);
374 assert_eq!(A | B | C | D, trunc_invalid);
376 let set: EnumSet<ThresholdEnum> = $too_big_expr | A;
377 assert_eq!(2, set.$to_truncated());
384 bits_tests!(test_u8_bits, U8, (U16), u8,
385 to_u8 to_u8_checked to_u8_truncated from_u8 try_from_u8 from_u8_truncated);
386 bits_tests!(test_u16_bits, U16, (U32), u16,
387 to_u16 to_u16_checked to_u16_truncated from_u16 try_from_u16 from_u16_truncated);
388 bits_tests!(test_u32_bits, U32, (U64), u32,
389 to_u32 to_u32_checked to_u32_truncated from_u32 try_from_u32 from_u32_truncated);
390 bits_tests!(test_u64_bits, U64, (U128), u64,
391 to_u64 to_u64_checked to_u64_truncated from_u64 try_from_u64 from_u64_truncated);
392 bits_tests!(test_u128_bits, U128, (), u128,
393 to_u128 to_u128_checked to_u128_truncated from_u128 try_from_u128 from_u128_truncated);