1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
use std::cmp;
pub type Bitline8 = u8;
pub type Bitline16 = u16;
pub type Bitline32 = u32;
pub type Bitline64 = u64;
pub type Bitline128 = u128;
pub trait Bitline {
/// Return the bits all set to 0
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!(Bitline8::as_empty(), 0b00000000);
/// ```
fn as_empty() -> Self;
/// Return the bits all set to 1
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!(Bitline8::as_full(), 0b11111111);
/// ```
fn as_full() -> Self;
/// Return the bits standing in the given range.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!(Bitline8::by_range(2, 5), 0b00111000_u8);
/// ```
fn by_range(begin: usize, end: usize) -> Self;
/// Return true if the bit is filled with zero.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert!(!0b00111000_u8.is_empty());
/// assert!(0b00000000_u8.is_empty());
/// ```
fn is_empty(&self) -> bool;
/// Return true if the bit is not filled with zero.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert!(0b00111000_u8.is_not_empty());
/// assert!(!0b00000000_u8.is_not_empty());
/// ```
fn is_not_empty(&self) -> bool;
/// Return true if the bit is filled with one.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert!((0b11111111 as Bitline8).is_full());
/// assert!(!(0b01111111 as Bitline8).is_full());
/// ```
fn is_full(&self) -> bool;
/// Return true if the bit is not filled with one.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert!((0b01111111 as Bitline8).is_not_full());
/// assert!(!(0b11111111 as Bitline8).is_not_full());
/// ```
fn is_not_full(&self) -> bool;
/// Return the first bit index that is set to one.
/// If there is no bit set to one, return None.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!((0b00111000 as Bitline8).first_index(), Some(2));
/// assert_eq!((0b00000000 as Bitline8).first_index(), None);
/// ```
fn first_index(&self) -> Option<usize>;
/// Return the last bit index that is set to one.
/// If there is no bit set to one, return None.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!((0b00111000 as Bitline8).last_index(), Some(4));
/// assert_eq!((0b00000000 as Bitline8).last_index(), None);
/// ```
fn last_index(&self) -> Option<usize>;
/// Return the bits standing in n distance from the original starting bit.
/// If there is no bit set to one, return None.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b00001000 as Bitline8;
/// assert_eq!(bitline.radius(0), 0b00000000);
/// assert_eq!(bitline.radius(1), 0b00010100);
/// assert_eq!(bitline.radius(2), 0b00100010);
///
/// let bitline = 0b00000000 as Bitline8;
/// assert_eq!(bitline.radius(0), 0b00000000);
/// assert_eq!(bitline.radius(1), 0b00000000);
/// assert_eq!(bitline.radius(2), 0b00000000);
///
/// let bitline = 0b00100100 as Bitline8;
/// assert_eq!(bitline.radius(0), 0b00000000);
/// assert_eq!(bitline.radius(1), 0b01011010);
/// assert_eq!(bitline.radius(2), 0b10011001);
/// ```
fn radius(&self, n: usize) -> Self;
/// Return all bits standing between n distance from the standing bits (without original standing bits).
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b00001000 as Bitline8;
/// assert_eq!(bitline.around(0), 0b00000000);
/// assert_eq!(bitline.around(1), 0b00010100);
/// assert_eq!(bitline.around(2), 0b00110110);
///
/// let bitline = 0b00000000 as Bitline8;
/// assert_eq!(bitline.around(0), 0b00000000);
/// assert_eq!(bitline.around(1), 0b00000000);
/// assert_eq!(bitline.around(2), 0b00000000);
/// ```
fn around(&self, n: usize) -> Self;
/// Return all bits standing between n distance from the standing bits (with original standing bits).
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b00001000 as Bitline8;
/// assert_eq!(bitline.with_around(0), 0b00001000);
/// assert_eq!(bitline.with_around(1), 0b00011100);
/// assert_eq!(bitline.with_around(2), 0b00111110);
///
/// let bitline = 0b00000000 as Bitline8;
/// assert_eq!(bitline.with_around(0), 0b00000000);
/// assert_eq!(bitline.with_around(1), 0b00000000);
/// assert_eq!(bitline.with_around(2), 0b00000000);
/// ```
fn with_around(&self, n: usize) -> Self;
/// Return the first bit from the most significant bit. (last bit from the least significant bit)
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01001000_u8;
/// assert_eq!(bitline.first_bit(), 0b01000000_u8);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.first_bit(), 0b00000000_u8);
/// ```
fn first_bit(&self) -> Self;
/// Return the last bit from the most significant bit. (first bit from the least significant bit)
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01001000_u8;
/// assert_eq!(bitline.last_bit(), 0b00001000_u8);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.last_bit(), 0b00000000_u8);
/// ```
fn last_bit(&self) -> Self;
/// Return the first bits of each consecutive bits.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert_eq!(bitline.first_bits(), 0b01001000_u8);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.first_bits(), 0b00000000_u8);
/// ```
fn first_bits(&self) -> Self;
/// Return the last bits of each consecutive bits.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert_eq!(bitline.last_bits(), 0b00100100_u8);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.last_bits(), 0b00000000_u8);
/// ```
fn last_bits(&self) -> Self;
/// Return the bits filled from the first bit to the last bit.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01000100_u8;
/// assert_eq!(bitline.filled_first_bit_to_last_bit(), 0b01111100_u8);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.filled_first_bit_to_last_bit(), 0b00000000_u8);
/// ```
fn filled_first_bit_to_last_bit(&self) -> Self;
/// Return the bytes size of the bitline.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8, Bitline16, Bitline32, Bitline64};
/// assert_eq!(Bitline8::bytes_length(), 1);
/// assert_eq!(Bitline16::bytes_length(), 2);
/// assert_eq!(Bitline32::bytes_length(), 4);
/// assert_eq!(Bitline64::bytes_length(), 8);
/// ```
fn bytes_length() -> usize;
/// Return the bits size of the bitline.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8, Bitline16, Bitline32, Bitline64};
/// assert_eq!(Bitline8::length(), 8);
/// assert_eq!(Bitline16::length(), 16);
/// assert_eq!(Bitline32::length(), 32);
/// assert_eq!(Bitline64::length(), 64);
/// ```
fn length() -> usize;
/// Return the bits standing in the given range. (bit count)
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert_eq!(bitline.num_bits(), 4);
/// let bitline = 0b00000000_u8;
/// assert_eq!(bitline.num_bits(), 0);
/// ```
fn num_bits(&self) -> usize;
/// Return true if every bits are standing in the given standing bits.
/// empty bitlines are always included. (like a empty set in a set)
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert!(bitline.includes(0b01100000_u8));
/// assert!(!bitline.includes(0b01100001_u8));
/// assert!(bitline.includes(0b00000000_u8));
///
/// let bitline = 0b00000000_u8;
/// assert!(bitline.includes(0b00000000_u8));
/// assert!(!bitline.includes(0b00000001_u8));
/// ```
fn includes(&self, other: Self) -> bool;
/// Return true if some bits are standing in the given standing bits.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert!(bitline.overlaps(0b01100000_u8));
/// assert!(!bitline.overlaps(0b00000001_u8));
/// assert!(!bitline.overlaps(0b00000000_u8));
/// ```
fn overlaps(&self, other: Self) -> bool;
/// Return the standing bits by the given range.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert_eq!(bitline.range(0, 4), 0b01100000_u8);
/// assert_eq!(bitline.range(4, 8), 0b00001100_u8);
/// ```
fn range(&self, begin: usize, end: usize) -> Self;
/// Return the standing bits not included by the given range.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// let bitline = 0b01101100_u8;
/// assert_eq!(bitline.remove(0b01100000_u8), 0b00001100_u8);
/// assert_eq!(bitline.remove(0b00001100_u8), 0b01100000_u8);
/// ```
fn remove(&self, other: Self) -> Self;
/// Return the string representation of the bitline.
/// # Examples
/// ```
/// use bittersweet::bitline::{Bitline, Bitline8};
/// assert_eq!(0b01101100_u8.bit_repr(), "01101100");
/// ```
fn bit_repr(&self) -> String;
}
macro_rules! impl_Bitline {
($T:ty) => {
impl Bitline for $T {
#[inline]
fn as_empty() -> Self {
0
}
#[inline]
fn as_full() -> Self {
Self::max_value()
}
#[inline]
fn by_range(begin: usize, end: usize) -> Self {
let bits_size = Self::BITS as usize;
let last_index = cmp::min(end, bits_size);
let first_index = cmp::min(begin, last_index);
let size = last_index - first_index;
if (size <= 0) {
return Self::as_empty();
}
if (size >= bits_size) {
return Self::as_full();
}
let fill_bits = (1 << size) - 1;
let right_pad = bits_size - last_index;
fill_bits << right_pad
}
#[inline]
fn is_empty(&self) -> bool {
*self == Self::as_empty()
}
#[inline]
fn is_not_empty(&self) -> bool {
!self.is_empty()
}
#[inline]
fn is_full(&self) -> bool {
*self == Self::as_full()
}
#[inline]
fn is_not_full(&self) -> bool {
!self.is_full()
}
#[inline]
fn first_index(&self) -> Option<usize> {
let zeros = self.leading_zeros() as usize;
if (zeros == Self::length()) {
return None;
}
Some(zeros)
}
#[inline]
fn last_index(&self) -> Option<usize> {
let zeros = (Self::length() - self.trailing_zeros() as usize);
if (zeros < 1) {
return None;
}
Some(zeros - 1)
}
#[inline]
fn radius(&self, n: usize) -> Self {
(self << n) ^ (self >> n)
}
#[inline]
fn around(&self, n: usize) -> Self {
let mut a = 0;
for m in 0..(n + 1) {
a |= self.radius(m);
}
a
}
#[inline]
fn with_around(&self, n: usize) -> Self {
self | self.around(n)
}
#[inline]
fn first_bit(&self) -> Self {
let zeros = self.leading_zeros() as usize;
if (zeros == Self::length()) {
return Self::as_empty();
}
1 << (Self::length() - zeros - 1)
}
#[inline]
fn last_bit(&self) -> Self {
let zeros = self.trailing_zeros() as usize;
if (zeros == Self::length()) {
return Self::as_empty();
}
1 << self.trailing_zeros()
}
#[inline]
fn first_bits(&self) -> Self {
self & !(self >> 1)
}
#[inline]
fn last_bits(&self) -> Self {
self & !(self << 1)
}
#[inline]
fn filled_first_bit_to_last_bit(&self) -> Self {
if (self.is_empty()) {
return Self::as_empty();
}
let first_index = self.first_index().unwrap();
let last_index = self.last_index().unwrap();
Self::by_range(first_index, last_index + 1)
}
#[inline]
fn length() -> usize {
Self::BITS as usize
}
#[inline]
fn bytes_length() -> usize {
(Self::BITS / 8) as usize
}
#[inline]
fn num_bits(&self) -> usize {
self.count_ones() as usize
}
#[inline]
fn includes(&self, other: Self) -> bool {
(self | other) - self == 0
}
#[inline]
fn overlaps(&self, other: Self) -> bool {
self & other != 0
}
#[inline]
fn range(&self, begin: usize, end: usize) -> Self {
self & Self::by_range(begin, end)
}
#[inline]
fn remove(&self, other: Self) -> Self {
self & !other
}
#[inline]
fn bit_repr(&self) -> String {
let formatted = format!("{:b}", self);
let lack_bits = Self::length() - formatted.len();
"0".repeat(lack_bits) + &formatted
}
}
};
}
impl_Bitline!(Bitline8);
impl_Bitline!(Bitline16);
impl_Bitline!(Bitline32);
impl_Bitline!(Bitline64);
impl_Bitline!(Bitline128);
#[test]
fn test_as_empty() {
assert_eq!(u8::as_empty().bit_repr(), "0".repeat(8));
assert_eq!(u16::as_empty().bit_repr(), "0".repeat(16));
assert_eq!(u32::as_empty().bit_repr(), "0".repeat(32));
assert_eq!(u64::as_empty().bit_repr(), "0".repeat(64));
assert_eq!(u128::as_empty().bit_repr(), "0".repeat(128));
}
#[test]
fn test_as_full() {
assert_eq!(u8::as_full().bit_repr(), "1".repeat(8));
assert_eq!(u16::as_full().bit_repr(), "1".repeat(16));
assert_eq!(u32::as_full().bit_repr(), "1".repeat(32));
assert_eq!(u64::as_full().bit_repr(), "1".repeat(64));
assert_eq!(u128::as_full().bit_repr(), "1".repeat(128));
}
#[test]
fn test_by_range() {
assert_eq!(u8::by_range(3, 4), 0b00010000);
assert_eq!(u8::by_range(0, 8), 0b11111111);
assert_eq!(u8::by_range(0, 0), 0b00000000);
}
#[test]
fn test_first_index() {
assert_eq!(0b01000000_u8.first_index(), Some(1));
assert_eq!(0b00010000_u8.first_index(), Some(3));
assert_eq!(0b00010100_u8.first_index(), Some(3));
assert_eq!(0b00000100_u8.first_index(), Some(5));
assert_eq!(0b00000001_u8.first_index(), Some(7));
assert!(0b00000000_u8.first_index().is_none());
}
#[test]
fn test_last_index() {
assert_eq!(0b01000000_u8.last_index(), Some(1));
assert_eq!(0b00010000_u8.last_index(), Some(3));
assert_eq!(0b00010100_u8.last_index(), Some(5));
assert_eq!(0b00000100_u8.last_index(), Some(5));
assert_eq!(0b00000001_u8.last_index(), Some(7));
assert!(0b00000000_u8.last_index().is_none());
}
#[test]
fn test_radius() {
assert_eq!(0b00010000_u8.radius(0), 0b00000000_u8);
assert_eq!(0b00010000_u8.radius(1), 0b00101000_u8);
assert_eq!(0b00010000_u8.radius(2), 0b01000100_u8);
assert_eq!(0b00010000_u8.radius(3), 0b10000010_u8);
assert_eq!(0b00010000_u8.radius(4), 0b00000001_u8);
assert_eq!(0b00000000_u8.radius(0), 0b00000000_u8);
assert_eq!(0b00000000_u8.radius(1), 0b00000000_u8);
assert_eq!(0b00000000_u8.radius(2), 0b00000000_u8);
}
#[test]
fn test_around() {
assert_eq!(0b00010000_u8.around(0), 0b00000000_u8);
assert_eq!(0b00010000_u8.around(1), 0b00101000_u8);
assert_eq!(0b00010000_u8.around(2), 0b01101100_u8);
assert_eq!(0b00010000_u8.around(3), 0b11101110_u8);
assert_eq!(0b00010000_u8.around(4), 0b11101111_u8);
assert_eq!(0b00000000_u8.around(0), 0b00000000_u8);
assert_eq!(0b00000000_u8.around(1), 0b00000000_u8);
assert_eq!(0b00000000_u8.around(2), 0b00000000_u8);
}
#[test]
fn test_with_around() {
assert_eq!(0b00010000_u8.with_around(0), 0b00010000_u8);
assert_eq!(0b00010000_u8.with_around(1), 0b00111000_u8);
assert_eq!(0b00010000_u8.with_around(2), 0b01111100_u8);
assert_eq!(0b00010000_u8.with_around(3), 0b11111110_u8);
assert_eq!(0b00010000_u8.with_around(4), 0b11111111_u8);
assert_eq!(0b00000000_u8.with_around(0), 0b00000000_u8);
assert_eq!(0b00000000_u8.with_around(1), 0b00000000_u8);
assert_eq!(0b00000000_u8.with_around(2), 0b00000000_u8);
}
#[test]
fn test_first_bit() {
assert_eq!(0b01000000_u8.first_bit(), 0b01000000_u8);
assert_eq!(0b00010000_u8.first_bit(), 0b00010000_u8);
assert_eq!(0b00010100_u8.first_bit(), 0b00010000_u8);
assert_eq!(0b00000100_u8.first_bit(), 0b00000100_u8);
assert_eq!(0b00000001_u8.first_bit(), 0b00000001_u8);
assert_eq!(0b00000000_u8.first_bit(), 0b00000000_u8);
}
#[test]
fn test_last_bit() {
assert_eq!(0b01000000_u8.last_bit(), 0b01000000_u8);
assert_eq!(0b00010000_u8.last_bit(), 0b00010000_u8);
assert_eq!(0b00010100_u8.last_bit(), 0b00000100_u8);
assert_eq!(0b00000100_u8.last_bit(), 0b00000100_u8);
assert_eq!(0b00000001_u8.last_bit(), 0b00000001_u8);
assert_eq!(0b00000000_u8.last_bit(), 0b00000000_u8);
}
#[test]
fn test_first_bits() {
assert_eq!(0b11111111_u8.first_bits(), 0b10000000_u8);
assert_eq!(0b01000000_u8.first_bits(), 0b01000000_u8);
assert_eq!(0b01100110_u8.first_bits(), 0b01000100_u8);
}
#[test]
fn test_last_bits() {
assert_eq!(0b11111111_u8.last_bits(), 0b00000001_u8);
assert_eq!(0b01000000_u8.last_bits(), 0b01000000_u8);
assert_eq!(0b01100110_u8.last_bits(), 0b00100010_u8);
}
#[test]
fn test_filled_first_bit_to_last_bit() {
assert_eq!(0b01000000_u8.filled_first_bit_to_last_bit(), 0b01000000_u8);
assert_eq!(0b00010000_u8.filled_first_bit_to_last_bit(), 0b00010000_u8);
assert_eq!(0b00010100_u8.filled_first_bit_to_last_bit(), 0b00011100_u8);
assert_eq!(0b00000100_u8.filled_first_bit_to_last_bit(), 0b00000100_u8);
assert_eq!(0b00000001_u8.filled_first_bit_to_last_bit(), 0b00000001_u8);
assert_eq!(0b00000000_u8.filled_first_bit_to_last_bit(), 0b00000000_u8);
}
#[test]
fn test_length() {
assert_eq!(u8::length(), 8);
assert_eq!(u16::length(), 16);
assert_eq!(u32::length(), 32);
assert_eq!(u64::length(), 64);
assert_eq!(u128::length(), 128);
}
#[test]
fn test_bytes_length() {
assert_eq!(u8::bytes_length(), 1);
assert_eq!(u16::bytes_length(), 2);
assert_eq!(u32::bytes_length(), 4);
assert_eq!(u64::bytes_length(), 8);
assert_eq!(u128::bytes_length(), 16);
}
#[test]
fn test_is_empty() {
assert!(u8::as_empty().is_empty());
assert!(u16::as_empty().is_empty());
assert!(u32::as_empty().is_empty());
assert!(u64::as_empty().is_empty());
assert!(u128::as_empty().is_empty());
assert!(0b00000000_u8.is_empty());
assert!(!0b00000001_u8.is_empty());
assert!(!0b10000000_u8.is_empty());
assert!(!0b00001000_u8.is_empty());
assert!(!0b00000000_u8.is_not_empty());
assert!(0b00000001_u8.is_not_empty());
assert!(0b10000000_u8.is_not_empty());
assert!(0b00001000_u8.is_not_empty());
}
#[test]
fn test_is_full() {
assert!(u8::as_full().is_full());
assert!(u16::as_full().is_full());
assert!(u32::as_full().is_full());
assert!(u64::as_full().is_full());
assert!(u128::as_full().is_full());
assert!(0b11111111_u8.is_full());
assert!(!0b11111110_u8.is_full());
assert!(!0b01111111_u8.is_full());
assert!(!0b11101111_u8.is_full());
assert!(!0b11111111_u8.is_not_full());
assert!(0b11111110_u8.is_not_full());
assert!(0b01111111_u8.is_not_full());
assert!(0b11101111_u8.is_not_full());
}
#[test]
fn test_num_bits() {
assert_eq!(0b00000000_u8.num_bits(), 0);
assert_eq!(0b00001000_u8.num_bits(), 1);
assert_eq!(0b01001000_u8.num_bits(), 2);
assert_eq!(0b01101000_u8.num_bits(), 3);
assert_eq!(0b11111111_u8.num_bits(), 8);
}
#[test]
fn test_includes() {
assert!(0b00000000_u8.includes(0b00000000_u8));
assert!(0b00011110_u8.includes(0b00000110_u8));
}
#[test]
fn test_overlaps() {
assert!(!0b11110000_u8.overlaps(0b00001111_u8));
assert!(0b00011110_u8.overlaps(0b00011000_u8));
}
#[test]
fn test_range() {
assert_eq!(0b11111111_u8.range(2, 6), 0b00111100_u8);
assert_eq!(0b10101010_u8.range(2, 6), 0b00101000_u8);
assert_eq!(0b01010101_u8.range(2, 6), 0b00010100_u8);
}
#[test]
fn test_remove() {
assert_eq!(0b11110000_u8.remove(0b00001111_u8), 0b11110000_u8);
assert_eq!(0b11110000_u8.remove(0b00111100_u8), 0b11000000_u8);
}
#[test]
fn test_bin_repr() {
assert_eq!(0b11110000_u8.bit_repr(), "11110000");
}