microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/paulimer/tests/bit_test.rs
97lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use paulimer::{ |
| 5 | bits::{ |
| 6 | bitblock::BitBlock, BitVec, Bitwise, BitwiseBinaryOps, BitwiseNeutralElement, |
| 7 | IndexAssignable, IndexSet, |
| 8 | }, |
| 9 | NeutralElement, |
| 10 | }; |
| 11 | use std::any::type_name; |
| 12 | |
| 13 | /// # Panics |
| 14 | /// |
| 15 | /// Will panic |
| 16 | pub fn test_one_bit_index<T: IndexAssignable + Bitwise + BitwiseNeutralElement>( |
| 17 | mut bits: T, |
| 18 | index: usize, |
| 19 | ) -> T { |
| 20 | bits.clear_bits(); |
| 21 | assert!(bits.is_zero()); |
| 22 | bits.assign_index(index, true); |
| 23 | assert!(bits.index(index)); |
| 24 | assert!(bits.support().any(|elt| elt == index)); |
| 25 | assert_eq!(bits.support().count(), 1); |
| 26 | assert_eq!(bits.weight(), 1); |
| 27 | assert!(bits.parity()); |
| 28 | bits.negate_index(index); |
| 29 | assert!(!bits.index(index)); |
| 30 | assert_eq!(bits.support().count(), 0); |
| 31 | assert_eq!(bits.weight(), 0); |
| 32 | assert!(!bits.parity()); |
| 33 | |
| 34 | let mut other_bits = bits.neutral_element(); |
| 35 | bits.negate_index(index); |
| 36 | assert!(other_bits.is_zero()); |
| 37 | other_bits.neutral_element(); |
| 38 | other_bits.bitxor_assign(&bits); |
| 39 | assert_eq!(other_bits.support().count(), 1); |
| 40 | assert_eq!(other_bits.weight(), 1); |
| 41 | assert!(other_bits.parity()); |
| 42 | bits.clear_bits(); |
| 43 | |
| 44 | other_bits.negate_index(index + 1); |
| 45 | other_bits.bitand_assign(&bits); |
| 46 | assert_eq!(bits.weight(), 0); |
| 47 | assert!(!bits.parity()); |
| 48 | |
| 49 | bits |
| 50 | } |
| 51 | |
| 52 | /// # Panics |
| 53 | /// |
| 54 | /// Will panic |
| 55 | pub fn test_unary_bit_traits<T: BitwiseNeutralElement>(size: usize, index: usize) |
| 56 | where |
| 57 | T::NeutralElementType: Bitwise + IndexAssignable + BitwiseNeutralElement, |
| 58 | { |
| 59 | assert!(index + 1 < size); |
| 60 | println!( |
| 61 | "Testing: test_unary_bit_traits::<{}> size:{} index:{}", |
| 62 | type_name::<T>(), |
| 63 | size, |
| 64 | index |
| 65 | ); |
| 66 | let item = T::neutral_element_of_size(size); |
| 67 | test_one_bit_index(item, index); |
| 68 | } |
| 69 | |
| 70 | macro_rules! call_test_per_uint { |
| 71 | ($function:ident,$uint:ty,$first:expr $(, $rest:expr)*) => { |
| 72 | $function::<$uint>($first,$($rest),*); |
| 73 | $function::<[$uint;ARRAY_SIZE]>($first,$($rest),*); |
| 74 | $function::<Vec<$uint>>($first,$($rest),*); |
| 75 | $function::<Vec<[$uint;ARRAY_SIZE]>>($first,$($rest),*); |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | macro_rules! call_test { |
| 80 | ($function:ident,$first:expr $(, $rest:expr)*) => { |
| 81 | call_test_per_uint!($function,u16,$first,$($rest),*); |
| 82 | call_test_per_uint!($function,u32,$first,$($rest),*); |
| 83 | call_test_per_uint!($function,u64,$first,$($rest),*); |
| 84 | call_test_per_uint!($function,u128,$first,$($rest),*); |
| 85 | $function::<IndexSet>($first,$($rest),*); |
| 86 | $function::<BitBlock>($first,$($rest),*); |
| 87 | $function::<BitVec>($first,$($rest),*); |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn bit_test() { |
| 93 | const ARRAY_SIZE: usize = 4; |
| 94 | let size = 10; |
| 95 | let index = 7; |
| 96 | call_test!(test_unary_bit_traits, size, index); |
| 97 | } |
| 98 | |