File: ipcalc_test.go

package info (click to toggle)
golang-github-coredhcp-coredhcp 0.0.0%2Bgit.20250806.f7e98e4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 460 kB
  • sloc: makefile: 8; sh: 6
file content (77 lines) | stat: -rw-r--r-- 2,759 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

package allocators

import (
	"fmt"
	"net"
	"testing"

	"math/rand"
)

func ExampleOffset() {
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 0))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 16))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 32))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 48))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 64))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 73))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 80))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 96))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 112))
	fmt.Println(Offset(net.ParseIP("2001:db8:0:aabb::"), net.ParseIP("2001:db8:ff::34"), 128))
	// Output:
	// 0 <nil>
	// 0 <nil>
	// 0 <nil>
	// 254 <nil>
	// 16667973 <nil>
	// 8534002176 <nil>
	// 1092352278528 <nil>
	// 71588398925611008 <nil>
	// 0 Operation overflows
	// 0 Operation overflows
}

func ExampleAddPrefixes() {
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 64))
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0x1, 128))
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 32))
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0x1, 16))
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 65))
	// Error cases
	fmt.Println(AddPrefixes(net.ParseIP("2001:db8::"), 0xff, 8))
	fmt.Println(AddPrefixes(net.IP{10, 0, 0, 1}, 64, 32))
	// Output:
	// 2001:db8:0:ff:: <nil>
	// 2001:db8::1 <nil>
	// 2001:eb7:: <nil>
	// 2002:db8:: <nil>
	// 2001:db8:0:7f:8000:: <nil>
	// <nil> Operation overflows
	// <nil> AddPrefixes needs 128-bit IPs
}

// Offset is used as a hash function, so it needs to be reasonably fast
func BenchmarkOffset(b *testing.B) {
	// Need predictable randomness for benchmark reproducibility
	rng := rand.New(rand.NewSource(0))
	addresses := make([]byte, b.N*net.IPv6len*2)
	_, err := rng.Read(addresses)
	if err != nil {
		b.Fatalf("Could not generate random addresses: %v", err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// The arrays will be in cache, so this should amortize to measure mostly just the offset
		// computation itself
		_, _ = Offset(
			addresses[i*2*net.IPv6len:(i*2+1)*net.IPv6len],
			addresses[(i*2+1)*net.IPv6len:(i+1)*2*net.IPv6len],
			(i*4)%128,
		)
	}
}