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::Map; 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> Map<'a, usize, String>) { 23 ensure_map_is_empty(&map); 24 25 for i in 0..NUM_ITEMS { 26 println!("i: {i}"); 27 28 assert!(!map.contains_key_confirmed(&i).unwrap()); 29 assert!(!map.contains_key_speculative(&i).unwrap()); 30 31 // Insert an item into the map. 32 map.insert(i, i.to_string()).unwrap(); 33 34 assert!(map.contains_key_confirmed(&i).unwrap()); 35 assert!(map.contains_key_speculative(&i).unwrap()); 36 } 37 38 /* test atomic insertions */ 39 40 { 41 // Start an atomic write batch. 42 map.start_atomic(); 43 44 for i in NUM_ITEMS..NUM_TOTAL_ITEMS { 45 println!("i: {i}"); 46 47 assert!(!map.contains_key_confirmed(&i).unwrap()); 48 assert!(!map.contains_key_speculative(&i).unwrap()); 49 50 // Insert an item into the map. 51 map.insert(i, i.to_string()).unwrap(); 52 53 assert!(!map.contains_key_confirmed(&i).unwrap()); 54 assert!(map.contains_key_speculative(&i).unwrap()); 55 } 56 57 // Finish the current atomic write batch. 58 map.finish_atomic().unwrap(); 59 } 60 61 for i in 0..NUM_TOTAL_ITEMS { 62 assert!(map.contains_key_confirmed(&i).unwrap()); 63 assert!(map.contains_key_speculative(&i).unwrap()); 64 65 // Remove the item from the map. 66 map.remove(&i).unwrap(); 67 68 assert!(!map.contains_key_confirmed(&i).unwrap()); 69 assert!(!map.contains_key_speculative(&i).unwrap()); 70 } 71 72 ensure_map_is_empty(&map); 73 }