Add more enc/dec tests

next
Sayan Nandan 7 months ago
parent 525dc46f86
commit 2670e5a974
No known key found for this signature in database
GPG Key ID: 0EBD769024B24F0A

@ -206,7 +206,7 @@ pub struct UIntSpec(FullTag);
impl UIntSpec {
pub const LIM_MAX: [u64; 4] = as_array![u8::MAX, u16::MAX, u32::MAX, u64::MAX];
pub const DEFAULT: Self = Self::UINT64;
pub const UINT64: Self = Self(FullTag::new_uint(TagSelector::UInt64));
const UINT64: Self = Self(FullTag::new_uint(TagSelector::UInt64));
pub const unsafe fn from_full(f: FullTag) -> Self {
Self(f)
}
@ -228,7 +228,7 @@ impl SIntSpec {
pub const LIM_MIN: [i64; 4] = as_array![i8::MIN, i16::MIN, i32::MIN, i64::MIN];
pub const LIM_MAX: [i64; 4] = as_array![i8::MAX, i16::MAX, i32::MAX, i64::MAX];
pub const DEFAULT: Self = Self::SINT64;
pub const SINT64: Self = Self(FullTag::new_sint(TagSelector::SInt64));
const SINT64: Self = Self(FullTag::new_sint(TagSelector::SInt64));
pub const unsafe fn from_full(f: FullTag) -> Self {
Self(f)
}
@ -251,7 +251,7 @@ impl FloatSpec {
pub const LIM_MIN: [f64; 2] = as_array![f32::MIN, f64::MIN];
pub const LIM_MAX: [f64; 2] = as_array![f32::MAX, f64::MAX];
pub const DEFAULT: Self = Self::F64;
pub const F64: Self = Self(FullTag::new_float(TagSelector::Float64));
const F64: Self = Self(FullTag::new_float(TagSelector::Float64));
pub const unsafe fn from_full(f: FullTag) -> Self {
Self(f)
}

@ -34,10 +34,12 @@ use {
data::{
cell::Datacell,
dict::{DictEntryGeneric, DictGeneric},
tag::TagSelector,
tag::{FloatSpec, SIntSpec, TagSelector, UIntSpec},
uuid::Uuid,
},
idx::{IndexBaseSpec, IndexSTSeqCns, STIndex, STIndexSeq},
mem::BufferedScanner,
storage::common_encoding::r1::obj::cell::StorageCellTypeID,
},
};
@ -119,3 +121,119 @@ fn space() {
let dec = super::dec::full::<obj::SpaceLayoutRef>(&enc).unwrap();
assert_eq!(space, dec);
}
#[test]
fn dc_encode_decode() {
fn enc_dec(dc: &Datacell) {
let mut encoded = vec![];
super::obj::cell::encode(&mut encoded, &dc);
let mut scanner = BufferedScanner::new(&encoded);
let tag = scanner
.try_next_byte()
.map(StorageCellTypeID::try_from_raw)
.unwrap()
.unwrap();
let dc_restored: Datacell = unsafe {
super::obj::cell::decode_element::<Datacell, BufferedScanner>(&mut scanner, tag)
.unwrap()
};
assert_eq!(dc, &dc_restored);
assert_eq!(dc.tag(), dc_restored.tag());
}
let dc_tests = [
// null
Datacell::null(),
// bool
Datacell::new_bool(true),
Datacell::new_bool(false),
// uint
// uint (8)
Datacell::new_uint(u8::MIN as _, unsafe {
UIntSpec::from_full(TagSelector::UInt8.into_full())
}),
Datacell::new_uint(u8::MAX as _, unsafe {
UIntSpec::from_full(TagSelector::UInt8.into_full())
}),
// uint (16)
Datacell::new_uint(u16::MIN as _, unsafe {
UIntSpec::from_full(TagSelector::UInt16.into_full())
}),
Datacell::new_uint(u16::MAX as _, unsafe {
UIntSpec::from_full(TagSelector::UInt16.into_full())
}),
// uint (32)
Datacell::new_uint(u32::MIN as _, unsafe {
UIntSpec::from_full(TagSelector::UInt32.into_full())
}),
Datacell::new_uint(u32::MAX as _, unsafe {
UIntSpec::from_full(TagSelector::UInt32.into_full())
}),
// uint (64)
Datacell::new_uint(u64::MIN as _, unsafe {
UIntSpec::from_full(TagSelector::UInt64.into_full())
}),
Datacell::new_uint(u64::MAX as _, unsafe {
UIntSpec::from_full(TagSelector::UInt64.into_full())
}),
// sint
// sint (8)
Datacell::new_sint(i8::MIN as _, unsafe {
SIntSpec::from_full(TagSelector::SInt8.into_full())
}),
Datacell::new_sint(i8::MAX as _, unsafe {
SIntSpec::from_full(TagSelector::SInt8.into_full())
}),
// sint (16)
Datacell::new_sint(i16::MIN as _, unsafe {
SIntSpec::from_full(TagSelector::SInt16.into_full())
}),
Datacell::new_sint(i16::MAX as _, unsafe {
SIntSpec::from_full(TagSelector::SInt16.into_full())
}),
// sint (32)
Datacell::new_sint(i32::MIN as _, unsafe {
SIntSpec::from_full(TagSelector::SInt32.into_full())
}),
Datacell::new_sint(i32::MAX as _, unsafe {
SIntSpec::from_full(TagSelector::SInt32.into_full())
}),
// sint (64)
Datacell::new_sint(i64::MIN as _, unsafe {
SIntSpec::from_full(TagSelector::SInt64.into_full())
}),
Datacell::new_sint(i64::MAX as _, unsafe {
SIntSpec::from_full(TagSelector::SInt64.into_full())
}),
// float
// float (32)
Datacell::new_float(f32::MIN as _, unsafe {
FloatSpec::from_full(TagSelector::Float32.into_full())
}),
Datacell::new_float(f32::MAX as _, unsafe {
FloatSpec::from_full(TagSelector::Float32.into_full())
}),
// float (64)
Datacell::new_float(f64::MIN as _, unsafe {
FloatSpec::from_full(TagSelector::Float64.into_full())
}),
Datacell::new_float(f64::MAX as _, unsafe {
FloatSpec::from_full(TagSelector::Float64.into_full())
}),
// bin
Datacell::new_bin(b"".to_vec().into_boxed_slice()),
Datacell::new_bin(b"abcdefghijkl".to_vec().into_boxed_slice()),
// str
Datacell::new_str("".to_owned().into_boxed_str()),
Datacell::new_str("abcdefghijkl".to_owned().into_boxed_str()),
// list
Datacell::new_list(vec![]),
];
for value in dc_tests {
enc_dec(&value)
}
let mut dc = Datacell::new_list(vec![]);
for _ in 0..100 {
enc_dec(&dc);
dc = Datacell::new_list(vec![dc.clone()]);
}
}

Loading…
Cancel
Save