extern crate alloc;
use core::{
fmt::Debug,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
num::{
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroU8, NonZeroU16, NonZeroU32,
NonZeroU64, NonZeroU128,
},
};
use smol_bytes::Utf8Bytes;
use uuid::Uuid;
use super::{ColumnDoor, ConformanceError, round_trip_fulltext, round_trip_vector};
use crate::store::{
ColumnReader, ColumnWriter, Field, FulltextEngine, NoColumns, VectorEngine,
column::testkit::{Col, Fallback, FallbackRead, Native, NativeRead, Refusal},
};
struct Scene;
struct ToyVector;
impl VectorEngine for ToyVector {
type Options = ();
type SearchOptions = ();
type Error = Refusal;
type Score = f32;
type Element = f32;
type Address = Vec<Col>;
type Connection = ();
type WriteBuffer = Native;
type ReadBuffer = NativeRead;
fn buffer() -> Self::WriteBuffer {
Native::default()
}
fn finalize(buf: Self::WriteBuffer) -> Self::Address {
buf.0
}
fn open(address: Self::Address) -> Self::ReadBuffer {
Native(address).read()
}
}
struct FallbackFulltext;
impl FulltextEngine for FallbackFulltext {
type Options = ();
type SearchOptions = ();
type Error = Refusal;
type Score = f32;
type Address = Vec<Col>;
type Connection = ();
type WriteBuffer = Fallback;
type ReadBuffer = FallbackRead;
fn buffer() -> Self::WriteBuffer {
Fallback::default()
}
fn finalize(buf: Self::WriteBuffer) -> Self::Address {
buf.0
}
fn open(address: Self::Address) -> Self::ReadBuffer {
Fallback(address).read()
}
}
#[track_caller]
fn on_toy<K: Field + PartialEq + Debug>(key: K) {
if let Err(refusal) = round_trip_vector::<ToyVector, K>(key) {
panic!(
"{} does not conform at the toy vector engine: {refusal:?}",
core::any::type_name::<K>()
);
}
}
#[track_caller]
fn on_fallback<K: Field + PartialEq + Debug>(key: K) {
if let Err(refusal) = round_trip_fulltext::<FallbackFulltext, K>(key) {
panic!(
"{} does not conform at the fallback-only engine: {refusal:?}",
core::any::type_name::<K>()
);
}
}
#[cfg(feature = "tantivy")]
#[track_caller]
fn on_tantivy<K: Field + PartialEq + Debug>(key: K) {
if let Err(refusal) = round_trip_fulltext::<crate::store::TantivyFulltext, K>(key) {
panic!(
"{} does not conform at the shipped engine: {refusal:?}",
core::any::type_name::<K>()
);
}
}
macro_rules! census {
($run:ident) => {{
$run(i8::MIN);
$run(i16::MIN);
$run(i32::MIN);
$run(i64::MIN);
$run(i128::MIN);
$run(u8::MAX);
$run(u16::MAX);
$run(u32::MAX);
$run(u64::MAX);
$run(u128::MAX);
$run(NonZeroI8::new(-1).expect("-1 is not zero"));
$run(NonZeroI16::new(-1).expect("-1 is not zero"));
$run(NonZeroI32::new(-1).expect("-1 is not zero"));
$run(NonZeroI64::new(-1).expect("-1 is not zero"));
$run(NonZeroI128::new(-1).expect("-1 is not zero"));
$run(NonZeroU8::new(u8::MAX).expect("the maximum is not zero"));
$run(NonZeroU16::new(u16::MAX).expect("the maximum is not zero"));
$run(NonZeroU32::new(u32::MAX).expect("the maximum is not zero"));
$run(NonZeroU64::new(u64::MAX).expect("the maximum is not zero"));
$run(NonZeroU128::new(u128::MAX).expect("the maximum is not zero"));
$run(identity());
$run("a caption".to_owned());
$run(String::new());
$run(Utf8Bytes::from("médià".to_owned()));
#[cfg(feature = "smol_str")]
$run(smol_str::SmolStr::from("médià"));
$run(Ipv4Addr::new(10, 0, 0, 1));
$run(host());
$run(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)));
$run(IpAddr::V6(host()));
$run(SocketAddrV4::new(Ipv4Addr::new(10, 0, 0, 1), 8080));
$run(SocketAddrV6::new(host(), 8080, 3, 5));
$run(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(10, 0, 0, 1),
8080,
)));
$run(SocketAddr::V6(SocketAddrV6::new(host(), 8080, 3, 5)));
$run([1_u8, 2, 3, 4]);
$run([7_u8; 12]);
$run(alloc::sync::Arc::new(7_u64));
$run(alloc::rc::Rc::new(7_u64));
$run(alloc::boxed::Box::new(7_u64));
$run(alloc::sync::Arc::new(alloc::boxed::Box::new(7_u64)));
$run(crate::TypedId::<Uuid, Scene>::new(identity()));
#[cfg(feature = "mongo")]
$run(bson::Uuid::from_bytes(*identity().as_bytes()));
#[cfg(feature = "mongo")]
$run(bson::oid::ObjectId::from_bytes([7; 12]));
}};
}
fn identity() -> Uuid {
Uuid::from_bytes([
0x9f, 0x1c, 0x2d, 0x3e, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f, 0xa0, 0xb1, 0xc2, 0xd3, 0xe4, 0xf5,
])
}
fn host() -> Ipv6Addr {
Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1)
}
#[derive(Debug, PartialEq)]
struct Anonymous;
impl Field for Anonymous {
fn write(&self, _w: &mut impl ColumnWriter) {}
fn read<R: ColumnReader>(_r: &mut R) -> Result<Self, R::Error> {
Ok(Self)
}
}
#[derive(Debug, PartialEq)]
struct Split(u32);
impl Field for Split {
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&self.0.to_be_bytes());
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let value = r.bytes()?;
Ok(Self(u32::from_be_bytes(
<[u8; 4]>::try_from(value).expect("a column this row measured at four bytes"),
)))
}
}
#[derive(Debug, PartialEq)]
struct SplitSecond(u64, u32);
impl Field for SplitSecond {
fn write(&self, w: &mut impl ColumnWriter) {
w.u64(self.0);
w.fixed(&self.1.to_be_bytes());
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let first = r.u64()?;
let second = r.bytes()?;
Ok(Self(
first,
u32::from_be_bytes(<[u8; 4]>::try_from(second).expect("four bytes")),
))
}
}
#[derive(Debug, PartialEq)]
struct SplitDefaulted(u32);
impl Field for SplitDefaulted {
fn write(&self, w: &mut impl ColumnWriter) {
w.u32(self.0);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.fixed::<4>().map(|value| Self(u32::from_be_bytes(value)))
}
}
#[derive(Debug, PartialEq)]
struct Greedy(u64);
impl Field for Greedy {
fn write(&self, w: &mut impl ColumnWriter) {
w.u64(self.0);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let value = r.u64()?;
r.u64()?;
Ok(Self(value))
}
}
#[derive(Debug, PartialEq)]
struct Chatty(u64);
impl Field for Chatty {
fn write(&self, w: &mut impl ColumnWriter) {
w.u64(self.0);
w.u64(0);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.u64().map(Self)
}
}
#[derive(Debug, PartialEq)]
struct Lossy(u64);
impl Field for Lossy {
fn write(&self, w: &mut impl ColumnWriter) {
w.u64(self.0 | 1);
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.u64().map(Self)
}
}
#[test]
fn the_whole_census_conforms_at_a_toy_vector_engine() {
census!(on_toy);
}
#[test]
fn the_whole_census_conforms_at_a_fallback_only_engine() {
census!(on_fallback);
}
#[cfg(feature = "tantivy")]
#[test]
fn the_whole_census_conforms_at_real_tantivy() {
census!(on_tantivy);
}
#[test]
fn a_row_split_across_two_doors_is_refused_with_the_column_named() {
assert!(matches!(
round_trip_vector::<ToyVector, Split>(Split(7)),
Err(ConformanceError::KindSplit {
at: 0,
wrote: ColumnDoor::Fixed(4),
read: ColumnDoor::Bytes,
})
));
#[cfg(feature = "tantivy")]
assert!(matches!(
round_trip_fulltext::<crate::store::TantivyFulltext, Split>(Split(7)),
Err(ConformanceError::KindSplit {
at: 0,
wrote: ColumnDoor::Fixed(4),
read: ColumnDoor::Bytes,
})
));
}
#[cfg(feature = "tantivy")]
#[test]
fn the_engine_leg_alone_passes_the_row_the_kind_leg_refuses() {
use crate::{FulltextEngine, store::TantivyFulltext};
let mut buf = TantivyFulltext::buffer();
Split(7).write(&mut buf);
let mut phase = TantivyFulltext::open(TantivyFulltext::finalize(buf));
assert_eq!(
Split::read(&mut phase).expect("the address reads back"),
Split(7)
);
phase.finish().expect("the key took every column");
}
#[test]
fn the_split_column_is_named_by_its_index() {
assert!(matches!(
round_trip_vector::<ToyVector, SplitSecond>(SplitSecond(7, 9)),
Err(ConformanceError::KindSplit {
at: 1,
wrote: ColumnDoor::Fixed(4),
read: ColumnDoor::Bytes,
})
));
}
#[test]
fn a_narrow_integer_is_not_the_fixed_column_its_default_lands_in() {
assert!(matches!(
round_trip_vector::<ToyVector, SplitDefaulted>(SplitDefaulted(7)),
Err(ConformanceError::KindSplit {
at: 0,
wrote: ColumnDoor::U32,
read: ColumnDoor::Fixed(4),
})
));
}
#[test]
fn a_row_that_reads_past_its_own_write_is_short() {
assert!(matches!(
round_trip_vector::<ToyVector, Greedy>(Greedy(7)),
Err(ConformanceError::Short {
at: 1,
wanted: ColumnDoor::U64,
})
));
}
#[test]
fn a_row_that_writes_more_than_it_reads_leaves_a_surplus() {
assert!(matches!(
round_trip_vector::<ToyVector, Chatty>(Chatty(7)),
Err(ConformanceError::Surplus { unread: 1 })
));
}
#[test]
fn a_row_that_does_not_restore_is_a_mismatch() {
let Err(ConformanceError::Mismatch { wrote, read }) =
round_trip_vector::<ToyVector, Lossy>(Lossy(2))
else {
panic!("a row that renders another value has to be caught");
};
assert_eq!(wrote, "Lossy(2)");
assert_eq!(read, "Lossy(3)");
}
#[test]
fn the_unit_engine_refuses_a_key_it_never_held() {
assert!(matches!(
round_trip_vector::<(), u64>(7),
Err(ConformanceError::Read(NoColumns))
));
assert!(matches!(
round_trip_fulltext::<(), u64>(7),
Err(ConformanceError::Read(NoColumns))
));
}
#[test]
fn the_unit_engine_passes_a_key_with_no_columns() {
assert!(round_trip_vector::<(), Anonymous>(Anonymous).is_ok());
assert!(round_trip_fulltext::<(), Anonymous>(Anonymous).is_ok());
}
#[test]
fn a_refusal_names_the_column_and_both_kinds() {
let refusal = ConformanceError::<NoColumns>::KindSplit {
at: 3,
wrote: ColumnDoor::Fixed(6),
read: ColumnDoor::Bytes,
};
assert_eq!(
refusal.to_string(),
"column 3 was written as fixed<6> and read as bytes"
);
}