Skip to content

Commit

Permalink
test: simplify unit tests even more
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Dec 23, 2023
1 parent eb1b858 commit 39ac5c1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 52 deletions.
1 change: 1 addition & 0 deletions mast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{Hash, Hasher, HASH_LEN};
// - cache hashing

// TODO: remove unwrap
// TODO: KeyType and ValueType

#[derive(Debug, Clone, PartialEq)]
/// In memory reprsentation of treap node.
Expand Down
71 changes: 19 additions & 52 deletions mast/src/operations/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,14 @@ fn binary_search_path(

#[cfg(test)]
mod test {
use crate::test::{test_operations, Entry, Operation};
use crate::test::{test_operations, Entry};

#[test]
fn insert_single_entry() {
let case = ["A"];

let expected = case.map(|key| Entry {
key: key.as_bytes().to_vec(),
value: [b"v", key.as_bytes()].concat(),
});

test_operations(
&expected.map(|e| (e, Operation::Insert)),
&case.map(|key| Entry::insert(key.as_bytes(), &[b"v", key.as_bytes()].concat())),
Some("78fd7507ef338f1a5816ffd702394999680a9694a85f4b8af77795d9fdd5854d"),
)
}
Expand All @@ -245,13 +240,11 @@ mod test {
"R", "S", "T", "U", "V", "W", "X", "Y", "Z",
];

let expected = case.map(|key| Entry {
key: key.as_bytes().to_vec(),
value: [b"v", key.as_bytes()].concat(),
});
let expected =
case.map(|key| Entry::insert(key.as_bytes(), &[b"v", key.as_bytes()].concat()));

test_operations(
&expected.map(|e| (e, Operation::Insert)),
&case.map(|key| Entry::insert(key.as_bytes(), &[b"v", key.as_bytes()].concat())),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
);
}
Expand All @@ -262,16 +255,10 @@ mod test {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z",
];

case.reverse();

let expected = case.map(|key| Entry {
key: key.as_bytes().to_vec(),
value: [b"v", key.as_bytes()].concat(),
});

test_operations(
&expected.map(|e| (e, Operation::Insert)),
&case.map(|key| Entry::insert(key.as_bytes(), &[b"v", key.as_bytes()].concat())),
Some("02af3de6ed6368c5abc16f231a17d1140e7bfec483c8d0aa63af4ef744d29bc3"),
)
}
Expand All @@ -280,13 +267,8 @@ mod test {
fn unsorted() {
let case = ["D", "N", "P", "X", "A", "G", "C", "M", "H", "I", "J"];

let expected = case.map(|key| Entry {
key: key.as_bytes().to_vec(),
value: [b"v", key.as_bytes()].concat(),
});

test_operations(
&expected.map(|e| (e, Operation::Insert)),
&case.map(|key| Entry::insert(key.as_bytes(), &[b"v", key.as_bytes()].concat())),
Some("0957cc9b87c11cef6d88a95328cfd9043a3d6a99e9ba35ee5c9c47e53fb6d42b"),
)
}
Expand All @@ -297,16 +279,11 @@ mod test {

let mut i = 0;

let entries = case.map(|key| {
i += 1;
Entry {
key: key.as_bytes().to_vec(),
value: i.to_string().into(),
}
});

test_operations(
&entries.map(|e| (e, Operation::Insert)),
&case.map(|key| {
i += 1;
Entry::insert(key.as_bytes(), i.to_string().as_bytes())
}),
Some("4538b4de5e58f9be9d54541e69fab8c94c31553a1dec579227ef9b572d1c1dff"),
)
}
Expand All @@ -318,16 +295,11 @@ mod test {

let mut i = 0;

let entries = case.map(|key| {
i += 1;
Entry {
key: key.as_bytes().to_vec(),
value: i.to_string().into(),
}
});

test_operations(
&entries.map(|e| (e, Operation::Insert)),
&case.map(|key| {
i += 1;
Entry::insert(key.as_bytes(), i.to_string().as_bytes())
}),
Some("c9f7aaefb18ec8569322b9621fc64f430a7389a790e0bf69ec0ad02879d6ce54"),
)
}
Expand All @@ -339,16 +311,11 @@ mod test {

let mut i = 0;

let entries = case.map(|key| {
i += 1;
Entry {
key: key.as_bytes().to_vec(),
value: i.to_string().into(),
}
});

test_operations(
&entries.map(|e| (e, Operation::Insert)),
&case.map(|key| {
i += 1;
Entry::insert(key.as_bytes(), i.to_string().as_bytes())
}),
Some("02e26311f2b55bf6d4a7163399f99e17c975891a05af2f1e09bc969f8bf0f95d"),
)
}
Expand Down
12 changes: 12 additions & 0 deletions mast/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ pub struct Entry {
pub(crate) value: Vec<u8>,
}

impl Entry {
pub fn insert(key: &[u8], value: &[u8]) -> (Self, Operation) {
(
Self {
key: key.to_vec(),
value: value.to_vec(),
},
Operation::Insert,
)
}
}

impl std::fmt::Debug for Entry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({:?}, {:?})", self.key, self.value)
Expand Down

0 comments on commit 39ac5c1

Please sign in to comment.