OSDN Git Service

a0ee55cc2f41ef64acf33c8b3d50ce449810376a
[bytom/vapor.git] / vendor / github.com / multiformats / go-multiaddr / util_test.go
1 package multiaddr
2
3 import (
4         "strings"
5         "testing"
6 )
7
8 func TestSplitFirstLast(t *testing.T) {
9         ipStr := "/ip4/0.0.0.0"
10         tcpStr := "/tcp/123"
11         quicStr := "/quic"
12         ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
13
14         for _, x := range [][]string{
15                 []string{ipStr, tcpStr, quicStr, ipfsStr},
16                 []string{ipStr, tcpStr, ipfsStr},
17                 []string{ipStr, tcpStr},
18                 []string{ipStr},
19                 []string{},
20         } {
21                 addr := StringCast(strings.Join(x, ""))
22                 head, tail := SplitFirst(addr)
23                 rest, last := SplitLast(addr)
24                 if len(x) == 0 {
25                         if head != nil {
26                                 t.Error("expected head to be nil")
27                         }
28                         if tail != nil {
29                                 t.Error("expected tail to be nil")
30                         }
31                         if rest != nil {
32                                 t.Error("expected rest to be nil")
33                         }
34                         if last != nil {
35                                 t.Error("expected last to be nil")
36                         }
37                         continue
38                 }
39                 if !head.Equal(StringCast(x[0])) {
40                         t.Errorf("expected %s to be %s", head, x[0])
41                 }
42                 if !last.Equal(StringCast(x[len(x)-1])) {
43                         t.Errorf("expected %s to be %s", head, x[len(x)-1])
44                 }
45                 if len(x) == 1 {
46                         if tail != nil {
47                                 t.Error("expected tail to be nil")
48                         }
49                         if rest != nil {
50                                 t.Error("expected rest to be nil")
51                         }
52                         continue
53                 }
54                 tailExp := strings.Join(x[1:], "")
55                 if !tail.Equal(StringCast(tailExp)) {
56                         t.Errorf("expected %s to be %s", tail, tailExp)
57                 }
58                 restExp := strings.Join(x[:len(x)-1], "")
59                 if !rest.Equal(StringCast(restExp)) {
60                         t.Errorf("expected %s to be %s", rest, restExp)
61                 }
62         }
63
64         c, err := NewComponent("ip4", "127.0.0.1")
65         if err != nil {
66                 t.Fatal(err)
67         }
68
69         ci, m := SplitFirst(c)
70         if !ci.Equal(c) || m != nil {
71                 t.Error("split first on component failed")
72         }
73         m, ci = SplitLast(c)
74         if !ci.Equal(c) || m != nil {
75                 t.Error("split last on component failed")
76         }
77         cis := Split(c)
78         if len(cis) != 1 || !cis[0].Equal(c) {
79                 t.Error("split on component failed")
80         }
81         m1, m2 := SplitFunc(c, func(c Component) bool {
82                 return true
83         })
84         if m1 != nil || !m2.Equal(c) {
85                 t.Error("split func(true) on component failed")
86         }
87         m1, m2 = SplitFunc(c, func(c Component) bool {
88                 return false
89         })
90         if !m1.Equal(c) || m2 != nil {
91                 t.Error("split func(false) on component failed")
92         }
93
94         i := 0
95         ForEach(c, func(ci Component) bool {
96                 if i != 0 {
97                         t.Error("expected exactly one component")
98                 }
99                 i++
100                 if !ci.Equal(c) {
101                         t.Error("foreach on component failed")
102                 }
103                 return true
104         })
105 }
106
107 func TestSplitFunc(t *testing.T) {
108         ipStr := "/ip4/0.0.0.0"
109         tcpStr := "/tcp/123"
110         quicStr := "/quic"
111         ipfsStr := "/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7"
112
113         for _, x := range [][]string{
114                 []string{ipStr, tcpStr, quicStr, ipfsStr},
115                 []string{ipStr, tcpStr, ipfsStr},
116                 []string{ipStr, tcpStr},
117                 []string{ipStr},
118         } {
119                 addr := StringCast(strings.Join(x, ""))
120                 for i, cs := range x {
121                         target := StringCast(cs)
122                         a, b := SplitFunc(addr, func(c Component) bool {
123                                 return c.Equal(target)
124                         })
125                         if i == 0 {
126                                 if a != nil {
127                                         t.Error("expected nil addr")
128                                 }
129                         } else {
130                                 if !a.Equal(StringCast(strings.Join(x[:i], ""))) {
131                                         t.Error("split failed")
132                                 }
133                                 if !b.Equal(StringCast(strings.Join(x[i:], ""))) {
134                                         t.Error("split failed")
135                                 }
136                         }
137                 }
138                 a, b := SplitFunc(addr, func(_ Component) bool { return false })
139                 if !a.Equal(addr) || b != nil {
140                         t.Error("should not have split")
141                 }
142         }
143 }