- #[test]
- fn test_entry() {
- let mut map: AnyMap = AnyMap::new();
- assert_eq!(map.insert(A(10)), None);
- assert_eq!(map.insert(B(20)), None);
- assert_eq!(map.insert(C(30)), None);
- assert_eq!(map.insert(D(40)), None);
- assert_eq!(map.insert(E(50)), None);
- assert_eq!(map.insert(F(60)), None);
-
- // Existing key (insert)
- match map.entry::<A>() {
- Entry::Vacant(_) => unreachable!(),
- Entry::Occupied(mut view) => {
- assert_eq!(view.get(), &A(10));
- assert_eq!(view.insert(A(100)), A(10));
- }
- }
- assert_eq!(map.get::<A>().unwrap(), &A(100));
- assert_eq!(map.len(), 6);
+ macro_rules! test_entry {
+ ($name:ident, $init:ty) => {
+ #[test]
+ fn $name() {
+ let mut map = <$init>::new();
+ assert_eq!(map.insert(A(10)), None);
+ assert_eq!(map.insert(B(20)), None);
+ assert_eq!(map.insert(C(30)), None);
+ assert_eq!(map.insert(D(40)), None);
+ assert_eq!(map.insert(E(50)), None);
+ assert_eq!(map.insert(F(60)), None);
+
+ // Existing key (insert)
+ match map.entry::<A>() {
+ Entry::Vacant(_) => unreachable!(),
+ Entry::Occupied(mut view) => {
+ assert_eq!(view.get(), &A(10));
+ assert_eq!(view.insert(A(100)), A(10));
+ }
+ }
+ assert_eq!(map.get::<A>().unwrap(), &A(100));
+ assert_eq!(map.len(), 6);
+
+
+ // Existing key (update)
+ match map.entry::<B>() {
+ Entry::Vacant(_) => unreachable!(),
+ Entry::Occupied(mut view) => {
+ let v = view.get_mut();
+ let new_v = B(v.0 * 10);
+ *v = new_v;
+ }
+ }
+ assert_eq!(map.get::<B>().unwrap(), &B(200));
+ assert_eq!(map.len(), 6);