Skip to content

Commit 4bf1c08

Browse files
committed
rename and refactor testfiles
1 parent a19139f commit 4bf1c08

8 files changed

+153
-152
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ provides clear advantages for any routing daemon.
177177
But as always, it depends on the specific use case.
178178

179179
See the concurrent tests for concrete examples of this pattern:
180-
- [ExampleFast](zz-example_fast_concurrent_test.go)
181-
- [ExampleLite](zz-example_lite_concurrent_test.go)
182-
- [ExampleTable](zz-example_table_concurrent_test.go)
180+
- [ExampleFast](example_fast_concurrent_test.go)
181+
- [ExampleLite](example_lite_concurrent_test.go)
182+
- [ExampleTable](example_table_concurrent_test.go)
183183

184184

185185
## Additional Use Cases
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package bart_test
55

66
import (
77
"fmt"
8+
"maps"
89
"net/netip"
910
"os"
1011

@@ -185,3 +186,30 @@ func ExampleTable_Supernets_rangeoverfunc() {
185186
// 2001:db8::/32
186187
// 2000::/3
187188
}
189+
190+
type route struct {
191+
ASN int
192+
Attrs map[string]string
193+
}
194+
195+
func (r route) Equal(other route) bool {
196+
return r.ASN == other.ASN && maps.Equal(r.Attrs, other.Attrs)
197+
}
198+
199+
func (r route) Clone() route {
200+
return route{
201+
ASN: r.ASN,
202+
Attrs: maps.Clone(r.Attrs),
203+
}
204+
}
205+
206+
// Example of a custom value type with both equality and cloning
207+
func ExampleTable_customValue() {
208+
table := new(bart.Table[route])
209+
table = table.InsertPersist(mpp("10.0.0.0/8"), route{ASN: 64512, Attrs: map[string]string{"foo": "bar"}})
210+
clone := table.Clone()
211+
fmt.Printf("Cloned tables are equal: %v\n", table.Equal(clone))
212+
213+
// Output:
214+
// Cloned tables are equal: true
215+
}

zz-example_custom_value_test.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

zz-helpers_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) 2025 Karl Gaissmaier
2+
// SPDX-License-Identifier: MIT
3+
4+
package bart
5+
6+
import (
7+
"fmt"
8+
"iter"
9+
"net/netip"
10+
"testing"
11+
)
12+
13+
// #####################################################
14+
// this file contains helpers for other test functions
15+
// #####################################################
16+
17+
// workLoadN to adjust loops for tests with -short
18+
func workLoadN() int {
19+
if testing.Short() {
20+
return 100
21+
}
22+
return 1_000
23+
}
24+
25+
// abbreviation
26+
var mpa = netip.MustParseAddr
27+
28+
// abbreviation and panic on non masked input
29+
var mpp = func(s string) netip.Prefix {
30+
pfx := netip.MustParsePrefix(s)
31+
if pfx == pfx.Masked() {
32+
return pfx
33+
}
34+
panic(fmt.Sprintf("%s is not canonicalized as %s", s, pfx.Masked()))
35+
}
36+
37+
// tests for deep copies with Clone method
38+
type MyInt int
39+
40+
// implement the Clone method
41+
func (i *MyInt) Clone() *MyInt {
42+
a := *i
43+
return &a
44+
}
45+
46+
func countDumpListNodes[V any](nodes []DumpListNode[V]) int {
47+
count := len(nodes)
48+
for _, node := range nodes {
49+
count += countDumpListNodes(node.Subnets)
50+
}
51+
return count
52+
}
53+
54+
func verifyAllIPv4Nodes[V any](t *testing.T, nodes []DumpListNode[V]) {
55+
for i, node := range nodes {
56+
if !node.CIDR.Addr().Is4() {
57+
t.Errorf("Node %d is not IPv4 prefix: %v", i, node.CIDR)
58+
}
59+
// Recursively check subnets
60+
verifyAllIPv4Nodes(t, node.Subnets)
61+
}
62+
}
63+
64+
func verifyAllIPv6Nodes[V any](t *testing.T, nodes []DumpListNode[V]) {
65+
for i, node := range nodes {
66+
if !node.CIDR.Addr().Is6() {
67+
t.Errorf("Node %d is not IPv6 prefix: %v", i, node.CIDR)
68+
}
69+
// Recursively check subnets
70+
verifyAllIPv6Nodes(t, node.Subnets)
71+
}
72+
}
73+
74+
func mustPanic(t *testing.T, name string, fn func()) {
75+
t.Helper()
76+
defer func() {
77+
if recover() == nil {
78+
t.Fatalf("%s must panic", name)
79+
}
80+
}()
81+
fn()
82+
}
83+
84+
func noPanic(t *testing.T, name string, fn func()) {
85+
t.Helper()
86+
defer func() {
87+
if r := recover(); r != nil {
88+
t.Fatalf("%s panicked: %v", name, r)
89+
}
90+
}()
91+
fn()
92+
}
93+
94+
func noPanicRangeOverFunc[V any](t *testing.T, name string, fn any) {
95+
t.Helper()
96+
defer func() {
97+
if r := recover(); r != nil {
98+
t.Fatalf("%s panicked: %v", name, r)
99+
}
100+
}()
101+
102+
switch iterFunc := fn.(type) {
103+
case func() iter.Seq[netip.Prefix]:
104+
for range iterFunc() {
105+
}
106+
case func() iter.Seq2[netip.Prefix, V]:
107+
for range iterFunc() {
108+
}
109+
case func(netip.Prefix) iter.Seq[netip.Prefix]:
110+
pfx := mpp("1.2.3.4/32")
111+
for range iterFunc(pfx) {
112+
}
113+
case func(netip.Prefix) iter.Seq2[netip.Prefix, V]:
114+
pfx := mpp("1.2.3.4/32")
115+
for range iterFunc(pfx) {
116+
}
117+
default:
118+
t.Fatalf("%s unknown iter function: %T", name, iterFunc)
119+
}
120+
}

zz-setup_test.go renamed to zz-init_test.go

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,16 @@ import (
77
"bufio"
88
"compress/gzip"
99
"fmt"
10-
"iter"
1110
"math/rand/v2"
1211
"net/netip"
1312
"os"
1413
"strings"
1514
"sync"
16-
"testing"
1715
)
1816

1917
// location of the full tier1 routing info
2018
const prefixFile = "./internal/tests/testdata/prefixes.txt.gz"
2119

22-
// workLoadN to adjust loops for tests with -short
23-
func workLoadN() int {
24-
if testing.Short() {
25-
return 100
26-
}
27-
return 1_000
28-
}
29-
30-
// this file contains helpers for other test functions
31-
32-
// holds the tier1 routes
33-
var tier1 = &tier1T{}
34-
3520
type tier1T struct {
3621
_once sync.Once // load and parse it only once
3722

@@ -52,105 +37,8 @@ type tier1T struct {
5237
_missPfx6 netip.Prefix // missing v6 prefix in tier1 routes
5338
}
5439

55-
// abbreviation
56-
var mpa = netip.MustParseAddr
57-
58-
// abbreviation and apnic on non masked input
59-
var mpp = func(s string) netip.Prefix {
60-
pfx := netip.MustParsePrefix(s)
61-
if pfx == pfx.Masked() {
62-
return pfx
63-
}
64-
panic(fmt.Sprintf("%s is not canonicalized as %s", s, pfx.Masked()))
65-
}
66-
67-
// #########################################################
68-
69-
// tests for deep copies with Cloner interface
70-
type MyInt int
71-
72-
// implement the Cloner interface
73-
func (i *MyInt) Clone() *MyInt {
74-
a := *i
75-
return &a
76-
}
77-
78-
// Helper functions
79-
func countDumpListNodes[V any](nodes []DumpListNode[V]) int {
80-
count := len(nodes)
81-
for _, node := range nodes {
82-
count += countDumpListNodes(node.Subnets)
83-
}
84-
return count
85-
}
86-
87-
func verifyAllIPv4Nodes[V any](t *testing.T, nodes []DumpListNode[V]) {
88-
for i, node := range nodes {
89-
if !node.CIDR.Addr().Is4() {
90-
t.Errorf("Node %d is not IPv4 prefix: %v", i, node.CIDR)
91-
}
92-
// Recursively check subnets
93-
verifyAllIPv4Nodes(t, node.Subnets)
94-
}
95-
}
96-
97-
func verifyAllIPv6Nodes[V any](t *testing.T, nodes []DumpListNode[V]) {
98-
for i, node := range nodes {
99-
if !node.CIDR.Addr().Is6() {
100-
t.Errorf("Node %d is not IPv6 prefix: %v", i, node.CIDR)
101-
}
102-
// Recursively check subnets
103-
verifyAllIPv6Nodes(t, node.Subnets)
104-
}
105-
}
106-
107-
func mustPanic(t *testing.T, name string, fn func()) {
108-
t.Helper()
109-
defer func() {
110-
if recover() == nil {
111-
t.Fatalf("%s must panic", name)
112-
}
113-
}()
114-
fn()
115-
}
116-
117-
func noPanic(t *testing.T, name string, fn func()) {
118-
t.Helper()
119-
defer func() {
120-
if r := recover(); r != nil {
121-
t.Fatalf("%s panicked: %v", name, r)
122-
}
123-
}()
124-
fn()
125-
}
126-
127-
func noPanicRangeOverFunc[V any](t *testing.T, name string, fn any) {
128-
t.Helper()
129-
defer func() {
130-
if r := recover(); r != nil {
131-
t.Fatalf("%s panicked: %v", name, r)
132-
}
133-
}()
134-
135-
switch iterFunc := fn.(type) {
136-
case func() iter.Seq[netip.Prefix]:
137-
for range iterFunc() {
138-
}
139-
case func() iter.Seq2[netip.Prefix, V]:
140-
for range iterFunc() {
141-
}
142-
case func(netip.Prefix) iter.Seq[netip.Prefix]:
143-
pfx := mpp("1.2.3.4/32")
144-
for range iterFunc(pfx) {
145-
}
146-
case func(netip.Prefix) iter.Seq2[netip.Prefix, V]:
147-
pfx := mpp("1.2.3.4/32")
148-
for range iterFunc(pfx) {
149-
}
150-
default:
151-
t.Fatalf("%s unknown iter function: %T", name, iterFunc)
152-
}
153-
}
40+
// holds the tier1 routes
41+
var tier1 = &tier1T{}
15442

15543
// init parses the tier1 route table once at first use and caches it
15644
func (t1 *tier1T) init() {

0 commit comments

Comments
 (0)