extern crate std;
use super::*;
use crate::storage::warp_table::secondary_index::StableSlot;
use crate::storage::warp_table::{Capacity, WarpTableInsertWithIdOutcome};
use alloc::string::String;
use core::num::NonZeroU32;
use core::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
#[derive(WarpTableRow)]
#[index(primary = u32)]
struct Stock {
#[index(secondary)]
quantity: u32,
}
fn fresh() -> Result<WarpTable<Stock>, &'static str> {
let mut table =
WarpTable::try_new(Capacity::new(NonZeroU32::MIN)).map_err(|_| "table unavailable")?;
assert!(matches!(
table.insert_with_id(7, Stock { quantity: 42 }),
WarpTableInsertWithIdOutcome::Inserted { .. }
));
Ok(table)
}
#[test]
fn corrupted_selection_identity_cannot_silently_skip_removal() -> Result<(), &'static str> {
for missing_column in [true, false] {
let mut table = fresh()?;
if missing_column {
table.ids.clear();
} else {
table.primary_index =
crate::storage::warp_table::dense_key_index::DenseKeyIndex::try_new(1)
.map_err(|_| "index unavailable")?;
}
let Err(payload) = catch_unwind(AssertUnwindSafe(|| {
MutableSelection {
table: &mut table,
slots: Some(SlotSet::one(StableSlot::new(0))),
}
.remove_first()
})) else {
return Err("corrupt selection silently removed nothing");
};
assert_eq!(
payload
.downcast_ref::<String>()
.ok_or("unexpected diagnostic")?,
"WarpTable invariant violated: RowUnavailable { row: 0 }"
);
assert_eq!(table.len(), 1);
assert_eq!(table.columns.quantity, [42]);
}
Ok(())
}