Make tests compile

This commit is contained in:
Nadja Reitzenstein 2021-10-18 11:27:42 +02:00
parent fb8cbfc864
commit d837e1c364
6 changed files with 55 additions and 50 deletions

1
Cargo.lock generated
View File

@ -398,6 +398,7 @@ dependencies = [
"libc", "libc",
"lmdb-rkv", "lmdb-rkv",
"ptr_meta", "ptr_meta",
"rand",
"rkyv", "rkyv",
"rkyv_dyn", "rkyv_dyn",
"rkyv_typename", "rkyv_typename",

View File

@ -85,6 +85,7 @@ walkdir = "2.3.2"
futures-test = "0.3.16" futures-test = "0.3.16"
tempfile = "3.2" tempfile = "3.2"
bincode = "2.0.0-dev" bincode = "2.0.0-dev"
rand = "0.8"
[patch.crates-io] [patch.crates-io]
bincode = { git = "https://github.com/dequbed/bincode.git", branch = "feature/in_place_buffer" } bincode = { git = "https://github.com/dequbed/bincode.git", branch = "feature/in_place_buffer" }

View File

@ -53,6 +53,7 @@ pub use resources::{
ResourceDB, ResourceDB,
}; };
#[derive(Debug)]
pub enum DBError { pub enum DBError {
LMDB(lmdb::Error), LMDB(lmdb::Error),
RKYV(<AllocSerializer<1024> as Fallible>::Error), RKYV(<AllocSerializer<1024> as Fallible>::Error),
@ -88,5 +89,4 @@ impl<V: Serialize<Ser>> Adapter for AllocAdapter<V> {
fn from_db_err(e: lmdb::Error) -> Self::Error { fn from_db_err(e: lmdb::Error) -> Self::Error {
e.into() e.into()
} }
} }

View File

@ -149,54 +149,38 @@ impl StateAccessor {
} }
} }
#[cfg(test_dis)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::db::tests::open_test_env; use crate::state::value::Vec3u8;
use lmdb::{ use crate::state::value::{OID_COLOUR, OID_POWERED, OID_INTENSITY};
EnvironmentFlags as EF, use std::ops::Deref;
DatabaseFlags as DF,
WriteFlags as WF,
};
use rkyv::Infallible;
use rkyv::ser::serializers::AllocSerializer;
use rkyv::archived_root;
use rkyv::util::archived_value;
#[test] #[test]
fn construct_state() { fn construct_state() {
let tmpdir = tempfile::tempdir().unwrap();
let mut tmppath = tmpdir.path().to_owned();
tmppath.push("db");
let db = StateDB::init(tmppath).unwrap();
let b = State::build() let b = State::build()
.add("Colour".to_string(), Vec3u8 { a: 1, b: 2, c: 3}) .add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3}))
.add("Powered".to_string(), Bool(true)) .add(OID_POWERED.clone(), Box::new(true))
.add("Intensity".to_string(), UInt32(4242)) .add(OID_INTENSITY.clone(), Box::new(1023))
.finish(); .finish();
println!("({}) {:?}", b.hash(), b); println!("({}) {:?}", b.hash(), b);
let mut serializer = AllocSerializer::<256>::default(); let c = State::build()
let pos = serializer.serialize_value(&b).unwrap(); .add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3}))
let buf = serializer.into_serializer().into_inner(); .add(OID_POWERED.clone(), Box::new(true))
.add(OID_INTENSITY.clone(), Box::new(1023))
.finish();
println!("Encsize: {}", buf.len()); let key = rand::random();
db.update(key, &b, &c).unwrap();
let archived_state = unsafe { let d = db.get_input(key).unwrap().unwrap();
archived_value::<State>(buf.as_ref(), pos) let e = db.get_output(key).unwrap().unwrap();
}; assert_eq!(&b, d.deref());
let s: State = archived_state.deserialize(&mut Infallible).unwrap(); assert_eq!(&c, e.deref());
println!("({}) {:?}", pos, s);
}
#[test]
fn function_name_test() {
let te = open_text_env();
let ildb = e.create_db(Some("input"), DF::empty()).expect("Failed to create db file");
let oldb = e.create_db(Some("output"), DF::empty()).expect("Failed to create db file");
let idb = DB::new(e.env.clone(), ildb);
let odb = DB::new(e.env.clone(), oldb);
let db = StateDB::new(idb, odb);
} }
} }

View File

@ -33,6 +33,7 @@ use serde::de::Error as _;
#[derive(serde::Serialize, serde::Deserialize)] #[derive(serde::Serialize, serde::Deserialize)]
#[derive(Archive, Serialize, Deserialize)] #[derive(Archive, Serialize, Deserialize)]
#[archive_attr(derive(Debug))]
/// State object of a resource /// State object of a resource
/// ///
/// This object serves three functions: /// This object serves three functions:
@ -59,6 +60,12 @@ impl PartialEq for State {
} }
} }
impl PartialEq<Archived<State>> for State {
fn eq(&self, other: &Archived<Self>) -> bool {
self.hash == other.hash
}
}
impl Eq for State {} impl Eq for State {}
impl fmt::Debug for State { impl fmt::Debug for State {
@ -117,6 +124,7 @@ pub struct Entry<'a> {
} }
#[derive(Debug, Archive, Serialize, Deserialize)] #[derive(Debug, Archive, Serialize, Deserialize)]
#[archive_attr(derive(Debug))]
pub struct OwnedEntry { pub struct OwnedEntry {
pub oid: ObjectIdentifier, pub oid: ObjectIdentifier,
pub val: Box<dyn SerializeValue>, pub val: Box<dyn SerializeValue>,

View File

@ -261,6 +261,7 @@ impl ArchiveUnsized for dyn SerializeValue {
} }
} }
#[derive(Debug)]
pub struct ArchivedValueMetadata { pub struct ArchivedValueMetadata {
type_oid: Archived<ObjectIdentifier>, type_oid: Archived<ObjectIdentifier>,
} }
@ -465,36 +466,46 @@ lazy_static::lazy_static! {
pub static ref OID_U8: ObjectIdentifier = { pub static ref OID_U8: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.2").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.2").unwrap()
}; };
static ref OID_U16: ObjectIdentifier = { pub static ref OID_U16: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.3").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.3").unwrap()
}; };
static ref OID_U32: ObjectIdentifier = { pub static ref OID_U32: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.4").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.4").unwrap()
}; };
static ref OID_U64: ObjectIdentifier = { pub static ref OID_U64: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.5").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.5").unwrap()
}; };
static ref OID_U128: ObjectIdentifier = { pub static ref OID_U128: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.6").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.6").unwrap()
}; };
static ref OID_I8: ObjectIdentifier = { pub static ref OID_I8: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.7").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.7").unwrap()
}; };
static ref OID_I16: ObjectIdentifier = { pub static ref OID_I16: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.8").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.8").unwrap()
}; };
static ref OID_I32: ObjectIdentifier = { pub static ref OID_I32: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.9").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.9").unwrap()
}; };
static ref OID_I64: ObjectIdentifier = { pub static ref OID_I64: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.10").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.10").unwrap()
}; };
static ref OID_I128: ObjectIdentifier = { pub static ref OID_I128: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.11").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.11").unwrap()
}; };
static ref OID_VEC3U8: ObjectIdentifier = { pub static ref OID_VEC3U8: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.13").unwrap() ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.13").unwrap()
}; };
pub static ref OID_POWERED: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.1").unwrap()
};
pub static ref OID_INTENSITY: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.2").unwrap()
};
pub static ref OID_COLOUR: ObjectIdentifier = {
ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.3").unwrap()
};
} }
oidvalue!(OID_BOOL, bool); oidvalue!(OID_BOOL, bool);
oidvalue!(OID_U8, u8); oidvalue!(OID_U8, u8);