check_get_map.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 fn check_get_unique_maps(map: &impl for<'a> NestedMap<'a, usize, usize, String>) { 23 ensure_map_is_empty(map); 24 25 for i in 0..NUM_ITEMS { 26 println!("insert({i})"); 27 28 assert_eq!(map.get_map_confirmed(&i).unwrap(), Vec::new()); 29 assert_eq!(map.get_map_speculative(&i).unwrap(), Vec::new()); 30 31 // Insert an item into the map. 32 map.insert(i, i, i.to_string()).unwrap(); 33 34 assert_eq!(map.get_map_confirmed(&i).unwrap(), vec![(i, i.to_string())]); 35 assert_eq!(map.get_map_speculative(&i).unwrap(), vec![(i, i.to_string())]); 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!("insert({i}) [atomic]"); 46 47 assert_eq!(map.get_map_confirmed(&i).unwrap(), Vec::new()); 48 assert_eq!(map.get_map_speculative(&i).unwrap(), Vec::new()); 49 50 // Insert an item into the map. 51 map.insert(i, i, i.to_string()).unwrap(); 52 53 assert_eq!(map.get_map_confirmed(&i).unwrap(), Vec::new()); 54 assert_eq!(map.get_map_speculative(&i).unwrap(), vec![(i, i.to_string())]); 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 println!("remove_map({i})"); 63 64 map.remove_map(&i).unwrap(); 65 } 66 67 ensure_map_is_empty(map); 68 } 69 70 fn check_get_same_map(map: &impl for<'a> NestedMap<'a, usize, usize, String>) { 71 ensure_map_is_empty(map); 72 73 const MAP: usize = 0; 74 75 let mut confirmed = Vec::new(); 76 let mut speculative = Vec::new(); 77 78 for i in 0..NUM_ITEMS { 79 println!("i: {i}"); 80 81 assert_eq!(map.get_map_confirmed(&MAP).unwrap(), confirmed); 82 assert_eq!(map.get_map_speculative(&MAP).unwrap(), speculative); 83 84 // Insert an item into the map. 85 map.insert(MAP, i, i.to_string()).unwrap(); 86 confirmed.push((i, i.to_string())); 87 speculative.push((i, i.to_string())); 88 89 assert_eq!(map.get_map_confirmed(&MAP).unwrap(), confirmed); 90 assert_eq!(map.get_map_speculative(&MAP).unwrap(), speculative); 91 } 92 93 /* test atomic insertions */ 94 95 { 96 // Start an atomic write batch. 97 map.start_atomic(); 98 99 for i in NUM_ITEMS..NUM_TOTAL_ITEMS { 100 println!("i: {i}"); 101 102 assert_eq!(map.get_map_confirmed(&MAP).unwrap(), confirmed); 103 assert_eq!(map.get_map_speculative(&MAP).unwrap(), speculative); 104 105 // Insert an item into the map. 106 map.insert(MAP, i, i.to_string()).unwrap(); 107 speculative.push((i, i.to_string())); 108 109 assert_eq!(map.get_map_confirmed(&MAP).unwrap(), confirmed); 110 assert_eq!(map.get_map_speculative(&MAP).unwrap(), speculative); 111 } 112 113 // Finish the current atomic write batch. 114 map.finish_atomic().unwrap(); 115 116 assert_eq!(map.get_map_confirmed(&MAP).unwrap(), speculative); 117 assert_eq!(map.get_map_speculative(&MAP).unwrap(), speculative); 118 } 119 120 map.remove_map(&MAP).unwrap(); 121 122 ensure_map_is_empty(map); 123 } 124 125 pub fn check_get_map(map: impl for<'a> NestedMap<'a, usize, usize, String>) { 126 println!("Checking get unique maps"); 127 check_get_unique_maps(&map); 128 129 println!("Checking get same map"); 130 check_get_same_map(&map); 131 }