microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/paulimer/tests/bitblock_test.rs
97lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use paulimer::bits::bitblock::BitBlock; |
| 5 | use paulimer::bits::WORD_COUNT_DEFAULT; |
| 6 | use proptest::prelude::*; |
| 7 | |
| 8 | proptest! { |
| 9 | #[test] |
| 10 | fn from_array(bits in arbitrary_bool_array()) { |
| 11 | let block = BitBlock::from_array(bits); |
| 12 | for index in 0..bits.len() { |
| 13 | assert_eq!(block[index], bits[index]); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn set(block in arbitrary_bitblock(), index in 0..BITS) { |
| 19 | let mut clone = block.clone(); |
| 20 | for value in [true, false] { |
| 21 | clone.set(index, value); |
| 22 | assert_eq!(clone[index], value); |
| 23 | for index2 in 0..BITS { |
| 24 | if index != index2 { |
| 25 | assert_eq!(clone[index2], block[index2]); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn xor(left in arbitrary_bitblock(), right in arbitrary_bitblock()) { |
| 33 | let xor = &left ^ &right; |
| 34 | for index in 0..BITS { |
| 35 | assert_eq!(xor[index], left[index] ^ right[index]); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn xor_assign(mut left in arbitrary_bitblock(), right in arbitrary_bitblock()) { |
| 41 | let xor = &left ^ &right; |
| 42 | left ^= &right; |
| 43 | assert_eq!(left, xor); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn and(left in arbitrary_bitblock(), right in arbitrary_bitblock()) { |
| 48 | let and = &left & &right; |
| 49 | for index in 0..BITS { |
| 50 | assert_eq!(and[index], left[index] & right[index]); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn and_assign(mut left in arbitrary_bitblock(), right in arbitrary_bitblock()) { |
| 56 | let and = &left & &right; |
| 57 | left &= &right; |
| 58 | assert_eq!(left, and); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn zeros() { |
| 65 | let block = BitBlock::zeros(); |
| 66 | for index in 0..BITS { |
| 67 | assert!(!block[index], "{}", index); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn ones() { |
| 73 | let block = BitBlock::ones(); |
| 74 | for index in 0..BITS { |
| 75 | assert!(block[index], "{}", index); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn all() { |
| 81 | for value in [true, false] { |
| 82 | let block = BitBlock::all(value); |
| 83 | for index in 0..BITS { |
| 84 | assert_eq!(block[index], value, "{index}"); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | const BITS: usize = BitBlock::<WORD_COUNT_DEFAULT>::BITS; |
| 90 | |
| 91 | fn arbitrary_bool_array() -> impl Strategy<Value = [bool; BITS]> { |
| 92 | proptest::array::uniform::<proptest::bool::Any, BITS>(proptest::bool::ANY) |
| 93 | } |
| 94 | |
| 95 | fn arbitrary_bitblock() -> impl Strategy<Value = BitBlock> { |
| 96 | arbitrary_bool_array().prop_map(BitBlock::from_array) |
| 97 | } |
| 98 | |