156 lines
3.9 KiB
Rust
156 lines
3.9 KiB
Rust
#![cfg(test)]
|
|
|
|
use crate::{Error, no_flash, Store, StoreBackend, WriteError};
|
|
|
|
const SIZE: usize = 1024;
|
|
static mut BACKUP_SPACE: [u8; SIZE] = [0; SIZE];
|
|
|
|
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 backup_space(&self) -> &'static mut [u8] {
|
|
unsafe { &mut BACKUP_SPACE }
|
|
}
|
|
}
|
|
|
|
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_read_str() {
|
|
let mut store = make_store();
|
|
store.write("foo", "bar").unwrap();
|
|
assert_eq!(store.read_str("foo").unwrap().unwrap(), "bar");
|
|
}
|
|
|
|
#[test]
|
|
fn write_read_str_utf8() {
|
|
let mut store = make_store();
|
|
store.write("réseau", "point à point").unwrap();
|
|
assert_eq!(store.read_str("réseau").unwrap().unwrap(), "point à point");
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_read_value_str() {
|
|
let mut store = make_store();
|
|
store.write_value("foo", &"bar", [0; 4]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some("bar")));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_read_value_u32() {
|
|
let mut store = make_store();
|
|
store.write_value("foo", &42005u32, [0; 4]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some(42005u32)));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_read_value_i64() {
|
|
let mut store = make_store();
|
|
store.write_value("foo", &-99999999i64, &mut [0; 8]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some(-99999999i64)));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_read_value_f32() {
|
|
let mut store = make_store();
|
|
store.write_value("foo", &3.75e17f32, &mut [0; 4]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some(3.75e17f32)));
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_read_value_f64() {
|
|
let mut store = make_store();
|
|
store.write_value("foo", &-1.999e-13f64, [0; 8]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some(-1.999e-13f64)));
|
|
}
|
|
|
|
#[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");
|
|
}
|
|
|
|
#[test]
|
|
#[cfg(feature = "postcard-values")]
|
|
fn write_value_many() {
|
|
let mut store = make_store();
|
|
store.write_value("quux", &(false, 3u8, -42i16), [0; 4]).unwrap();
|
|
for i in 0..1000u32 {
|
|
store.write_value("bar", &i, [0; 4]).unwrap();
|
|
assert_eq!(store.read_value("bar"), Ok(Some(i)));
|
|
store.write_value("foo", &(i % 2 == 0), [0]).unwrap();
|
|
assert_eq!(store.read_value("foo"), Ok(Some(i % 2 == 0)));
|
|
}
|
|
assert_eq!(store.read_value("quux"), Ok(Some((false, 3u8, -42i16))));
|
|
}
|
|
|
|
#[test]
|
|
fn no_flash() {
|
|
let mut store = Store::new(no_flash::NoFlash);
|
|
assert_eq!(store.read("foo").unwrap(), None);
|
|
assert_eq!(
|
|
store.write("foo", b"bar"),
|
|
Err(Error::Write(WriteError::Backend(no_flash::NoFlashError)))
|
|
);
|
|
}
|