-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from worm-blossom/add_queue_trait
Add Queue with Fixed implementation
- Loading branch information
Showing
9 changed files
with
535 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Cargo.lock | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target | ||
corpus | ||
artifacts | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "ufotofu_queues-fuzz" | ||
version = "0.0.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
arbitrary = { version = "1", features = ["derive"] } | ||
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] } | ||
|
||
[dependencies.ufotofu_queues] | ||
path = ".." | ||
|
||
[[bin]] | ||
name = "fixed_bulk" | ||
path = "fuzz_targets/fixed_bulk.rs" | ||
test = false | ||
doc = false | ||
bench = false | ||
|
||
[[bin]] | ||
name = "fixed_enqueue_dequeue" | ||
path = "fuzz_targets/fixed_enqueue_dequeue.rs" | ||
test = false | ||
doc = false | ||
bench = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#![no_main] | ||
|
||
use core::mem::MaybeUninit; | ||
use core::slice; | ||
|
||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
use std::collections::VecDeque; | ||
|
||
use ufotofu_queues::fixed::Fixed; | ||
use ufotofu_queues::Queue; | ||
|
||
#[derive(Debug, Arbitrary)] | ||
enum Operation<T> { | ||
Enqueue(T), | ||
Dequeue, | ||
BulkEnqueue(Vec<T>), | ||
BulkDequeue(u8), | ||
} | ||
|
||
fn maybe_uninit_slice_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] { | ||
let ptr = slice.as_mut_ptr().cast::<MaybeUninit<T>>(); | ||
unsafe { slice::from_raw_parts_mut(ptr, slice.len()) } | ||
} | ||
|
||
fuzz_target!(|data: (Vec<Operation<u8>>, usize)| { | ||
let operations = data.0; | ||
let capacity = data.1; | ||
|
||
// Restrict capacity to between 1 and 2048 bytes (inclusive). | ||
if capacity < 1 || capacity > 2048 { | ||
return; | ||
} | ||
|
||
let mut control = VecDeque::new(); | ||
let mut test = Fixed::new(capacity); | ||
|
||
for operation in operations { | ||
match operation { | ||
Operation::Enqueue(item) => { | ||
let control_result = if control.len() >= capacity { | ||
Some(item) | ||
} else { | ||
control.push_back(item.clone()); | ||
None | ||
}; | ||
let test_result = test.enqueue(item.clone()); | ||
assert_eq!(test_result, control_result); | ||
} | ||
Operation::Dequeue => { | ||
let control_result = control.pop_front(); | ||
let test_result = test.dequeue(); | ||
assert_eq!(test_result, control_result); | ||
} | ||
Operation::BulkEnqueue(items) => { | ||
let amount = test.bulk_enqueue(&items); | ||
for (count, item) in items.iter().enumerate() { | ||
if count >= amount { | ||
break; | ||
} else { | ||
control.push_back(item.clone()); | ||
} | ||
} | ||
} | ||
Operation::BulkDequeue(n) => { | ||
let n = n as usize; | ||
if n > 0 { | ||
let mut control_buffer = vec![]; | ||
let mut test_buffer = vec![]; | ||
test_buffer.resize(n, 0_u8); | ||
|
||
let test_amount = test.bulk_dequeue(maybe_uninit_slice_mut(&mut test_buffer)); | ||
for _ in 0..test_amount { | ||
if let Some(item) = control.pop_front() { | ||
control_buffer.push(item.clone()); | ||
} | ||
} | ||
|
||
assert_eq!(&test_buffer[..test_amount], &control_buffer[..test_amount]); | ||
} | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![no_main] | ||
use std::collections::VecDeque; | ||
|
||
use arbitrary::Arbitrary; | ||
use libfuzzer_sys::fuzz_target; | ||
|
||
use ufotofu_queues::fixed::Fixed; | ||
use ufotofu_queues::Queue; | ||
|
||
#[derive(Debug, Arbitrary)] | ||
enum Operation<T> { | ||
Enqueue(T), | ||
Dequeue, | ||
} | ||
|
||
fuzz_target!(|data: (Vec<Operation<u8>>, usize)| { | ||
let operations = data.0; | ||
let capacity = data.1; | ||
|
||
// Restrict capacity to between 1 and 2048 bytes (inclusive). | ||
if capacity < 1 || capacity > 2048 { | ||
return; | ||
} | ||
|
||
let mut control = VecDeque::new(); | ||
let mut test = Fixed::new(capacity); | ||
|
||
for operation in operations { | ||
match operation { | ||
Operation::Enqueue(item) => { | ||
let control_result = if control.len() >= capacity { | ||
Some(item) | ||
} else { | ||
control.push_back(item.clone()); | ||
None | ||
}; | ||
let test_result = test.enqueue(item.clone()); | ||
assert_eq!(test_result, control_result); | ||
} | ||
Operation::Dequeue => { | ||
let control_result = control.pop_front(); | ||
let test_result = test.dequeue(); | ||
assert_eq!(test_result, control_result); | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[toolchain] | ||
channel = "nightly" |
Oops, something went wrong.