|
| 1 | +use borsh::BorshDeserialize; |
| 2 | +use indexmap::{IndexMap, IndexSet}; |
| 3 | + |
| 4 | +#[test] |
| 5 | +// Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L100 |
| 6 | +// license: MIT OR Apache-2.0 |
| 7 | +fn test_indexmap_roundtrip() { |
| 8 | + let original_map: IndexMap<i32, i32> = { |
| 9 | + let mut map = IndexMap::new(); |
| 10 | + map.insert(1, 2); |
| 11 | + map.insert(3, 4); |
| 12 | + map.insert(5, 6); |
| 13 | + map |
| 14 | + }; |
| 15 | + let serialized_map = borsh::to_vec(&original_map).unwrap(); |
| 16 | + #[cfg(feature = "std")] |
| 17 | + insta::assert_debug_snapshot!(serialized_map); |
| 18 | + |
| 19 | + let deserialized_map: IndexMap<i32, i32> = |
| 20 | + BorshDeserialize::try_from_slice(&serialized_map).unwrap(); |
| 21 | + assert_eq!(original_map, deserialized_map); |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +// Taken from https://github.com/indexmap-rs/indexmap/blob/dd06e5773e4f91748396c67d00c83637f5c0dd49/src/borsh.rs#L115 |
| 26 | +// license: MIT OR Apache-2.0 |
| 27 | +fn test_indexset_roundtrip() { |
| 28 | + let mut original_set = IndexSet::new(); |
| 29 | + [1, 2, 3, 4, 5, 6].iter().for_each(|&i| { |
| 30 | + original_set.insert(i); |
| 31 | + }); |
| 32 | + |
| 33 | + let serialized_set = borsh::to_vec(&original_set).unwrap(); |
| 34 | + |
| 35 | + #[cfg(feature = "std")] |
| 36 | + insta::assert_debug_snapshot!(serialized_set); |
| 37 | + |
| 38 | + let deserialized_set: IndexSet<i32> = |
| 39 | + BorshDeserialize::try_from_slice(&serialized_set).unwrap(); |
| 40 | + assert_eq!(original_set, deserialized_set); |
| 41 | +} |
0 commit comments