Skip to content

Commit b20f916

Browse files
authored
feat: extending tests (#17)
1 parent 16ea1ec commit b20f916

14 files changed

Lines changed: 1883 additions & 1342 deletions

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ categories = ["compression"]
1111
rust-version = "1.83.0"
1212

1313
[dependencies]
14+
rand = "0.8.5"
1415
thiserror = "2.0.7"
1516

1617
[lints.rust]

src/bytebuffer.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#[derive(Debug)]
22
pub struct ByteBuffer {
33
pub buffer: Vec<u8>,
4-
position: i32,
5-
limit: i32,
6-
capacity: i32,
4+
position: u32,
5+
limit: u32,
6+
capacity: u32,
77
}
88

99
#[derive(Debug)]
1010
pub struct IntBuffer {
11-
pub buffer: Vec<i32>,
11+
pub buffer: Vec<u32>,
1212
}
1313

1414
impl ByteBuffer {
1515
// Create a new ByteBuffer with the specified capacity
16-
pub fn new(capacity: i32) -> Self {
16+
pub fn new(capacity: u32) -> Self {
1717
ByteBuffer {
1818
buffer: vec![0; capacity as usize],
1919
position: 0,
@@ -29,21 +29,21 @@ impl ByteBuffer {
2929
}
3030

3131
// Get the current position
32-
pub fn position(&self) -> i32 {
32+
pub fn position(&self) -> u32 {
3333
self.position
3434
}
3535

3636
// Set a new position
37-
pub fn set_position(&mut self, pos: i32) {
38-
if pos > self.limit as i32 {
37+
pub fn set_position(&mut self, pos: u32) {
38+
if pos > self.limit {
3939
panic!("Position exceeds limit");
4040
}
4141
self.position = pos;
4242
}
4343

4444
// Write a single byte to the buffer
4545
pub fn put(&mut self, byte: u8) {
46-
if self.position >= self.limit as i32 {
46+
if self.position >= self.limit {
4747
panic!("Buffer overflow");
4848
}
4949

@@ -58,7 +58,7 @@ impl ByteBuffer {
5858

5959
// Read a single byte from the buffer
6060
pub fn get(&mut self) -> u8 {
61-
if self.position >= self.limit as i32 {
61+
if self.position >= self.limit {
6262
panic!("Buffer underflow");
6363
}
6464
let byte = self.buffer[self.position as usize];
@@ -91,7 +91,7 @@ impl ByteBuffer {
9191
for chunk in self.buffer.chunks(4) {
9292
let mut bytes = [0; 4];
9393
bytes.copy_from_slice(chunk);
94-
result.push(i32::from_le_bytes(bytes));
94+
result.push(u32::from_le_bytes(bytes));
9595
}
9696
result
9797
},
@@ -101,17 +101,14 @@ impl ByteBuffer {
101101

102102
impl IntBuffer {
103103
// Create a new IntBuffer with the specified capacity
104-
pub fn get(&self, dst: &mut [i32], offset: usize, length: usize) {
104+
pub fn get(&self, dst: &mut [u32], offset: usize, length: usize) {
105105
if offset + length > dst.len() {
106106
panic!("Buffer overflow");
107107
}
108108
dst[offset..offset + length].copy_from_slice(&self.buffer[..length]);
109109
}
110110

111-
// for (int i = off; i < off + len; i++)
112-
// dst.put(src[i]);
113-
114-
pub fn put(&mut self, src: &[i32], offset: usize, length: i32) -> Vec<u8> {
111+
pub fn put(&mut self, src: &[u32], offset: usize, length: u32) -> Vec<u8> {
115112
if offset + length as usize > src.len() {
116113
panic!("Buffer underflow");
117114
};
@@ -120,11 +117,5 @@ impl IntBuffer {
120117
result.extend_from_slice(&src[i].to_le_bytes());
121118
}
122119
result
123-
124-
// if offset + length as usize > src.len() {
125-
// panic!("Buffer underflow");
126-
// }
127-
// self.buffer
128-
// .extend_from_slice(&src[offset..offset + length as usize]);
129120
}
130121
}

src/cursor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::io::Cursor;
22

33
pub trait IncrementCursor {
44
fn increment(&mut self);
5-
fn add(&mut self, n: i32);
5+
fn add(&mut self, n: u32);
66
}
77

8-
impl IncrementCursor for Cursor<i32> {
8+
impl IncrementCursor for Cursor<u32> {
99
fn increment(&mut self) {
1010
self.set_position(self.position() + 1); // Position needs to be a u64
1111
}
12-
fn add(&mut self, n: i32) {
12+
fn add(&mut self, n: u32) {
1313
self.set_position(self.position() + n as u64);
1414
}
1515
}

0 commit comments

Comments
 (0)