This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hack_test.go
79 lines (63 loc) · 1.83 KB
/
hack_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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bson
import "testing"
func TestStringArena(t *testing.T) {
sarena := NewStringArena(10)
s0 := sarena.NewString(nil)
checkint(t, len(sarena.buf), 0)
checkint(t, sarena.SpaceLeft(), 10)
checkstring(t, s0, "")
s1 := sarena.NewString([]byte("01234"))
checkint(t, len(sarena.buf), 5)
checkint(t, sarena.SpaceLeft(), 5)
checkstring(t, s1, "01234")
s2 := sarena.NewString([]byte("5678"))
checkint(t, len(sarena.buf), 9)
checkint(t, sarena.SpaceLeft(), 1)
checkstring(t, s2, "5678")
// s3 will be allocated outside of sarena
s3 := sarena.NewString([]byte("ab"))
checkint(t, len(sarena.buf), 9)
checkint(t, sarena.SpaceLeft(), 1)
checkstring(t, s3, "ab")
// s4 should still fit in sarena
s4 := sarena.NewString([]byte("9"))
checkint(t, len(sarena.buf), 10)
checkint(t, sarena.SpaceLeft(), 0)
checkstring(t, s4, "9")
sarena.buf[0] = 'A'
checkstring(t, s1, "A1234")
sarena.buf[5] = 'B'
checkstring(t, s2, "B678")
sarena.buf[9] = 'C'
// s3 will not change
checkstring(t, s3, "ab")
checkstring(t, s4, "C")
checkstring(t, sarena.str, "A1234B678C")
}
func checkstring(t *testing.T, actual, expected string) {
if actual != expected {
t.Errorf("received %s, expecting %s", actual, expected)
}
}
func checkint(t *testing.T, actual, expected int) {
if actual != expected {
t.Errorf("received %d, expecting %d", actual, expected)
}
}
func TestByteToString(t *testing.T) {
v1 := []byte("1234")
if s := StringFn(v1); s != "1234" {
t.Errorf("String(\"1234\"): %q, want 1234", s)
}
v1 = []byte("")
if s := StringFn(v1); s != "" {
t.Errorf("String(\"\"): %q, want empty", s)
}
v1 = nil
if s := StringFn(v1); s != "" {
t.Errorf("String(\"\"): %q, want empty", s)
}
}