1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Arbitrary value generation and test harnesses for the finalized state.

#![allow(dead_code)]

use std::{ops::Deref, sync::Arc};

use crate::service::finalized_state::{
    disk_format::{FromDisk, IntoDisk},
    FinalizedState, ZebraDb,
};

// Enable older test code to automatically access the inner database via Deref coercion.
impl Deref for FinalizedState {
    type Target = ZebraDb;

    fn deref(&self) -> &Self::Target {
        &self.db
    }
}

pub fn round_trip<T>(input: T) -> T
where
    T: IntoDisk + FromDisk,
{
    let bytes = input.as_bytes();
    T::from_bytes(bytes)
}

pub fn assert_round_trip<T>(input: T)
where
    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
{
    let before = input.clone();
    let after = round_trip(input);
    assert_eq!(before, after);
}

pub fn round_trip_ref<T>(input: &T) -> T
where
    T: IntoDisk + FromDisk,
{
    let bytes = input.as_bytes();
    T::from_bytes(bytes)
}

pub fn assert_round_trip_ref<T>(input: &T)
where
    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
{
    let before = input;
    let after = round_trip_ref(input);
    assert_eq!(before, &after);
}

pub fn round_trip_arc<T>(input: Arc<T>) -> T
where
    T: IntoDisk + FromDisk,
{
    let bytes = input.as_bytes();
    T::from_bytes(bytes)
}

pub fn assert_round_trip_arc<T>(input: Arc<T>)
where
    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
{
    let before = input.clone();
    let after = round_trip_arc(input);
    assert_eq!(*before, after);
}

/// The round trip test covers types that are used as value field in a rocksdb
/// column family. Only these types are ever deserialized, and so they're the only
/// ones that implement both `IntoDisk` and `FromDisk`.
pub fn assert_value_properties<T>(input: T)
where
    T: IntoDisk + FromDisk + Clone + PartialEq + std::fmt::Debug,
{
    assert_round_trip_ref(&input);
    assert_round_trip_arc(Arc::new(input.clone()));
    assert_round_trip(input);
}