use super::*; use crate::DatabaseAllocators; use std::{ ptr::NonNull, sync::{ atomic::{AtomicUsize, Ordering}, Arc as StdArc, }, }; struct LowerBoundOnly { next: usize, end: usize, } impl Iterator for LowerBoundOnly { type Item = usize; fn next(&mut self) -> Option { if self.next == self.end { return None; } let value = self.next; self.next += 1; Some(value) } fn size_hint(&self) -> (usize, Option) { (self.end - self.next, None) } } struct UnderreportedLowerBound { next: usize, end: usize, } impl Iterator for UnderreportedLowerBound { type Item = usize; fn next(&mut self) -> Option { if self.next == self.end { return None; } let value = self.next; self.next += 1; Some(value) } fn size_hint(&self) -> (usize, Option) { ((self.end - self.next).saturating_sub(1), None) } } #[derive(Clone)] struct CountingAlloc { allocations: StdArc, deallocations: StdArc, } unsafe impl ApiAllocator for CountingAlloc { fn allocate(&self, layout: Layout) -> Result, AllocError> { self.allocations.fetch_add(1, Ordering::Relaxed); ::allocate(&Global, layout) } unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { self.deallocations.fetch_add(1, Ordering::Relaxed); unsafe { ::deallocate(&Global, ptr, layout); } } } #[test] fn database_allocators_preserve_distinct_concrete_types() { let allocations = StdArc::new(AtomicUsize::new(0)); let deallocations = StdArc::new(AtomicUsize::new(0)); let allocators: DatabaseAllocators = DatabaseAllocators { mv_store: Global, fts: CountingAlloc { allocations: allocations.clone(), deallocations: deallocations.clone(), }, statement: DynAllocator::default(), }; let cloned = allocators.clone(); let layout = Layout::new::(); let mv_block = cloned.mv_store.allocate(layout).unwrap(); assert_eq!(allocations.load(Ordering::Relaxed), 0); let fts_block = cloned.fts.allocate(layout).unwrap(); assert_eq!(allocations.load(Ordering::Relaxed), 1); unsafe { allocators.mv_store.deallocate(mv_block.cast(), layout); allocators.fts.deallocate(fts_block.cast(), layout); } assert_eq!(deallocations.load(Ordering::Relaxed), 1); } #[test] fn dyn_allocator_delegates_skiplist_allocations() { let allocations = StdArc::new(AtomicUsize::new(0)); let deallocations = StdArc::new(AtomicUsize::new(0)); let alloc = DynAllocator::new(CountingAlloc { allocations: allocations.clone(), deallocations, }); let map: crate::skiplist::SkipMap = crate::skiplist::SkipMap::new_in(alloc); map.try_insert(1, 2).unwrap(); assert!(allocations.load(Ordering::Relaxed) > 0); } #[test] fn dyn_box_allocates_from_its_allocator() { let allocations = StdArc::new(AtomicUsize::new(0)); let deallocations = StdArc::new(AtomicUsize::new(0)); let alloc = DynAllocator::new(CountingAlloc { allocations: allocations.clone(), deallocations: deallocations.clone(), }); let boxed = DynBox::try_new_in([7u64; 4], alloc).unwrap(); assert_eq!(allocations.load(Ordering::Relaxed), 1); assert_eq!(TursoBoxExt::into_inner(boxed), [7u64; 4]); assert_eq!(deallocations.load(Ordering::Relaxed), 1); } struct FailingAlloc; unsafe impl ApiAllocator for FailingAlloc { fn allocate(&self, _layout: Layout) -> Result, AllocError> { Err(AllocError) } unsafe fn deallocate(&self, _ptr: NonNull, _layout: Layout) { unreachable!("FailingAlloc gives no memory, so nothing can return memory to it"); } } #[test] fn dyn_box_returns_an_error_when_its_allocator_fails() { let alloc = DynAllocator::new(FailingAlloc); assert!(DynBox::try_new_in(7u64, alloc).is_err()); } #[cfg(nightly)] #[test] fn logical_log_shared_buffer_retains_dyn_allocator() { let allocations = StdArc::new(AtomicUsize::new(0)); let deallocations = StdArc::new(AtomicUsize::new(0)); let alloc = DynAllocator::new(CountingAlloc { allocations: allocations.clone(), deallocations: deallocations.clone(), }); let record = crate::mvcc::database::LogRecord::new(1, alloc).unwrap(); let data: DynBoxedSlice = record.buf.into_boxed_slice(); let shared = crate::io::SharedBufferData::new(crate::sync::Arc::new(data)); let returned = shared.clone(); assert!(allocations.load(Ordering::Relaxed) > 0); drop(shared); assert_eq!(deallocations.load(Ordering::Relaxed), 0); drop(returned); assert!(deallocations.load(Ordering::Relaxed) > 0); } #[test] fn try_extend_accepts_exact_size_iterators() { let mut values: Vec<_> = TursoAllocExt::new(); values.try_extend([1, 2, 3]).unwrap(); assert_eq!(values.as_slice(), &[1, 2, 3]); } #[test] fn try_extend_accepts_iterators_without_upper_bounds() { let mut values: Vec<_> = TursoAllocExt::new(); values .try_extend(LowerBoundOnly { next: 0, end: 3 }) .unwrap(); assert_eq!(values.as_slice(), &[0, 1, 2]); } #[test] fn try_extend_accepts_underreported_lower_bound_iterators() { let mut values = as TursoTryWithCapacityExt>::try_with_capacity_ext(4).unwrap(); values .try_extend(UnderreportedLowerBound { next: 0, end: 4 }) .unwrap(); assert_eq!(values.as_slice(), &[0, 1, 2, 3]); } #[test] fn vec_push_within_capacity_uses_reserved_slot() { let mut values = as TursoTryWithCapacityExt>::try_with_capacity_ext(1).unwrap(); let initial_capacity = values.capacity(); *values.push_within_capacity(1).unwrap() = 2; assert_eq!(values.as_slice(), &[2]); assert_eq!(values.capacity(), initial_capacity); } #[test] fn vec_push_within_capacity_returns_value_when_full() { let mut values = as TursoTryWithCapacityExt>::try_with_capacity_ext(0).unwrap(); assert_eq!(values.push_within_capacity(1), Err(1)); assert!(values.is_empty()); } #[test] fn try_vec_with_allocator_builds_requested_values() { let values: DynVec<_> = try_vec![7; 3; DynAllocator::default()].unwrap(); assert_eq!(values.as_slice(), &[7, 7, 7]); } #[test] fn iterator_try_collect_accepts_iterators_without_upper_bounds() { let values: Vec<_> = LowerBoundOnly { next: 0, end: 3 }.try_collect().unwrap(); assert_eq!(values.as_slice(), &[0, 1, 2]); } #[cfg(nightly)] #[test] fn vec_try_extend_reserves_before_mutation() { let mut values = self::vec![1]; let result = values.try_extend(std::iter::repeat(2).take(usize::MAX)); assert!(result.is_err()); assert_eq!(values.as_slice(), &[1]); } #[test] fn hash_set_try_insert_and_extend_reserve_before_mutation() { let mut values: HashSet = HashSet::default(); assert!(TursoHashSetExt::try_insert(&mut values, 1).unwrap()); assert!(!TursoHashSetExt::try_insert(&mut values, 1).unwrap()); values.try_extend([2, 3]).unwrap(); assert!(values.contains(&1)); assert!(values.contains(&2)); assert!(values.contains(&3)); } #[test] fn vec_deque_try_push_and_extend_reserve_before_mutation() { let mut values: VecDeque = TursoAllocExt::new(); values.try_push_back(2).unwrap(); values.try_push_front(1).unwrap(); values.try_extend([3, 4]).unwrap(); assert_eq!(values.pop_front(), Some(1)); assert_eq!(values.pop_front(), Some(2)); assert_eq!(values.pop_front(), Some(3)); assert_eq!(values.pop_front(), Some(4)); assert_eq!(values.pop_front(), None); } #[test] fn binary_heap_try_push_and_extend_reserve_before_mutation() { let mut values: BinaryHeap = TursoAllocExt::new(); values.try_push(2).unwrap(); values.try_extend([1, 3]).unwrap(); assert_eq!(values.pop(), Some(3)); assert_eq!(values.pop(), Some(2)); assert_eq!(values.pop(), Some(1)); assert_eq!(values.pop(), None); } #[test] fn iterator_try_collect_builds_turso_vec() { let values: Vec<_> = [1, 2, 3].into_iter().try_collect().unwrap(); assert_eq!(values.as_slice(), &[1, 2, 3]); } #[cfg(nightly)] #[test] fn iterator_try_collect_in_builds_global_vec() { let values: Vec<_, Global> = [1, 2, 3].into_iter().try_collect_in(Global).unwrap(); assert_eq!(values.as_slice(), &[1, 2, 3]); } #[test] fn iterator_try_collect_builds_boxed_slice() { let values: Box<[_]> = [1, 2, 3].into_iter().try_collect().unwrap(); assert_eq!(&*values, &[1, 2, 3]); } #[test] fn iterator_try_collect_builds_turso_collections() { let map: HashMap<_, _> = [("one", 1), ("two", 2)].into_iter().try_collect().unwrap(); let set: HashSet<_> = [1, 2, 3].into_iter().try_collect().unwrap(); let heap: BinaryHeap<_> = [1, 3, 2].into_iter().try_collect().unwrap(); assert_eq!(map.get("one"), Some(&1)); assert!(set.contains(&3)); assert_eq!(heap.peek(), Some(&3)); } #[test] fn iterator_try_collect_builds_option_collection() { let values: Option> = [Some(1), Some(2), Some(3)] .into_iter() .try_collect() .unwrap(); let none: Option> = [Some(1), None, Some(3)].into_iter().try_collect().unwrap(); assert_eq!(values.unwrap().as_slice(), &[1, 2, 3]); assert!(none.is_none()); } #[test] fn iterator_try_collect_builds_result_collection() { let values: Result, &str> = [Ok::<_, &str>(1), Ok(2), Ok(3)] .into_iter() .try_collect() .unwrap(); let error: Result, &str> = [Ok(1), Err("bad"), Ok(3)] .into_iter() .try_collect() .unwrap(); assert_eq!(values.unwrap().as_slice(), &[1, 2, 3]); assert_eq!(error, Err("bad")); } #[cfg(nightly)] #[test] fn iterator_try_collect_result_trusted_len_reserves_exact_capacity() { let values = (0..10) .map(Ok::<_, TryReserveError>) .try_collect::, TryReserveError>>() .unwrap() .unwrap(); assert_eq!(values.as_slice(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); assert_eq!(values.capacity(), values.len()); } #[cfg(nightly)] #[test] fn iterator_try_collect_result_trusted_len_stops_on_error() { let mut consumed = 0; let result = [Ok(1), Err("bad"), Ok(3)] .into_iter() .inspect(|_| consumed += 1) .try_collect::, &str>>() .unwrap(); assert_eq!(result, Err("bad")); assert_eq!(consumed, 2); } #[test] fn iterator_try_collect_converts_result_error() { #[derive(Debug, PartialEq)] struct Converted(&'static str); impl From<&'static str> for Converted { fn from(value: &'static str) -> Self { Self(value) } } let values: Result, Converted> = [ Ok::<_, &'static str>(1), Ok::<_, &'static str>(2), Ok::<_, &'static str>(3), ] .into_iter() .try_collect::, Converted>>() .unwrap(); let error: Result, Converted> = [Ok(1), Err("bad"), Ok(3)] .into_iter() .try_collect::, Converted>>() .unwrap(); assert_eq!(values.unwrap().as_slice(), &[1, 2, 3]); assert_eq!(error, Err(Converted("bad"))); } #[test] fn tuple_try_extend_extends_both_collections() { let mut values: (Vec<_>, VecDeque<_>) = (TursoAllocExt::new(), VecDeque::new()); values .try_extend([(1, "one"), (2, "two"), (3, "three")]) .unwrap(); assert_eq!(values.0.as_slice(), &[1, 2, 3]); assert_eq!( values.1.into_iter().collect::>(), ["one", "two", "three"] ); } #[test] fn tuple_try_collect_builds_three_collections() { let (numbers, words, flags): (Vec<_>, VecDeque<_>, Vec<_>) = [(1, "one", true), (2, "two", false), (3, "three", true)] .into_iter() .try_collect() .unwrap(); assert_eq!(numbers.as_slice(), &[1, 2, 3]); assert_eq!( words.into_iter().collect::>(), ["one", "two", "three"] ); assert_eq!(flags.as_slice(), &[true, false, true]); } #[test] fn iterator_try_unzip_builds_turso_collections() { let (numbers, words): (Vec<_>, VecDeque<_>) = [(1, "one"), (2, "two"), (3, "three")] .into_iter() .try_unzip() .unwrap(); assert_eq!(numbers.as_slice(), &[1, 2, 3]); assert_eq!( words.into_iter().collect::>(), ["one", "two", "three"] ); } #[test] fn into_boxed_slice_builds_boxed_slice_alias() { let values: Vec = self::vec![1, 2, 3]; let boxed: BoxedSlice = values.into_boxed_slice(); assert_eq!(&*boxed, &[1, 2, 3]); } #[test] fn slice_try_to_vec_builds_turso_vec() { let slice: &[u8] = &[1, 2, 3]; let values: Vec = slice.try_to_vec().unwrap(); assert_eq!(values.as_slice(), slice); assert!(values.capacity() >= 3); } #[test] fn try_with_capacity_builds_turso_collections() { let values: Vec = TursoTryWithCapacityExt::try_with_capacity_ext(3).unwrap(); let map: HashMap = TursoTryWithCapacityExt::try_with_capacity_ext(3).unwrap(); let set: HashSet = TursoTryWithCapacityExt::try_with_capacity_ext(3).unwrap(); let queue: VecDeque = TursoTryWithCapacityExt::try_with_capacity_ext(3).unwrap(); let heap: BinaryHeap = TursoTryWithCapacityExt::try_with_capacity_ext(3).unwrap(); assert!(values.capacity() >= 3); assert!(map.capacity() >= 3); assert!(set.capacity() >= 3); assert!(queue.capacity() >= 3); assert!(heap.capacity() >= 3); } /// Element type whose fallible clone always fails: proves `Vec::try_clone` /// clones elements through `TryClone` instead of the infallible `Clone`. #[derive(Clone)] struct FallibleElem; impl TryClone for FallibleElem { type Error = TryReserveError; fn try_clone(&self) -> Result { Err(TryReserveError) } } #[test] fn try_clone_from_uses_default_replacement() { let source = 7_u32; let mut destination = 3_u32; destination.try_clone_from(&source).unwrap(); assert_eq!(destination, source); } #[test] fn vec_try_clone_clones_elements_fallibly() { let mut source: Vec = self::vec![]; source.try_push(FallibleElem).unwrap(); assert!( source.try_clone().is_err(), "Vec::try_clone must clone elements through TryClone" ); } #[test] fn vec_try_clone_bulk_copies_copy_elements() { let mut source: Vec = self::vec![]; source.try_push(7).unwrap(); assert_eq!(source.try_clone().unwrap().as_slice(), &[7]); } /// Fails cloning a marked element: exercises the early-`Err` path of the /// spare-capacity write loop (partially written clone must drop cleanly, /// source must stay intact). #[derive(Clone, Debug, PartialEq)] struct FailOnMarked { payload: std::string::String, fail: bool, } impl TryClone for FailOnMarked { type Error = TryReserveError; fn try_clone(&self) -> Result { if self.fail { Err(TryReserveError) } else { Ok(self.clone()) } } } #[test] fn vec_try_clone_partial_element_failure_is_clean() { let elem = |payload: &str, fail| FailOnMarked { payload: payload.into(), fail, }; let mut source: Vec = self::vec![]; source.try_push(elem("a", false)).unwrap(); source.try_push(elem("b", false)).unwrap(); source.try_push(elem("c", true)).unwrap(); source.try_push(elem("d", false)).unwrap(); assert!(source.try_clone().is_err()); // Source unchanged; the two successfully written clones were dropped. assert_eq!(source.len(), 4); assert_eq!(source[0], elem("a", false)); assert_eq!(source[3], elem("d", false)); } #[test] fn vec_try_clone_deep_values_roundtrip() { let mut source: Vec<(std::string::String, u32)> = self::vec![]; for i in 0..100u32 { source.try_push((format!("value-{i}"), i)).unwrap(); } let cloned = source.try_clone().unwrap(); assert_eq!(cloned.as_slice(), source.as_slice()); }