]> git.lizzy.rs Git - enumset.git/blobdiff - enumset/tests/ops.rs
Merge branch 'master' of github.com:Lymia/enumset
[enumset.git] / enumset / tests / ops.rs
index e82ad640bd8e749ee969251c30ccc22500fbd9da..aa7019b8c3334d8d8c5bf48da393d60fe0a009fa 100644 (file)
@@ -48,6 +48,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]
@@ -132,15 +153,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);
@@ -153,6 +173,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>) {
@@ -160,9 +196,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() {
@@ -220,7 +258,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_u128(value.to_u128()), value);
+            assert_eq!(EnumSet::from_u128(value.as_u128()), value);
         }
 
         #[test]
@@ -300,6 +338,10 @@ 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 {
@@ -363,12 +405,15 @@ macro_rules! bits_tests {
 }
 
 bits_tests!(test_u8_bits, U8, (U16), u8,
-            to_u8 to_u8_checked to_u8_truncated from_u8 try_from_u8 from_u8_truncated);
+            as_u8 try_as_u8 as_u8_truncated from_u8 try_from_u8 from_u8_truncated);
 bits_tests!(test_u16_bits, U16, (U32), u16,
-            to_u16 to_u16_checked to_u16_truncated from_u16 try_from_u16 from_u16_truncated);
+            as_u16 try_as_u16 as_u16_truncated from_u16 try_from_u16 from_u16_truncated);
 bits_tests!(test_u32_bits, U32, (U64), u32,
-            to_u32 to_u32_checked to_u32_truncated from_u32 try_from_u32 from_u32_truncated);
+            as_u32 try_as_u32 as_u32_truncated from_u32 try_from_u32 from_u32_truncated);
 bits_tests!(test_u64_bits, U64, (U128), u64,
-            to_u64 to_u64_checked to_u64_truncated from_u64 try_from_u64 from_u64_truncated);
+            as_u64 try_as_u64 as_u64_truncated from_u64 try_from_u64 from_u64_truncated);
 bits_tests!(test_u128_bits, U128, (), u128,
-            to_u128 to_u128_checked to_u128_truncated from_u128 try_from_u128 from_u128_truncated);
\ No newline at end of file
+            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