dsi-bitstream 0.9.2

A Rust implementation of read/write bit streams supporting several types of instantaneous codes
Documentation
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
 * SPDX-FileCopyrightText: 2023 Inria
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

use core::any::TypeId;
use core::{mem, ptr};

use crate::codes::params::{DefaultWriteParams, WriteParams};
use crate::traits::*;
#[cfg(feature = "mem_dbg")]
use mem_dbg::{MemDbg, MemSize};
use num_primitive::{PrimitiveInteger, PrimitiveNumber, PrimitiveNumberAs};

/// An implementation of [`BitWrite`] for a [`WordWrite`].
///
/// This implementation uses a bit buffer to store bits that are not yet
/// written. The size of the bit buffer is the size of the word used by the
/// [`WordWrite`], which on most platforms should be `usize`.
///
/// The additional type parameter `WP` is used to select the parameters for the
/// instantaneous codes, but the casual user should be happy with the default
/// value. See [`WriteParams`] for more details.
///
/// The convenience functions [`from_path`] and [`from_file`] (requiring the
/// `std` feature) create a [`BufBitWriter`] around a buffered file writer.
///
/// For additional flexibility, when the `std` feature is enabled, this
/// structure implements [`std::io::Write`]. Note that because of coherence
/// rules it is not possible to implement [`std::io::Write`] for a generic
/// [`BitWrite`].
///
/// # Panics on drop
///
/// The [`Drop`] implementation flushes the buffer, which requires writing to
/// the backend. If the backend write fails, the drop will panic. To handle
/// errors gracefully, call [`flush`](BitWrite::flush) or [`into_inner`](Self::into_inner)
/// explicitly before dropping.

#[derive(Debug)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
pub struct BufBitWriter<E: Endianness, WW: WordWrite, WP: WriteParams = DefaultWriteParams> {
    /// The [`WordWrite`] to which we will write words.
    backend: WW,
    /// The buffer where we store bits until we have a word's worth of them. It
    /// might be empty, but it is never full. Only the lower (BE) or upper (LE)
    /// `WW::Word::BITS - space_left_in_buffer`
    /// bits are valid: the rest have undefined value.
    buffer: WW::Word,
    /// Number of upper (BE) or lower (LE) free bits in the buffer. It is always greater than zero.
    space_left_in_buffer: usize,
    _marker: core::marker::PhantomData<(E, WP)>,
}

/// Creates a new [`BufBitWriter`] with [default write
/// parameters](`DefaultWriteParams`) from a file path using the provided
/// endianness and write word.
///
/// The file will be created if it does not exist, and truncated if it already
/// exists.
///
/// # Examples
///
/// ```no_run
/// use dsi_bitstream::prelude::*;
/// let mut writer = buf_bit_writer::from_path::<LE, u64>("data.bin")?;
/// # Ok::<(), Box<dyn core::error::Error>>(())
/// ```
#[cfg(feature = "std")]
pub fn from_path<E: Endianness, W: Word>(
    path: impl AsRef<std::path::Path>,
) -> std::io::Result<
    BufBitWriter<E, super::WordAdapter<W, std::io::BufWriter<std::fs::File>>, DefaultWriteParams>,
>
where
    W::Bytes: AsRef<[u8]>,
{
    Ok(from_file::<E, W>(std::fs::File::create(path)?))
}

/// Creates a new [`BufBitWriter`] with [default write
/// parameters](`DefaultWriteParams`) from a file using the provided
/// endianness and write word.
///
/// See also [`from_path`] for a version that takes a path.
#[must_use]
#[cfg(feature = "std")]
pub fn from_file<E: Endianness, W: Word>(
    file: std::fs::File,
) -> BufBitWriter<E, super::WordAdapter<W, std::io::BufWriter<std::fs::File>>, DefaultWriteParams>
where
    W::Bytes: AsRef<[u8]>,
{
    BufBitWriter::<E, super::WordAdapter<W, std::io::BufWriter<std::fs::File>>>::new(
        super::WordAdapter::new(std::io::BufWriter::new(file)),
    )
}

impl<E: Endianness, WW: WordWrite, WP: WriteParams> BufBitWriter<E, WW, WP> {
    const WORD_BITS: usize = WW::Word::BITS as usize;

    /// Creates a new [`BufBitWriter`] around a [`WordWrite`].
    ///
    /// # Examples
    /// ```
    /// #[cfg(not(feature = "std"))]
    /// # fn main() {}
    /// # #[cfg(feature = "std")]
    /// # fn main() {
    /// use dsi_bitstream::prelude::*;
    /// let buffer = Vec::<usize>::new();
    /// let word_writer = MemWordWriterVec::new(buffer);
    /// let mut buf_bit_writer = <BufBitWriter<BE, _>>::new(word_writer);
    /// # }
    /// ```
    #[must_use]
    pub const fn new(backend: WW) -> Self {
        Self {
            backend,
            buffer: WW::Word::ZERO,
            space_left_in_buffer: Self::WORD_BITS,
            _marker: core::marker::PhantomData,
        }
    }
}

impl<E: Endianness, WW: WordWrite + Default, WP: WriteParams> Default for BufBitWriter<E, WW, WP>
where
    BufBitWriter<E, WW, WP>: BitWrite<E>,
{
    fn default() -> Self {
        Self::new(WW::default())
    }
}

impl<E: Endianness, WW: WordWrite, WP: WriteParams> BufBitWriter<E, WW, WP>
where
    BufBitWriter<E, WW, WP>: BitWrite<E>,
{
    /// Returns the backend, consuming this writer after
    /// [flushing it](BufBitWriter::flush).
    pub fn into_inner(mut self) -> Result<WW, <Self as BitWrite<E>>::Error> {
        self.flush()?;
        // SAFETY: forget(self) prevents double dropping backend
        let backend = unsafe { ptr::read(&self.backend) };
        mem::forget(self);
        Ok(backend)
    }
}

impl<E: Endianness, WW: WordWrite, WP: WriteParams> core::ops::Drop for BufBitWriter<E, WW, WP> {
    fn drop(&mut self) {
        if TypeId::of::<E>() == TypeId::of::<LE>() {
            flush_le(self).unwrap();
        } else {
            // TypeId::of::<E>() == TypeId::of::<BE>()
            flush_be(self).unwrap();
        }
    }
}

/// Helper function that flushes a [`BufBitWriter`] in big-endian fashion.
///
/// The endianness is hardwired because the function is called
/// from [`BufBitWriter::drop`] using a check on the
/// [`TypeId`] of the endianness.
fn flush_be<E: Endianness, WW: WordWrite, WP: WriteParams>(
    buf_bit_writer: &mut BufBitWriter<E, WW, WP>,
) -> Result<usize, WW::Error> {
    let to_flush = WW::Word::BITS as usize - buf_bit_writer.space_left_in_buffer;
    if to_flush != 0 {
        buf_bit_writer.buffer <<= buf_bit_writer.space_left_in_buffer;
        buf_bit_writer
            .backend
            .write_word(buf_bit_writer.buffer.to_be())?;
        buf_bit_writer.space_left_in_buffer = WW::Word::BITS as usize;
    }
    buf_bit_writer.backend.flush()?;
    Ok(to_flush)
}

impl<WW: WordWrite, WP: WriteParams> BitWrite<BE> for BufBitWriter<BE, WW, WP>
where
    u64: PrimitiveNumberAs<WW::Word>,
{
    type Error = <WW as WordWrite>::Error;

    fn flush(&mut self) -> Result<usize, Self::Error> {
        flush_be(self)
    }

    #[allow(unused_mut)]
    #[inline(always)]
    fn write_bits(&mut self, mut value: u64, num_bits: usize) -> Result<usize, Self::Error> {
        debug_assert!(num_bits <= 64);
        #[cfg(feature = "checks")]
        assert!(
            value & (1_u128 << num_bits).wrapping_sub(1) as u64 == value,
            "Error: value {} does not fit in {} bits",
            value,
            num_bits
        );
        debug_assert!(self.space_left_in_buffer > 0);

        #[cfg(test)]
        if num_bits < 64 {
            // We put garbage in the higher bits for testing
            value |= u64::MAX << num_bits;
        }

        // Easy way out: we fit the buffer
        if num_bits < self.space_left_in_buffer {
            self.buffer <<= num_bits;
            // Clean up bits higher than num_bits
            self.buffer |= value.as_to() & !(WW::Word::MAX << num_bits as u32);
            self.space_left_in_buffer -= num_bits;
            return Ok(num_bits);
        }

        // Load the bottom of the buffer, if necessary, and dump the whole buffer
        self.buffer = self.buffer << (self.space_left_in_buffer - 1) << 1;
        // The first shift discards bits higher than num_bits
        self.buffer |= (value << (64 - num_bits) >> (64 - self.space_left_in_buffer)).as_to();
        self.backend.write_word(self.buffer.to_be())?;

        let mut to_write = num_bits - self.space_left_in_buffer;

        for _ in 0..to_write / (Self::WORD_BITS) {
            to_write -= Self::WORD_BITS;
            self.backend
                .write_word((value >> to_write).as_to().to_be())?;
        }

        self.space_left_in_buffer = Self::WORD_BITS - to_write;
        self.buffer = value.as_to();
        Ok(num_bits)
    }

    #[inline(always)]
    #[allow(clippy::collapsible_if)]
    fn write_unary(&mut self, mut n: u64) -> Result<usize, Self::Error> {
        debug_assert!(n < u64::MAX);
        debug_assert!(self.space_left_in_buffer > 0);

        let code_length = n + 1;

        // Easy way out: we fit the buffer
        if code_length <= self.space_left_in_buffer as u64 {
            self.space_left_in_buffer -= code_length as usize;
            self.buffer = self.buffer << n << 1;
            self.buffer |= WW::Word::ONE;
            if self.space_left_in_buffer == 0 {
                self.backend.write_word(self.buffer.to_be())?;
                self.space_left_in_buffer = Self::WORD_BITS;
            }
            return Ok(code_length as usize);
        }

        self.buffer = self.buffer << (self.space_left_in_buffer - 1) << 1;
        self.backend.write_word(self.buffer.to_be())?;

        n -= self.space_left_in_buffer as u64;

        for _ in 0..n / WW::Word::BITS as u64 {
            self.backend.write_word(WW::Word::ZERO)?;
        }

        n %= WW::Word::BITS as u64;

        if n == WW::Word::BITS as u64 - 1 {
            self.backend.write_word(WW::Word::ONE.to_be())?;
            self.space_left_in_buffer = Self::WORD_BITS;
        } else {
            self.buffer = WW::Word::ONE;
            self.space_left_in_buffer = Self::WORD_BITS - (n as usize + 1);
        }

        Ok(code_length as usize)
    }

    #[cfg(not(feature = "no_copy_impls"))]
    fn copy_from<F: Endianness, R: BitRead<F>>(
        &mut self,
        bit_read: &mut R,
        mut n: u64,
    ) -> Result<(), CopyError<R::Error, Self::Error>> {
        if n < self.space_left_in_buffer as u64 {
            self.buffer = (self.buffer << n)
                | bit_read
                    .read_bits(n as usize)
                    .map_err(CopyError::ReadError)?
                    .as_to();
            self.space_left_in_buffer -= n as usize;
            return Ok(());
        }

        self.buffer = (self.buffer << (self.space_left_in_buffer - 1) << 1)
            | bit_read
                .read_bits(self.space_left_in_buffer)
                .map_err(CopyError::ReadError)?
                .as_to();
        n -= self.space_left_in_buffer as u64;

        self.backend
            .write_word(self.buffer.to_be())
            .map_err(CopyError::WriteError)?;

        for _ in 0..n / WW::Word::BITS as u64 {
            self.backend
                .write_word(
                    bit_read
                        .read_bits(Self::WORD_BITS)
                        .map_err(CopyError::ReadError)?
                        .as_to()
                        .to_be(),
                )
                .map_err(CopyError::WriteError)?;
        }

        n %= WW::Word::BITS as u64;
        self.buffer = bit_read
            .read_bits(n as usize)
            .map_err(CopyError::ReadError)?
            .as_to();
        self.space_left_in_buffer = Self::WORD_BITS - n as usize;

        Ok(())
    }
}

/// Helper function that flushes a [`BufBitWriter`] in little-endian fashion.
///
/// The endianness is hardwired because the function is called
/// from [`BufBitWriter::drop`] using a check on the
/// [`TypeId`] of the endianness.
fn flush_le<E: Endianness, WW: WordWrite, WP: WriteParams>(
    buf_bit_writer: &mut BufBitWriter<E, WW, WP>,
) -> Result<usize, WW::Error> {
    let to_flush = WW::Word::BITS as usize - buf_bit_writer.space_left_in_buffer;
    if to_flush != 0 {
        buf_bit_writer.buffer >>= buf_bit_writer.space_left_in_buffer;
        buf_bit_writer
            .backend
            .write_word(buf_bit_writer.buffer.to_le())?;
        buf_bit_writer.space_left_in_buffer = WW::Word::BITS as usize;
    }
    buf_bit_writer.backend.flush()?;
    Ok(to_flush)
}

impl<WW: WordWrite, WP: WriteParams> BitWrite<LE> for BufBitWriter<LE, WW, WP>
where
    u64: PrimitiveNumberAs<WW::Word>,
{
    type Error = <WW as WordWrite>::Error;

    fn flush(&mut self) -> Result<usize, Self::Error> {
        flush_le(self)
    }

    #[inline(always)]
    fn write_bits(&mut self, mut value: u64, num_bits: usize) -> Result<usize, Self::Error> {
        debug_assert!(num_bits <= 64);
        #[cfg(feature = "checks")]
        assert!(
            value & (1_u128 << num_bits).wrapping_sub(1) as u64 == value,
            "Error: value {} does not fit in {} bits",
            value,
            num_bits
        );
        debug_assert!(self.space_left_in_buffer > 0);

        #[cfg(test)]
        if num_bits < 64 {
            // We put garbage in the higher bits for testing
            value |= u64::MAX << num_bits;
        }

        // Easy way out: we fit the buffer
        if num_bits < self.space_left_in_buffer {
            self.buffer >>= num_bits;
            // Clean up bits higher than num_bits
            self.buffer |=
                (value.as_to() & !(WW::Word::MAX << num_bits as u32)).rotate_right(num_bits as u32);
            self.space_left_in_buffer -= num_bits;
            return Ok(num_bits);
        }

        // Load the top of the buffer, if necessary, and dump the whole buffer
        self.buffer = self.buffer >> (self.space_left_in_buffer - 1) >> 1;
        self.buffer |= value.as_to() << (Self::WORD_BITS - self.space_left_in_buffer);
        self.backend.write_word(self.buffer.to_le())?;

        let to_write = num_bits - self.space_left_in_buffer;
        value = value >> (self.space_left_in_buffer - 1) >> 1;

        for _ in 0..to_write / (Self::WORD_BITS) {
            self.backend.write_word(value.as_to().to_le())?;
            // This cannot be executed with WW::Word::BITS >= 64
            value >>= WW::Word::BITS;
        }

        self.space_left_in_buffer = Self::WORD_BITS - to_write % (Self::WORD_BITS);
        self.buffer = value.as_to().rotate_right(to_write as u32);
        Ok(num_bits)
    }

    #[inline(always)]
    #[allow(clippy::collapsible_if)]
    fn write_unary(&mut self, mut n: u64) -> Result<usize, Self::Error> {
        debug_assert!(n < u64::MAX);
        debug_assert!(self.space_left_in_buffer > 0);

        let code_length = n + 1;

        // Easy way out: we fit the buffer
        if code_length <= self.space_left_in_buffer as u64 {
            self.space_left_in_buffer -= code_length as usize;
            self.buffer = self.buffer >> n >> 1;
            self.buffer |= WW::Word::ONE << (Self::WORD_BITS - 1);
            if self.space_left_in_buffer == 0 {
                self.backend.write_word(self.buffer.to_le())?;
                self.space_left_in_buffer = Self::WORD_BITS;
            }
            return Ok(code_length as usize);
        }

        self.buffer = self.buffer >> (self.space_left_in_buffer - 1) >> 1;
        self.backend.write_word(self.buffer.to_le())?;

        n -= self.space_left_in_buffer as u64;

        for _ in 0..n / WW::Word::BITS as u64 {
            self.backend.write_word(WW::Word::ZERO)?;
        }

        n %= WW::Word::BITS as u64;

        if n == WW::Word::BITS as u64 - 1 {
            self.backend
                .write_word((WW::Word::ONE << (Self::WORD_BITS - 1)).to_le())?;
            self.space_left_in_buffer = Self::WORD_BITS;
        } else {
            self.buffer = WW::Word::ONE << (Self::WORD_BITS - 1);
            self.space_left_in_buffer = Self::WORD_BITS - (n as usize + 1);
        }

        Ok(code_length as usize)
    }

    #[cfg(not(feature = "no_copy_impls"))]
    fn copy_from<F: Endianness, R: BitRead<F>>(
        &mut self,
        bit_read: &mut R,
        mut n: u64,
    ) -> Result<(), CopyError<R::Error, Self::Error>> {
        if n < self.space_left_in_buffer as u64 {
            self.buffer = (self.buffer >> n)
                | (bit_read
                    .read_bits(n as usize)
                    .map_err(CopyError::ReadError)?)
                .as_to()
                .rotate_right(n as u32);
            self.space_left_in_buffer -= n as usize;
            return Ok(());
        }

        self.buffer = (self.buffer >> (self.space_left_in_buffer - 1) >> 1)
            | (bit_read
                .read_bits(self.space_left_in_buffer)
                .map_err(CopyError::ReadError)?
                .as_to())
            .rotate_right(self.space_left_in_buffer as u32);
        n -= self.space_left_in_buffer as u64;

        self.backend
            .write_word(self.buffer.to_le())
            .map_err(CopyError::WriteError)?;

        for _ in 0..n / WW::Word::BITS as u64 {
            self.backend
                .write_word(
                    bit_read
                        .read_bits(Self::WORD_BITS)
                        .map_err(CopyError::ReadError)?
                        .as_to()
                        .to_le(),
                )
                .map_err(CopyError::WriteError)?;
        }

        n %= WW::Word::BITS as u64;
        self.buffer = bit_read
            .read_bits(n as usize)
            .map_err(CopyError::ReadError)?
            .as_to()
            .rotate_right(n as u32);
        self.space_left_in_buffer = Self::WORD_BITS - n as usize;

        Ok(())
    }
}

#[cfg(feature = "std")]
impl<WW: WordWrite, WP: WriteParams> std::io::Write for BufBitWriter<BE, WW, WP>
where
    u64: PrimitiveNumberAs<WW::Word>,
{
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut iter = buf.chunks_exact(8);

        for word in &mut iter {
            self.write_bits(u64::from_be_bytes(word.try_into().unwrap()), 64)
                .map_err(|_| std::io::Error::other("could not write bits to stream"))?;
        }

        let rem = iter.remainder();
        if !rem.is_empty() {
            let mut word = 0;
            let bits = rem.len() * 8;
            for byte in rem.iter() {
                word <<= 8;
                word |= *byte as u64;
            }
            self.write_bits(word, bits)
                .map_err(|_| std::io::Error::other("could not write bits to stream"))?;
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        flush_be(self).map_err(|_| std::io::Error::other("could not flush bits to stream"))?;
        Ok(())
    }
}

#[cfg(feature = "std")]
impl<WW: WordWrite, WP: WriteParams> std::io::Write for BufBitWriter<LE, WW, WP>
where
    u64: PrimitiveNumberAs<WW::Word>,
{
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut iter = buf.chunks_exact(8);

        for word in &mut iter {
            self.write_bits(u64::from_le_bytes(word.try_into().unwrap()), 64)
                .map_err(|_| std::io::Error::other("could not write bits to stream"))?;
        }

        let rem = iter.remainder();
        if !rem.is_empty() {
            let mut word = 0;
            let bits = rem.len() * 8;
            for byte in rem.iter().rev() {
                word <<= 8;
                word |= *byte as u64;
            }
            self.write_bits(word, bits)
                .map_err(|_| std::io::Error::other("could not write bits to stream"))?;
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        flush_le(self).map_err(|_| std::io::Error::other("could not flush bits to stream"))?;
        Ok(())
    }
}

#[cfg(test)]
#[cfg(feature = "std")]
mod tests {
    use super::*;
    use crate::prelude::MemWordWriterVec;
    use std::io::Write;

    #[test]
    fn test_write() -> Result<(), Box<dyn core::error::Error>> {
        let data = [
            0x90, 0x2d, 0xd0, 0x26, 0xdf, 0x89, 0xbb, 0x7e, 0x3a, 0xd6, 0xc6, 0x96, 0x73, 0xe9,
            0x9d, 0xc9, 0x2a, 0x77, 0x82, 0xa9, 0xe6, 0x4b, 0x53, 0xcc, 0x83, 0x80, 0x4a, 0xf3,
            0xcd, 0xe3, 0x50, 0x4e, 0x45, 0x4a, 0x3a, 0x42, 0x00, 0x4b, 0x4d, 0xbe, 0x4c, 0x88,
            0x24, 0xf2, 0x4b, 0x6b, 0xbd, 0x79, 0xeb, 0x74, 0xbc, 0xe8, 0x7d, 0xff, 0x4b, 0x3d,
            0xa7, 0xd6, 0x0d, 0xef, 0x9c, 0x5b, 0xb3, 0xec, 0x94, 0x97, 0xcc, 0x8b, 0x41, 0xe1,
            0x9c, 0xcc, 0x1a, 0x03, 0x58, 0xc4, 0xfb, 0xd0, 0xc0, 0x10, 0xe2, 0xa0, 0xc9, 0xac,
            0xa7, 0xbb, 0x50, 0xf6, 0x5c, 0x87, 0x68, 0x0f, 0x42, 0x93, 0x3f, 0x2e, 0x28, 0x28,
            0x76, 0x83, 0x9b, 0xeb, 0x12, 0xe0, 0x4f, 0xc5, 0xb0, 0x8d, 0x14, 0xda, 0x3b, 0xdf,
            0xd3, 0x4b, 0x80, 0xd1, 0xfc, 0x87, 0x85, 0xae, 0x54, 0xc7, 0x45, 0xc9, 0x38, 0x43,
            0xa7, 0x9f, 0xdd, 0xa9, 0x71, 0xa7, 0x52, 0x36, 0x82, 0xff, 0x49, 0x55, 0xdb, 0x84,
            0xc2, 0x95, 0xad, 0x45, 0x80, 0xc6, 0x02, 0x80, 0xf8, 0xfc, 0x86, 0x79, 0xae, 0xb9,
            0x57, 0xe7, 0x3b, 0x33, 0x64, 0xa8,
        ];

        for i in 0..data.len() {
            let mut buffer = Vec::<u64>::new();
            let mut writer = BufBitWriter::<BE, _>::new(MemWordWriterVec::new(&mut buffer));

            writer.write_all(&data[..i])?;
            std::io::Write::flush(&mut writer)?;

            let buffer = writer.into_inner()?.into_inner();
            assert_eq!(unsafe { &buffer.align_to::<u8>().1[..i] }, &data[..i]);

            let mut buffer = Vec::<u64>::new();
            let mut writer = BufBitWriter::<LE, _>::new(MemWordWriterVec::new(&mut buffer));

            writer.write_all(&data[..i])?;
            std::io::Write::flush(&mut writer)?;

            let buffer = writer.into_inner()?.into_inner();
            assert_eq!(unsafe { &buffer.align_to::<u8>().1[..i] }, &data[..i]);
        }
        Ok(())
    }

    macro_rules! test_buf_bit_writer {
        ($f: ident, $word:ty) => {
            #[test]
            fn $f() -> Result<(), Box<dyn core::error::Error + Send + Sync + 'static>> {
                #[allow(unused_imports)]
                use crate::{
                    codes::{GammaRead, GammaWrite},
                    prelude::{
                        BufBitReader, DeltaRead, DeltaWrite, MemWordReader, len_delta, len_gamma,
                    },
                };

                use rand::{RngExt, SeedableRng, rngs::SmallRng};

                let mut buffer_be: Vec<$word> = vec![];
                let mut buffer_le: Vec<$word> = vec![];
                let mut big = BufBitWriter::<BE, _>::new(MemWordWriterVec::new(&mut buffer_be));
                let mut little = BufBitWriter::<LE, _>::new(MemWordWriterVec::new(&mut buffer_le));

                let mut r = SmallRng::seed_from_u64(0);
                const ITER: usize = 1_000_000;

                for _ in 0..ITER {
                    let value = r.random_range(0..128);
                    assert_eq!(big.write_gamma(value)?, len_gamma(value));
                    let value = r.random_range(0..128);
                    assert_eq!(little.write_gamma(value)?, len_gamma(value));
                    let value = r.random_range(0..128);
                    assert_eq!(big.write_gamma(value)?, len_gamma(value));
                    let value = r.random_range(0..128);
                    assert_eq!(little.write_gamma(value)?, len_gamma(value));
                    let value = r.random_range(0..128);
                    assert_eq!(big.write_delta(value)?, len_delta(value));
                    let value = r.random_range(0..128);
                    assert_eq!(little.write_delta(value)?, len_delta(value));
                    let value = r.random_range(0..128);
                    assert_eq!(big.write_delta(value)?, len_delta(value));
                    let value = r.random_range(0..128);
                    assert_eq!(little.write_delta(value)?, len_delta(value));
                    let n_bits = r.random_range(0..=64);
                    if n_bits == 0 {
                        big.write_bits(0, 0)?;
                    } else {
                        big.write_bits(r.random::<u64>() & u64::MAX >> 64 - n_bits, n_bits)?;
                    }
                    let n_bits = r.random_range(0..=64);
                    if n_bits == 0 {
                        little.write_bits(0, 0)?;
                    } else {
                        little.write_bits(r.random::<u64>() & u64::MAX >> 64 - n_bits, n_bits)?;
                    }
                    let value = r.random_range(0..128);
                    assert_eq!(big.write_unary(value)?, value as usize + 1);
                    let value = r.random_range(0..128);
                    assert_eq!(little.write_unary(value)?, value as usize + 1);
                }

                drop(big);
                drop(little);

                type ReadWord = u16;
                #[allow(clippy::size_of_in_element_count)] // false positive
                let be_trans: &[ReadWord] = unsafe {
                    core::slice::from_raw_parts(
                        buffer_be.as_ptr() as *const ReadWord,
                        buffer_be.len()
                            * (core::mem::size_of::<$word>() / core::mem::size_of::<ReadWord>()),
                    )
                };
                #[allow(clippy::size_of_in_element_count)] // false positive
                let le_trans: &[ReadWord] = unsafe {
                    core::slice::from_raw_parts(
                        buffer_le.as_ptr() as *const ReadWord,
                        buffer_le.len()
                            * (core::mem::size_of::<$word>() / core::mem::size_of::<ReadWord>()),
                    )
                };

                let mut big_buff = BufBitReader::<BE, _>::new(MemWordReader::new_inf(be_trans));
                let mut little_buff = BufBitReader::<LE, _>::new(MemWordReader::new_inf(le_trans));

                let mut r = SmallRng::seed_from_u64(0);

                for _ in 0..ITER {
                    assert_eq!(big_buff.read_gamma()?, r.random_range(0..128));
                    assert_eq!(little_buff.read_gamma()?, r.random_range(0..128));
                    assert_eq!(big_buff.read_gamma()?, r.random_range(0..128));
                    assert_eq!(little_buff.read_gamma()?, r.random_range(0..128));
                    assert_eq!(big_buff.read_delta()?, r.random_range(0..128));
                    assert_eq!(little_buff.read_delta()?, r.random_range(0..128));
                    assert_eq!(big_buff.read_delta()?, r.random_range(0..128));
                    assert_eq!(little_buff.read_delta()?, r.random_range(0..128));
                    let n_bits = r.random_range(0..=64);
                    if n_bits == 0 {
                        assert_eq!(big_buff.read_bits(0)?, 0);
                    } else {
                        assert_eq!(
                            big_buff.read_bits(n_bits)?,
                            r.random::<u64>() & u64::MAX >> 64 - n_bits
                        );
                    }
                    let n_bits = r.random_range(0..=64);
                    if n_bits == 0 {
                        assert_eq!(little_buff.read_bits(0)?, 0);
                    } else {
                        assert_eq!(
                            little_buff.read_bits(n_bits)?,
                            r.random::<u64>() & u64::MAX >> 64 - n_bits
                        );
                    }

                    assert_eq!(big_buff.read_unary()?, r.random_range(0..128));
                    assert_eq!(little_buff.read_unary()?, r.random_range(0..128));
                }

                Ok(())
            }
        };
    }

    test_buf_bit_writer!(test_u128, u128);
    test_buf_bit_writer!(test_u64, u64);
    test_buf_bit_writer!(test_u32, u32);

    test_buf_bit_writer!(test_u16, u16);
    test_buf_bit_writer!(test_usize, usize);
}