Files
qb/chunkenc/chunkenc_test.go

635 lines
13 KiB
Go
Raw Normal View History

2026-05-10 00:59:47 +00:00
package chunkenc
import (
2026-06-07 01:06:16 +00:00
"bytes"
2026-05-10 00:59:47 +00:00
"fmt"
2026-06-07 01:06:16 +00:00
"slices"
2026-05-10 00:59:47 +00:00
"testing"
)
func TestCumdelta(t *testing.T) {
var (
fracDigits byte = 0
2026-06-05 19:43:01 +00:00
buf = make([]byte, minBufferSize)
2026-05-10 00:59:47 +00:00
value float64
done bool
)
2026-06-07 01:06:16 +00:00
c := NewCumulativeDeltaCompressor(buf, 0, fracDigits)
2026-05-10 00:59:47 +00:00
// c.Append(1.55)
// c.Append(23)
// c.Append(23)
// c.Append(23)
// c.Append(23.5)
c.Append(130)
c.Append(191)
c.Append(248)
c.Append(305)
//fmt.Printf("pos: %d\n", c.pos)
//fmt.Printf("%d\n", buf.Chunks()[0])
2026-06-07 01:06:16 +00:00
d := NewCumulativeDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-10 00:59:47 +00:00
for range 8 {
value, done = d.NextValue()
fmt.Println(value, done)
}
}
func TestInsdelta(t *testing.T) {
var (
fracDigits byte = 2
2026-06-05 19:43:01 +00:00
buf = make([]byte, minBufferSize)
2026-05-10 00:59:47 +00:00
value float64
done bool
)
2026-06-07 01:06:16 +00:00
c := NewInstantDeltaCompressor(buf, 0, fracDigits)
2026-05-10 00:59:47 +00:00
c.Append(-1.55)
c.Append(23)
//fmt.Printf("pos: %d\n", c.pos)
//fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(23)
c.Append(23)
c.Append(-23.5)
//fmt.Printf("pos: %d\n", c.pos)
//fmt.Printf("%d\n", buf.Chunks()[0])
2026-06-07 01:06:16 +00:00
d := NewInstantDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-10 00:59:47 +00:00
for range 8 {
value, done = d.NextValue()
fmt.Println(value, done)
}
}
2026-06-07 01:06:16 +00:00
func TestTimeDeltaCompressor(t *testing.T) {
2026-05-10 00:59:47 +00:00
var (
2026-06-07 01:06:16 +00:00
testCases = []struct {
Nums []uint32
RepeatLastDeltaNTimes int
Code int
RequiredSize int
Buf []byte
LastUnixtime uint32
LastDelta uint32
H byte
}{
{
Nums: []uint32{
1780777000,
},
Code: addUnixtime,
RequiredSize: 4,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0, 0,
},
LastUnixtime: 1780777000,
LastDelta: 0,
H: 0,
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
},
Code: add1stDelta,
RequiredSize: 6,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0, 0,
},
LastUnixtime: 1780777060,
LastDelta: 60,
H: 128,
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
},
Code: startRun, // literal changed by run
RequiredSize: 6,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0, 0,
},
LastUnixtime: 1780777120,
LastDelta: 60,
H: 0, // run, length = 2
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
1780777130, // +70
1780777200, // +70
},
Code: startRun, // literal decreased by 1
RequiredSize: 7, // unixtime, end of literal, new delta, new h
Buf: []byte{
0, 0, 0, 0, 0, 0, 128, 188,
},
LastUnixtime: 1780777200,
LastDelta: 70,
H: 0, // run, length = 2
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
1780777180, // +60
},
Code: incrementRun,
RequiredSize: 5,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0, 0,
},
LastUnixtime: 1780777180,
LastDelta: 60,
H: 1, // run, length = 3
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
1780777180, // +60
1780777200, // +20
},
Code: endSeries, // run
RequiredSize: 8,
Buf: []byte{
0, 0, 0, 0, 0, 0,
1, // h of run, length = 3
188, // varUint64(60)
},
LastUnixtime: 1780777200,
LastDelta: 20,
H: 128, // literal, length = 1
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
},
RepeatLastDeltaNTimes: 128,
Code: incrementRun, // h byte full filled
RequiredSize: 5,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0, 0,
},
LastUnixtime: 1780777060 + 128*60,
LastDelta: 60,
H: 127, // run, length = 129
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
},
RepeatLastDeltaNTimes: 129,
Code: endSeries, // run, h byte overflowed
RequiredSize: 8,
Buf: []byte{
0, 0, 0, 0, 0, 0,
127, // h - end of run (length = 129)
188, // varUint64(60)
},
LastUnixtime: 1780777060 + 129*60,
LastDelta: 60,
H: 128, // literal, length = 1
},
{
Nums: []uint32{
1780777000,
1780777060, // +60
1780777122, // +62
},
Code: incrementLiteral,
RequiredSize: 7,
Buf: []byte{
0, 0, 0, 0, 0, 0, 0,
188, // varUint64(60)
},
LastUnixtime: 1780777122,
LastDelta: 62,
H: 129, // literal, length = 2
},
}
2026-05-10 00:59:47 +00:00
)
2026-06-07 01:06:16 +00:00
for caseIdx, testCase := range testCases {
//fmt.Println("----------")
var (
buf = make([]byte, 8)
code int
requiredSize int
)
c := NewTimeDeltaCompressor(buf, 0)
for _, num := range testCase.Nums {
code, requiredSize = c.CalcRequiredSpace(num)
c.Append(code, num)
}
if testCase.RepeatLastDeltaNTimes > 0 {
for range testCase.RepeatLastDeltaNTimes {
num := c.lastUnixtime + c.lastDelta
code, requiredSize = c.CalcRequiredSpace(num)
c.Append(code, num)
}
}
if code != testCase.Code {
t.Fatalf("%d: got code %d are not equal expected %d",
caseIdx, code, testCase.Code)
}
if requiredSize != testCase.RequiredSize {
t.Fatalf("%d: got requiredSize %d are not equal expected %d",
caseIdx, requiredSize, testCase.RequiredSize)
}
if !bytes.Equal(buf, testCase.Buf) {
t.Fatalf("%d: got buf %v are not equal expected %v",
caseIdx, buf, testCase.Buf)
}
if c.lastUnixtime != testCase.LastUnixtime {
t.Fatalf("%d: got lastUnixtime %d are not equal expected %d",
caseIdx, c.lastUnixtime, testCase.LastUnixtime)
}
if c.lastDelta != testCase.LastDelta {
t.Fatalf("%d: got lastDelta %d are not equal expected %d",
caseIdx, c.lastDelta, testCase.LastDelta)
}
if c.h != testCase.H {
t.Fatalf("%d: got h %d are not equal expected %d",
caseIdx, c.h, testCase.H)
}
}
}
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
func TestTimeDeltaDecompressorFromCapturedState(t *testing.T) {
var (
testCases = []struct {
Nums []uint32
//RepeatLastDeltaNTimes int
}{
{
Nums: []uint32{
1780777000,
},
},
{
// add1stDelta
Nums: []uint32{
1780777000,
1780777060, // +60
},
},
{
// startRun
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
},
},
{
// incrementRun
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
1780777180, // +60
},
},
{
// endRun
Nums: []uint32{
1780777000,
1780777060, // +60
1780777120, // +60
1780777180, // +60
1780777200, // +20
},
},
{
// incrementLiteral
Nums: []uint32{
1780777000,
1780777060, // +60
1780777122, // +62
},
},
{
// ...
Nums: []uint32{
1780777000,
1780777010, // +10
1780777120, // +10
1780777130, // +10
1780777135, // +5
1780777141, // +6
1780777148, // +7
1780777156, // +8
1780777165, // +9
1780777174, // +9
1780777183, // +9
1780777190, // +7
},
},
}
)
for caseIdx, testCase := range testCases {
var (
buf = make([]byte, 16)
decodedNums []uint32
)
c := NewTimeDeltaCompressor(buf, 0)
for _, num := range testCase.Nums {
code, _ := c.CalcRequiredSpace(num)
c.Append(code, num)
}
//
c.CaptureState()
d := c.CreateDecompressor()
//fmt.Println(testCase.Nums)
// fmt.Println("---------------")
// fmt.Println(buf)
for {
num, done := d.NextValue()
//fmt.Println(num, done)
if done {
break
}
decodedNums = append(decodedNums, num)
}
slices.Reverse(decodedNums)
if !slices.Equal(testCase.Nums, decodedNums) {
t.Fatalf("%d: got nums %v not equal expected %v", caseIdx, decodedNums, testCase.Nums)
}
}
}
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// func TestTimeDelta(t *testing.T) {
// var (
// buf = make([]byte, minBufferSize)
// value uint32
// done bool
// )
// c := NewReverseTimeDeltaCompressor(buf, 0)
// c.Append(10)
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// //c.Append(131)
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// //fmt.Printf("pos: %d\n", c.pos)
// //fmt.Printf("%d\n", buf.Chunks()[0])
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// c.Append(70)
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// c.Append(130)
// c.Append(191)
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// c.Append(248)
// c.Append(305)
// c.Append(330)
// c.Append(390)
// c.Append(450)
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// // fmt.Printf("pos: %d\n", c.pos)
// // fmt.Printf("%d\n", buf.Chunks()[0])
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// //c.Sync()
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
// // bound := c.GetState()
// // fmt.Println("AFTER SYNC")
// // fmt.Printf("pos: %d\n", c.pos)
// // chunks := buf.Chunks()
// // if len(chunks) > 0 {
// // fmt.Printf("%d\n", chunks[0])
// // }
// d := NewReverseTimeDeltaDecompressor(buf, c.Size())
// // d.RestoreFromBound(bound)
// //d.RestoreFromEnd()
// for range 12 {
// value, done = d.NextValue()
// fmt.Println(value, done)
// }
// }
2026-05-10 00:59:47 +00:00
func TestCumdeltaBound(t *testing.T) {
var (
fracDigits byte = 0
2026-06-05 19:43:01 +00:00
buf = make([]byte, minBufferSize)
2026-05-10 00:59:47 +00:00
value float64
done bool
)
2026-06-07 01:06:16 +00:00
c := NewCumulativeDeltaCompressor(buf, 0, fracDigits)
2026-05-10 00:59:47 +00:00
// c.Append(1.55)
// c.Append(23)
// c.Append(23)
// c.Append(23)
// c.Append(23.5)
c.Append(10)
c.Append(70)
c.Append(130)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(191)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(191)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
//////////////////////////
2026-05-10 19:15:11 +00:00
c.Lock()
2026-05-10 00:59:47 +00:00
2026-05-10 19:15:11 +00:00
//fmt.Printf("bound: pos=%d, h=%d\n", bound.Pos, bound.H)
2026-05-10 00:59:47 +00:00
c.Append(248)
c.Append(305)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
d := NewCumulativeDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-10 00:59:47 +00:00
d.RestoreFromEnd()
for range 8 {
value, done = d.NextValue()
fmt.Println(value, done)
}
2026-05-10 19:15:11 +00:00
//boundDecompressor := NewReverseCumulativeDeltaDecompressor(buf, c.Size(), fracDigits)
//boundDecompressor.RestoreFromBound(bound)
boundDecompressor := c.CreateDecompressor(fracDigits)
2026-05-10 00:59:47 +00:00
fmt.Println("from bound:")
for range 8 {
value, done = boundDecompressor.NextValue()
fmt.Println(value, done)
}
}
func TestInsdeltaBound(t *testing.T) {
var (
fracDigits byte = 0
2026-06-05 19:43:01 +00:00
buf = make([]byte, minBufferSize)
2026-05-10 00:59:47 +00:00
value float64
done bool
)
2026-06-07 01:06:16 +00:00
c := NewInstantDeltaCompressor(buf, 0, fracDigits)
2026-05-10 00:59:47 +00:00
// c.Append(1.55)
// c.Append(23)
// c.Append(23)
// c.Append(23)
// c.Append(23.5)
c.Append(10)
c.Append(70)
c.Append(130)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(191)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(191)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
//////////////////////////
2026-05-31 20:01:28 +00:00
//bound := c.GetState()
2026-05-10 00:59:47 +00:00
2026-05-31 20:01:28 +00:00
//fmt.Printf("bound: pos=%d, h=%d\n", bound.Pos, bound.H)
2026-05-10 00:59:47 +00:00
c.Append(248)
c.Append(305)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
d := NewInstantDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-10 00:59:47 +00:00
d.RestoreFromEnd()
for range 8 {
value, done = d.NextValue()
fmt.Println(value, done)
}
2026-06-07 01:06:16 +00:00
boundDecompressor := NewInstantDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-31 20:01:28 +00:00
//boundDecompressor.RestoreFromBound(bound)
2026-05-10 00:59:47 +00:00
fmt.Println("from bound:")
for range 8 {
value, done = boundDecompressor.NextValue()
fmt.Println(value, done)
}
}
func TestInsdeltaBound2(t *testing.T) {
var (
fracDigits byte = 0
2026-06-05 19:43:01 +00:00
buf = make([]byte, minBufferSize)
2026-05-10 00:59:47 +00:00
value float64
done bool
)
2026-06-07 01:06:16 +00:00
c := NewInstantDeltaCompressor(buf, 0, fracDigits)
2026-05-10 00:59:47 +00:00
// c.Append(1.55)
// c.Append(23)
// c.Append(23)
// c.Append(23)
// c.Append(23.5)
c.Append(305)
c.Append(248)
c.Append(191)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(191)
// fmt.Printf("pos: %d\n", c.pos)
// fmt.Printf("%d\n", buf.Chunks()[0])
c.Append(130)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
//////////////////////////
2026-05-31 20:01:28 +00:00
//bound := c.GetState()
2026-05-10 00:59:47 +00:00
2026-05-31 20:01:28 +00:00
//fmt.Printf("bound: pos=%d, h=%d\n", bound.Pos, bound.H)
2026-05-10 00:59:47 +00:00
c.Append(70)
c.Append(10)
fmt.Printf("pos: %d\n", c.pos)
2026-06-06 15:36:27 +03:00
fmt.Printf("%d\n", c.Chunks())
2026-05-10 00:59:47 +00:00
2026-06-07 01:06:16 +00:00
d := NewInstantDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-10 00:59:47 +00:00
d.RestoreFromEnd()
for range 8 {
value, done = d.NextValue()
fmt.Println(value, done)
}
2026-06-07 01:06:16 +00:00
boundDecompressor := NewInstantDeltaDecompressor(buf, c.Size(), fracDigits)
2026-05-31 20:01:28 +00:00
//sboundDecompressor.RestoreFromBound(bound)
2026-05-10 00:59:47 +00:00
fmt.Println("from bound:")
for range 8 {
value, done = boundDecompressor.NextValue()
fmt.Println(value, done)
}
}
2026-06-05 19:43:01 +00:00
func TestCap(t *testing.T) {
a := make([]byte, 0, 10)
fmt.Println("len:", len(a))
fmt.Println("cap:", cap(a))
a = append(a, 1)
fmt.Println("after append:")
fmt.Println("len:", len(a))
fmt.Println("cap:", cap(a))
fmt.Println("a:", a)
a[1] = 55
fmt.Println("after []:")
fmt.Println("len:", len(a))
fmt.Println("cap:", cap(a))
fmt.Println("a:", a)
a = append(a, 2)
fmt.Println("after 2nd append:")
fmt.Println("len:", len(a))
fmt.Println("cap:", cap(a))
fmt.Println("a:", a)
}