Skip to content

Commit 623aa34

Browse files
committed
Fix issue hroi#13 also for IpLookupTable::{matches, matches_mut}
1 parent 0a16733 commit 623aa34

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/tree_bitmap/mod.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<T: Sized> TreeBitmap<T> {
121121
let match_mask = node::MATCH_MASKS[*nibble as usize];
122122

123123
if let MatchResult::Match(result_hdl, result_index, matching_bit_index) =
124-
cur_node.match_internal(match_mask)
124+
cur_node.match_internal(match_mask)
125125
{
126126
let bits_matched = bits_searched + node::BIT_MATCH[matching_bit_index as usize];
127127
best_match = Some((result_hdl, result_index, bits_matched));
@@ -159,9 +159,10 @@ impl<T: Sized> TreeBitmap<T> {
159159
/// longest match lookup of ```nibbles```. Returns bits matched as u32, and mutable reference to T.
160160
pub fn longest_match_mut(&mut self, nibbles: &[u8]) -> Option<(u32, &mut T)> {
161161
match self.longest_match_internal(&nibbles) {
162-
Some((result_hdl, result_index, bits_matched)) => {
163-
Some((bits_matched, self.results.get_mut(&result_hdl, result_index)))
164-
}
162+
Some((result_hdl, result_index, bits_matched)) => Some((
163+
bits_matched,
164+
self.results.get_mut(&result_hdl, result_index),
165+
)),
165166
None => None,
166167
}
167168
}
@@ -173,7 +174,15 @@ impl<T: Sized> TreeBitmap<T> {
173174
let mut bits_searched = 0;
174175
let mut matches = Vec::new();
175176

176-
for nibble in nibbles {
177+
let mut loop_count = 0;
178+
loop {
179+
let nibble = if loop_count < nibbles.len() {
180+
nibbles[loop_count]
181+
} else {
182+
0
183+
};
184+
loop_count += 1;
185+
let nibble = &nibble;
177186
let cur_node = *self.trienodes.get(&cur_hdl, cur_index);
178187

179188
for i in 0..5 {
@@ -668,12 +677,16 @@ mod tests {
668677
let (nibbles_b, mask_b) = (&[0, 10, 0, 10, 0, 10, 0, 0], 24);
669678
tbm.insert(nibbles_a, mask_a, "foo");
670679
tbm.insert(nibbles_b, mask_b, "bar");
680+
671681
{
672-
let matches = tbm.matches(nibbles_b);
682+
let mut matches = tbm.matches(nibbles_b);
673683
assert_eq!(
674684
true,
675-
matches.contains(&(mask_a, &"foo")) && matches.contains(&(mask_b, &"bar"))
685+
matches.any(|p| p == (mask_a, &"foo")) && matches.any(|p| p == (mask_b, &"bar"))
676686
);
687+
}
688+
689+
{
677690
let value = tbm.remove(nibbles_b, mask_b);
678691
assert_eq!(value, Some("bar"));
679692
let lookup_result = tbm.longest_match(nibbles_b);

tests/tests.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,28 @@ fn longest_match() {
102102
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 0, 0), 10, &100004)));
103103

104104
let result = tbm.longest_match_mut(Ipv4Addr::new(100, 100, 100, 100));
105-
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 0, 0), 10, &mut 100004)));
105+
assert_eq!(
106+
result,
107+
Some((Ipv4Addr::new(100, 64, 0, 0), 10, &mut 100004))
108+
);
106109

107110
let result = tbm.longest_match(Ipv4Addr::new(100, 64, 0, 100));
108111
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 0, 0), 24, &10064024)));
109112

110113
let result = tbm.longest_match_mut(Ipv4Addr::new(100, 64, 0, 100));
111-
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 0, 0), 24, &mut 10064024)));
114+
assert_eq!(
115+
result,
116+
Some((Ipv4Addr::new(100, 64, 0, 0), 24, &mut 10064024))
117+
);
112118

113119
let result = tbm.longest_match(Ipv4Addr::new(100, 64, 1, 100));
114120
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 1, 0), 24, &10064124)));
115121

116122
let result = tbm.longest_match_mut(Ipv4Addr::new(100, 64, 1, 100));
117-
assert_eq!(result, Some((Ipv4Addr::new(100, 64, 1, 0), 24, &mut 10064124)));
123+
assert_eq!(
124+
result,
125+
Some((Ipv4Addr::new(100, 64, 1, 0), 24, &mut 10064124))
126+
);
118127

119128
let result = tbm.longest_match(Ipv4Addr::new(200, 200, 200, 200));
120129
assert_eq!(result, None);
@@ -123,14 +132,6 @@ fn longest_match() {
123132
assert_eq!(result, None);
124133
}
125134

126-
#[test]
127-
fn matches() {
128-
let mut tbm = IpLookupTable::new();
129-
tbm.insert(Ipv4Addr::new(10, 0, 0, 0), 8, 1);
130-
tbm.insert(Ipv4Addr::new(10, 1, 0, 0), 16, 2);
131-
assert_eq!(2, tbm.matches(Ipv4Addr::new(10, 1, 0, 30)).count());
132-
}
133-
134135
#[test]
135136
fn iter() {
136137
let mut tbl = IpLookupTable::new();
@@ -191,10 +192,17 @@ fn into_iter() {
191192

192193
#[test]
193194
fn send() {
194-
fn check_if_send<T: Send>() { }
195+
fn check_if_send<T: Send>() {}
195196
check_if_send::<IpLookupTable<Ipv4Addr, ()>>();
196197
}
197198

199+
#[test]
200+
fn matches() {
201+
let mut tbm = IpLookupTable::new();
202+
tbm.insert(Ipv4Addr::new(10, 0, 0, 0), 8, 1);
203+
tbm.insert(Ipv4Addr::new(10, 1, 0, 0), 16, 2);
204+
assert_eq!(2, tbm.matches(Ipv4Addr::new(10, 1, 0, 30)).count());
205+
}
198206

199207
#[test]
200208
fn matches_all_zeros() {
@@ -242,7 +250,10 @@ fn matches_ipv6() {
242250
table.insert(more_specific, 48, "bar");
243251
assert_eq!(table.matches(less_specific).count(), 1);
244252
assert_eq!(table.matches(more_specific).count(), 2);
245-
assert_eq!(table.matches(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).count(), 0);
253+
assert_eq!(
254+
table.matches(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).count(),
255+
0
256+
);
246257
}
247258

248259
// https://github.com/hroi/treebitmap/issues/7
@@ -270,19 +281,21 @@ fn issue_13() {
270281
let mut table = IpLookupTable::new();
271282

272283
println!("insert 28");
273-
table.insert(Ipv4Addr::new(49, 255, 11, 16), 28, ());
284+
table.insert(Ipv4Addr::new(49, 255, 11, 16), 28, 28);
274285
assert_eq!(
275286
table.exact_match(Ipv4Addr::new(49, 255, 11, 16), 28),
276-
Some(&())
287+
Some(&28)
277288
);
278289
println!("insert 32");
279-
table.insert(ADDR, 32, ());
290+
table.insert(ADDR, 32, 32);
280291

281292
println!("match 32");
282-
assert_eq!(table.exact_match(ADDR, 32), Some(&()));
293+
assert_eq!(table.exact_match(ADDR, 32), Some(&32));
283294
assert!(table.longest_match(ADDR).is_some());
284295
assert!(table.longest_match_mut(ADDR).is_some());
285296

297+
assert_eq!(table.matches(ADDR).count(), 2);
298+
286299
let v = table.remove(ADDR, 32);
287300
println!("removed: {:?}", v);
288301
}

0 commit comments

Comments
 (0)