-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
135 lines (106 loc) · 2.62 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package intern_test
import (
"fmt"
"github.com/chriso/go-intern"
)
func ExampleRepository_Intern() {
repo := intern.NewRepository()
fmt.Println(repo.Intern("foo"))
fmt.Println(repo.Intern("bar"))
fmt.Println(repo.Intern("baz"))
fmt.Println(repo.Intern("foo"))
// Output:
// 1
// 2
// 3
// 1
}
func ExampleRepository_Count() {
repo := intern.NewRepository()
fmt.Printf("Initial count is %d\n", repo.Count())
strings := []string{"foo", "bar", "qux", "qux", "qux", "foo"}
for _, str := range strings {
repo.Intern(str)
}
fmt.Printf("There are now %d unique strings\n", repo.Count())
// Output:
// Initial count is 0
// There are now 3 unique strings
}
func ExampleRepository_Lookup() {
repo := intern.NewRepository()
repo.Intern("foo")
for _, str := range []string{"foo", "bar"} {
if id, ok := repo.Lookup(str); ok {
fmt.Printf("Found string %#v with id %d\n", str, id)
} else {
fmt.Printf("Did not find string %#v\n", str)
}
}
// Output:
// Found string "foo" with id 1
// Did not find string "bar"
}
func ExampleRepository_LookupID() {
repo := intern.NewRepository()
repo.Intern("foo")
for _, id := range []uint32{1, 2} {
if str, ok := repo.LookupID(id); ok {
fmt.Printf("Found string %#v with id %d\n", str, id)
} else {
fmt.Printf("Did not find id %d\n", id)
}
}
// Output:
// Found string "foo" with id 1
// Did not find id 2
}
func ExampleRepository_Cursor() {
repo := intern.NewRepository()
strings := []string{"foo", "bar", "baz"}
for _, str := range strings {
repo.Intern(str)
}
cursor := repo.Cursor()
for cursor.Next() {
fmt.Printf("String %#v has id %d\n", cursor.String(), cursor.ID())
}
// Output:
// String "foo" has id 1
// String "bar" has id 2
// String "baz" has id 3
}
func ExampleRepository_Optimize() {
repo := intern.NewRepository()
frequencies := intern.NewFrequency()
strings := []string{"foo", "bar", "qux", "qux", "qux", "foo"}
for _, str := range strings {
id := repo.Intern(str)
frequencies.Add(id)
}
optimized := repo.Optimize(frequencies)
cursor := optimized.Cursor()
for cursor.Next() {
fmt.Printf("String %#v has id %d\n", cursor.String(), cursor.ID())
}
// Output:
// String "qux" has id 1
// String "foo" has id 2
// String "bar" has id 3
}
func ExampleRepository_Restore() {
repo := intern.NewRepository()
repo.Intern("foo")
snapshot := repo.Snapshot()
repo.Intern("bar")
repo.Intern("qux")
repo.Restore(snapshot)
repo.Intern("xyz")
cursor := repo.Cursor()
for cursor.Next() {
fmt.Printf("String %#v has id %d\n", cursor.String(), cursor.ID())
}
// Output:
// String "foo" has id 1
// String "xyz" has id 2
}