]> git.lizzy.rs Git - enumset.git/blobdiff - enumset/tests/ops.rs
Add `no_super_impls` option to `EnumSetType` derive.
[enumset.git] / enumset / tests / ops.rs
index b6cfe2aca69f4bbf94ac2897a4f5007568c69fb0..b76881e3df25b8d5f2edec352c7475f4ceeadd13 100644 (file)
@@ -15,6 +15,11 @@ pub enum Enum1 {
 pub enum SmallEnum {
     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,
 }
+#[derive(Clone, Copy, Debug, EnumSetType, Eq, PartialEq)]
+#[enumset(no_super_impls)]
+pub enum SmallEnumExplicitDerive {
+    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,
+}
 #[derive(EnumSetType, Debug)]
 pub enum LargeEnum {
     _00,  _01,  _02,  _03,  _04,  _05,  _06,  _07,
@@ -48,6 +53,27 @@ pub enum SparseEnum {
     A = 0xA, B = 20, C = 30, D = 40, E = 50, F = 60, G = 70, H = 80,
 }
 
+#[repr(u32)]
+#[derive(EnumSetType, Debug)]
+pub enum ReprEnum {
+    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,
+}
+#[repr(u64)]
+#[derive(EnumSetType, Debug)]
+pub enum ReprEnum2 {
+    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,
+}
+#[repr(isize)]
+#[derive(EnumSetType, Debug)]
+pub enum ReprEnum3 {
+    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,
+}
+#[repr(C)]
+#[derive(EnumSetType, Debug)]
+pub enum ReprEnum4 {
+    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,
+}
+
 macro_rules! test_variants {
     ($enum_name:ident $all_empty_test:ident $($variant:ident,)*) => {
         #[test]
@@ -65,6 +91,9 @@ macro_rules! test_variants {
 test_variants! { SmallEnum small_enum_all_empty
     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,
 }
+test_variants! { SmallEnumExplicitDerive small_enum_explicit_derive_all_empty
+    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,
+}
 test_variants! { LargeEnum large_enum_all_empty
     _00,  _01,  _02,  _03,  _04,  _05,  _06,  _07,
     _10,  _11,  _12,  _13,  _14,  _15,  _16,  _17,
@@ -83,10 +112,12 @@ test_variants! { SparseEnum sparse_enum_all_empty
 macro_rules! test_enum {
     ($e:ident, $mem_size:expr) => {
         const CONST_SET: EnumSet<$e> = enum_set!($e::A | $e::C);
+        const CONST_1_SET: EnumSet<$e> = enum_set!($e::A);
         const EMPTY_SET: EnumSet<$e> = enum_set!();
         #[test]
         fn const_set() {
             assert_eq!(CONST_SET.len(), 2);
+            assert_eq!(CONST_1_SET.len(), 1);
             assert!(CONST_SET.contains($e::A));
             assert!(CONST_SET.contains($e::C));
             assert!(EMPTY_SET.is_empty());
@@ -112,6 +143,15 @@ macro_rules! test_enum {
             assert!(set.is_empty());
         }
 
+        #[test]
+        fn already_present_element() {
+            let mut set = EnumSet::new();
+            assert!(set.insert($e::A));
+            assert!(!set.insert($e::A));
+            set.remove($e::A);
+            assert!(set.insert($e::A));
+        }
+
         #[test]
         fn empty_is_empty() {
             assert_eq!(EnumSet::<$e>::empty().len(), 0)
@@ -123,15 +163,14 @@ macro_rules! test_enum {
         }
 
         #[test]
-        fn basic_iter_test() {
+        fn iter_test() {
             let mut set = EnumSet::new();
             set.insert($e::A);
             set.insert($e::B);
-            set.insert($e::C);
-            set.insert($e::E);
+            set.extend($e::C | $e::E);
 
             let mut set_2 = EnumSet::new();
-            let vec: Vec<$e> = set.iter().collect();
+            let vec: Vec<_> = set.iter().collect();
             for val in vec {
                 assert!(!set_2.contains(val));
                 set_2.insert(val);
@@ -144,6 +183,22 @@ macro_rules! test_enum {
                 set_3.insert(val);
             }
             assert_eq!(set, set_3);
+
+            let mut set_4 = EnumSet::new();
+            let vec: EnumSet<_> = set.into_iter().map(EnumSet::only).collect();
+            for val in vec {
+                assert!(!set_4.contains(val));
+                set_4.insert(val);
+            }
+            assert_eq!(set, set_4);
+
+            let mut set_5 = EnumSet::new();
+            let vec: EnumSet<_> = set.iter().collect();
+            for val in vec {
+                assert!(!set_5.contains(val));
+                set_5.insert(val);
+            }
+            assert_eq!(set, set_5);
         }
 
         fn check_iter_size_hint(set: EnumSet<$e>) {
@@ -151,9 +206,11 @@ macro_rules! test_enum {
             let mut itr = set.iter();
             for idx in 0 .. count {
                 assert_eq!(itr.size_hint(), (count-idx, Some(count-idx)));
+                assert_eq!(itr.len(), count-idx);
                 assert!(itr.next().is_some());
             }
             assert_eq!(itr.size_hint(), (0, Some(0)));
+            assert_eq!(itr.len(), 0);
         }
         #[test]
         fn test_iter_size_hint() {
@@ -211,7 +268,7 @@ macro_rules! test_enum {
         #[test]
         fn to_from_bits() {
             let value = $e::A | $e::C | $e::D | $e::F | $e::E | $e::G;
-            assert_eq!(EnumSet::from_bits(value.to_bits()), value);
+            assert_eq!(EnumSet::from_u128(value.as_u128()), value);
         }
 
         #[test]
@@ -220,7 +277,7 @@ macro_rules! test_enum {
             if EnumSet::<$e>::variant_count() == 128 {
                 panic!("(test skipped)")
             }
-            EnumSet::<$e>::from_bits(!0);
+            EnumSet::<$e>::from_u128(!0);
         }
 
         #[test]
@@ -268,14 +325,31 @@ macro_rules! test_enum {
                     assert!(!$set.contains(&SET_TEST_E));
                 }}
             }
-            
+
             let mut hash_set = HashSet::new();
             test_set!(hash_set);
-            
+
             let mut tree_set = BTreeSet::new();
             test_set!(tree_set);
         }
 
+        #[test]
+        fn sum_test() {
+            let target = $e::A | $e::B | $e::D | $e::E | $e::G | $e::H;
+
+            let list_a = [$e::A | $e::B, $e::D | $e::E, $e::G | $e::H];
+            let sum_a: EnumSet<$e> = list_a.iter().map(|x| *x).sum();
+            assert_eq!(target, sum_a);
+            let sum_b: EnumSet<$e> = list_a.iter().sum();
+            assert_eq!(target, sum_b);
+
+            let list_b = [$e::A, $e::B, $e::D, $e::E, $e::G, $e::H];
+            let sum_c: EnumSet<$e> = list_b.iter().map(|x| *x).sum();
+            assert_eq!(target, sum_c);
+            let sum_d: EnumSet<$e> = list_b.iter().sum();
+            assert_eq!(target, sum_d);
+        }
+
         #[test]
         fn check_size() {
             assert_eq!(::std::mem::size_of::<EnumSet<$e>>(), $mem_size);
@@ -287,7 +361,87 @@ macro_rules! tests {
 }
 
 tests!(small_enum, test_enum!(SmallEnum, 4));
+tests!(small_enum_explicit_derive, test_enum!(SmallEnumExplicitDerive, 4));
 tests!(large_enum, test_enum!(LargeEnum, 16));
 tests!(enum8, test_enum!(Enum8, 1));
 tests!(enum128, test_enum!(Enum128, 16));
 tests!(sparse_enum, test_enum!(SparseEnum, 16));
+tests!(repr_enum_u32, test_enum!(ReprEnum, 4));
+tests!(repr_enum_u64, test_enum!(ReprEnum2, 4));
+tests!(repr_enum_isize, test_enum!(ReprEnum3, 4));
+tests!(repr_enum_c, test_enum!(ReprEnum4, 4));
+
+#[derive(EnumSetType, Debug)]
+pub enum ThresholdEnum {
+    A = 1, B, C, D,
+    U8 = 0, U16 = 8, U32 = 16, U64 = 32, U128 = 64,
+}
+macro_rules! bits_tests {
+    (
+        $mod_name:ident, $threshold_expr:expr, ($($too_big_expr:expr),*), $ty:ty,
+        $to:ident $try_to:ident $to_truncated:ident
+        $from:ident $try_from:ident $from_truncated:ident
+    ) => {
+        mod $mod_name {
+            use super::*;
+            use crate::ThresholdEnum::*;
+
+            #[test]
+            fn to_from_basic() {
+                for &mask in &[
+                    $threshold_expr | B | C | D,
+                    $threshold_expr | A | D,
+                    $threshold_expr | B | C,
+                ] {
+                    assert_eq!(mask, EnumSet::<ThresholdEnum>::$from(mask.$to()));
+                    assert_eq!(mask.$to_truncated(), mask.$to());
+                    assert_eq!(Some(mask.$to()), mask.$try_to())
+                }
+            }
+
+            #[test]
+            #[should_panic]
+            fn from_invalid() {
+                let invalid_mask: $ty = 0x80;
+                EnumSet::<ThresholdEnum>::$from(invalid_mask);
+            }
+
+            #[test]
+            fn try_from_invalid() {
+                assert!(EnumSet::<ThresholdEnum>::$try_from(0xFF).is_none());
+            }
+
+            $(
+                #[test]
+                fn try_to_overflow() {
+                        let set: EnumSet<ThresholdEnum> = $too_big_expr.into();
+                        assert!(set.$try_to().is_none());
+                }
+            )*
+
+            #[test]
+            fn truncated_overflow() {
+                let trunc_invalid = EnumSet::<ThresholdEnum>::$from_truncated(0xFE);
+                assert_eq!(A | B | C | D, trunc_invalid);
+                $(
+                    let set: EnumSet<ThresholdEnum> = $too_big_expr | A;
+                    assert_eq!(2, set.$to_truncated());
+                )*
+            }
+        }
+    }
+}
+
+bits_tests!(test_u8_bits, U8, (U16), u8,
+            as_u8 try_as_u8 as_u8_truncated from_u8 try_from_u8 from_u8_truncated);
+bits_tests!(test_u16_bits, U16, (U32), u16,
+            as_u16 try_as_u16 as_u16_truncated from_u16 try_from_u16 from_u16_truncated);
+bits_tests!(test_u32_bits, U32, (U64), u32,
+            as_u32 try_as_u32 as_u32_truncated from_u32 try_from_u32 from_u32_truncated);
+bits_tests!(test_u64_bits, U64, (U128), u64,
+            as_u64 try_as_u64 as_u64_truncated from_u64 try_from_u64 from_u64_truncated);
+bits_tests!(test_u128_bits, U128, (), u128,
+            as_u128 try_as_u128 as_u128_truncated from_u128 try_from_u128 from_u128_truncated);
+bits_tests!(test_uize_bits, U32, (U128), usize,
+            as_usize try_as_usize as_usize_truncated
+            from_usize try_from_usize from_usize_truncated);
\ No newline at end of file