Skip to content

Commit bf2442a

Browse files
committed
Refactor tests
1) Refactor tests to make them easier to extend, also reduce duplicated code. 2) Add one or two additional tests to increase code coverage score.
1 parent 088296f commit bf2442a

File tree

7 files changed

+464
-245
lines changed

7 files changed

+464
-245
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
coverage.html
12
coverage.txt

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ go:
77
- master
88

99
script:
10-
- go test -v
11-
- go test -race -coverprofile=coverage.txt -covermode=atomic .
10+
- make test
11+
12+
notifications:
13+
email: false
1214

1315
after_success:
1416
- bash <(curl -s https://codecov.io/bash)

Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
GOPATH := /go
21
GOOS := linux
32
GOARCH := amd64
43

5-
.PHONY: run, clean
4+
.PHONY: clean
65

76
all: test
87

98
# .go files are reformatted to conform to gofmt standards
109
fmt:
11-
GOPATH=$(GOPATH) GOOS=$(GOOS) GOARCH=$(GOARCH) gofmt -d -e -s -w *.go
10+
GOOS=$(GOOS) GOARCH=$(GOARCH) gofmt -d -e -s -w *.go
1211

1312
lint: fmt
14-
GOPATH=$(GOPATH) GOOS=$(GOOS) GOARCH=$(GOARCH) golint -set_exit_status *.go
13+
GOOS=$(GOOS) GOARCH=$(GOARCH) golint -set_exit_status *.go
1514

1615
vet: lint
17-
GOPATH=$(GOPATH) GOOS=$(GOOS) GOARCH=$(GOARCH) go tool vet *.go
16+
GOOS=$(GOOS) GOARCH=$(GOARCH) go tool vet *.go
1817

1918
test: vet
20-
GOPATH=$(GOPATH) GOOS=$(GOOS) GOARCH=$(GOARCH) go test -v
19+
GOOS=$(GOOS) GOARCH=$(GOARCH) go test -race -coverprofile=coverage.txt -covermode=atomic -v .
20+
GOOS=$(GOOS) GOARCH=$(GOARCH) go tool cover -html=coverage.txt -o coverage.html
21+
22+
clean:
23+
@rm -f coverage.html
24+
@rm -f coverage.txt

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ UPDATE: I found this example (also from Wikipedia) that shows an interesting edg
1818

1919
![Edge_case](https://upload.wikimedia.org/wikipedia/commons/6/63/An_example_of_how_to_find_a_string_in_a_Patricia_trie.png)
2020

21-
It shows that __slow__ can be both a node - and a leaf - at the same time, something which
22-
I had not considered. Interestingly, my understanding was that the root node of the trie
23-
(trie apparently comes from _retrieval_ - although I have my doubts about this) was always
24-
a single character whereas this diagram shows the root node as the entire word `slow`.
21+
It shows that __slow__ can be both a node - and a leaf - at the same time. This is something
22+
which I had not considered (another example might be __real__ and __realistic__). Interestingly,
23+
my understanding was that the root node of the trie (it may - or may not - be true that trie
24+
comes from _retrieval_) is always a single character whereas this diagram shows the root node
25+
as the entire word `slow`.
2526

2627
For retrieval purposes I am inclined to persist with using a single character as a root.
2728
The only practical purpose for a trie that I have been able to find is for search bars
2829
and the like and being able to respond quickly to that first typed character sounds like
29-
what I am after.
30+
what I am after. In the case of __bytes__ this would make it possible to use the initial
31+
byte as an offset index, although I doubt this would be practical for runes.
3032

3133
## Motivation
3234

@@ -113,6 +115,7 @@ than for system testing.
113115
## To Do
114116

115117
- [ ] Investigate applications of Patricia tries
118+
- [x] Refactor tests to avoid some of the duplicated code
116119
- [ ] Find out the idiom for stacking __Insert__ and __Find__ tests (avoiding mocks)
117120
- [ ] Investigate whether byte-based __and__ rune-based options are viable
118121
- [ ] Find more examples of tries in use - specifically Rune-based CJKV (Chinese, Japanese, Korean, Vietnamese)

node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Node struct {
1111
childCount int
1212
}
1313

14-
// IsLeaf may be called to determine of the current node is a leaf.
14+
// IsLeaf may be called to determine if the current node is a leaf.
1515
func (n *Node) IsLeaf() bool {
1616
return n.childCount == 0
1717
}

trie.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Trie struct {
1313
}
1414

1515
// NewTrie is used to create a new radix trie.
16-
func NewTrie() *Trie {
17-
return &Trie{}
16+
func NewTrie() Trie {
17+
return Trie{}
1818
}
1919

2020
// Count returns the number of nodes in the trie.
@@ -111,7 +111,7 @@ func (t *Trie) Find(s string) (bool, *Node) {
111111
trimmed := strings.TrimSpace(s)
112112

113113
// Sanity check (should catch empty strings too)
114-
if len(s) < 2 {
114+
if len(trimmed) < 2 {
115115
return false, nil
116116
}
117117

0 commit comments

Comments
 (0)