Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

- name: Build Fuzzers
run: |
cargo install cargo-fuzz
cargo +nightly install cargo-fuzz
cargo +nightly fuzz build

examples:
Expand Down
10 changes: 10 additions & 0 deletions src/base_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ impl<T: Storable, M: Memory> BaseVec<T, M> {
self.memory
}

/// Removes all items from the vector.
///
/// The next `push` will write at slot 0, overwriting old data naturally.
/// Headers are preserved.
///
/// Complexity: O(1)
pub fn clear(&self) {
self.set_len(0);
}

/// Returns true if the vector is empty.
///
/// Complexity: O(1)
Expand Down
10 changes: 10 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ impl<T: Storable, INDEX: Memory, DATA: Memory> Log<T, INDEX, DATA> {
(self.index_memory, self.data_memory)
}

/// Removes all entries from the log.
///
/// After clearing, `len()` returns 0 and the next `append` writes at
/// data offset 0, overwriting old data naturally. Headers are preserved.
///
/// Complexity: O(1)
pub fn clear(&self) {
write_u64(&self.index_memory, Address::from(HEADER_OFFSET), 0);
}

/// Returns true iff this log does not have any entries.
pub fn is_empty(&self) -> bool {
self.len() == 0
Expand Down
50 changes: 50 additions & 0 deletions src/log/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@ fn test_log_construct() {
assert_eq!(log.index_size_bytes(), 40);
}

#[test]
fn test_log_clear() {
let log = Log::<Vec<u8>, _, _>::new(VectorMemory::default(), VectorMemory::default());
log.append(&b"DEADBEEF".to_vec())
.expect("failed to append entry");
log.append(&b"FEEDBAD".to_vec())
.expect("failed to append entry");
assert_eq!(log.len(), 2);

log.clear();

assert_eq!(log.len(), 0);
assert!(log.is_empty());
assert_eq!(log.get(0), None);
assert_eq!(log.iter().count(), 0);
assert_eq!(log.log_size_bytes(), 0);
}

#[test]
fn test_log_clear_then_append() {
let log = Log::<Vec<u8>, _, _>::new(VectorMemory::default(), VectorMemory::default());
log.append(&b"old_entry".to_vec()).unwrap();
log.clear();

let idx = log.append(&b"new_entry".to_vec()).unwrap();
assert_eq!(idx, 0);
assert_eq!(log.len(), 1);
assert_eq!(log.get(0).unwrap(), b"new_entry".to_vec());
}

#[test]
fn test_log_clear_empty() {
let log = Log::<Vec<u8>, _, _>::new(VectorMemory::default(), VectorMemory::default());
log.clear();
assert_eq!(log.len(), 0);
assert!(log.is_empty());
}

#[test]
fn test_log_clear_persistence() {
let log = Log::<Vec<u8>, _, _>::new(VectorMemory::default(), VectorMemory::default());
log.append(&b"entry".to_vec()).unwrap();
log.clear();

let (index_memory, data_memory) = log.into_memories();
let log = Log::<Vec<u8>, _, _>::init(index_memory, data_memory);
assert_eq!(log.len(), 0);
assert!(log.is_empty());
}

#[test]
fn test_new_overwrites() {
let log = Log::<Vec<u8>, _, _>::new(VectorMemory::default(), VectorMemory::default());
Expand Down
7 changes: 7 additions & 0 deletions src/min_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ where
self.0.len()
}

/// Removes all items from the heap.
///
/// Complexity: O(1)
pub fn clear(&mut self) {
self.0.clear();
}

/// Returns true if the heap is empty.
///
/// Complexity: O(1)
Expand Down
38 changes: 38 additions & 0 deletions src/min_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ impl Memory for EmptyMem {
}
}

#[test]
fn test_clear() {
let mut heap = StableMinHeap::<u64, M>::new(M::default());
heap.push(&3);
heap.push(&1);
heap.push(&2);
assert_eq!(heap.len(), 3);

heap.clear();

assert_eq!(heap.len(), 0);
assert!(heap.is_empty());
assert_eq!(heap.peek(), None);
assert_eq!(heap.iter().count(), 0);
}

#[test]
fn test_clear_then_push() {
let mut heap = StableMinHeap::<u64, M>::new(M::default());
heap.push(&10);
heap.push(&5);
heap.clear();

heap.push(&42);
assert_eq!(heap.len(), 1);
assert_eq!(heap.peek(), Some(42));
assert_eq!(heap.pop(), Some(42));
assert!(heap.is_empty());
}

#[test]
fn test_clear_empty() {
let mut heap = StableMinHeap::<u64, M>::new(M::default());
heap.clear();
assert_eq!(heap.len(), 0);
assert!(heap.is_empty());
}

#[test]
#[should_panic(expected = "GrowFailed { current_size: 0, delta: 1 }")]
fn test_failure_new() {
Expand Down
7 changes: 7 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ impl<T: Storable, M: Memory> Vec<T, M> {
self.0.into_memory()
}

/// Removes all items from the vector.
///
/// Complexity: O(1)
pub fn clear(&self) {
self.0.clear();
}

/// Returns true if the vector is empty.
///
/// Complexity: O(1)
Expand Down
48 changes: 48 additions & 0 deletions src/vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,54 @@ impl crate::Storable for BuggyStruct {
};
}

#[test]
fn clear() {
let sv = StableVec::<u64, M>::new(M::default());
sv.push(&1);
sv.push(&2);
sv.push(&3);
assert_eq!(sv.len(), 3);

sv.clear();

assert_eq!(sv.len(), 0);
assert!(sv.is_empty());
assert_eq!(sv.get(0), None);
assert_eq!(sv.iter().count(), 0);
}

#[test]
fn clear_then_push() {
let sv = StableVec::<u64, M>::new(M::default());
sv.push(&1);
sv.push(&2);
sv.clear();

sv.push(&42);
assert_eq!(sv.len(), 1);
assert_eq!(sv.get(0), Some(42));
}

#[test]
fn clear_empty() {
let sv = StableVec::<u64, M>::new(M::default());
sv.clear();
assert_eq!(sv.len(), 0);
assert!(sv.is_empty());
}

#[test]
fn clear_persistence() {
let sv = StableVec::<u64, M>::new(M::default());
sv.push(&1);
sv.clear();

let mem = sv.into_memory();
let sv = StableVec::<u64, M>::init(mem);
assert_eq!(sv.len(), 0);
assert!(sv.is_empty());
}

#[test]
#[should_panic(expected = "expected an element with length <= 1 bytes, but found 4")]
fn push_element_bigger_than_max_size_panics() {
Expand Down
26 changes: 26 additions & 0 deletions tests/api_conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ fn api_conformance_min_heap() {
assert_eq!(stable.len(), 0);
assert!(stable.is_empty());
assert!(std.is_empty());

// Clear.
for i in 0..n {
stable.push(&i);
std.push(Reverse(i));
}
stable.clear();
std.clear();
assert_eq!(stable.len(), std.len() as u64);
assert_eq!(stable.is_empty(), std.is_empty());
}

#[test]
Expand Down Expand Up @@ -334,6 +344,16 @@ fn api_conformance_vec() {
// After popping everything, both should be empty.
assert_eq!(stable.len(), std.len() as u64);
assert_eq!(stable.is_empty(), std.is_empty());

// Clear.
for i in 0..n {
stable.push(&i);
std.push(i);
}
stable.clear();
std.clear();
assert_eq!(stable.len(), std.len() as u64);
assert_eq!(stable.is_empty(), std.is_empty());
}

#[test]
Expand Down Expand Up @@ -391,4 +411,10 @@ fn api_conformance_log() {
// Iteration
let log_items: Vec<_> = log.iter().collect();
assert_eq!(log_items, std);

// Clear.
log.clear();
std.clear();
assert_eq!(log.len(), std.len() as u64);
assert_eq!(log.is_empty(), std.is_empty());
}