Skip to content

Commit b9d9c0e

Browse files
committed
[simple] Write a test for GcVec
Mostly a copy of the "arrays" test. It's even in the same file.
1 parent 6f00161 commit b9d9c0e

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

libs/simple/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::layout::{StaticGcType, GcType, SimpleVecRepr, DynamicObj, StaticVecTy
6969
use zerogc_context::collector::{RawSimpleAlloc, RawCollectorImpl};
7070
use zerogc_context::handle::{GcHandleList, RawHandleImpl};
7171
use zerogc_context::{CollectionManager as AbstractCollectionManager, RawContext as AbstractRawContext, CollectorContext};
72-
use zerogc::vec::{GcVec, GcRawVec};
72+
use zerogc::vec::{GcRawVec};
7373

7474
#[cfg(feature = "small-object-arenas")]
7575
mod alloc;
@@ -136,6 +136,8 @@ pub type CollectorId = ::zerogc_context::CollectorId<RawSimpleCollector>;
136136
pub type Gc<'gc, T> = ::zerogc::Gc<'gc, T, CollectorId>;
137137
/// A garbage collected array, allocated in the "simple" collector
138138
pub type GcArray<'gc, T> = ::zerogc::vec::GcArray<'gc, T, CollectorId>;
139+
/// A garbage colelcted vector, allocated in the "simple" collector
140+
pub type GcVec<'gc, T> = ::zerogc::vec::GcVec<'gc, T, SimpleCollectorContext>;
139141

140142
#[cfg(not(feature = "multiple-collectors"))]
141143
static GLOBAL_COLLECTOR: AtomicPtr<RawSimpleCollector> = AtomicPtr::new(std::ptr::null_mut());
@@ -164,7 +166,7 @@ unsafe impl RawSimpleAlloc for RawSimpleCollector {
164166
(context.collector().id(), ptr.cast())
165167
}
166168

167-
fn alloc_vec_with_capacity<'gc, T>(context: &'gc CollectorContext<Self>, capacity: usize) -> GcVec<'gc, T, CollectorContext<Self>> where T: GcSafe + 'gc {
169+
fn alloc_vec_with_capacity<'gc, T>(context: &'gc CollectorContext<Self>, capacity: usize) -> GcVec<'gc, T> where T: GcSafe + 'gc {
168170
let ptr = if capacity == 0 {
169171
NonNull::dangling()
170172
} else {

libs/simple/tests/arrays.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use zerogc::GcSimpleAlloc;
44
use zerogc::safepoint;
55
use zerogc_derive::Trace;
66

7-
use zerogc_simple::{SimpleCollector, GcArray, Gc, CollectorId as SimpleCollectorId, GcConfig};
7+
use zerogc_simple::{SimpleCollector, GcArray, GcVec, Gc, CollectorId as SimpleCollectorId, GcConfig};
8+
use zerogc::vec::GcRawVec;
89

910
fn test_collector() -> SimpleCollector {
1011
let mut config = GcConfig::default();
@@ -22,7 +23,7 @@ struct Dummy<'gc> {
2223
}
2324

2425
#[test]
25-
fn int_array() {
26+
fn array() {
2627
let collector = test_collector();
2728
let mut context = collector.into_context();
2829
let array1 = context.alloc_slice_fill_copy(5, 12u32);
@@ -55,3 +56,47 @@ fn int_array() {
5556
}
5657
}
5758
}
59+
60+
#[test]
61+
fn vec() {
62+
let collector = test_collector();
63+
let mut context = collector.into_context();
64+
let mut vec1 = context.alloc_vec();
65+
for _ in 0..5 {
66+
vec1.push(12u32);
67+
}
68+
assert_eq!(*vec1.as_slice(), *vec![12u32; 5]);
69+
drop(vec1);
70+
safepoint!(context, ());
71+
const TEXT: &[u8] = b"all cows eat grass";
72+
let mut vec_text = context.alloc_vec();
73+
vec_text.extend_from_slice(TEXT);
74+
let mut vec_none: GcVec<Option<usize>> = context.alloc_vec_with_capacity(12);
75+
for _ in 0..12 {
76+
vec_none.push(None);
77+
}
78+
for val in vec_none.iter() {
79+
assert_eq!(*val, None);
80+
}
81+
drop(vec_none);
82+
let vec_text: GcVec<u8> = GcVec { raw: safepoint!(context, vec_text.into_raw()), context: &context };
83+
assert_eq!(vec_text.as_slice(), TEXT);
84+
let mut nested_trace: GcVec<Gc<Dummy>> = context.alloc_vec_with_capacity(3);
85+
let mut last = None;
86+
for i in 0..16 {
87+
let obj = context.alloc(Dummy {
88+
val: i,
89+
inner: last
90+
});
91+
nested_trace.push(obj);
92+
last = Some(obj);
93+
}
94+
drop(vec_text);
95+
let nested_trace: GcVec<Gc<Dummy>> = GcVec{ raw: safepoint!(context, nested_trace.into_raw()), context: &context };
96+
for (idx, val) in nested_trace.iter().enumerate() {
97+
assert_eq!(val.val, idx, "Invalid val: {:?}", val);
98+
if let Some(last) = val.inner {
99+
assert_eq!(last.val, idx - 1);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)