-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
181 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
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,170 @@ | ||
use crate::{local::LocalRb, prelude::*}; | ||
use core::mem::MaybeUninit; | ||
|
||
fn head_tail(observer: &impl Observer) -> (usize, usize) { | ||
use crate::raw::RawRb; | ||
(observer.as_raw().read_end(), observer.as_raw().write_end()) | ||
} | ||
|
||
type LocalStaticRb<T, const N: usize> = LocalRb<[MaybeUninit<T>; N]>; | ||
|
||
#[test] | ||
fn capacity() { | ||
const CAP: usize = 13; | ||
let buf = LocalStaticRb::<i32, CAP>::default(); | ||
assert_eq!(buf.capacity(), CAP); | ||
} | ||
#[test] | ||
fn split_capacity() { | ||
const CAP: usize = 13; | ||
let mut buf = LocalStaticRb::<i32, CAP>::default(); | ||
let (prod, cons) = (&mut buf).split(); | ||
|
||
assert_eq!(prod.capacity(), CAP); | ||
assert_eq!(cons.capacity(), CAP); | ||
} | ||
|
||
#[test] | ||
fn try_push() { | ||
let mut buf = LocalStaticRb::<i32, 2>::default(); | ||
let (mut prod, _) = (&mut buf).split(); | ||
|
||
assert_eq!(head_tail(&prod), (0, 0)); | ||
|
||
assert_eq!(prod.try_push(123), Ok(())); | ||
assert_eq!(head_tail(&prod), (0, 1)); | ||
|
||
assert_eq!(prod.try_push(234), Ok(())); | ||
assert_eq!(head_tail(&prod), (0, 2)); | ||
|
||
assert_eq!(prod.try_push(345), Err(345)); | ||
assert_eq!(head_tail(&prod), (0, 2)); | ||
} | ||
|
||
#[test] | ||
fn pop_empty() { | ||
let mut buf = LocalStaticRb::<i32, 2>::default(); | ||
let (_, mut cons) = (&mut buf).split(); | ||
|
||
assert_eq!(head_tail(&cons), (0, 0)); | ||
|
||
assert_eq!(cons.try_pop(), None); | ||
assert_eq!(head_tail(&cons), (0, 0)); | ||
} | ||
|
||
#[test] | ||
fn push_pop_one() { | ||
const CAP: usize = 2; | ||
let mut buf = LocalStaticRb::<i32, CAP>::default(); | ||
let (mut prod, mut cons) = (&mut buf).split(); | ||
|
||
const MOD: usize = 2 * CAP; | ||
let values = [12, 34, 56, 78, 90]; | ||
assert_eq!(head_tail(&cons), (0, 0)); | ||
|
||
for (i, v) in values.iter().enumerate() { | ||
assert_eq!(prod.try_push(*v), Ok(())); | ||
assert_eq!(head_tail(&cons), (i % MOD, (i + 1) % MOD)); | ||
|
||
assert_eq!(cons.try_pop().unwrap(), *v); | ||
assert_eq!(head_tail(&cons), ((i + 1) % MOD, (i + 1) % MOD)); | ||
|
||
assert_eq!(cons.try_pop(), None); | ||
assert_eq!(head_tail(&cons), ((i + 1) % MOD, (i + 1) % MOD)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn push_pop_all() { | ||
const CAP: usize = 2; | ||
let mut buf = LocalStaticRb::<i32, CAP>::default(); | ||
let (mut prod, mut cons) = (&mut buf).split(); | ||
|
||
const MOD: usize = 2 * CAP; | ||
let values = [(12, 34, 13), (56, 78, 57), (90, 10, 91)]; | ||
assert_eq!(head_tail(&cons), (0, 0)); | ||
|
||
for (i, v) in values.iter().enumerate() { | ||
assert_eq!(prod.try_push(v.0), Ok(())); | ||
assert_eq!(head_tail(&cons), (CAP * i % MOD, (CAP * i + 1) % MOD)); | ||
|
||
assert_eq!(prod.try_push(v.1), Ok(())); | ||
assert_eq!(head_tail(&cons), (CAP * i % MOD, (CAP * i + 2) % MOD)); | ||
|
||
assert_eq!(prod.try_push(v.2).unwrap_err(), v.2); | ||
assert_eq!(head_tail(&cons), (CAP * i % MOD, (CAP * i + 2) % MOD)); | ||
|
||
assert_eq!(cons.try_pop().unwrap(), v.0); | ||
assert_eq!(head_tail(&cons), ((CAP * i + 1) % MOD, (CAP * i + 2) % MOD)); | ||
|
||
assert_eq!(cons.try_pop().unwrap(), v.1); | ||
assert_eq!(head_tail(&cons), ((CAP * i + 2) % MOD, (CAP * i + 2) % MOD)); | ||
|
||
assert_eq!(cons.try_pop(), None); | ||
assert_eq!(head_tail(&cons), ((CAP * i + 2) % MOD, (CAP * i + 2) % MOD)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn empty_full() { | ||
let mut buf = LocalStaticRb::<i32, 1>::default(); | ||
let (mut prod, cons) = (&mut buf).split(); | ||
|
||
assert!(prod.is_empty()); | ||
assert!(cons.is_empty()); | ||
assert!(!prod.is_full()); | ||
assert!(!cons.is_full()); | ||
|
||
assert_eq!(prod.try_push(123), Ok(())); | ||
|
||
assert!(!prod.is_empty()); | ||
assert!(!cons.is_empty()); | ||
assert!(prod.is_full()); | ||
assert!(cons.is_full()); | ||
} | ||
|
||
#[test] | ||
fn len_remaining() { | ||
let mut buf = LocalStaticRb::<i32, 2>::default(); | ||
let (mut prod, mut cons) = (&mut buf).split(); | ||
|
||
assert_eq!(prod.occupied_len(), 0); | ||
assert_eq!(cons.occupied_len(), 0); | ||
assert_eq!(prod.vacant_len(), 2); | ||
assert_eq!(cons.vacant_len(), 2); | ||
|
||
assert_eq!(prod.try_push(123), Ok(())); | ||
|
||
assert_eq!(prod.occupied_len(), 1); | ||
assert_eq!(cons.occupied_len(), 1); | ||
assert_eq!(prod.vacant_len(), 1); | ||
assert_eq!(cons.vacant_len(), 1); | ||
|
||
assert_eq!(prod.try_push(456), Ok(())); | ||
|
||
assert_eq!(prod.occupied_len(), 2); | ||
assert_eq!(cons.occupied_len(), 2); | ||
assert_eq!(prod.vacant_len(), 0); | ||
assert_eq!(cons.vacant_len(), 0); | ||
|
||
assert_eq!(cons.try_pop(), Some(123)); | ||
|
||
assert_eq!(prod.occupied_len(), 1); | ||
assert_eq!(cons.occupied_len(), 1); | ||
assert_eq!(prod.vacant_len(), 1); | ||
assert_eq!(cons.vacant_len(), 1); | ||
|
||
assert_eq!(cons.try_pop(), Some(456)); | ||
|
||
assert_eq!(prod.occupied_len(), 0); | ||
assert_eq!(cons.occupied_len(), 0); | ||
assert_eq!(prod.vacant_len(), 2); | ||
assert_eq!(cons.vacant_len(), 2); | ||
|
||
assert_eq!(prod.try_push(789), Ok(())); | ||
|
||
assert_eq!(prod.occupied_len(), 1); | ||
assert_eq!(cons.occupied_len(), 1); | ||
assert_eq!(prod.vacant_len(), 1); | ||
assert_eq!(cons.vacant_len(), 1); | ||
} |
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 @@ | ||
mod basic; |