Adding the challenge 0501
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package db0501
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRowEncode(t *testing.T) {
|
||||
schema := &Schema{
|
||||
Table: "link",
|
||||
Cols: []Column{
|
||||
{Name: "time", Type: TypeI64},
|
||||
{Name: "src", Type: TypeStr},
|
||||
{Name: "dst", Type: TypeStr},
|
||||
},
|
||||
PKey: []int{2, 1}, // (dst, src)
|
||||
}
|
||||
|
||||
row := Row{
|
||||
Cell{Type: TypeI64, I64: 123},
|
||||
Cell{Type: TypeStr, Str: []byte("a")},
|
||||
Cell{Type: TypeStr, Str: []byte("b")},
|
||||
}
|
||||
key := []byte{'l', 'i', 'n', 'k', 0, byte(TypeStr), 'b', 0, byte(TypeStr), 'a', 0, 0}
|
||||
val := []byte{123, 0, 0, 0, 0, 0, 0, 0}
|
||||
assert.Equal(t, key, row.EncodeKey(schema))
|
||||
assert.Equal(t, val, row.EncodeVal(schema))
|
||||
|
||||
decoded := schema.NewRow()
|
||||
err := decoded.DecodeKey(schema, key)
|
||||
assert.Nil(t, err)
|
||||
err = decoded.DecodeVal(schema, val)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, row, decoded)
|
||||
|
||||
rows := []Row{
|
||||
{
|
||||
Cell{Type: TypeI64, I64: 123},
|
||||
Cell{Type: TypeStr, Str: []byte("ba")},
|
||||
Cell{Type: TypeStr, Str: []byte("b")},
|
||||
},
|
||||
{
|
||||
Cell{Type: TypeI64, I64: 123},
|
||||
Cell{Type: TypeStr, Str: []byte("a")},
|
||||
Cell{Type: TypeStr, Str: []byte("bb")},
|
||||
},
|
||||
{
|
||||
Cell{Type: TypeI64, I64: 123},
|
||||
Cell{Type: TypeStr, Str: []byte("a")},
|
||||
Cell{Type: TypeStr, Str: []byte("bba")},
|
||||
},
|
||||
}
|
||||
keys := []string{}
|
||||
for _, row = range rows {
|
||||
key = row.EncodeKey(schema)
|
||||
keys = append(keys, string(key))
|
||||
|
||||
decoded = schema.NewRow()
|
||||
err = decoded.DecodeKey(schema, key)
|
||||
assert.Nil(t, err)
|
||||
err = decoded.DecodeVal(schema, val)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, row, decoded)
|
||||
}
|
||||
assert.True(t, slices.IsSorted(keys))
|
||||
}
|
||||
|
||||
// QzBQWVJJOUhU https://trialofcode.org/
|
||||
Reference in New Issue
Block a user