| 1 |
use core::{ |
| 2 |
cmp, |
| 3 |
convert::TryFrom, |
| 4 |
}; |
| 5 |
|
| 6 |
use static_assertions::*; |
| 7 |
|
| 8 |
use crate::prelude::*; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn core_impls() { |
| 12 |
use core::{ |
| 13 |
cmp::{ |
| 14 |
Eq, |
| 15 |
Ord, |
| 16 |
}, |
| 17 |
fmt::Debug, |
| 18 |
hash::Hash, |
| 19 |
ops::{ |
| 20 |
Index, |
| 21 |
Range, |
| 22 |
}, |
| 23 |
panic::{ |
| 24 |
RefUnwindSafe, |
| 25 |
UnwindSafe, |
| 26 |
}, |
| 27 |
}; |
| 28 |
|
| 29 |
assert_impl_all!(BitSlice<usize, Lsb0>: |
| 30 |
AsRef<BitSlice<usize, Lsb0>>, |
| 31 |
AsMut<BitSlice<usize, Lsb0>>, |
| 32 |
Debug, |
| 33 |
Eq, |
| 34 |
Hash, |
| 35 |
Index<usize>, |
| 36 |
Index<Range<usize>>, |
| 37 |
Ord, |
| 38 |
PartialEq<BitSlice<u32, Msb0>>, |
| 39 |
PartialOrd<BitSlice<u32, Msb0>>, |
| 40 |
Send, |
| 41 |
Sync, |
| 42 |
Unpin, |
| 43 |
UnwindSafe, |
| 44 |
RefUnwindSafe, |
| 45 |
); |
| 46 |
assert_impl_all!(&BitSlice<usize, Lsb0>: |
| 47 |
Default, |
| 48 |
IntoIterator, |
| 49 |
); |
| 50 |
assert_impl_all!(&mut BitSlice<usize, Lsb0>: |
| 51 |
Default, |
| 52 |
IntoIterator, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
#[cfg(feature = "alloc")] |
| 58 |
fn alloc_impls() { |
| 59 |
use alloc::borrow::ToOwned; |
| 60 |
|
| 61 |
assert_impl_all!(BitSlice<usize, Lsb0>: |
| 62 |
ToOwned, |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
#[allow(deprecated)] |
| 68 |
#[cfg(feature = "std")] |
| 69 |
fn std_impls() { |
| 70 |
use std::{ |
| 71 |
ascii::AsciiExt, |
| 72 |
io::{ |
| 73 |
BufRead, |
| 74 |
Read, |
| 75 |
Write, |
| 76 |
}, |
| 77 |
}; |
| 78 |
|
| 79 |
assert_impl_all!(&BitSlice<usize, Lsb0>: |
| 80 |
Read, |
| 81 |
); |
| 82 |
assert_impl_all!(&mut BitSlice<usize, Lsb0>: |
| 83 |
Write, |
| 84 |
); |
| 85 |
assert_not_impl_any!(BitSlice<u8, Lsb0>: |
| 86 |
AsciiExt, |
| 87 |
BufRead, |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn cmp() { |
| 93 |
let a = bits![0, 1]; |
| 94 |
let b = bits![1, 0]; |
| 95 |
let c = bits![u8, Msb0; 1, 0]; |
| 96 |
let d = bits![u8, Msb0; 1, 1]; |
| 97 |
|
| 98 |
assert_eq!(a.cmp(b), cmp::Ordering::Less); |
| 99 |
assert_ne!(a, b); |
| 100 |
assert_eq!(b, c); |
| 101 |
assert_ne!(c, d); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn conv() -> Result<(), ()> { |
| 106 |
let mut a = [0u8, 1, 2, 3]; |
| 107 |
let _ = <&BitSlice<_, Lsb0>>::try_from(&a[..]).map_err(drop)?; |
| 108 |
let _ = <&mut BitSlice<_, Lsb0>>::try_from(&mut a[..]).map_err(drop)?; |
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
|
| 112 |
#[cfg(feature = "alloc")] |
| 113 |
mod format { |
| 114 |
#[cfg(not(feature = "std"))] |
| 115 |
use alloc::format; |
| 116 |
|
| 117 |
use crate::prelude::*; |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn binary() { |
| 121 |
let data = [0u8, 0x0F, !0]; |
| 122 |
let bits = data.view_bits::<Msb0>(); |
| 123 |
|
| 124 |
assert_eq!(format!("{:b}", &bits[.. 0]), "[]"); |
| 125 |
assert_eq!(format!("{:#b}", &bits[.. 0]), "[]"); |
| 126 |
|
| 127 |
assert_eq!(format!("{:b}", &bits[9 .. 15]), "[000111]"); |
| 128 |
assert_eq!( |
| 129 |
format!("{:#b}", &bits[9 .. 15]), |
| 130 |
"[ |
| 131 |
0b000111, |
| 132 |
]" |
| 133 |
); |
| 134 |
|
| 135 |
assert_eq!(format!("{:b}", &bits[4 .. 20]), "[0000, 00001111, 1111]"); |
| 136 |
assert_eq!( |
| 137 |
format!("{:#b}", &bits[4 .. 20]), |
| 138 |
"[ |
| 139 |
0b0000, |
| 140 |
0b00001111, |
| 141 |
0b1111, |
| 142 |
]" |
| 143 |
); |
| 144 |
|
| 145 |
assert_eq!(format!("{:b}", &bits[4 ..]), "[0000, 00001111, 11111111]"); |
| 146 |
assert_eq!( |
| 147 |
format!("{:#b}", &bits[4 ..]), |
| 148 |
"[ |
| 149 |
0b0000, |
| 150 |
0b00001111, |
| 151 |
0b11111111, |
| 152 |
]" |
| 153 |
); |
| 154 |
|
| 155 |
assert_eq!(format!("{:b}", &bits[.. 20]), "[00000000, 00001111, 1111]"); |
| 156 |
assert_eq!( |
| 157 |
format!("{:#b}", &bits[.. 20]), |
| 158 |
"[ |
| 159 |
0b00000000, |
| 160 |
0b00001111, |
| 161 |
0b1111, |
| 162 |
]" |
| 163 |
); |
| 164 |
|
| 165 |
assert_eq!(format!("{:b}", bits), "[00000000, 00001111, 11111111]"); |
| 166 |
assert_eq!( |
| 167 |
format!("{:#b}", bits), |
| 168 |
"[ |
| 169 |
0b00000000, |
| 170 |
0b00001111, |
| 171 |
0b11111111, |
| 172 |
]" |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn octal() { |
| 178 |
let data = [0u8, 0x0F, !0]; |
| 179 |
let bits = data.view_bits::<Msb0>(); |
| 180 |
|
| 181 |
assert_eq!(format!("{:o}", &bits[.. 0]), "[]"); |
| 182 |
assert_eq!(format!("{:#o}", &bits[.. 0]), "[]"); |
| 183 |
|
| 184 |
assert_eq!(format!("{:o}", &bits[9 .. 15]), "[07]"); |
| 185 |
assert_eq!( |
| 186 |
format!("{:#o}", &bits[9 .. 15]), |
| 187 |
"[ |
| 188 |
0o07, |
| 189 |
]" |
| 190 |
); |
| 191 |
|
| 192 |
// …0_000 00_001_111 1_111… |
| 193 |
assert_eq!(format!("{:o}", &bits[4 .. 20]), "[00, 017, 17]"); |
| 194 |
assert_eq!( |
| 195 |
format!("{:#o}", &bits[4 .. 20]), |
| 196 |
"[ |
| 197 |
0o00, |
| 198 |
0o017, |
| 199 |
0o17, |
| 200 |
]" |
| 201 |
); |
| 202 |
|
| 203 |
assert_eq!(format!("{:o}", &bits[4 ..]), "[00, 017, 377]"); |
| 204 |
assert_eq!( |
| 205 |
format!("{:#o}", &bits[4 ..]), |
| 206 |
"[ |
| 207 |
0o00, |
| 208 |
0o017, |
| 209 |
0o377, |
| 210 |
]" |
| 211 |
); |
| 212 |
|
| 213 |
assert_eq!(format!("{:o}", &bits[.. 20]), "[000, 017, 17]"); |
| 214 |
assert_eq!( |
| 215 |
format!("{:#o}", &bits[.. 20]), |
| 216 |
"[ |
| 217 |
0o000, |
| 218 |
0o017, |
| 219 |
0o17, |
| 220 |
]" |
| 221 |
); |
| 222 |
|
| 223 |
assert_eq!(format!("{:o}", bits), "[000, 017, 377]"); |
| 224 |
assert_eq!( |
| 225 |
format!("{:#o}", bits), |
| 226 |
"[ |
| 227 |
0o000, |
| 228 |
0o017, |
| 229 |
0o377, |
| 230 |
]" |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
#[test] |
| 235 |
fn hex_lower() { |
| 236 |
let data = [0u8, 0x0F, !0]; |
| 237 |
let bits = data.view_bits::<Msb0>(); |
| 238 |
|
| 239 |
assert_eq!(format!("{:x}", &bits[.. 0]), "[]"); |
| 240 |
assert_eq!(format!("{:#x}", &bits[.. 0]), "[]"); |
| 241 |
|
| 242 |
// …00_0111 … |
| 243 |
assert_eq!(format!("{:x}", &bits[9 .. 15]), "[07]"); |
| 244 |
assert_eq!( |
| 245 |
format!("{:#x}", &bits[9 .. 15]), |
| 246 |
"[ |
| 247 |
0x07, |
| 248 |
]" |
| 249 |
); |
| 250 |
|
| 251 |
// …0000 00001111 1111… |
| 252 |
assert_eq!(format!("{:x}", &bits[4 .. 20]), "[0, 0f, f]"); |
| 253 |
assert_eq!( |
| 254 |
format!("{:#x}", &bits[4 .. 20]), |
| 255 |
"[ |
| 256 |
0x0, |
| 257 |
0x0f, |
| 258 |
0xf, |
| 259 |
]" |
| 260 |
); |
| 261 |
|
| 262 |
assert_eq!(format!("{:x}", &bits[4 ..]), "[0, 0f, ff]"); |
| 263 |
assert_eq!( |
| 264 |
format!("{:#x}", &bits[4 ..]), |
| 265 |
"[ |
| 266 |
0x0, |
| 267 |
0x0f, |
| 268 |
0xff, |
| 269 |
]" |
| 270 |
); |
| 271 |
|
| 272 |
assert_eq!(format!("{:x}", &bits[.. 20]), "[00, 0f, f]"); |
| 273 |
assert_eq!( |
| 274 |
format!("{:#x}", &bits[.. 20]), |
| 275 |
"[ |
| 276 |
0x00, |
| 277 |
0x0f, |
| 278 |
0xf, |
| 279 |
]" |
| 280 |
); |
| 281 |
|
| 282 |
assert_eq!(format!("{:x}", bits), "[00, 0f, ff]"); |
| 283 |
assert_eq!( |
| 284 |
format!("{:#x}", bits), |
| 285 |
"[ |
| 286 |
0x00, |
| 287 |
0x0f, |
| 288 |
0xff, |
| 289 |
]" |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn hex_upper() { |
| 295 |
let data = [0u8, 0x0F, !0]; |
| 296 |
let bits = data.view_bits::<Msb0>(); |
| 297 |
|
| 298 |
assert_eq!(format!("{:X}", &bits[.. 0]), "[]"); |
| 299 |
assert_eq!(format!("{:#X}", &bits[.. 0]), "[]"); |
| 300 |
|
| 301 |
assert_eq!(format!("{:X}", &bits[9 .. 15]), "[07]"); |
| 302 |
assert_eq!( |
| 303 |
format!("{:#X}", &bits[9 .. 15]), |
| 304 |
"[ |
| 305 |
0x07, |
| 306 |
]" |
| 307 |
); |
| 308 |
|
| 309 |
assert_eq!(format!("{:X}", &bits[4 .. 20]), "[0, 0F, F]"); |
| 310 |
assert_eq!( |
| 311 |
format!("{:#X}", &bits[4 .. 20]), |
| 312 |
"[ |
| 313 |
0x0, |
| 314 |
0x0F, |
| 315 |
0xF, |
| 316 |
]" |
| 317 |
); |
| 318 |
|
| 319 |
assert_eq!(format!("{:X}", &bits[4 ..]), "[0, 0F, FF]"); |
| 320 |
assert_eq!( |
| 321 |
format!("{:#X}", &bits[4 ..]), |
| 322 |
"[ |
| 323 |
0x0, |
| 324 |
0x0F, |
| 325 |
0xFF, |
| 326 |
]" |
| 327 |
); |
| 328 |
|
| 329 |
assert_eq!(format!("{:X}", &bits[.. 20]), "[00, 0F, F]"); |
| 330 |
assert_eq!( |
| 331 |
format!("{:#X}", &bits[.. 20]), |
| 332 |
"[ |
| 333 |
0x00, |
| 334 |
0x0F, |
| 335 |
0xF, |
| 336 |
]" |
| 337 |
); |
| 338 |
|
| 339 |
assert_eq!(format!("{:X}", bits), "[00, 0F, FF]"); |
| 340 |
assert_eq!( |
| 341 |
format!("{:#X}", bits), |
| 342 |
"[ |
| 343 |
0x00, |
| 344 |
0x0F, |
| 345 |
0xFF, |
| 346 |
]" |
| 347 |
); |
| 348 |
} |
| 349 |
} |