/ ledger / store / src / helpers / test_helpers / map / check_remove_and_get_speculative.rs
check_remove_and_get_speculative.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the deltavm library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  use crate::helpers::Map;
17  
18  use std::borrow::Cow;
19  
20  pub fn check_remove_and_get_speculative(map: impl for<'a> Map<'a, usize, String>) {
21      // Sanity check.
22      assert!(map.iter_confirmed().next().is_none());
23  
24      // Insert an item into the map.
25      map.insert(0, "0".to_string()).unwrap();
26  
27      // Check that the item is present in the map .
28      assert_eq!(map.get_confirmed(&0).unwrap(), Some(Cow::Owned("0".to_string())));
29      // Check that the item is not in the batch.
30      assert_eq!(map.get_pending(&0), None);
31      // Check that the item can be speculatively retrieved.
32      assert_eq!(map.get_speculative(&0).unwrap(), Some(Cow::Owned("0".to_string())));
33  
34      /* test atomic removals */
35  
36      // Start an atomic write batch.
37      map.start_atomic();
38  
39      // Remove the item from the map.
40      map.remove(&0).unwrap();
41  
42      // Check that the item still exists in the map.
43      assert_eq!(map.get_confirmed(&0).unwrap(), Some(Cow::Owned("0".to_string())));
44      // Check that the item is removed in the batch.
45      assert_eq!(map.get_pending(&0), Some(None));
46      // Check that the item is removed when speculatively retrieved.
47      assert_eq!(map.get_speculative(&0).unwrap(), None);
48  
49      // Try removing the item again.
50      map.remove(&0).unwrap();
51  
52      // Check that the item still exists in the map.
53      assert_eq!(map.get_confirmed(&0).unwrap(), Some(Cow::Owned("0".to_string())));
54      // Check that the item is removed in the batch.
55      assert_eq!(map.get_pending(&0), Some(None));
56      // Check that the item is removed when speculatively retrieved.
57      assert_eq!(map.get_speculative(&0).unwrap(), None);
58  
59      // Finish the current atomic write batch.
60      map.finish_atomic().unwrap();
61  
62      // Check that the item is not present in the map now.
63      assert!(map.get_confirmed(&0).unwrap().is_none());
64      // Check that the item is not in the batch.
65      assert_eq!(map.get_pending(&0), None);
66      // Check that the item is removed when speculatively retrieved.
67      assert_eq!(map.get_speculative(&0).unwrap(), None);
68  
69      // Check that the map is empty now.
70      assert!(map.iter_confirmed().next().is_none());
71  }