Skip to content

Commit

Permalink
another test for Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
amunra committed Sep 25, 2024
1 parent a781194 commit 0203705
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::alloc::Allocator;
use alloc::collections::TryReserveError;
use alloc::vec::Vec as InnerVec;
use core::fmt::Debug;

pub struct Vec<T, A: Allocator> {
inner: InnerVec<T, A>,
Expand Down Expand Up @@ -36,25 +37,33 @@ impl<T, A: Allocator> Vec<T, A> {
}
}

impl<T: Debug, A: Allocator> Debug for Vec<T, A> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::alloc::Global;
use alloc::collections::TryReserveError;
use alloc::sync::Arc;
use core::alloc::{AllocError, Layout};
use core::ptr::NonNull;
use core::sync::atomic::{AtomicUsize, Ordering};

#[derive(Clone)]
struct WatermarkAllocator {
watermark: usize,
in_use: AtomicUsize,
in_use: Arc<AtomicUsize>,
}

impl WatermarkAllocator {
fn new(watermark: usize) -> Self {
Self {
watermark,
in_use: AtomicUsize::new(0),
in_use: AtomicUsize::new(0).into(),
}
}
}
Expand Down Expand Up @@ -87,7 +96,7 @@ mod tests {
}

#[test]
fn test_vec() {
fn test_try_push() {
let wma = WatermarkAllocator::new(32);
let mut vec = Vec::new_in(wma);
vec.try_push(1).unwrap();
Expand All @@ -97,4 +106,13 @@ mod tests {
let _err: TryReserveError = vec.try_push(5).unwrap_err();
assert_eq!(vec.inner.as_slice(), &[1, 2, 3, 4]);
}

#[test]
fn test_with_capacity_in() {
let wma = WatermarkAllocator::new(32);
let vec: Vec<usize, _> = Vec::with_capacity_in(4, wma.clone()).unwrap();
assert_eq!(vec.inner.capacity(), 4);

let _err: TryReserveError = Vec::<i8, _>::with_capacity_in(5, wma).unwrap_err();
}
}

0 comments on commit 0203705

Please sign in to comment.