]> git.lizzy.rs Git - enumset.git/blob - enumset/tests/ops.rs
fda83d567d1f81c6d55ef9dbcc556b71b40a31b1
[enumset.git] / enumset / tests / ops.rs
1 #![allow(dead_code)]
2
3 use enumset::*;
4 use std::collections::{HashSet, BTreeSet};
5
6 #[derive(EnumSetType, Debug)]
7 pub enum EmptyEnum { }
8
9 #[derive(EnumSetType, Debug)]
10 pub enum Enum1 {
11     A,
12 }
13
14 #[derive(EnumSetType, Debug)]
15 pub enum SmallEnum {
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,
17 }
18 #[derive(EnumSetType, Debug)]
19 pub enum LargeEnum {
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,
29 }
30 #[derive(EnumSetType, Debug)]
31 pub enum Enum8 {
32     A, B, C, D, E, F, G, H,
33 }
34 #[derive(EnumSetType, Debug)]
35 pub enum Enum128 {
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,
45 }
46 #[derive(EnumSetType, Debug)]
47 pub enum SparseEnum {
48     A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80,
49 }
50
51 #[repr(u32)]
52 #[derive(EnumSetType, Debug)]
53 pub enum ReprEnum {
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,
55 }
56 #[repr(u64)]
57 #[derive(EnumSetType, Debug)]
58 pub enum ReprEnum2 {
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,
60 }
61 #[repr(isize)]
62 #[derive(EnumSetType, Debug)]
63 pub enum ReprEnum3 {
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,
65 }
66
67 macro_rules! test_variants {
68     ($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => {
69         #[test]
70         fn $all_empty_test() {
71             let all = EnumSet::<$enum_name>::all();
72             let empty = EnumSet::<$enum_name>::empty();
73
74             $(
75                 assert!(!empty.contains($enum_name::$variant));
76                 assert!(all.contains($enum_name::$variant));
77             )*
78         }
79     }
80 }
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,
83 }
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,
94 }
95 test_variants! { SparseEnum sparse_enum_all_empty
96     A, B, C, D, E, F, G,
97 }
98
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!();
103         #[test]
104         fn const_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());
109         }
110
111         #[test]
112         fn basic_add_remove() {
113             let mut set = EnumSet::new();
114             set.insert($e::A);
115             set.insert($e::B);
116             set.insert($e::C);
117             assert_eq!(set, $e::A | $e::B | $e::C);
118             set.remove($e::B);
119             assert_eq!(set, $e::A | $e::C);
120             set.insert($e::D);
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());
127             set.clear();
128             assert!(set.is_empty());
129         }
130
131         #[test]
132         fn already_present_element() {
133             let mut set = EnumSet::new();
134             assert!(set.insert($e::A));
135             assert!(!set.insert($e::A));
136             set.remove($e::A);
137             assert!(set.insert($e::A));
138         }
139
140         #[test]
141         fn empty_is_empty() {
142             assert_eq!(EnumSet::<$e>::empty().len(), 0)
143         }
144
145         #[test]
146         fn all_len() {
147             assert_eq!(EnumSet::<$e>::all().len(), EnumSet::<$e>::variant_count() as usize)
148         }
149
150         #[test]
151         fn basic_iter_test() {
152             let mut set = EnumSet::new();
153             set.insert($e::A);
154             set.insert($e::B);
155             set.insert($e::C);
156             set.insert($e::E);
157
158             let mut set_2 = EnumSet::new();
159             let vec: Vec<$e> = set.iter().collect();
160             for val in vec {
161                 assert!(!set_2.contains(val));
162                 set_2.insert(val);
163             }
164             assert_eq!(set, set_2);
165
166             let mut set_3 = EnumSet::new();
167             for val in set {
168                 assert!(!set_3.contains(val));
169                 set_3.insert(val);
170             }
171             assert_eq!(set, set_3);
172         }
173
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());
180             }
181             assert_eq!(itr.size_hint(), (0, Some(0)));
182         }
183         #[test]
184         fn test_iter_size_hint() {
185             check_iter_size_hint(EnumSet::<$e>::all());
186             let mut set = EnumSet::new();
187             set.insert($e::A);
188             set.insert($e::C);
189             set.insert($e::E);
190             check_iter_size_hint(set);
191         }
192
193         #[test]
194         fn iter_ops_test() {
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);
198         }
199
200         #[test]
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());
207         }
208
209         #[test]
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);
215             set -= $e::C;
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);
221         }
222
223         #[test]
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));
229         }
230
231         #[test]
232         fn debug_impl() {
233             assert_eq!(format!("{:?}", $e::A | $e::B | $e::D), "EnumSet(A | B | D)");
234         }
235
236         #[test]
237         fn to_from_bits() {
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);
240         }
241
242         #[test]
243         #[should_panic]
244         fn too_many_bits() {
245             if EnumSet::<$e>::variant_count() == 128 {
246                 panic!("(test skipped)")
247             }
248             EnumSet::<$e>::from_u128(!0);
249         }
250
251         #[test]
252         fn match_const_test() {
253             match CONST_SET {
254                 CONST_SET => { /* ok */ }
255                 _ => panic!("match fell through?"),
256             }
257         }
258
259         #[test]
260         fn set_test() {
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 {
267                 ($set:ident) => {{
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));
294                 }}
295             }
296             
297             let mut hash_set = HashSet::new();
298             test_set!(hash_set);
299             
300             let mut tree_set = BTreeSet::new();
301             test_set!(tree_set);
302         }
303
304         #[test]
305         fn check_size() {
306             assert_eq!(::std::mem::size_of::<EnumSet<$e>>(), $mem_size);
307         }
308     }
309 }
310 macro_rules! tests {
311     ($m:ident, $($tt:tt)*) => { mod $m { use super::*; $($tt)*; } }
312 }
313
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));
322
323 #[derive(EnumSetType, Debug)]
324 pub enum ThresholdEnum {
325     A = 1, B, C, D,
326     U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64,
327 }
328 macro_rules! bits_tests {
329     (
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
333     ) => {
334         mod $mod_name {
335             use super::*;
336             use crate::ThresholdEnum::*;
337
338             #[test]
339             fn to_from_basic() {
340                 for &mask in &[
341                     $threshold_expr | B | C | D,
342                     $threshold_expr | A | D,
343                     $threshold_expr | B | C,
344                 ] {
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())
348                 }
349             }
350
351             #[test]
352             #[should_panic]
353             fn from_invalid() {
354                 let invalid_mask: $ty = 0x80;
355                 EnumSet::<ThresholdEnum>::$from(invalid_mask);
356             }
357
358             #[test]
359             fn try_from_invalid() {
360                 assert!(EnumSet::<ThresholdEnum>::$try_from(0xFF).is_none());
361             }
362
363             $(
364                 #[test]
365                 fn try_to_overflow() {
366                         let set: EnumSet<ThresholdEnum> = $too_big_expr.into();
367                         assert!(set.$try_to().is_none());
368                 }
369             )*
370
371             #[test]
372             fn truncated_overflow() {
373                 let trunc_invalid = EnumSet::<ThresholdEnum>::$from_truncated(0xFE);
374                 assert_eq!(A | B | C | D, trunc_invalid);
375                 $(
376                     let set: EnumSet<ThresholdEnum> = $too_big_expr | A;
377                     assert_eq!(2, set.$to_truncated());
378                 )*
379             }
380         }
381     }
382 }
383
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);