Expose send_payment_for_bolt12_invoice
[rust-lightning] / fuzz / src / indexedmap.rs
index 795d6175bb5dc2dd8aee2b0cdce76ccc6a95c12b..ee21f04619fa3051540c495e06c4b265d80e0952 100644 (file)
@@ -7,20 +7,41 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
-use lightning::util::indexed_map::{IndexedMap, self};
-use std::collections::{BTreeMap, btree_map};
-use hashbrown::HashSet;
+use lightning::util::hash_tables::*;
+use lightning::util::indexed_map::{self, IndexedMap};
+use std::collections::{btree_map, BTreeMap};
 
 use crate::utils::test_logger;
 
-fn check_eq(btree: &BTreeMap<u8, u8>, indexed: &IndexedMap<u8, u8>) {
+use std::ops::{Bound, RangeBounds};
+
+struct ExclLowerInclUpper(u8, u8);
+impl RangeBounds<u8> for ExclLowerInclUpper {
+       fn start_bound(&self) -> Bound<&u8> {
+               Bound::Excluded(&self.0)
+       }
+       fn end_bound(&self) -> Bound<&u8> {
+               Bound::Included(&self.1)
+       }
+}
+struct ExclLowerExclUpper(u8, u8);
+impl RangeBounds<u8> for ExclLowerExclUpper {
+       fn start_bound(&self) -> Bound<&u8> {
+               Bound::Excluded(&self.0)
+       }
+       fn end_bound(&self) -> Bound<&u8> {
+               Bound::Excluded(&self.1)
+       }
+}
+
+fn check_eq(btree: &BTreeMap<u8, u8>, mut indexed: IndexedMap<u8, u8>) {
        assert_eq!(btree.len(), indexed.len());
        assert_eq!(btree.is_empty(), indexed.is_empty());
 
        let mut btree_clone = btree.clone();
        assert!(btree_clone == *btree);
        let mut indexed_clone = indexed.clone();
-       assert!(indexed_clone == *indexed);
+       assert!(indexed_clone == indexed);
 
        for k in 0..=255 {
                assert_eq!(btree.contains_key(&k), indexed.contains_key(&k));
@@ -33,46 +54,65 @@ fn check_eq(btree: &BTreeMap<u8, u8>, indexed: &IndexedMap<u8, u8>) {
                                if let indexed_map::Entry::Occupied(mut io) = indexed_entry {
                                        assert_eq!(bo.get(), io.get());
                                        assert_eq!(bo.get_mut(), io.get_mut());
-                               } else { panic!(); }
+                               } else {
+                                       panic!();
+                               }
                        },
                        btree_map::Entry::Vacant(_) => {
                                if let indexed_map::Entry::Vacant(_) = indexed_entry {
-                               } else { panic!(); }
-                       }
+                               } else {
+                                       panic!();
+                               }
+                       },
                }
        }
 
        const STRIDE: u8 = 16;
-       for k in 0..=255/STRIDE {
-               let lower_bound = k * STRIDE;
-               let upper_bound = lower_bound + (STRIDE - 1);
-               let mut btree_iter = btree.range(lower_bound..=upper_bound);
-               let mut indexed_iter = indexed.range(lower_bound..=upper_bound);
-               loop {
-                       let b_v = btree_iter.next();
-                       let i_v = indexed_iter.next();
-                       assert_eq!(b_v, i_v);
-                       if b_v.is_none() { break; }
+       for range_type in 0..4 {
+               for k in 0..=255 / STRIDE {
+                       let lower_bound = k * STRIDE;
+                       let upper_bound = lower_bound + (STRIDE - 1);
+                       macro_rules! range {
+                               ($map: expr) => {
+                                       match range_type {
+                                               0 => $map.range(lower_bound..upper_bound),
+                                               1 => $map.range(lower_bound..=upper_bound),
+                                               2 => $map.range(ExclLowerInclUpper(lower_bound, upper_bound)),
+                                               3 => $map.range(ExclLowerExclUpper(lower_bound, upper_bound)),
+                                               _ => unreachable!(),
+                                       }
+                               };
+                       }
+                       let mut btree_iter = range!(btree);
+                       let mut indexed_iter = range!(indexed);
+                       loop {
+                               let b_v = btree_iter.next();
+                               let i_v = indexed_iter.next();
+                               assert_eq!(b_v, i_v);
+                               if b_v.is_none() {
+                                       break;
+                               }
+                       }
                }
        }
 
-       let mut key_set = HashSet::with_capacity(256);
+       let mut key_set = hash_map_with_capacity(1024);
        for k in indexed.unordered_keys() {
-               assert!(key_set.insert(*k));
+               assert!(key_set.insert(*k, ()).is_none());
                assert!(btree.contains_key(k));
        }
        assert_eq!(key_set.len(), btree.len());
 
        key_set.clear();
        for (k, v) in indexed.unordered_iter() {
-               assert!(key_set.insert(*k));
+               assert!(key_set.insert(*k, ()).is_none());
                assert_eq!(btree.get(k).unwrap(), v);
        }
        assert_eq!(key_set.len(), btree.len());
 
        key_set.clear();
        for (k, v) in indexed_clone.unordered_iter_mut() {
-               assert!(key_set.insert(*k));
+               assert!(key_set.insert(*k, ()).is_none());
                assert_eq!(btree.get(k).unwrap(), v);
        }
        assert_eq!(key_set.len(), btree.len());
@@ -80,7 +120,9 @@ fn check_eq(btree: &BTreeMap<u8, u8>, indexed: &IndexedMap<u8, u8>) {
 
 #[inline]
 pub fn do_test(data: &[u8]) {
-       if data.len() % 2 != 0 { return; }
+       if data.len() % 2 != 0 {
+               return;
+       }
        let mut btree = BTreeMap::new();
        let mut indexed = IndexedMap::new();
 
@@ -91,7 +133,7 @@ pub fn do_test(data: &[u8]) {
                let prev_value_i = indexed.insert(tuple[0], tuple[1]);
                assert_eq!(prev_value_b, prev_value_i);
        }
-       check_eq(&btree, &indexed);
+       check_eq(&btree, indexed.clone());
 
        // Now, modify the maps in all the ways we have to do so, checking that the maps remain
        // equivalent as we go.
@@ -99,7 +141,7 @@ pub fn do_test(data: &[u8]) {
                *v = *k;
                *btree.get_mut(k).unwrap() = *k;
        }
-       check_eq(&btree, &indexed);
+       check_eq(&btree, indexed.clone());
 
        for k in 0..=255 {
                match btree.entry(k) {
@@ -114,17 +156,21 @@ pub fn do_test(data: &[u8]) {
                                        } else {
                                                assert_eq!(bo.remove_entry(), io.remove_entry());
                                        }
-                               } else { panic!(); }
+                               } else {
+                                       panic!();
+                               }
                        },
                        btree_map::Entry::Vacant(bv) => {
                                if let indexed_map::Entry::Vacant(iv) = indexed.entry(k) {
                                        bv.insert(k);
                                        iv.insert(k);
-                               } else { panic!(); }
+                               } else {
+                                       panic!();
+                               }
                        },
                }
        }
-       check_eq(&btree, &indexed);
+       check_eq(&btree, indexed);
 }
 
 pub fn indexedmap_test<Out: test_logger::Output>(data: &[u8], _out: Out) {