summaryrefslogtreecommitdiffhomepage
path: root/trie_rand_test.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 19:33:41 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-13 19:34:28 +0200
commit2326d6a4d75f9f3736046cc526eb593a403d4c7a (patch)
treeda31622899bb84e256b22addd1604b8250582c44 /trie_rand_test.go
parente94185681f4c19874392019b0d43bdfefcbb85b5 (diff)
Odds and ends
Diffstat (limited to 'trie_rand_test.go')
-rw-r--r--trie_rand_test.go131
1 files changed, 0 insertions, 131 deletions
diff --git a/trie_rand_test.go b/trie_rand_test.go
deleted file mode 100644
index 157c270..0000000
--- a/trie_rand_test.go
+++ /dev/null
@@ -1,131 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0
- *
- * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
- */
-
-package main
-
-import (
- "math/rand"
- "sort"
- "testing"
-)
-
-const (
- NumberOfPeers = 100
- NumberOfAddresses = 250
- NumberOfTests = 10000
-)
-
-type SlowNode struct {
- peer *Peer
- cidr uint
- bits []byte
-}
-
-type SlowRouter []*SlowNode
-
-func (r SlowRouter) Len() int {
- return len(r)
-}
-
-func (r SlowRouter) Less(i, j int) bool {
- return r[i].cidr > r[j].cidr
-}
-
-func (r SlowRouter) Swap(i, j int) {
- r[i], r[j] = r[j], r[i]
-}
-
-func (r SlowRouter) Insert(addr []byte, cidr uint, peer *Peer) SlowRouter {
- for _, t := range r {
- if t.cidr == cidr && commonBits(t.bits, addr) >= cidr {
- t.peer = peer
- t.bits = addr
- return r
- }
- }
- r = append(r, &SlowNode{
- cidr: cidr,
- bits: addr,
- peer: peer,
- })
- sort.Sort(r)
- return r
-}
-
-func (r SlowRouter) Lookup(addr []byte) *Peer {
- for _, t := range r {
- common := commonBits(t.bits, addr)
- if common >= t.cidr {
- return t.peer
- }
- }
- return nil
-}
-
-func TestTrieRandomIPv4(t *testing.T) {
- var trie *Trie
- var slow SlowRouter
- var peers []*Peer
-
- rand.Seed(1)
-
- const AddressLength = 4
-
- for n := 0; n < NumberOfPeers; n += 1 {
- peers = append(peers, &Peer{})
- }
-
- for n := 0; n < NumberOfAddresses; n += 1 {
- var addr [AddressLength]byte
- rand.Read(addr[:])
- cidr := uint(rand.Uint32() % (AddressLength * 8))
- index := rand.Int() % NumberOfPeers
- trie = trie.Insert(addr[:], cidr, peers[index])
- slow = slow.Insert(addr[:], cidr, peers[index])
- }
-
- for n := 0; n < NumberOfTests; n += 1 {
- var addr [AddressLength]byte
- rand.Read(addr[:])
- peer1 := slow.Lookup(addr[:])
- peer2 := trie.Lookup(addr[:])
- if peer1 != peer2 {
- t.Error("Trie did not match naive implementation, for:", addr)
- }
- }
-}
-
-func TestTrieRandomIPv6(t *testing.T) {
- var trie *Trie
- var slow SlowRouter
- var peers []*Peer
-
- rand.Seed(1)
-
- const AddressLength = 16
-
- for n := 0; n < NumberOfPeers; n += 1 {
- peers = append(peers, &Peer{})
- }
-
- for n := 0; n < NumberOfAddresses; n += 1 {
- var addr [AddressLength]byte
- rand.Read(addr[:])
- cidr := uint(rand.Uint32() % (AddressLength * 8))
- index := rand.Int() % NumberOfPeers
- trie = trie.Insert(addr[:], cidr, peers[index])
- slow = slow.Insert(addr[:], cidr, peers[index])
- }
-
- for n := 0; n < NumberOfTests; n += 1 {
- var addr [AddressLength]byte
- rand.Read(addr[:])
- peer1 := slow.Lookup(addr[:])
- peer2 := trie.Lookup(addr[:])
- if peer1 != peer2 {
- t.Error("Trie did not match naive implementation, for:", addr)
- }
- }
-}