From 80fe45f354597cad8051f24af2606193edc52aae Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Fri, 2 Aug 2024 16:29:38 +0200 Subject: [PATCH] test(versionable): test `Versionize` with various rust types --- utils/tfhe-versionable/tests/types.rs | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 utils/tfhe-versionable/tests/types.rs diff --git a/utils/tfhe-versionable/tests/types.rs b/utils/tfhe-versionable/tests/types.rs new file mode 100644 index 0000000000..1ef96a6ef2 --- /dev/null +++ b/utils/tfhe-versionable/tests/types.rs @@ -0,0 +1,74 @@ +//! Test the impl of Versionize for various types + +use std::collections::{HashMap, HashSet}; +use std::io::Cursor; +use std::marker::PhantomData; +use std::num::Wrapping; +use std::sync::Arc; + +use aligned_vec::{ABox, AVec}; +use num_complex::Complex; +use tfhe_versionable::{Unversionize, Versionize}; + +use backward_compat::MyStructVersions; + +#[derive(PartialEq, Clone, Debug, Versionize)] +#[versionize(MyStructVersions)] +pub struct MyStruct { + wrap: Wrapping, + base_box: Box, + sliced_box: Box<[u16; 50]>, + base_vec: Vec, + s: String, + opt: Option, + phantom: PhantomData, + arc: Arc, + complex: Complex, + aligned_box: ABox, + aligned_vec: AVec, + never: (), + tuple: (f32, f64), + set: HashSet, + map: HashMap, +} + +mod backward_compat { + use tfhe_versionable::VersionsDispatch; + + use super::MyStruct; + + #[derive(VersionsDispatch)] + #[allow(unused)] + pub enum MyStructVersions { + V0(MyStruct), + } +} + +#[test] +fn test_types() { + let stru = MyStruct { + wrap: Wrapping(false), + base_box: Box::new(42), + sliced_box: vec![11; 50].into_boxed_slice().try_into().unwrap(), + base_vec: vec![1234, 5678], + s: String::from("test"), + opt: Some(0xdeadbeef), + phantom: PhantomData, + arc: Arc::new(-1), + complex: Complex::new(-37, -45), + aligned_box: ABox::new(0, -98765), + aligned_vec: AVec::from_slice(0, &[1, 2, 3, 4]), + never: (), + tuple: (3.14, 2.71), + set: HashSet::from_iter([1, 2, 3].into_iter()), + map: HashMap::from_iter([('t', true), ('e', false), ('s', true)].into_iter()), + }; + + let mut ser = Vec::new(); + ciborium::ser::into_writer(&stru.versionize(), &mut ser).unwrap(); + + let unser = + MyStruct::unversionize(ciborium::de::from_reader(&mut Cursor::new(&ser)).unwrap()).unwrap(); + + assert_eq!(stru, unser); +}