| 1 |
//! Unit tests for boxed bit-slices. |
| 2 |
|
| 3 |
#![cfg(test)] |
| 4 |
|
| 5 |
#[cfg(not(feature = "std"))] |
| 6 |
use alloc::vec; |
| 7 |
use alloc::{ |
| 8 |
borrow::Cow, |
| 9 |
boxed::Box, |
| 10 |
}; |
| 11 |
use core::{ |
| 12 |
any, |
| 13 |
borrow::{ |
| 14 |
Borrow, |
| 15 |
BorrowMut, |
| 16 |
}, |
| 17 |
convert::TryFrom, |
| 18 |
fmt::{ |
| 19 |
Debug, |
| 20 |
Display, |
| 21 |
Pointer, |
| 22 |
}, |
| 23 |
hash::Hash, |
| 24 |
iter::{ |
| 25 |
FromIterator, |
| 26 |
FusedIterator, |
| 27 |
}, |
| 28 |
ops::{ |
| 29 |
Deref, |
| 30 |
DerefMut, |
| 31 |
}, |
| 32 |
}; |
| 33 |
|
| 34 |
use static_assertions::*; |
| 35 |
|
| 36 |
use crate::prelude::*; |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn inherents() { |
| 40 |
let bits = bits![0, 1, 0, 0, 1]; |
| 41 |
let mut boxed = BitBox::from_bitslice(&bits[1 ..]); |
| 42 |
assert_eq!(boxed, bits[1 ..]); |
| 43 |
assert_eq!(boxed.bitspan.head().into_inner(), 1); |
| 44 |
boxed.force_align(); |
| 45 |
assert_eq!(boxed.bitspan.head().into_inner(), 0); |
| 46 |
|
| 47 |
let data = vec![0u8, 1, 2, 3].into_boxed_slice(); |
| 48 |
let ptr = data.as_ptr(); |
| 49 |
let boxed: BitBox<u8> = BitBox::from_boxed_slice(data); |
| 50 |
assert_eq!(boxed.len(), 32); |
| 51 |
assert_eq!(boxed.count_ones(), 4); |
| 52 |
let data = boxed.into_boxed_slice(); |
| 53 |
assert_eq!(data.as_ptr(), ptr); |
| 54 |
|
| 55 |
let bv = BitBox::<u8>::from_boxed_slice(data).into_bitvec(); |
| 56 |
assert_eq!(bv.len(), 32); |
| 57 |
|
| 58 |
assert_eq!(bitbox![0, 1, 0, 0, 1].as_bitslice(), bits![0, 1, 0, 0, 1]); |
| 59 |
assert_eq!(bitbox![0; 5].as_mut_bitslice(), bits![0; 5]); |
| 60 |
|
| 61 |
let mut bb = bitbox![0; 5]; |
| 62 |
bb.fill_uninitialized(true); |
| 63 |
assert_eq!(bb.as_raw_slice(), &[!0usize << 5][..]); |
| 64 |
|
| 65 |
let ptr = BitBox::into_raw(bb); |
| 66 |
let bb = unsafe { BitBox::from_raw(ptr) }; |
| 67 |
assert_eq!(ptr as *const BitSlice, bb.as_bitslice() as *const BitSlice); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn iter() { |
| 72 |
let bb = bitbox![0, 1, 1, 0, 0, 1]; |
| 73 |
let mut iter = bb.into_iter(); |
| 74 |
assert_eq!(iter.len(), 6); |
| 75 |
|
| 76 |
assert!(!iter.next().unwrap()); |
| 77 |
assert_eq!(iter.as_bitslice(), bits![1, 1, 0, 0, 1]); |
| 78 |
assert!(iter.next_back().unwrap()); |
| 79 |
assert_eq!(iter.as_mut_bitslice(), bits![1, 1, 0, 0]); |
| 80 |
assert!(iter.nth(1).unwrap()); |
| 81 |
assert!(!iter.nth_back(1).unwrap()); |
| 82 |
assert!(iter.next().is_none()); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn traits() { |
| 87 |
assert_impl_all!( |
| 88 |
BitBox: AsMut<BitSlice>, |
| 89 |
AsRef<BitSlice>, |
| 90 |
Borrow<BitSlice>, |
| 91 |
BorrowMut<BitSlice>, |
| 92 |
Clone, |
| 93 |
Debug, |
| 94 |
Default, |
| 95 |
Deref, |
| 96 |
DerefMut, |
| 97 |
Display, |
| 98 |
Drop, |
| 99 |
Eq, |
| 100 |
From<&'static BitSlice>, |
| 101 |
From<BitArray>, |
| 102 |
From<Box<usize>>, |
| 103 |
From<Cow<'static, BitSlice>>, |
| 104 |
From<BitVec>, |
| 105 |
FromIterator<bool>, |
| 106 |
Hash, |
| 107 |
Ord, |
| 108 |
PartialEq<BitSlice>, |
| 109 |
PartialOrd<BitSlice>, |
| 110 |
Pointer, |
| 111 |
TryFrom<Box<[usize]>>, |
| 112 |
Unpin, |
| 113 |
); |
| 114 |
assert_impl_all!( |
| 115 |
super::IntoIter: AsRef<BitSlice>, |
| 116 |
Clone, |
| 117 |
Debug, |
| 118 |
DoubleEndedIterator, |
| 119 |
ExactSizeIterator, |
| 120 |
FusedIterator, |
| 121 |
Send, |
| 122 |
Sync, |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn conversions() { |
| 128 |
let bits = bits![0, 1, 0, 0, 1]; |
| 129 |
assert_eq!(BitBox::from(bits), bits); |
| 130 |
|
| 131 |
let arr: BitArray = BitArray::new(rand::random()); |
| 132 |
assert_eq!(BitBox::from(arr), arr); |
| 133 |
|
| 134 |
let boxed = Box::new(5usize); |
| 135 |
assert_eq!( |
| 136 |
BitBox::<_, Lsb0>::from(boxed.clone()), |
| 137 |
boxed.view_bits::<Lsb0>() |
| 138 |
); |
| 139 |
|
| 140 |
let cow = Cow::Borrowed([0usize, 1].view_bits::<Lsb0>()); |
| 141 |
assert_eq!(BitBox::from(cow.clone()), &*cow); |
| 142 |
|
| 143 |
assert_eq!(BitBox::from(bitvec![0, 1]), bits![0, 1]); |
| 144 |
|
| 145 |
let boxed: Box<[usize]> = BitBox::from(cow.clone()).into(); |
| 146 |
assert_eq!(boxed[..], [0usize, 1][..]); |
| 147 |
|
| 148 |
assert!(BitBox::<_, Lsb0>::try_from(boxed).is_ok()); |
| 149 |
|
| 150 |
assert!(BitBox::<usize, Lsb0>::default().is_empty()); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn ops() { |
| 155 |
let a = bitbox![0, 0, 1, 1]; |
| 156 |
let b = bitbox![0, 1, 0, 1]; |
| 157 |
|
| 158 |
let c = a.clone() & b.clone(); |
| 159 |
assert_eq!(c, bitbox![0, 0, 0, 1]); |
| 160 |
|
| 161 |
let d = a.clone() | b.clone(); |
| 162 |
assert_eq!(d, bitbox![0, 1, 1, 1]); |
| 163 |
|
| 164 |
let e = a.clone() ^ b; |
| 165 |
assert_eq!(e, bitbox![0, 1, 1, 0]); |
| 166 |
|
| 167 |
let mut f = !e; |
| 168 |
assert_eq!(f, bitbox![1, 0, 0, 1]); |
| 169 |
|
| 170 |
let _: &BitSlice = &a; |
| 171 |
let _: &mut BitSlice = &mut f; |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn format() { |
| 176 |
#[cfg(not(feature = "std"))] |
| 177 |
use alloc::format; |
| 178 |
|
| 179 |
let render = format!("{:?}", bitbox![0, 1, 0, 0, 1]); |
| 180 |
assert!( |
| 181 |
render.starts_with(&format!( |
| 182 |
"BitBox<usize, {}>", |
| 183 |
any::type_name::<Lsb0>(), |
| 184 |
)) |
| 185 |
); |
| 186 |
assert!(render.ends_with("[0, 1, 0, 0, 1]")); |
| 187 |
} |