Skip to content

Commit 00dffbf

Browse files
committed
add some test code and examples
1 parent a2ca546 commit 00dffbf

4 files changed

Lines changed: 137 additions & 10 deletions

File tree

cedar_bm_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
11
package cedar
2+
3+
import (
4+
"testing"
5+
6+
"github.com/vcaesar/tt"
7+
)
8+
9+
func init() {
10+
InitCd()
11+
}
12+
13+
func BenchmarkInsert(t *testing.B) {
14+
fn := func() {
15+
cd.Insert([]byte("a"), 1)
16+
cd.Insert([]byte("b"), 3)
17+
cd.Insert([]byte("d"), 6)
18+
}
19+
20+
tt.BM(t, fn)
21+
}
22+
23+
func BenchmarkJump(t *testing.B) {
24+
fn := func() {
25+
cd.Jump([]byte("a"), 1)
26+
}
27+
28+
tt.BM(t, fn)
29+
}
30+
31+
func BenchmarkFind(t *testing.B) {
32+
fn := func() {
33+
cd.Find([]byte("a"), 1)
34+
}
35+
36+
tt.BM(t, fn)
37+
}
38+
39+
func BenchmarkValue(t *testing.B) {
40+
fn := func() {
41+
cd.Value(1)
42+
}
43+
44+
tt.BM(t, fn)
45+
}
46+
47+
func BenchmarkUpdate(t *testing.B) {
48+
fn := func() {
49+
cd.Update([]byte("a"), 1)
50+
}
51+
52+
tt.BM(t, fn)
53+
}
54+
55+
func BenchmarkDelete(t *testing.B) {
56+
fn := func() {
57+
cd.Delete([]byte("b"))
58+
}
59+
60+
tt.BM(t, fn)
61+
}

cedar_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
11
package cedar
2+
3+
import (
4+
"testing"
5+
6+
"github.com/vcaesar/tt"
7+
)
8+
9+
var (
10+
cd *Cedar
11+
12+
words = []string{
13+
"a", "aa", "ab", "abc", "abcd", "abcdef",
14+
"太阳系", "太阳系水星", "太阳系金星", "太阳系地球", "太阳系火星",
15+
"太阳系木星", "太阳系土星", "太阳系天王星", "太阳系海王星",
16+
}
17+
)
18+
19+
func InitCd(reduced ...bool) error {
20+
cd = New(reduced...)
21+
return nil
22+
}
23+
24+
func TestLoadData(t *testing.T) {
25+
if cd == nil {
26+
cd = New()
27+
}
28+
29+
// insert the keys
30+
for i, word := range words {
31+
err := cd.Insert([]byte(word), i)
32+
tt.Nil(t, err)
33+
}
34+
35+
for _, word := range words {
36+
err := cd.Delete([]byte(word))
37+
tt.Nil(t, err)
38+
}
39+
40+
for i, word := range words {
41+
err := cd.Update([]byte(word), i)
42+
tt.Nil(t, err)
43+
}
44+
45+
// delete the keys
46+
for i := 0; i < len(words); i += 3 {
47+
err := cd.Delete([]byte(words[i]))
48+
tt.Nil(t, err)
49+
}
50+
}

examples/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/vcaesar/cedar"
7+
)
8+
9+
func main() {
10+
// Create a new cedar trie.
11+
d := cedar.New()
12+
d.Insert([]byte("ab"), 1)
13+
d.Insert([]byte("abc"), 2)
14+
d.Insert([]byte("abcd"), 3)
15+
16+
fmt.Println(d.Jump([]byte("ab"), 0))
17+
fmt.Println(d.Jump([]byte("bc"), 0))
18+
}

fn.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func (cd *Cedar) get(key []byte, from, pos int) *int {
3232
return &cd.array[to].baseV
3333
}
3434

35-
// GetVal get follow node by key, split by update()
36-
func (cd *Cedar) getNode(key []byte, from, pos int, redched ...bool) int {
35+
// getNode get follow node by key, split by update()
36+
func (cd *Cedar) getNode(key []byte, from, pos int, reduced ...bool) int {
3737
for ; pos < len(key); pos++ {
38-
if isReduced(redched...) {
38+
if isReduced(reduced...) {
3939
value := cd.array[from].baseV
4040
if value >= 0 && value != ValLimit {
4141
to := cd.follow(from, 0)
@@ -47,7 +47,7 @@ func (cd *Cedar) getNode(key []byte, from, pos int, redched ...bool) int {
4747
}
4848

4949
to := from
50-
if cd.array[from].baseV < 0 || !isReduced(redched...) {
50+
if cd.array[from].baseV < 0 || !isReduced(reduced...) {
5151
to = cd.follow(from, 0)
5252
}
5353

@@ -122,10 +122,10 @@ func (cd *Cedar) Insert(key []byte, val int) error {
122122
}
123123

124124
// Update the key for the value, it is public interface that works on &str
125-
func (cd *Cedar) Update(key []byte, value int, redched ...bool) error {
125+
func (cd *Cedar) Update(key []byte, value int, reduced ...bool) error {
126126
p := cd.get(key, 0, 0)
127127

128-
if *p == ValLimit && isReduced(redched...) {
128+
if *p == ValLimit && isReduced(reduced...) {
129129
*p = value
130130
return nil
131131
}
@@ -135,27 +135,27 @@ func (cd *Cedar) Update(key []byte, value int, redched ...bool) error {
135135
}
136136

137137
// Delete the key from the trie, the internal interface that works on &[u8]
138-
func (cd *Cedar) Delete(key []byte, redched ...bool) error {
138+
func (cd *Cedar) Delete(key []byte, reduced ...bool) error {
139139
// move the cursor to the right place and use erase__ to delete it.
140140
to, err := cd.Jump(key, 0)
141141
if err != nil {
142142
return ErrNoKey
143143
}
144144

145-
if cd.array[to].baseV < 0 && isReduced(redched...) {
145+
if cd.array[to].baseV < 0 && isReduced(reduced...) {
146146
base := cd.array[to].base()
147147
if cd.array[base].check == to {
148148
to = base
149149
}
150150
}
151151

152-
if !isReduced(redched...) {
152+
if !isReduced(reduced...) {
153153
to = cd.array[to].base()
154154
}
155155

156156
from := to
157157
for to > 0 {
158-
if isReduced(redched...) {
158+
if isReduced(reduced...) {
159159
from = cd.array[to].check
160160
}
161161
base := cd.array[from].base()

0 commit comments

Comments
 (0)