From 41930942328eea72cfaccdbb2be66dca95dbf544 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:35:41 -0400 Subject: [PATCH 1/7] add test_produce_blob_from_fit_vec --- storage/src/sc_db_utils.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 2ebe5f8..59f59af 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -116,3 +116,21 @@ pub fn compare_blob_lists( changed_ids } + +#[cfg(test)] +mod tests { + use super::*; + use serde::Serialize; + + const TEST_BLOB_SIZE: usize = 256; // Define a test blob size for simplicity + static SC_DATA_BLOB_SIZE: usize = TEST_BLOB_SIZE; + + #[test] + fn test_produce_blob_from_fit_vec() { + let data = (0..0 + 255).collect(); + let blob = produce_blob_from_fit_vec(data); + assert_eq!(blob[..4], [0, 1, 2, 3]); + } + + +} From 5b2098c9f78fe2e8453fb8e440687a16fa0c9caf Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:36:06 -0400 Subject: [PATCH 2/7] add test_produce_blob_from_fit_vec_panic --- storage/src/sc_db_utils.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 59f59af..1cd32e4 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -132,5 +132,12 @@ mod tests { assert_eq!(blob[..4], [0, 1, 2, 3]); } + #[test] + #[should_panic] + fn test_produce_blob_from_fit_vec_panic() { + let data = vec![0; SC_DATA_BLOB_SIZE + 1]; + let _ = produce_blob_from_fit_vec(data); + } + } From dc1f975f13fbf89ff2e75ed09faad5794feed856 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:36:24 -0400 Subject: [PATCH 3/7] add test_produce_blob_list_from_sc_public_state --- storage/src/sc_db_utils.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 1cd32e4..982857b 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -139,5 +139,18 @@ mod tests { let _ = produce_blob_from_fit_vec(data); } + #[derive(Serialize)] + struct TestState { + a: u32, + b: u32, + } + + #[test] + fn test_produce_blob_list_from_sc_public_state() { + let state = TestState { a: 42, b: 99 }; + let result = produce_blob_list_from_sc_public_state(&state).unwrap(); + assert!(!result.is_empty()); + } + } From 2aec310c1e80fffd2b6bf91ed878cb0c9b74b817 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:36:44 -0400 Subject: [PATCH 4/7] add test_compare_blob_lists_created --- storage/src/sc_db_utils.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 982857b..84e2366 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -152,5 +152,15 @@ mod tests { assert!(!result.is_empty()); } + #[test] + fn test_compare_blob_lists_created() { + let old_list: Vec = vec![]; + let new_list: Vec = vec![[1; SC_DATA_BLOB_SIZE]]; + + let changes = compare_blob_lists(&old_list, &new_list); + assert_eq!(changes.len(), 1); + assert!(matches!(changes[0], DataBlobChangeVariant::Created { .. })); + } + } From 6b302a051fd477ff9dda8c3c42d5ff2ba9fd9107 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:37:05 -0400 Subject: [PATCH 5/7] add test_compare_blob_lists_deleted --- storage/src/sc_db_utils.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 84e2366..fea1b02 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -162,5 +162,15 @@ mod tests { assert!(matches!(changes[0], DataBlobChangeVariant::Created { .. })); } + #[test] + fn test_compare_blob_lists_deleted() { + let old_list: Vec = vec![[1; SC_DATA_BLOB_SIZE]]; + let new_list: Vec = vec![]; + + let changes = compare_blob_lists(&old_list, &new_list); + assert_eq!(changes.len(), 1); + assert!(matches!(changes[0], DataBlobChangeVariant::Deleted { .. })); + } + } From ae5645251daad85e3acda709088617bc576722e5 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:37:23 -0400 Subject: [PATCH 6/7] add test_compare_blob_lists_modified --- storage/src/sc_db_utils.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index fea1b02..593795b 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -172,5 +172,13 @@ mod tests { assert!(matches!(changes[0], DataBlobChangeVariant::Deleted { .. })); } + #[test] + fn test_compare_blob_lists_modified() { + let old_list: Vec = vec![[1; SC_DATA_BLOB_SIZE]]; + let new_list: Vec = vec![[2; SC_DATA_BLOB_SIZE]]; + let changes = compare_blob_lists(&old_list, &new_list); + assert_eq!(changes.len(), 1); + assert!(matches!(changes[0], DataBlobChangeVariant::Modified { .. })); + } } From 4ff5fe895bbe9501ef1c08417ce05d03f9df76fd Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Thu, 3 Apr 2025 18:37:36 -0400 Subject: [PATCH 7/7] fix bug --- storage/src/sc_db_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/src/sc_db_utils.rs b/storage/src/sc_db_utils.rs index 593795b..df30cc9 100644 --- a/storage/src/sc_db_utils.rs +++ b/storage/src/sc_db_utils.rs @@ -49,7 +49,7 @@ pub fn produce_blob_list_from_sc_public_state( let ser_data = serde_json::to_vec(state)?; //`ToDo` Replace with `next_chunk` usage, when feature stabilizes in Rust - for i in 0..(ser_data.len() / SC_DATA_BLOB_SIZE) { + for i in 0..=(ser_data.len() / SC_DATA_BLOB_SIZE) { let next_chunk: Vec; if (i + 1) * SC_DATA_BLOB_SIZE < ser_data.len() {