From 8b9d20423466a2c58b2d7e3763b2a99824626988 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 18 Sep 2024 23:34:29 +0200 Subject: [PATCH] Test and document behaviour for auto-incrementing IDs --- packages/storey/src/containers/column.rs | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/storey/src/containers/column.rs b/packages/storey/src/containers/column.rs index 91e8eb7..577fcb2 100644 --- a/packages/storey/src/containers/column.rs +++ b/packages/storey/src/containers/column.rs @@ -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]; @@ -483,6 +486,36 @@ mod tests { assert_eq!(access.len().unwrap(), 1); } + #[test] + fn remove() { + let mut storage = TestStorage::new(); + + let column = Column::::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();