Skip to content

Commit

Permalink
Add assert in debug mode and tests (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
elmattic authored Feb 10, 2022
1 parent e2a7a3e commit f96876c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion utils/bitfield/src/rleplus/writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

/// The maximum varint that can be serialized on 9 bytes.
const MAX_VARINT: u64 = 2u64.pow(63)-1;

#[derive(Default, Clone, Debug)]
/// A `BitWriter` allows for efficiently writing bits to a byte buffer, up to a byte at a time.
pub struct BitWriter {
Expand Down Expand Up @@ -37,6 +40,7 @@ impl BitWriter {
/// Writes a given length to the buffer according to RLE+ encoding.
pub fn write_len(&mut self, len: usize) {
debug_assert!(len > 0);
debug_assert!(len <= (MAX_VARINT as usize));

if len == 1 {
// Block Single (prefix 1)
Expand Down Expand Up @@ -85,7 +89,7 @@ impl BitWriter {

#[cfg(test)]
mod tests {
use super::BitWriter;
use super::{BitWriter, MAX_VARINT};

#[test]
fn write() {
Expand Down Expand Up @@ -178,4 +182,24 @@ mod tests {
let mut writer = BitWriter::new();
writer.write(0, 16);
}

#[test]
fn test_write_max_varint() {
let mut writer = BitWriter::new();
writer.write(0b_0000_0100, 3);
writer.write_len(MAX_VARINT as usize);
let rle = writer.finish();

crate::BitField::from_bytes(&rle).unwrap();
}

#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "assertion failed")]
fn test_write_succ_max_varint() {
let mut writer = BitWriter::new();
writer.write(0b_0000_0100, 3);
writer.write_len(MAX_VARINT as usize + 1);
writer.finish();
}
}

0 comments on commit f96876c

Please sign in to comment.