-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathwrap_test.go
More file actions
185 lines (151 loc) · 3.41 KB
/
wrap_test.go
File metadata and controls
185 lines (151 loc) · 3.41 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package sqlite3
import (
"bytes"
"context"
"math"
"testing"
)
func Test_sqlite_new(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
t.Run("MaxUint32", func(t *testing.T) {
defer func() { _ = recover() }()
wrp.New(math.MaxUint32)
t.Error("want panic")
})
}
func Test_sqlite_newArena(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
arena := wrp.NewArena()
defer arena.Free()
const title = "Lorem ipsum"
ptr := arena.String(title)
if ptr == 0 {
t.Fatalf("got nullptr")
}
if got := wrp.ReadString(ptr, math.MaxInt); got != title {
t.Errorf("got %q, want %q", got, title)
}
const body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
ptr = arena.String(body)
if ptr == 0 {
t.Fatalf("got nullptr")
}
if got := wrp.ReadString(ptr, math.MaxInt); got != body {
t.Errorf("got %q, want %q", got, body)
}
ptr = arena.Bytes(nil)
if ptr != 0 {
t.Errorf("want nullptr")
}
ptr = arena.Bytes([]byte(title))
if ptr == 0 {
t.Fatalf("got nullptr")
}
if got := wrp.Bytes(ptr, int64(len(title))); string(got) != title {
t.Errorf("got %q, want %q", got, title)
}
arena.Free()
}
func Test_sqlite_newBytes(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
ptr := wrp.NewBytes(nil)
if ptr != 0 {
t.Errorf("got %#x, want nullptr", ptr)
}
buf := []byte("sqlite3")
ptr = wrp.NewBytes(buf)
if ptr == 0 {
t.Fatal("got nullptr, want a pointer")
}
want := buf
if got := wrp.Bytes(ptr, int64(len(want))); !bytes.Equal(got, want) {
t.Errorf("got %q, want %q", got, want)
}
}
func Test_sqlite_newString(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
ptr := wrp.NewString("")
if ptr == 0 {
t.Error("got nullptr, want a pointer")
}
str := "sqlite3\000sqlite3"
ptr = wrp.NewString(str)
if ptr == 0 {
t.Fatal("got nullptr, want a pointer")
}
want := str + "\000"
if got := wrp.Bytes(ptr, int64(len(want))); string(got) != want {
t.Errorf("got %q, want %q", got, want)
}
}
func Test_sqlite_getString(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
ptr := wrp.NewString("")
if ptr == 0 {
t.Error("got nullptr, want a pointer")
}
str := "sqlite3" + "\000 drop this"
ptr = wrp.NewString(str)
if ptr == 0 {
t.Fatal("got nullptr, want a pointer")
}
want := "sqlite3"
if got := wrp.ReadString(ptr, math.MaxInt); got != want {
t.Errorf("got %q, want %q", got, want)
}
if got := wrp.ReadString(ptr, 0); got != "" {
t.Errorf("got %q, want empty", got)
}
func() {
defer func() { _ = recover() }()
wrp.ReadString(ptr, int64(len(want))/2)
t.Error("want panic")
}()
func() {
defer func() { _ = recover() }()
wrp.ReadString(0, math.MaxInt)
t.Error("want panic")
}()
}
func Test_sqlite_free(t *testing.T) {
t.Parallel()
wrp, err := createWrapper(testContext(t))
if err != nil {
t.Fatal(err)
}
defer wrp.Close()
wrp.Free(0)
ptr := wrp.New(1)
if ptr == 0 {
t.Error("got nullptr, want a pointer")
}
wrp.Free(ptr)
}
func testContext(t testing.TB) context.Context {
return WithMaxMemory(t.Context(), 32*1024*1024)
}