File: tree_node_v4_test.go

package info (click to toggle)
golang-github-kentik-patricia 0.0~git20200128.c35d94c-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,368 kB
  • sloc: makefile: 38
file content (55 lines) | stat: -rw-r--r-- 1,270 bytes parent folder | download
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
package template

import (
	"testing"

	"github.com/kentik/patricia"
	"github.com/stretchr/testify/assert"
)

func TestV4MatchCount(t *testing.T) {
	node := &treeNodeV4{
		prefix:       uint32(0xFFFFFFFF),
		prefixLength: 32,
	}

	// test moving the non-matching bit forward, making sure we never return more than the length of the prefix
	addr := uint32(0xFFFFFFFF)
	for i := 1; i < 32; i++ {
		address := patricia.IPv4Address{
			Address: clearBit32(addr, uint32(i)),
			Length:  32,
		}
		assert.Equal(t, uint(i-1), node.MatchCount(address))
	}

	// test moving the non-matching bit forward, with max of node address's 16 length
	node.prefixLength = 16
	for i := 1; i < 32; i++ {
		expected := uint(i - 1)
		if expected > 16 {
			expected = 16
		}

		address := patricia.IPv4Address{
			Address: clearBit32(addr, uint32(i)),
			Length:  32,
		}
		assert.Equal(t, expected, node.MatchCount(address))
	}

	// test moving the non-matching bit forward, with max of comparison address's 16 length
	node.prefixLength = 32
	for i := 1; i < 32; i++ {
		expected := uint(i - 1)
		if expected > 16 {
			expected = 16
		}

		address := patricia.IPv4Address{
			Address: clearBit32(addr, uint32(i)),
			Length:  16,
		}
		assert.Equal(t, expected, node.MatchCount(address))
	}
}