prefix-trie 0.9.4

Prefix trie (tree) datastructure (both a set and a map) that provides exact and longest-prefix matches.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#[generic_tests::define]
mod t {
    use super::super::*;

    #[test]
    fn set_aggregate_merges_siblings<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::from_iter([ip("10.0.0.0/24"), ip("10.0.1.0/24")]);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/23")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_cascades_four_into_one<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::from_iter([
            ip("10.0.0.0/24"),
            ip("10.0.1.0/24"),
            ip("10.0.2.0/24"),
            ip("10.0.3.0/24"),
        ]);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/22")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_drops_covered<P: Prefix + Copy + PartialEq>() {
        let mut set =
            PrefixSet::<P>::from_iter([ip("10.0.0.0/16"), ip("10.0.1.0/24"), ip("10.0.128.0/20")]);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/16")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_merges_across_node_boundary<P: Prefix + Copy + PartialEq>() {
        // /10 and /11 siblings span the K=5 node boundary at depth 10, so the merge must travel
        // up through a child pointer rather than within a single node.
        let mut set = PrefixSet::<P>::from_iter([ip("10.0.0.0/11"), ip("10.32.0.0/11")]);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/10")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_keeps_unmergeable_siblings<P: Prefix + Copy + PartialEq>() {
        // Only one half of each potential parent is present, so nothing merges or drops.
        let original = [ip("10.0.0.0/24"), ip("10.0.2.0/24"), ip("10.1.0.0/16")];
        let mut set = PrefixSet::<P>::from_iter(original);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), Vec::from(original));
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn set_aggregate_empty_is_noop<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::new();
        set.aggregate();
        assert!(set.is_empty());
    }

    #[test]
    fn set_aggregate_collapses_to_default_route<P: Prefix + Copy + PartialEq>() {
        // The two halves of the whole space merge all the way up to the default route.
        let mut set = PrefixSet::<P>::from_iter([ip("0.0.0.0/1"), ip("128.0.0.0/1")]);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), vec![ip("0.0.0.0/0")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_is_idempotent<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::from_iter([
            ip("10.0.0.0/24"),
            ip("10.0.1.0/24"),
            ip("10.0.2.0/23"),
            ip("10.0.4.0/23"),
            ip("10.0.5.0/24"),
        ]);
        set.aggregate();
        let once = Vec::from_iter(&set);
        set.aggregate();
        assert_eq!(Vec::from_iter(&set), once);
    }

    #[test]
    fn set_aggregate_consistent_drops_covered<P: Prefix + Copy + PartialEq>() {
        // A member covered by a less-specific member is redundant and is dropped.
        let mut set = PrefixSet::<P>::from_iter([ip("10.0.0.0/16"), ip("10.0.1.0/24")]);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/16")]);
        assert_eq!(set.len(), 1);
        assert!(set.0.check_memory_alloc());
    }

    #[test]
    fn set_aggregate_consistent_keeps_siblings<P: Prefix + Copy + PartialEq>() {
        // Unlike `aggregate`, drop-only must NOT merge sibling prefixes.
        let original = [ip("10.0.0.0/24"), ip("10.0.1.0/24")];
        let mut set = PrefixSet::<P>::from_iter(original);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), Vec::from(original));
        assert_eq!(set.len(), 2);
        assert!(set.0.check_memory_alloc());
    }

    #[test]
    fn set_aggregate_consistent_drops_chain<P: Prefix + Copy + PartialEq>() {
        let mut set =
            PrefixSet::<P>::from_iter([ip("10.0.0.0/8"), ip("10.0.0.0/16"), ip("10.0.0.0/24")]);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/8")]);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn set_aggregate_consistent_drops_across_node_boundary<P: Prefix + Copy + PartialEq>() {
        // /8 (a depth-5 node) covers /11 (a depth-10 node): the coverage crosses a node boundary.
        let mut set = PrefixSet::<P>::from_iter([ip("10.0.0.0/8"), ip("10.0.0.0/11")]);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), vec![ip("10.0.0.0/8")]);
        assert_eq!(set.len(), 1);
        assert!(set.0.check_memory_alloc());
    }

    #[test]
    fn set_aggregate_consistent_keeps_uncovered<P: Prefix + Copy + PartialEq>() {
        // Nothing covers anything else here; the set is unchanged.
        let original = [ip("10.0.0.0/24"), ip("10.0.2.0/24"), ip("10.1.0.0/16")];
        let mut set = PrefixSet::<P>::from_iter(original);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), Vec::from(original));
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn set_aggregate_consistent_empty_is_noop<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::new();
        set.aggregate_consistent();
        assert!(set.is_empty());
    }

    #[test]
    fn set_aggregate_consistent_preserves_lpm_presence<P: Prefix + Copy + PartialEq>() {
        let entries = [
            ip("10.0.0.0/8"),
            ip("10.0.0.0/16"),
            ip("10.0.5.0/24"),
            ip("10.0.8.0/23"),
            ip("11.0.0.0/24"),
        ];
        let original = PrefixSet::<P>::from_iter(entries);
        let mut set = original.clone();
        set.aggregate_consistent();
        // `get_lpm` presence (Some/None) must be unchanged for every probed prefix.
        let probes = [
            ip("10.0.0.0/8"),
            ip("10.0.0.0/16"),
            ip("10.0.5.0/24"),
            ip("10.0.5.128/25"),
            ip("10.0.8.0/23"),
            ip("10.0.9.0/24"),
            ip("11.0.0.0/24"),
            ip("9.0.0.0/8"),
            ip("12.0.0.0/8"),
        ];
        for p in probes {
            assert_eq!(
                set.get_lpm(&p).is_some(),
                original.get_lpm(&p).is_some(),
                "lpm presence changed for {p:?}",
            );
        }
        assert!(set.0.check_memory_alloc());
    }

    #[test]
    fn set_aggregate_consistent_is_idempotent<P: Prefix + Copy + PartialEq>() {
        let mut set = PrefixSet::<P>::from_iter([
            ip("10.0.0.0/8"),
            ip("10.0.0.0/16"),
            ip("10.0.1.0/24"),
            ip("10.1.0.0/16"),
        ]);
        set.aggregate_consistent();
        let once = Vec::from_iter(&set);
        set.aggregate_consistent();
        assert_eq!(Vec::from_iter(&set), once);
    }

    #[test]
    fn map_aggregate_consistent_drops_same_value_descendant<P: Prefix + Copy + PartialEq>() {
        // A more specific entry with the same value as its covering entry is redundant.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/16"), 1), (ip("10.0.1.0/24"), 1)]);
        map.aggregate_consistent();
        assert_eq!(Vec::from_iter(&map), vec![(ip("10.0.0.0/16"), &1)]);
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_consistent_keeps_diff_value_descendant<P: Prefix + Copy + PartialEq>() {
        // A more specific entry with a different value is a real exception and is kept.
        let original = [(ip("10.0.0.0/16"), 1), (ip("10.0.1.0/24"), 2)];
        let mut map = Map::<P>::from_iter(original);
        map.aggregate_consistent();
        assert_eq!(
            Vec::from_iter(&map),
            vec![(ip("10.0.0.0/16"), &1), (ip("10.0.1.0/24"), &2)]
        );
    }

    #[test]
    fn map_aggregate_consistent_keeps_equal_siblings<P: Prefix + Copy + PartialEq>() {
        // Drop-only never merges, even when sibling values are equal.
        let original = [(ip("10.0.0.0/24"), 1), (ip("10.0.1.0/24"), 1)];
        let mut map = Map::<P>::from_iter(original);
        map.aggregate_consistent();
        assert_eq!(
            Vec::from_iter(&map),
            vec![(ip("10.0.0.0/24"), &1), (ip("10.0.1.0/24"), &1)]
        );
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn map_aggregate_consistent_drops_chain<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/8"), 1),
            (ip("10.0.0.0/16"), 1),
            (ip("10.0.0.0/24"), 1),
        ]);
        map.aggregate_consistent();
        assert_eq!(Vec::from_iter(&map), vec![(ip("10.0.0.0/8"), &1)]);
        assert_eq!(map.len(), 1);
    }

    #[test]
    fn map_aggregate_consistent_chain_mixed_values<P: Prefix + Copy + PartialEq>() {
        // /16 differs from its covering /8 -> kept. /24 equals its covering /16 -> dropped.
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/8"), 1),
            (ip("10.0.0.0/16"), 2),
            (ip("10.0.0.0/24"), 2),
        ]);
        map.aggregate_consistent();
        assert_eq!(
            Vec::from_iter(&map),
            vec![(ip("10.0.0.0/8"), &1), (ip("10.0.0.0/16"), &2)]
        );
        assert_eq!(map.len(), 2);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_consistent_across_node_boundary<P: Prefix + Copy + PartialEq>() {
        // /8 (depth-5 node) covers /11 (depth-10 node). Same value -> dropped.
        let mut same = Map::<P>::from_iter([(ip("10.0.0.0/8"), 5), (ip("10.0.0.0/11"), 5)]);
        same.aggregate_consistent();
        assert_eq!(Vec::from_iter(&same), vec![(ip("10.0.0.0/8"), &5)]);
        assert!(same.check_memory_alloc());

        // Different value across the boundary -> both kept.
        let mut diff = Map::<P>::from_iter([(ip("10.0.0.0/8"), 5), (ip("10.0.0.0/11"), 6)]);
        diff.aggregate_consistent();
        assert_eq!(
            Vec::from_iter(&diff),
            vec![(ip("10.0.0.0/8"), &5), (ip("10.0.0.0/11"), &6)]
        );
    }

    #[test]
    fn map_aggregate_consistent_empty_is_noop<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::new();
        map.aggregate_consistent();
        assert!(map.is_empty());
    }

    #[test]
    fn map_aggregate_consistent_preserves_lpm<P: Prefix + Copy + PartialEq>() {
        let entries = [
            (ip("10.0.0.0/8"), 1),
            (ip("10.0.0.0/16"), 1), // redundant (same as /8)
            (ip("10.0.5.0/24"), 2), // exception
            (ip("10.0.8.0/23"), 1), // redundant (same as /8)
            (ip("11.0.0.0/24"), 3),
        ];
        let original = Map::<P>::from_iter(entries);
        let mut map = original.clone();
        map.aggregate_consistent();

        // `get_lpm` must return the same *value* (and Some/None) for every probed prefix.
        let probes = [
            ip("10.0.0.0/8"),
            ip("10.0.0.0/16"),
            ip("10.0.5.0/24"),
            ip("10.0.5.128/25"),
            ip("10.0.8.0/23"),
            ip("10.0.9.0/24"),
            ip("11.0.0.0/24"),
            ip("10.255.255.255/32"),
            ip("9.0.0.0/32"),
            ip("12.0.0.0/32"),
        ];
        for p in probes {
            assert_eq!(
                map.get_lpm(&p).map(|(_, v)| *v),
                original.get_lpm(&p).map(|(_, v)| *v),
                "lpm value changed for {p:?}",
            );
        }
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_consistent_is_idempotent<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/8"), 1),
            (ip("10.0.0.0/16"), 1),
            (ip("10.0.1.0/24"), 2),
            (ip("10.1.0.0/16"), 3),
        ]);
        map.aggregate_consistent();
        let once = Vec::from_iter(map.iter().map(|(p, v)| (p, *v)));
        map.aggregate_consistent();
        assert_eq!(Vec::from_iter(map.iter().map(|(p, v)| (p, *v))), once);
    }

    #[test]
    fn map_aggregate_merges_equal_value_siblings<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/24"), 1u32), (ip("10.0.1.0/24"), 1)]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/23"), 1)]
        );
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_keeps_diff_value_siblings<P: Prefix + Copy + PartialEq>() {
        let original = [(ip("10.0.0.0/24"), 1u32), (ip("10.0.1.0/24"), 2)];
        let mut map = Map::<P>::from_iter(original);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/23"), 1), (ip("10.0.1.0/24"), 2)]
        );
    }

    #[test]
    fn map_aggregate_keeps_hole<P: Prefix + Copy + PartialEq>() {
        // One sibling is absent (hole). Merging would cover uncovered space, so no merge.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/24"), 1u32)]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/24"), 1)]
        );
        // The absent sibling stays uncovered.
        assert_eq!(map.get_lpm(&ip("10.0.1.0/32")), None);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_drops_covered_same_value<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/8"), 1u32), (ip("10.0.0.0/16"), 1)]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/8"), 1)]
        );
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_keeps_diff_value_descendant<P: Prefix + Copy + PartialEq>() {
        let original = [(ip("10.0.0.0/8"), 1u32), (ip("10.0.0.0/16"), 2)];
        let mut map = Map::<P>::from_iter(original);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/8"), 1), (ip("10.0.0.0/16"), 2)]
        );
    }

    #[test]
    fn map_aggregate_merges_across_node_boundary<P: Prefix + Copy + PartialEq>() {
        // /11 siblings span the K=5 node boundary at depth 10.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/11"), 1u32), (ip("10.32.0.0/11"), 1)]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/10"), 1)]
        );
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_cascades<P: Prefix + Copy + PartialEq>() {
        // Four /25 leaves of the same value cascade into /23.
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/25"), 1u32),
            (ip("10.0.0.128/25"), 1),
            (ip("10.0.1.0/25"), 1),
            (ip("10.0.1.128/25"), 1),
        ]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/23"), 1)]
        );
        assert_eq!(map.len(), 1);
    }

    #[test]
    fn map_aggregate_merges_with_exception<P: Prefix + Copy + PartialEq>() {
        // combine({Some(1)}, {Some(1),Some(2)}) = {Some(1)} (non-empty intersection),
        // so ORTC emits /23=1 and keeps only the /25=2 exception.
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/24"), 1u32),
            (ip("10.0.1.0/25"), 1),
            (ip("10.0.1.128/25"), 2),
        ]);
        map.aggregate();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("10.0.0.0/23"), 1), (ip("10.0.1.128/25"), 2)]
        );
        assert_eq!(map.len(), 2);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_empty_is_noop<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::new();
        map.aggregate();
        assert!(map.is_empty());
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_preserves_lpm<P: Prefix + Copy + PartialEq>() {
        let entries = [
            (ip("10.0.0.0/8"), 1u32),
            (ip("10.0.0.0/16"), 1),
            (ip("10.0.5.0/24"), 2),
            (ip("10.0.8.0/23"), 1),
            (ip("11.0.0.0/24"), 3),
        ];
        let original = Map::<P>::from_iter(entries);
        let mut map = original.clone();
        map.aggregate();

        let probes = [
            ip("10.0.0.0/8"),
            ip("10.0.0.0/16"),
            ip("10.0.5.0/24"),
            ip("10.0.5.128/25"),
            ip("10.0.8.0/23"),
            ip("11.0.0.0/24"),
            ip("10.255.255.255/32"),
            ip("9.0.0.0/32"),
        ];
        for p in probes {
            assert_eq!(
                map.get_lpm(&p).map(|(_, v)| *v),
                original.get_lpm(&p).map(|(_, v)| *v),
                "lpm changed for {p:?}",
            );
        }
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_is_idempotent<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/24"), 1u32),
            (ip("10.0.1.0/24"), 1),
            (ip("10.0.2.0/24"), 2),
            (ip("10.0.3.0/24"), 2),
            (ip("10.1.0.0/16"), 1),
        ]);
        map.aggregate();
        let once = Vec::from_iter(map.iter().map(|(p, v)| (p, *v)));
        map.aggregate();
        assert_eq!(Vec::from_iter(map.iter().map(|(p, v)| (p, *v))), once);
    }

    #[test]
    fn map_aggregate_fill_empty_makes_default_route<P: Prefix + Copy + PartialEq>() {
        // Filling an empty map covers the whole space with the default: one default route.
        let mut map = Map::<P>::new();
        map.aggregate_fill(|| 5);
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("0.0.0.0/0"), 5)]
        );
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_fill_adds_default_over_covered<P: Prefix + Copy + PartialEq>() {
        // A default value that differs from the covered one adds a default route beside it.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/8"), 1u32)]);
        map.aggregate_fill(|| 9);
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("0.0.0.0/0"), 9), (ip("10.0.0.0/8"), 1)]
        );
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_fill_collapses_when_default_matches<P: Prefix + Copy + PartialEq>() {
        // The default equals every covered value, so the whole space becomes one default route.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/8"), 1u32), (ip("192.168.0.0/16"), 1)]);
        map.aggregate_fill(|| 1);
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("0.0.0.0/0"), 1)]
        );
        assert_eq!(map.len(), 1);
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_fill_merge_and_collapse<P: Prefix + Copy + PartialEq>() {
        // Siblings merge into a /23; the gaps and both /16 branches collapse into /0 = 1.
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/16"), 1u32),
            (ip("10.0.0.0/24"), 2),
            (ip("10.0.1.0/24"), 2),
            (ip("10.0.2.0/24"), 1),
            (ip("192.168.0.0/16"), 1),
        ]);
        map.aggregate_fill(|| 1);
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("0.0.0.0/0"), 1), (ip("10.0.0.0/23"), 2)]
        );
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_fill_keeps_distinct_values<P: Prefix + Copy + PartialEq>() {
        // Distinct-valued entries survive beside the default route; nothing merges. The /30 has a
        // default-valued sibling, so unlike a /24 (whose sibling is the /24=2) it is not promoted.
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/30"), 1u32),
            (ip("10.0.1.0/24"), 2),
            (ip("10.1.0.0/16"), 3),
        ]);
        map.aggregate_fill_default();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![
                (ip("0.0.0.0/0"), 0),
                (ip("10.0.0.0/30"), 1),
                (ip("10.0.1.0/24"), 2),
                (ip("10.1.0.0/16"), 3),
            ]
        );
        assert!(map.check_memory_alloc());
    }

    #[test]
    fn map_aggregate_fill_is_idempotent<P: Prefix + Copy + PartialEq>() {
        let mut map = Map::<P>::from_iter([
            (ip("10.0.0.0/24"), 1u32),
            (ip("10.0.1.0/24"), 2),
            (ip("10.1.0.0/16"), 3),
        ]);
        map.aggregate_fill_default();
        let once = Vec::from_iter(map.iter().map(|(p, v)| (p, *v)));
        map.aggregate_fill_default();
        assert_eq!(Vec::from_iter(map.iter().map(|(p, v)| (p, *v))), once);
    }

    #[test]
    fn map_aggregate_fill_default_uses_zero<P: Prefix + Copy + PartialEq>() {
        // `aggregate_fill_default` fills with `T::default()`, i.e. `0` for `u32`.
        let mut map = Map::<P>::from_iter([(ip("10.0.0.0/8"), 1u32)]);
        map.aggregate_fill_default();
        assert_eq!(
            Vec::from_iter(map.iter().map(|(p, v)| (p, *v))),
            vec![(ip("0.0.0.0/0"), 0), (ip("10.0.0.0/8"), 1)]
        );
        assert!(map.check_memory_alloc());
    }

    #[instantiate_tests(<(u32, u8)>)]
    mod raw32 {}

    #[instantiate_tests(<(u64, u8)>)]
    mod raw64 {}

    #[instantiate_tests(<(u128, u8)>)]
    mod raw128 {}

    #[cfg(feature = "ipnet")]
    #[instantiate_tests(<ipnet::Ipv4Net>)]
    mod ipv4net {}

    #[cfg(feature = "ipnet")]
    #[instantiate_tests(<ipnet::Ipv6Net>)]
    mod ipv6net {}

    #[cfg(feature = "ipnetwork")]
    #[instantiate_tests(<ipnetwork::Ipv4Network>)]
    mod ipv4network {}

    #[cfg(feature = "ipnetwork")]
    #[instantiate_tests(<ipnetwork::Ipv6Network>)]
    mod ipv6network {}

    #[cfg(feature = "cidr")]
    #[instantiate_tests(<cidr::Ipv4Cidr>)]
    mod ipv4cidr {}

    #[cfg(feature = "cidr")]
    #[instantiate_tests(<cidr::Ipv6Cidr>)]
    mod ipv6cidr {}

    #[cfg(feature = "cidr")]
    #[instantiate_tests(<cidr::Ipv4Inet>)]
    mod ipv4inet {}

    #[cfg(feature = "cidr")]
    #[instantiate_tests(<cidr::Ipv6Inet>)]
    mod ipv6inet {}
}