bittersweet/
bitline.rs

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