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
use std::collections::HashMap;

use primitive_types::H256;

use crate::{gc::DatabaseMut, Database};

impl Database for HashMap<H256, Vec<u8>> {
    fn get(&self, key: H256) -> &[u8] {
        self.get(&key)
            .unwrap_or_else(|| panic!("Key {} not found", key))
    }
}

impl DatabaseMut for HashMap<H256, Vec<u8>> {
    fn set(&mut self, key: H256, value: Option<&[u8]>) {
        if let Some(value) = value {
            self.insert(key, value.to_vec());
        } else {
            self.remove(&key);
        }
    }
}

#[cfg(test)]
pub mod tests {
    use std::convert::TryFrom;

    use quickcheck::{Arbitrary, Gen};
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
    pub struct K(usize);

    impl K {
        pub fn to_bytes(self) -> [u8; 8] {
            self.0.to_be_bytes()
        }

        #[allow(dead_code)]
        pub fn from_bytes(bytes: &[u8]) -> Self {
            Self(usize::from_be_bytes(
                <[u8; std::mem::size_of::<usize>()]>::try_from(bytes).unwrap(),
            ))
        }
    }

    impl Arbitrary for K {
        fn arbitrary(g: &mut Gen) -> Self {
            Self(usize::arbitrary(g))
        }
    }

    #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
    pub struct Data(Vec<usize>);

    const AVG_DATA_SIZE: usize = 16;

    impl Arbitrary for Data {
        fn arbitrary(_: &mut Gen) -> Self {
            Self(Vec::arbitrary(&mut Gen::new(AVG_DATA_SIZE)))
        }
    }
}