//! Most of the code here was taken directly from the `intmap` crate because I cannot be h\*cked to //! write tests. //! //! Thus, here is the original license: //! //! Copyright (c) 2016 Jesper Axelsson //! Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: //! The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // ! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #[cfg(test)] mod tests { use inttable::{Entry, IntTable}; fn get_random_range(count: usize) -> Vec { use rand::prelude::StdRng; use rand::{Rng, SeedableRng}; let mut vec = Vec::new(); let mut rng = StdRng::seed_from_u64(4242); for _ in 0..count { vec.push(rng.gen::()); } vec.sort(); vec.dedup(); vec } #[test] fn intmap_get_insert_impl() { let count = 20_000; let data = get_random_range(count); let mut map: IntTable = IntTable::new(); println!(); println!("Starting test"); for s in data.iter() { assert!( map.try_insert(*s, *s).is_ok(), "intmap insert failed! ix: {:?}", s ); } assert_eq!(map.len(), count); for s in data.iter() { assert_eq!(*map.get(*s).unwrap(), *s, "intmap get failed! key: {:?}", s); } assert_eq!(map.len(), count); for s in data.iter() { assert!( map.contains_key(*s), "intmap contains_key failed! key: {:?}", s ); } assert_eq!(map.len(), count); for s in data.iter() { let val = map.remove(*s).unwrap(); assert_eq!(val, *s, "intmap remove failed! key: {:?}", s); } assert_eq!(map.len(), 0); } #[test] fn reserve() { let mut map: IntTable = IntTable::new(); map.reserve(9001); } #[test] fn add_duplicate() { let mut map = IntTable::new(); for i in 0..20_000 { assert_eq!(map.insert(i, format!("item: {:?}", i)), None); assert_eq!( map.insert(i, format!("item: {:?}", i)), Some(format!("item: {:?}", i)) ); } } #[test] fn add_duplicate_replace() { let mut map = IntTable::new(); for i in 0..20_000 { assert!(map.try_insert(i, format!("item: {:?}", i)).is_ok()); assert!(map.try_insert(i, format!("item: {:?}", i)).is_err()); } } #[test] fn get_value_map() { let mut map = IntTable::new(); for i in 0..20_000 { assert!(map.try_insert(i, i + 1).is_ok()); } for i in 0..20_000 { assert!(map.contains_key(i)); assert_eq!(*map.get(i).unwrap(), i + 1); assert_eq!(*map.get_mut(i).unwrap(), i + 1); assert_eq!(map.remove(i).unwrap(), i + 1); } for i in 0..20_000 { assert!(!map.contains_key(i)); assert_eq!(map.remove(i), None); } assert!(map.is_empty()); } #[test] fn get_value_not_in_map() { let mut map = IntTable::new(); for i in 0..20_000 { map.try_insert(i, format!("item: {:?}", i)).unwrap(); } for i in 20_000..40_000 { assert_eq!(map.get(i), None); assert_eq!(map.get_mut(i), None); } } #[test] fn add_string() { let mut map = IntTable::new(); for i in 0..20_000 { map.insert(i, format!("item: {:?}", i)); } } #[test] fn retain() { let mut map = IntTable::new(); for i in 0..20_000 { map.insert(i, format!("item: {:?}", i)); } map.retain(|k, _v| *k > 10_000); for i in 10_001..20_000 { assert!(map.contains_key(i)); } } #[test] fn single_add_get() { let mut map: IntTable = IntTable::new(); map.insert(21, 42); let val = map.get(21); assert!(val.is_some()); assert_eq!(*val.unwrap(), 42); } #[test] fn map_iter() { let count = 20_000; let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for (k, v) in map.iter() { assert_eq!(*k, *v); } } #[test] fn map_iter_keys() { let count = 20_000; let data: Vec<_> = (0..count).collect(); let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for k in map.keys() { assert_eq!(*k, data[*k as usize]); } } #[test] fn map_iter_values() { let count = 20_000; let data: Vec<_> = (0..count).collect(); let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for v in map.values() { assert_eq!(*v, data[*v as usize]); } } #[test] fn map_iter_values_mut() { let count = 20_000; let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for value in map.values_mut() { *value += 1; } for n in 0..count { assert_eq!(n + 1, *map.get(n).expect("Failed to get number!")); } } #[test] fn map_mut_iter() { let count = 20_000; let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for kv in map.iter_mut() { *kv.1 += 1; } for n in 0..count { assert_eq!(n + 1, *map.get(n).expect("Failed to get number!")); } } #[test] fn map_iter_empty() { let mut map: IntTable = IntTable::new(); map.clear(); let mut map_iter = map.iter(); if let Some(kv) = map_iter.next() { panic!("Not printing: {:?}", kv); } } #[test] fn map_mut_iter_empty() { let mut map: IntTable = IntTable::new(); map.clear(); let mut map_iter = map.iter_mut(); if let Some(kv) = map_iter.next() { panic!("Not printing: {:?}", kv); } } #[test] fn map_into_iter() { let count = 20_000; let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for (k, v) in map.into_iter() { assert_eq!(k, v); } } #[test] fn map_drain() { let count = 20_000; let mut map: IntTable = IntTable::new(); for i in 0..count { map.insert(i, i); } for (k, v) in map.drain() { assert_eq!(k, v); } assert_eq!(map.len(), 0); } #[test] fn map_into_iter_empty() { let mut map: IntTable = IntTable::new(); map.clear(); if let Some(kv) = map.into_iter().next() { panic!("Not printing: {:?}", kv); } } #[test] fn extend_two_maps() { let count = 20_000; let mut map_1: IntTable = IntTable::new(); let mut map_2: IntTable = IntTable::new(); for i in 0..count { map_1.insert(i, i); } for i in count..(count * 2) { map_2.insert(i, i); } map_1.extend(map_2); assert_eq!(map_1.len(), (count * 2) as usize); for (k, v) in map_1.iter() { assert_eq!(k, v); } } #[test] fn from_iter_collect() { let count = 20_000; let map = (0..count).map(|i| (i, i * i)).collect::>(); for k in 0..count { assert!(map.contains_key(k)); } for (&k, &v) in map.iter() { assert_eq!(k * k, v); } } #[test] fn map_equality() { let count = 5_000; let map_1 = (0..count).map(|i| (i, i * i)).collect::>(); let map_2 = (0..count) .rev() .map(|i| (i, i * i)) .collect::>(); assert_eq!(map_1, map_2); } #[test] fn map_inequality() { let map_1 = (0..10).map(|i| (i, i * i)).collect::>(); let map_2 = (0..5).rev().map(|i| (i, i * i)).collect::>(); assert_ne!(map_1, map_2); assert_ne!(map_2, map_1); } #[test] fn entry_api() { let count = 20_000; let data: Vec = (0..count).collect(); let mut map: IntTable = IntTable::new(); // Insert values 0..19999 for i in 0..count { match map.entry(i) { Entry::Occupied(_) => panic!("unexpected while insert, i = {}", i), Entry::Vacant(entry) => entry.insert(i), }; } assert_eq!(map.len(), count as usize); for (k, v) in map.iter() { assert_eq!(*v, data[*k as usize]); } // Replace values 0..19999 with 20000..39999 for i in 0..count { match map.entry(i) { Entry::Occupied(mut entry) => { assert_eq!(*entry.get(), i); assert_eq!(*entry.get_mut(), i); assert_eq!(entry.insert(count + i), i); assert_eq!(*entry.into_mut(), count + i); } Entry::Vacant(_) => panic!("unexpected while replace, i = {}", i), }; } assert_eq!(map.len(), count as usize); for (k, v) in map.iter() { assert_eq!(*v, count + data[*k as usize]); } // Remove values 20000..39999 for i in 0..count { match map.entry(i) { Entry::Occupied(entry) => { assert_eq!(entry.remove(), count + i); } Entry::Vacant(_) => panic!("unexpected while remove, i = {}", i), }; } assert_eq!(map.len(), 0); } }