Repository navigation
Expand file tree
/
Copy pathstats_test.go
More file actions
114 lines (105 loc) · 4.02 KB
/
Copy pathstats_test.go
File metadata and controls
114 lines (105 loc) · 4.02 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
// Per-column statistics.
package columns
import (
"bytes"
"encoding/binary"
"math"
"testing"
)
func TestFillStats(t *testing.T) {
// Byte slices compare as raw bytes, so the widest value is also the
// longest one.
bb, err := canonicalColumnTyped([]any{
[]byte{1, 2, 3}, []byte{1}, []byte{1, 2, 3, 4, 5},
}, TypeBytes)
if err != nil {
t.Fatalf("canonicalColumnTyped: %v", err)
}
var meta ColMeta
fillStatsTyped(&meta, bb)
if !bytes.Equal(meta.MinVal, []byte{1}) || !bytes.Equal(meta.MaxVal, []byte{1, 2, 3, 4, 5}) {
t.Errorf("fillStatsTyped min = %v, max = %v", meta.MinVal, meta.MaxVal)
}
if meta.MinLen != 1 || meta.MaxLen != 5 {
t.Errorf("fillStatsTyped MinLen = %d, MaxLen = %d, want 1 and 5", meta.MinLen, meta.MaxLen)
}
// Strings compare as strings but are stored as bytes, and their lengths
// vary independently of their order: "a" is smallest, "ccc" largest, and
// "bb" is neither.
ss, err := canonicalColumnTyped([]any{"ccc", "a", "bb", "a"}, TypeString)
if err != nil {
t.Fatalf("canonicalColumnTyped: %v", err)
}
var strMeta ColMeta
fillStatsTyped(&strMeta, ss)
if !bytes.Equal(strMeta.MinVal, []byte("a")) || !bytes.Equal(strMeta.MaxVal, []byte("ccc")) {
t.Errorf("fillStatsTyped string min = %q, max = %q", strMeta.MinVal, strMeta.MaxVal)
}
if strMeta.MinLen != 1 || strMeta.MaxLen != 3 {
t.Errorf("fillStatsTyped string MinLen = %d, MaxLen = %d, want 1 and 3",
strMeta.MinLen, strMeta.MaxLen)
}
// An empty column leaves the statistics unset.
var empty ColMeta
fillStatsTyped(&empty, []string(nil))
if empty.MinVal != nil || empty.MaxVal != nil || empty.MinLen != 0 || empty.MaxLen != 0 {
t.Errorf("fillStatsTyped of no values left statistics set: %+v", empty)
}
// Fixed width values are stored little endian, whose most significant byte is
// the last one, so the min and the max have to be found by comparing values
// rather than comparing the bytes from index 0. 2.5 and 0.5 first differ in
// the byte before the sign, and a byte wise comparison from the low end reads
// that byte first and calls 2.5 the smaller.
ff, err := canonicalColumnTyped([]any{
float64(2.5), float64(0.5), float64(4.5), float64(-1.0),
}, TypeFloat64)
if err != nil {
t.Fatalf("canonicalColumnTyped: %v", err)
}
var f64Meta ColMeta
fillStatsTyped(&f64Meta, ff)
var want [8]byte
binary.LittleEndian.PutUint64(want[:], math.Float64bits(-1.0))
if !bytes.Equal(f64Meta.MinVal, want[:]) {
t.Errorf("fillStatsTyped float64 min = %x, want %x", f64Meta.MinVal, want)
}
binary.LittleEndian.PutUint64(want[:], math.Float64bits(4.5))
if !bytes.Equal(f64Meta.MaxVal, want[:]) {
t.Errorf("fillStatsTyped float64 max = %x, want %x", f64Meta.MaxVal, want)
}
if f64Meta.MinLen != 8 || f64Meta.MaxLen != 8 {
t.Errorf("fillStatsTyped float64 MinLen = %d, MaxLen = %d, want 8 and 8",
f64Meta.MinLen, f64Meta.MaxLen)
}
// Signed integers are likewise ordered by value, not by byte.
ii, err := canonicalColumnTyped([]any{
int64(300), int64(-300), int64(1),
}, TypeInt64)
if err != nil {
t.Fatalf("canonicalColumnTyped: %v", err)
}
var i64Meta ColMeta
fillStatsTyped(&i64Meta, ii)
smallest := int64(-300)
binary.LittleEndian.PutUint64(want[:], uint64(smallest))
if !bytes.Equal(i64Meta.MinVal, want[:]) {
t.Errorf("fillStatsTyped int64 min = %x, want %x", i64Meta.MinVal, want)
}
binary.LittleEndian.PutUint64(want[:], 300)
if !bytes.Equal(i64Meta.MaxVal, want[:]) {
t.Errorf("fillStatsTyped int64 max = %x, want %x", i64Meta.MaxVal, want)
}
// Booleans are one byte each, so the min is false and the max is true.
bb2, err := canonicalColumnTyped([]any{true, false, true}, TypeBool)
if err != nil {
t.Fatalf("canonicalColumnTyped: %v", err)
}
var boolMeta ColMeta
fillStatsTyped(&boolMeta, bb2)
if !bytes.Equal(boolMeta.MinVal, []byte{0}) || !bytes.Equal(boolMeta.MaxVal, []byte{1}) {
t.Errorf("fillStatsTyped bool min = %v, max = %v", boolMeta.MinVal, boolMeta.MaxVal)
}
if _, err := canonicalColumnTyped([]any{nil}, TypeBool); err == nil {
t.Error("canonicalColumnTyped of nil: want error, got nil")
}
}