Skip to content

Commit 8c83f67

Browse files
committed
Make golint && go test -cover happy
1 parent 9b214f3 commit 8c83f67

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

suffix.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func (node *_Node) insert(originKey []byte, key []byte, value interface{}) (
136136
// insert a new Leaf
137137
newNode := &_Node{
138138
edges: []*_Edge{
139-
&_Edge{
139+
{
140140
label: []byte{},
141141
point: point,
142142
},
143-
&_Edge{
143+
{
144144
label: label,
145145
point: &_Leaf{
146146
originKey: originKey,
@@ -433,11 +433,13 @@ func (node *_Node) walkNode(suffix [][]byte, f func(labels [][]byte, value inter
433433
}
434434
}
435435

436+
// Tree represents a suffix tree.
436437
type Tree struct {
437438
root *_Node
438439
leavesNum int
439440
}
440441

442+
// NewTree create a suffix tree for future usage.
441443
func NewTree() *Tree {
442444
return &Tree{
443445
root: &_Node{
@@ -455,12 +457,12 @@ func (tree *Tree) Insert(key []byte, value interface{}) (oldValue interface{}, o
455457
}
456458
oldValue, ok = tree.root.insert(key, key, value)
457459
if ok && oldValue == nil {
458-
tree.leavesNum += 1
460+
tree.leavesNum++
459461
}
460462
return oldValue, ok
461463
}
462464

463-
// Given a key, Get returns the value itself and a boolean to indicate
465+
// Get returns the value of given key and a boolean to indicate
464466
// whether the value is found.
465467
func (tree *Tree) Get(key []byte) (value interface{}, found bool) {
466468
if key == nil || len(tree.root.edges) == 0 {
@@ -480,15 +482,15 @@ func (tree *Tree) LongestSuffix(key []byte) (matchedKey []byte, value interface{
480482
return tree.root.longestSuffix(key)
481483
}
482484

483-
// Given a key, Remove returns the value itself and a boolean to indicate
485+
// Remove returns the value of given key and a boolean to indicate
484486
// whethe the value is found. Then the value will be removed.
485487
func (tree *Tree) Remove(key []byte) (oldValue interface{}, found bool) {
486488
if key == nil || len(tree.root.edges) == 0 {
487489
return nil, false
488490
}
489491
oldValue, found, _ = tree.root.remove(key)
490492
if found {
491-
tree.leavesNum -= 1
493+
tree.leavesNum--
492494
}
493495
return oldValue, found
494496
}

suffix_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,28 @@ func TestWalkSuffix_Base(t *testing.T) {
257257
assert.Equal(t, 0, count)
258258
}
259259

260+
func TestInvalidInput(t *testing.T) {
261+
lists, tree := getFixtures()
262+
count := len(lists)
263+
tree.WalkSuffix(nil, func(key []byte, value interface{}) bool {
264+
count--
265+
return false
266+
})
267+
assert.Equal(t, 0, count)
268+
269+
_, ok := tree.Insert(nil, "any")
270+
assert.False(t, ok)
271+
272+
_, found := tree.Get(nil)
273+
assert.False(t, found)
274+
275+
_, found = tree.Remove(nil)
276+
assert.False(t, found)
277+
278+
_, _, found = tree.LongestSuffix(nil)
279+
assert.False(t, found)
280+
}
281+
260282
func dumpTestData(wordRef map[string]bool, tree *Tree, ops []string, errMsg string) {
261283
opDumpFile, _ := ioutil.TempFile("", "suffix_test_op_dump_")
262284
defer opDumpFile.Close()
@@ -266,7 +288,7 @@ func dumpTestData(wordRef map[string]bool, tree *Tree, ops []string, errMsg stri
266288
}
267289
println("\nWord status:")
268290
words := []string{}
269-
for word, _ := range wordRef {
291+
for word := range wordRef {
270292
words = append(words, word)
271293
}
272294
sort.Sort(sort.StringSlice(words))
@@ -371,7 +393,7 @@ func TestAlhoc(t *testing.T) {
371393
bs := string(b)
372394
if _, ok := wordRef[bs]; !ok {
373395
wordRef[bs] = true
374-
wordCount += 1
396+
wordCount++
375397
}
376398
ops = append(ops, "Insert\t"+bs)
377399
tree.Insert(b, bs)
@@ -398,7 +420,7 @@ func TestAlhoc(t *testing.T) {
398420
existedNum := 0
399421
for _, existed := range wordRef {
400422
if existed {
401-
existedNum += 1
423+
existedNum++
402424
}
403425
}
404426
if tree.Len() != existedNum {

0 commit comments

Comments
 (0)