Fixup PrevLabel and add a test
This commit is contained in:
parent
4ba292d8b6
commit
1f586fdcda
|
@ -146,13 +146,16 @@ func NextLabel(s string, offset int) (i int, end bool) {
|
|||
|
||||
// PrevLabel returns the index of the label when starting from the right and
|
||||
// jumping n labels to the left.
|
||||
// The bool start is true when the start of the string has been reached.
|
||||
// The bool start is true when the start of the string has been overshot.
|
||||
func PrevLabel(s string, n int) (i int, start bool) {
|
||||
if n == 0 {
|
||||
return len(s), false
|
||||
}
|
||||
lab := Split(s)
|
||||
if lab == nil {
|
||||
return 0, true
|
||||
}
|
||||
if len(lab) < n {
|
||||
if n > len(lab) {
|
||||
return 0, true
|
||||
}
|
||||
return lab[len(lab)-n], false
|
||||
|
|
|
@ -94,6 +94,35 @@ func TestSplit2(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPrevLabel(t *testing.T) {
|
||||
type prev struct {
|
||||
string
|
||||
int
|
||||
}
|
||||
prever := map[prev]int{
|
||||
prev{"www.miek.nl.", 0}: 12,
|
||||
prev{"www.miek.nl.", 1}: 9,
|
||||
prev{"www.miek.nl.", 2}: 4,
|
||||
|
||||
prev{"www.miek.nl", 0}: 11,
|
||||
prev{"www.miek.nl", 1}: 9,
|
||||
prev{"www.miek.nl", 2}: 4,
|
||||
|
||||
prev{"www.miek.nl.", 5}: 0,
|
||||
prev{"www.miek.nl", 5}: 0,
|
||||
|
||||
prev{"www.miek.nl.", 3}: 0,
|
||||
prev{"www.miek.nl", 3}: 0,
|
||||
}
|
||||
for s, i := range prever {
|
||||
x, ok := PrevLabel(s.string, s.int)
|
||||
if i != x {
|
||||
t.Logf("Label should be %d, got %d, %t: preving %d, %s\n", i, x, ok, s.int, s.string)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountLabel(t *testing.T) {
|
||||
splitter := map[string]int{
|
||||
"www.miek.nl.": 3,
|
||||
|
|
Loading…
Reference in New Issue