-
Notifications
You must be signed in to change notification settings - Fork 66
/
value_test.go
89 lines (84 loc) · 2.27 KB
/
value_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
package super_test
import (
"testing"
"github.com/brimdata/super"
"github.com/brimdata/super/zcode"
"github.com/stretchr/testify/assert"
)
func TestNewStringNotNull(t *testing.T) {
assert.NotNil(t, super.NewString("").Bytes())
}
func BenchmarkValueUnder(b *testing.B) {
b.Run("primitive", func(b *testing.B) {
val := super.Null
b.ResetTimer()
for i := 0; i < b.N; i++ {
val.Under()
}
})
b.Run("named", func(b *testing.B) {
typ, _ := super.NewContext().LookupTypeNamed("name", super.TypeNull)
val := super.NewValue(typ, nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
val.Under()
}
})
}
func TestValueValidate(t *testing.T) {
recType := super.NewTypeRecord(0, []super.Field{
super.NewField("f", super.NewTypeSet(0, super.TypeString)),
})
t.Run("set/error/duplicate-element", func(t *testing.T) {
var b zcode.Builder
b.BeginContainer()
b.Append([]byte("dup"))
b.Append([]byte("dup"))
// Don't normalize.
b.EndContainer()
val := super.NewValue(recType, b.Bytes())
assert.EqualError(t, val.Validate(), "invalid ZNG: duplicate set element")
})
t.Run("set/error/unsorted-elements", func(t *testing.T) {
var b zcode.Builder
b.BeginContainer()
b.Append([]byte("a"))
b.Append([]byte("z"))
b.Append([]byte("b"))
// Don't normalize.
b.EndContainer()
val := super.NewValue(recType, b.Bytes())
assert.EqualError(t, val.Validate(), "invalid ZNG: set elements not sorted")
})
t.Run("set/primitive-elements", func(t *testing.T) {
var b zcode.Builder
b.BeginContainer()
b.Append([]byte("dup"))
b.Append([]byte("dup"))
b.Append([]byte("z"))
b.Append([]byte("a"))
b.TransformContainer(super.NormalizeSet)
b.EndContainer()
val := super.NewValue(recType, b.Bytes())
assert.NoError(t, val.Validate())
})
t.Run("set/complex-elements", func(t *testing.T) {
var b zcode.Builder
b.BeginContainer()
for _, s := range []string{"dup", "dup", "z", "a"} {
b.BeginContainer()
b.Append([]byte(s))
b.EndContainer()
}
b.TransformContainer(super.NormalizeSet)
b.EndContainer()
r := super.NewValue(
super.NewTypeRecord(0, []super.Field{
super.NewField("f", super.NewTypeSet(0, super.NewTypeRecord(0, []super.Field{
super.NewField("g", super.TypeString),
}))),
}),
b.Bytes())
assert.NoError(t, r.Validate())
})
}