From a53f07be1464ded1a34f7fa046ce7dfdb7e7f57a Mon Sep 17 00:00:00 2001 From: pluveto Date: Tue, 3 Dec 2024 21:28:23 +0800 Subject: [PATCH] test: add tests for trait of operations on various data structures, including conflict scenarios and checking for empty structures --- crates/core/tests/test_merge.rs | 119 ++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 crates/core/tests/test_merge.rs diff --git a/crates/core/tests/test_merge.rs b/crates/core/tests/test_merge.rs new file mode 100644 index 000000000..8bfd6a24b --- /dev/null +++ b/crates/core/tests/test_merge.rs @@ -0,0 +1,119 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use bdk_core::Merge; + +#[test] +fn test_btree_map_merge() { + let mut map1: BTreeMap = BTreeMap::new(); + map1.insert(1, "a"); + let mut map2: BTreeMap = BTreeMap::new(); + map2.insert(2, "b"); + + map1.merge(map2); + + let expected: BTreeMap = BTreeMap::from([(1, "a"), (2, "b")]); + assert_eq!(map1, expected); +} + +#[test] +fn test_btree_set_merge() { + let mut set1: BTreeSet = BTreeSet::new(); + set1.insert(1); + let mut set2: BTreeSet = BTreeSet::new(); + set2.insert(2); + + set1.merge(set2); + + let expected: BTreeSet = BTreeSet::from([1, 2]); + assert_eq!(set1, expected); +} + +#[test] +fn test_vec_merge() { + let mut vec1 = vec![1, 2, 3]; + let vec2 = vec![4, 5, 6]; + + vec1.merge(vec2); + + assert_eq!(vec1, vec![1, 2, 3, 4, 5, 6]); +} + +#[test] +fn test_tuple_merge() { + let mut tuple1 = (vec![1, 2], BTreeSet::from([3])); + let tuple2 = (vec![3, 4], BTreeSet::from([4])); + + tuple1.merge(tuple2); + + let expected_vec = vec![1, 2, 3, 4]; + assert_eq!(tuple1.0, expected_vec); + let expected_set: BTreeSet = BTreeSet::from([3, 4]); + assert_eq!(tuple1.1, expected_set); +} + +#[test] +fn test_is_empty() { + let map: BTreeMap = BTreeMap::new(); + assert!(Merge::is_empty(&map)); + + let set: BTreeSet = BTreeSet::new(); + assert!(Merge::is_empty(&set)); + + let vec: Vec = Vec::new(); + assert!(Merge::is_empty(&vec)); +} +#[test] +fn test_take() { + let mut map: BTreeMap = BTreeMap::new(); + map.insert(1, 1); + let taken_map = Merge::take(&mut map); + assert_eq!(taken_map, Some(BTreeMap::from([(1, 1)]))); + assert!(map.is_empty()); + + let mut set: BTreeSet = BTreeSet::new(); + set.insert(1); + let taken_set = Merge::take(&mut set); + assert_eq!(taken_set, Some(BTreeSet::from([1]))); + assert!(set.is_empty()); + + let mut vec: Vec = vec![1]; + let taken_vec = Merge::take(&mut vec); + assert_eq!(taken_vec, Some(vec![1])); + assert!(vec.is_empty()); +} + +#[test] +fn test_btree_map_merge_conflict() { + let mut map1: BTreeMap = BTreeMap::new(); + map1.insert(1, "a"); + let mut map2: BTreeMap = BTreeMap::new(); + map2.insert(1, "b"); + + map1.merge(map2); + + let expected: BTreeMap = BTreeMap::from([(1, "b")]); + assert_eq!(map1, expected); +} + +#[test] +fn test_btree_set_merge_conflict() { + let mut set1: BTreeSet = BTreeSet::new(); + set1.insert(1); + let mut set2: BTreeSet = BTreeSet::new(); + set2.insert(1); + + set1.merge(set2); + + let expected: BTreeSet = BTreeSet::from([1]); + assert_eq!(set1, expected); +} + +#[test] +fn test_vec_merge_duplicates() { + let mut vec1 = vec![1, 2, 3]; + let vec2 = vec![3, 4, 5]; + + vec1.merge(vec2); + + assert_eq!(vec1, vec![1, 2, 3, 3, 4, 5]); +}