Skip to content

Commit

Permalink
add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Dec 8, 2024
1 parent 6c74758 commit 83b2bb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions light-client-db-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use anyhow::anyhow;
use anyhow::Context;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use web_sys::js_sys::{Atomics, Int32Array, Uint8Array};

#[cfg(test)]
pub mod tests;

#[derive(Serialize, Deserialize, Debug)]
/// Represent a key-value pair
pub struct KV {
Expand Down
33 changes: 33 additions & 0 deletions light-client-db-common/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::js_sys::{Int32Array, SharedArrayBuffer, Uint8Array};

use crate::{write_command_with_payload, InputCommand, OutputCommand};

#[wasm_bindgen_test]
fn test_command_conversion() {
assert_eq!(InputCommand::DbRequest as i32, 2);
assert_eq!(OutputCommand::DbResponse as i32, 2);
assert!(InputCommand::try_from(20 as i32).is_ok());
assert!(OutputCommand::try_from(20 as i32).is_ok());
}

#[wasm_bindgen_test]
// #[test]
fn test_command_write() {
let arr_buf = SharedArrayBuffer::new(100);
let i32arr = Int32Array::new(&arr_buf);
let u8arr = Uint8Array::new(&arr_buf);
write_command_with_payload(
InputCommand::DbRequest as i32,
vec![1, 2, 3, 4],
&i32arr,
&u8arr,
)
.unwrap();
assert_eq!(i32arr.get_index(0), InputCommand::DbRequest as i32);
let mut buf = vec![0u8; 100];
u8arr.copy_to(&mut buf);
let result =
bincode::deserialize::<Vec<i32>>(&buf[8..i32arr.get_index(1) as usize + 8]).unwrap();
assert_eq!(result, vec![1, 2, 3, 4]);
}

0 comments on commit 83b2bb8

Please sign in to comment.