-
Notifications
You must be signed in to change notification settings - Fork 7
/
format_test.go
177 lines (165 loc) · 4.33 KB
/
format_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
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
package ron
import (
"math/rand"
"testing"
)
func TestUUID_String(t *testing.T) {
tests := [][]string{
{"}DcR-L8w", "}IYI-", "}IYI-0"},
{"0$author", "name$author2", "name{2"},
{"0", "1", "1"},
{"0", "123-0", "123-"},
{"0", "0000000001-orig", ")1-orig"},
{"1time01-src", "1time02+src", "{2+"},
{"hash%here", "hash%there", "%there"},
{"1", ")1", "0000000001"}, //7
{"0", "name$0", "name"},
{"time+orig", "time1+orig2", "(1(2"},
{"time-orig", "time1+orig2", "(1+(2"},
{"[1s9L3-[Wj8oO", "[1s9L3-(2Biejq", "-(2Biejq"},
{"}DcR-}L8w", "}IYI-", "}IYI}"}, //12
{"A$B", "A-B", "-"},
}
for i, tri := range tests {
context, e1 := ParseUUID([]byte(tri[0]))
uuid, e2 := ParseUUID([]byte(tri[1]))
if e1 != nil || e2 != nil {
t.Fail()
t.Log(e1, e2)
break
}
zip := uuid.ZipString(context)
if zip != tri[2] {
t.Logf("case %d: %s must be %s (%s, %s)", i, zip, tri[2], uuid.String(), context.String())
t.Fail()
}
}
}
func Test_IsBase(t *testing.T) {
for _, b := range BASE_PUNCT {
if !IsBase64(b) {
t.Fail()
}
}
if IsBase64('.') {
t.Fail()
}
}
func RandUint() uint64 {
var mask uint64 = (1 << 60) - 1
var num = rand.Uint64() & mask
length := rand.Uint32() % 60
num >>= length
if length > 6 {
shift := rand.Uint32() % length
num <<= shift
}
return num
}
func BenchmarkUnzip(b *testing.B) {
uuids := make([]UUID, b.N)
const m32 = 0xffffffff
for i := 0; i < b.N; i++ {
uuids[i] = NewEventUUID(RandUint(), RandUint())
//uuids[i] = UUID{uint64(i), '-', 100}
}
//sort.Slice(uuids, func(i, j int) bool { return uuids[i].LessThan(uuids[j]) })
zipped := make([]byte, 0, b.N*22+22)
lens := make([]int, b.N*2)
zipped = append(zipped, uuids[0].ZipString(ZERO_UUID)...)
lens[0] = len(zipped)
zipped = append(zipped, ' ')
for i := 1; i < b.N; i++ {
zipped = append(zipped, uuids[i].ZipString(uuids[i-1])...)
lens[i] = len(zipped) - lens[i-1]
zipped = append(zipped, ' ')
}
b.ResetTimer()
context := ZERO_UUID
var ro int = 0
for j := 0; j < b.N; j++ {
ind := lens[j] //bytes.IndexByte(zipped[ro:], ' ')
//unzip, l := ParseUUID(zipped[ro:ro+ind], context)
unzip := context
unzip, err := context.Parse(zipped[ro : ro+ind])
if err != nil {
b.Logf("parse fail at %d: %s should be %s context %s text '%s' err %s",
j, unzip.String(), uuids[j].String(), context.String(), string(zipped[ro:]), err.Error())
b.Fail()
break
}
if unzip != uuids[j] {
b.Logf("incorrect unzip at %d: %s should be %s context %s len %d segm %v\n",
j, unzip.String(), uuids[j].String(),
context.String(), lens[j], string(zipped[ro:]))
b.Fail()
break
}
ro += lens[j]
//fmt.Println(unzip.String())
ro += 1
context = unzip
}
//fmt.Println("compressed")
//b.Logf("%d bytes parsed\n", ro)
}
func TestOp_String(t *testing.T) {
// FIXME EMPTY_OP.String() is ".0#0..." !!!
str := "*lww#object@time-origin:loc=1,"
op := ParseFrameString(str)
if op.Count() != 1 || op.Atom(0).Type() != ATOM_INT {
t.Fail()
t.Logf("misparsed %s", str)
return
}
context := op.Clone()
op.atoms[2][0]++
op.atoms[3][0]++
cur := MakeFrame(1024)
cur.Append(context)
le := cur.Len()
cur.Append(op)
opstr := string(cur.Body[le:])
correct := "@)1:)1=1"
if opstr != correct {
t.Logf("incorrect: '%s' != '%s'", opstr, correct)
t.Fail()
}
}
func BenchmarkFormatOp(b *testing.B) {
str := "*lww#object@time-origin:loc=1"
op := ParseFrameString(str)
frame := MakeFrame(b.N*len(str)*2 + 100)
frame.Append(op)
for i := 0; i < b.N; i++ {
op.atoms[2][0]++
op.atoms[3][0]++
frame.Append(op)
}
}
type sample struct {
flags uint
correct string
}
func TestFormatOptions(t *testing.T) {
framestr := "*lww#test!@1:key'value'@2:number=1*rga#text@3'T'!*rga#text@6:3,@4'e'@5'x'@6't'*lww#more:a=1;"
formats := []sample{
{
FORMAT_FRAME_LINES | FORMAT_HEADER_SPACE,
"*lww#test! @1:key'value'@2:number=1\n*rga#text@3:0'T'! @6:3,@4'e'@5'x'@6't'\n*lww#more@0:a=1;", // TODO drop :0 @0
},
}
for k, f := range formats {
frame := OpenFrame([]byte(framestr))
formatted := MakeFormattedFrame(f.flags, 1024)
for !frame.EOF() {
formatted.Append(frame)
frame.Next()
}
if formatted.String() != f.correct {
t.Fail()
t.Logf("incorrect format at %d\n---\n%s\n---should be---\n%s\n", k, formatted.String(), f.correct)
t.Logf("parsed as %s\n", formatted.Rewind().Reformat(FRAME_FORMAT_CARPET).String())
}
}
}