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