Skip to content

Commit

Permalink
Fix new clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
terahlunah committed Sep 28, 2023
1 parent 0152f3a commit e17eeca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
43 changes: 23 additions & 20 deletions tests/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::{ErrorKind, Read, Write};
#[test]
fn test_api() {
let mut buffer = ByteBuffer::new();
buffer.write_bytes(&vec![0x1, 0xFF, 0x45]);
buffer.write_bytes(&[0x1, 0xFF, 0x45]);
buffer.write_u8(1);
buffer.write_i8(1);
buffer.write_u16(1);
Expand Down Expand Up @@ -50,9 +50,9 @@ fn test_api() {
#[test]
fn test_empty() {
let mut buffer = ByteBuffer::new();
assert_eq!(buffer.is_empty(), true);
assert!(buffer.is_empty());
buffer.write_u8(1);
assert_eq!(buffer.is_empty(), false);
assert!(!buffer.is_empty());
}

#[test]
Expand Down Expand Up @@ -240,17 +240,17 @@ fn test_to_bytes() {

#[test]
fn test_from_bytes() {
let mut buffer = ByteBuffer::from_bytes(&vec![1, 2]);
let mut buffer = ByteBuffer::from_bytes(&[1, 2]);
assert_eq!(buffer.read_u8().unwrap() + buffer.read_u8().unwrap(), 3);
}

#[test]
fn test_read_bit() {
let mut buffer = ByteBuffer::from_bytes(&vec![128]);
let mut buffer = ByteBuffer::from_bytes(&[128]);
let bit1 = buffer.read_bit().unwrap();
assert_eq!(bit1, true);
assert!(bit1);
let bit2 = buffer.read_bit().unwrap();
assert_eq!(bit2, false);
assert!(!bit2);
}

#[test]
Expand All @@ -264,14 +264,14 @@ fn test_cannot_read_bit_outside_data() {

#[test]
fn test_read_bits() {
let mut buffer = ByteBuffer::from_bytes(&vec![128]);
let mut buffer = ByteBuffer::from_bytes(&[128]);
let value = buffer.read_bits(3).unwrap();
assert_eq!(value, 4);
}

#[test]
fn test_cannot_read_more_than_64_bits() {
let mut buffer = ByteBuffer::from_bytes(&vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let mut buffer = ByteBuffer::from_bytes(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let result = buffer.read_bits(73);
assert!(result.is_err());
let error = result.err().unwrap();
Expand Down Expand Up @@ -304,24 +304,24 @@ fn test_flush_bit() {
assert_eq!(buffer_result_1[0], 128);
assert_eq!(buffer_result_1[1], 1);

let mut buffer2 = ByteBuffer::from_bytes(&vec![0xFF, 0x01]);
let mut buffer2 = ByteBuffer::from_bytes(&[0xFF, 0x01]);
let bit1 = buffer2.read_bit().unwrap();
let number1 = buffer2.read_i8().unwrap();

assert_eq!(bit1, true);
assert!(bit1);
assert_eq!(number1, 1);
}

#[test]
fn test_flush_bits() {
let mut buffer = ByteBuffer::from_bytes(&vec![0xFF, 0x01]);
let mut buffer = ByteBuffer::from_bytes(&[0xFF, 0x01]);
let bit1 = buffer.read_bit().unwrap();
let rpos1 = buffer.get_rpos();
/*
* 11111111 | 00000001
* ^
*/
assert_eq!(bit1, true);
assert!(bit1);
assert_eq!(rpos1, 0);

buffer.flush_bits();
Expand All @@ -332,7 +332,7 @@ fn test_flush_bits() {
* 11111111 | 00000001
* ^
*/
assert_eq!(bit2, false);
assert!(!bit2);
assert_eq!(rpos2, 1)
}

Expand All @@ -341,15 +341,15 @@ fn test_read_empty_buffer() {
let mut buffer = ByteBuffer::new();
buffer.write_u8(0xFF);
let mut res = [];
buffer.read(&mut res).unwrap();
buffer.read_exact(&mut res).unwrap();
}

#[test]
fn test_read_exact_buffer() {
let mut buffer = ByteBuffer::new();
buffer.write_u8(0xFF);
let mut res = [0; 1];
buffer.read(&mut res).unwrap();
buffer.read_exact(&mut res).unwrap();
assert_eq!(res[0], 0xFF);
}

Expand All @@ -358,27 +358,30 @@ fn test_read_larger_buffer() {
let mut buffer = ByteBuffer::new();
buffer.write_u8(0xFF);
let mut res = [0; 2];
buffer.read(&mut res).unwrap();
let read = buffer.read(&mut res).unwrap();
assert_eq!(res[0], 0xFF);
assert_eq!(res[1], 0);
assert_eq!(read, 1);
}

#[test]
fn test_read_larger_buffer_twice() {
let mut buffer = ByteBuffer::new();
buffer.write_u8(0xFF);
let mut res = [0; 2];
buffer.read(&mut res).unwrap();
let read = buffer.read(&mut res).unwrap();
assert_eq!(read, 1);
// Check for overflow on second read
buffer.read(&mut res).unwrap();
let read = buffer.read(&mut res).unwrap();
assert_eq!(read, 0);
assert_eq!(res[0], 0xFF);
assert_eq!(res[1], 0);
}

#[test]
fn test_write() {
let mut buffer = ByteBuffer::new();
buffer.write(&[0x1, 0xFF, 0x45]).unwrap();
buffer.write_all(&[0x1, 0xFF, 0x45]).unwrap();
assert_eq!(buffer.read_bytes(3).unwrap(), &[0x1, 0xFF, 0x45]);
}

Expand Down
16 changes: 8 additions & 8 deletions tests/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::ErrorKind;
#[test]
fn test_api() {
let mut buffer = ByteBuffer::new();
buffer.write_bytes(&vec![0x1, 0xFF, 0x45]);
buffer.write_bytes(&[0x1, 0xFF, 0x45]);
buffer.write_u8(1);
buffer.write_i8(1);
buffer.write_u16(1);
Expand Down Expand Up @@ -49,10 +49,10 @@ fn test_api() {
fn test_empty() {
let mut buffer = ByteBuffer::new();
let reader = ByteReader::from(buffer.as_bytes());
assert_eq!(reader.is_empty(), true);
assert!(reader.is_empty());
buffer.write_u8(1);
let reader = ByteReader::from(buffer.as_bytes());
assert_eq!(reader.is_empty(), false);
assert!(!reader.is_empty());
}

#[test]
Expand Down Expand Up @@ -261,9 +261,9 @@ fn test_read_bit() {
let data = vec![128];
let mut reader = ByteReader::from_bytes(&data);
let bit1 = reader.read_bit().unwrap();
assert_eq!(bit1, true);
assert!(bit1);
let bit2 = reader.read_bit().unwrap();
assert_eq!(bit2, false);
assert!(!bit2);
}

#[test]
Expand Down Expand Up @@ -300,7 +300,7 @@ fn test_flush_bit() {
let bit = reader.read_bit().unwrap();
let number = reader.read_i8().unwrap();

assert_eq!(bit, true);
assert!(bit);
assert_eq!(number, 1);
}

Expand All @@ -314,7 +314,7 @@ fn test_flush_bits() {
* 11111111 | 00000001
* ^
*/
assert_eq!(bit1, true);
assert!(bit1);
assert_eq!(rpos1, 0);

reader.flush_bits();
Expand All @@ -325,7 +325,7 @@ fn test_flush_bits() {
* 11111111 | 00000001
* ^
*/
assert_eq!(bit2, false);
assert!(!bit2);
assert_eq!(rpos2, 1)
}

Expand Down

0 comments on commit e17eeca

Please sign in to comment.