/ ledger / store / src / helpers / test_helpers / nested_map / check_contains_key.rs
check_contains_key.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 super::ensure_map_is_empty;
17  use crate::helpers::NestedMap;
18  
19  const NUM_ITEMS: usize = 10;
20  const NUM_TOTAL_ITEMS: usize = 20;
21  
22  pub fn check_contains_key(map: impl for<'a> NestedMap<'a, usize, usize, String>) {
23      ensure_map_is_empty(&map);
24  
25      const MAP: usize = 0;
26  
27      for i in 0..NUM_ITEMS {
28          println!("i: {i}");
29  
30          assert!(!map.contains_key_confirmed(&MAP, &i).unwrap());
31          assert!(!map.contains_key_speculative(&MAP, &i).unwrap());
32  
33          // Insert an item into the map.
34          map.insert(MAP, i, i.to_string()).unwrap();
35  
36          assert!(map.contains_key_confirmed(&MAP, &i).unwrap());
37          assert!(map.contains_key_speculative(&MAP, &i).unwrap());
38      }
39  
40      /* test atomic insertions */
41  
42      {
43          // Start an atomic write batch.
44          map.start_atomic();
45  
46          for i in NUM_ITEMS..NUM_TOTAL_ITEMS {
47              println!("i: {i}");
48  
49              assert!(!map.contains_key_confirmed(&MAP, &i).unwrap());
50              assert!(!map.contains_key_speculative(&MAP, &i).unwrap());
51  
52              // Insert an item into the map.
53              map.insert(MAP, i, i.to_string()).unwrap();
54  
55              assert!(!map.contains_key_confirmed(&MAP, &i).unwrap());
56              assert!(map.contains_key_speculative(&MAP, &i).unwrap());
57          }
58  
59          // Finish the current atomic write batch.
60          map.finish_atomic().unwrap();
61      }
62  
63      for i in 0..NUM_TOTAL_ITEMS {
64          assert!(map.contains_key_confirmed(&MAP, &i).unwrap());
65          assert!(map.contains_key_speculative(&MAP, &i).unwrap());
66  
67          // Remove the item from the map.
68          map.remove_key(&MAP, &i).unwrap();
69  
70          assert!(!map.contains_key_confirmed(&MAP, &i).unwrap());
71          assert!(!map.contains_key_speculative(&MAP, &i).unwrap());
72      }
73  
74      ensure_map_is_empty(&map);
75  }