80 lines
1.7 KiB
Rust
80 lines
1.7 KiB
Rust
#![cfg(test)]
|
|
|
|
use crate::{Store, StoreBackend};
|
|
|
|
const SIZE: usize = 1024;
|
|
|
|
pub struct TestBackend {
|
|
data: [u8; SIZE],
|
|
}
|
|
|
|
impl TestBackend {
|
|
pub fn new() -> Self {
|
|
TestBackend {
|
|
data: [!0; SIZE],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl StoreBackend for TestBackend {
|
|
type Data = [u8; SIZE];
|
|
|
|
fn data(&self) -> &Self::Data {
|
|
&self.data
|
|
}
|
|
|
|
type Error = ();
|
|
fn erase(&mut self) -> Result<(), Self::Error> {
|
|
for b in &mut self.data {
|
|
*b = !0;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> {
|
|
let end = offset + payload.len();
|
|
if end > self.len() {
|
|
return Err(());
|
|
}
|
|
self.data[offset..end].copy_from_slice(payload);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn make_store() -> Store<TestBackend> {
|
|
let backend = TestBackend::new();
|
|
Store::new(backend)
|
|
}
|
|
|
|
#[test]
|
|
fn empty_read_not_found() {
|
|
let store = make_store();
|
|
assert_eq!(store.read("foo").unwrap(), None)
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn write_read() {
|
|
let mut store = make_store();
|
|
store.write("foo", b"bar").unwrap();
|
|
assert_eq!(store.read("foo").unwrap().unwrap(), b"bar");
|
|
}
|
|
|
|
#[test]
|
|
fn write_int_read_str() {
|
|
let mut store = make_store();
|
|
store.write_int("foo", 42).unwrap();
|
|
assert_eq!(store.read_str("foo").unwrap().unwrap(), "42");
|
|
}
|
|
|
|
#[test]
|
|
fn write_many() {
|
|
let mut store = make_store();
|
|
store.write("foo", b"do not compact away").unwrap();
|
|
for _ in 0..10000 {
|
|
store.write("bar", b"SPAM").unwrap();
|
|
}
|
|
assert_eq!(store.read("foo").unwrap().unwrap(), b"do not compact away");
|
|
assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM");
|
|
}
|