-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate code using Go generics (#20)
* Generate code using Go generics The code mutation is a bit less pretty but it still works well. Tests are kept in this case as they are easy to mutate to check that everything is working. However, this requires bumping the minimal Go version to 1.18. For clarity, generated code will be in a separate commit. * Add generated code with generics * Bump golangci/golangci-lint-action version for Go 1.18 compatibility
- Loading branch information
1 parent
f3af281
commit ae6f737
Showing
21 changed files
with
3,867 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package generics_tree | ||
|
||
// Clears the bit at pos in n. | ||
func clearBit32(n uint32, pos uint32) uint32 { | ||
pos = 32 - pos | ||
mask := uint32(^(1 << pos)) | ||
n &= mask | ||
return n | ||
} | ||
|
||
// Clears the bit at pos in n. | ||
func clearBit64(n uint64, pos uint64) uint64 { | ||
pos = 64 - pos | ||
mask := uint64(^(1 << pos)) | ||
n &= mask | ||
return n | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package generics_tree | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kentik/patricia" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestV4MatchCount(t *testing.T) { | ||
node := &treeNodeV4[string]{ | ||
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)) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package generics_tree | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kentik/patricia" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestV6MatchCount(t *testing.T) { | ||
node := &treeNodeV6[string]{ | ||
prefixLeft: uint64(0xFFFFFFFFFFFFFFFF), | ||
prefixRight: uint64(0xFFFFFFFFFFFFFFFF), | ||
prefixLength: 84, | ||
} | ||
|
||
// test moving the non-matching bit forward, making sure we never return more than the length of the prefix | ||
left := uint64(0xFFFFFFFFFFFFFFFF) | ||
for i := 1; i < 128; i++ { | ||
addressLeft := left | ||
if i <= 64 { | ||
addressLeft = clearBit64(addressLeft, uint64(i)) | ||
} | ||
|
||
expected := i - 1 | ||
if i > 64 { | ||
expected = 84 | ||
} | ||
|
||
address := patricia.IPv6Address{ | ||
Left: addressLeft, | ||
Right: uint64(0xFFFFFFFFFFFFFFFF), | ||
Length: 128, | ||
} | ||
assert.Equal(t, uint(expected), node.MatchCount(address)) | ||
} | ||
|
||
// test moving the non-matching bit forward, making sure we never return more than the length of the address | ||
node.prefixLength = 128 | ||
left = uint64(0xFFFFFFFFFFFFFFFF) | ||
for i := 1; i < 128; i++ { | ||
addressLeft := left | ||
if i <= 64 { | ||
addressLeft = clearBit64(addressLeft, uint64(i)) | ||
} | ||
|
||
expected := i - 1 | ||
if i > 64 { | ||
expected = 84 | ||
} | ||
address := patricia.IPv6Address{ | ||
Left: addressLeft, | ||
Right: uint64(0xFFFFFFFFFFFFFFFF), | ||
Length: 84, | ||
} | ||
assert.Equal(t, uint(expected), node.MatchCount(address)) | ||
} | ||
} |
Oops, something went wrong.