Skip to content

Commit

Permalink
Merge pull request #64 from CosmWasm/column-remove-test
Browse files Browse the repository at this point in the history
Column: Test and document behaviour for auto-incrementing IDs
  • Loading branch information
webmaster128 authored Sep 20, 2024
2 parents 3813672 + 8b9d204 commit 55f0234
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::storage::{Storage, StorageMut};
use super::common::TryGetError;
use super::{BoundFor, BoundedIterableAccessor, IterableAccessor, NonTerminal, Storable};

/// The last index that has been pushed to the column.
/// This does not have to be the index of the last element as it is
/// not reset in case the last element is removed.
const META_LAST_IX: &[u8] = &[0];
const META_LEN: &[u8] = &[1];

Expand Down Expand Up @@ -483,6 +486,36 @@ mod tests {
assert_eq!(access.len().unwrap(), 1);
}

#[test]
fn remove() {
let mut storage = TestStorage::new();

let column = Column::<u64, TestEncoding>::new(0);
let mut access = column.access(&mut storage);

assert_eq!(access.push(&1337).unwrap(), 0);
assert_eq!(access.push(&42).unwrap(), 1);
assert_eq!(access.push(&17).unwrap(), 2);
assert_eq!(access.len().unwrap(), 3);

// remove middle
access.remove(1).unwrap();
assert_eq!(access.len().unwrap(), 2);

// remove first
access.remove(0).unwrap();
assert_eq!(access.len().unwrap(), 1);

// remove last
access.remove(2).unwrap();
assert_eq!(access.len().unwrap(), 0);

// Above removals do not reset the auto-incrementor,
// such that we get a fresh key for the next push.
assert_eq!(access.push(&99).unwrap(), 3);
assert_eq!(access.len().unwrap(), 1);
}

#[test]
fn iteration() {
let mut storage = TestStorage::new();
Expand Down

0 comments on commit 55f0234

Please sign in to comment.