addrsrcs_test.go 1.56 KB
Newer Older
1
2
3
4
5
6
package addr

import (
	"fmt"
	"testing"

Jeromy's avatar
Jeromy committed
7
	ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr"
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
78
)

func newAddrOrFatal(t *testing.T, s string) ma.Multiaddr {
	a, err := ma.NewMultiaddr(s)
	if err != nil {
		t.Fatal("error parsing multiaddr", err)
	}
	return a
}

func newAddrs(t *testing.T, n int) []ma.Multiaddr {
	addrs := make([]ma.Multiaddr, n)
	for i := 0; i < n; i++ {
		s := fmt.Sprintf("/ip4/1.2.3.4/tcp/%d", i)
		addrs[i] = newAddrOrFatal(t, s)
	}
	return addrs
}

func addrSetsSame(a, b []ma.Multiaddr) bool {
	if len(a) != len(b) {
		return false
	}
	for i, aa := range a {
		bb := b[i]
		if !aa.Equal(bb) {
			return false
		}
	}
	return true
}

func addrSourcesSame(a, b Source) bool {
	return addrSetsSame(a.Addrs(), b.Addrs())
}

func TestAddrCombine(t *testing.T) {
	addrs := newAddrs(t, 30)
	a := Slice(addrs[0:10])
	b := Slice(addrs[10:20])
	c := Slice(addrs[20:30])
	d := CombineSources(a, b, c)
	if !addrSetsSame(addrs, d.Addrs()) {
		t.Error("addrs differ")
	}
	if !addrSourcesSame(Slice(addrs), d) {
		t.Error("addrs differ")
	}
}

func TestAddrUnique(t *testing.T) {

	addrs := newAddrs(t, 40)
	a := Slice(addrs[0:20])
	b := Slice(addrs[10:30])
	c := Slice(addrs[20:40])
	d := CombineSources(a, b, c)
	e := UniqueSource(a, b, c)
	if addrSetsSame(addrs, d.Addrs()) {
		t.Error("addrs same")
	}
	if addrSourcesSame(Slice(addrs), d) {
		t.Error("addrs same")
	}
	if !addrSetsSame(addrs, e.Addrs()) {
		t.Error("addrs differ", addrs, "\n\n", e.Addrs(), "\n\n")
	}
	if !addrSourcesSame(Slice(addrs), e) {
		t.Error("addrs differ", addrs, "\n\n", e.Addrs(), "\n\n")
	}
}