added many var* funcs

This commit is contained in:
2026-06-04 23:56:18 +00:00
parent 12b8905852
commit bff16d774d
2 changed files with 478 additions and 58 deletions

View File

@@ -703,24 +703,25 @@ func TestWriteReadBool(t *testing.T) {
}
}
// VAR SIZE COUNT
// COUNT VAR SIZE
var varUint32TestCases = []struct {
Num uint32
Count int
}{
{Num: (1 << 7) - 1, Count: 1},
{Num: (1 << 7), Count: 2},
{Num: (1 << 14) - 1, Count: 2},
{Num: (1 << 14), Count: 3},
{Num: (1 << 21) - 1, Count: 3},
{Num: (1 << 21), Count: 4},
{Num: (1 << 28) - 1, Count: 4},
{Num: (1 << 28), Count: 5},
{Num: (1 << 32) - 1, Count: 5},
}
func TestCountVarUint32(t *testing.T) {
var testCases = []struct {
Num uint32
Count int
}{
{Num: (1 << 7) - 1, Count: 1},
{Num: (1 << 7), Count: 2},
{Num: (1 << 14) - 1, Count: 2},
{Num: (1 << 14), Count: 3},
{Num: (1 << 21) - 1, Count: 3},
{Num: (1 << 21), Count: 4},
{Num: (1 << 28) - 1, Count: 4},
{Num: (1 << 28), Count: 5},
{Num: (1 << 32) - 1, Count: 5},
}
for _, testCase := range testCases {
for _, testCase := range varUint32TestCases {
count := CountVarUint32(testCase.Num)
if count != testCase.Count {
t.Fatalf("calculated count %d is not equal %d for num %d\n",
@@ -729,30 +730,33 @@ func TestCountVarUint32(t *testing.T) {
}
}
var varUint64TestCases = []struct {
Num uint64
Count int
}{
{Num: 0, Count: 1},
{Num: 1, Count: 1},
{Num: (1 << 7) - 1, Count: 1},
{Num: (1 << 7), Count: 2},
{Num: (1 << 14) - 1, Count: 2},
{Num: (1 << 14), Count: 3},
{Num: (1 << 21) - 1, Count: 3},
{Num: (1 << 21), Count: 4},
{Num: (1 << 28) - 1, Count: 4},
{Num: (1 << 28), Count: 5},
{Num: (1 << 35) - 1, Count: 5},
{Num: (1 << 35), Count: 6},
{Num: (1 << 42) - 1, Count: 6},
{Num: (1 << 42), Count: 7},
{Num: (1 << 49) - 1, Count: 7},
{Num: (1 << 49), Count: 8},
{Num: (1 << 56) - 1, Count: 8},
{Num: (1 << 56), Count: 9},
{Num: (1 << 64) - 1, Count: 9},
}
func TestCountVarUint64(t *testing.T) {
var testCases = []struct {
Num uint64
Count int
}{
{Num: (1 << 7) - 1, Count: 1},
{Num: (1 << 7), Count: 2},
{Num: (1 << 14) - 1, Count: 2},
{Num: (1 << 14), Count: 3},
{Num: (1 << 21) - 1, Count: 3},
{Num: (1 << 21), Count: 4},
{Num: (1 << 28) - 1, Count: 4},
{Num: (1 << 28), Count: 5},
{Num: (1 << 35) - 1, Count: 5},
{Num: (1 << 35), Count: 6},
{Num: (1 << 42) - 1, Count: 6},
{Num: (1 << 42), Count: 7},
{Num: (1 << 49) - 1, Count: 7},
{Num: (1 << 49), Count: 8},
{Num: (1 << 56) - 1, Count: 8},
{Num: (1 << 56), Count: 9},
{Num: (1 << 64) - 1, Count: 9},
}
for _, testCase := range testCases {
for _, testCase := range varUint64TestCases {
count := CountVarUint64(testCase.Num)
if count != testCase.Count {
t.Fatalf("calculated count %d is not equal %d for num %d\n",
@@ -761,10 +765,65 @@ func TestCountVarUint64(t *testing.T) {
}
}
var varInt64TestCases = []struct {
Num int64
Count int
}{
{Num: -(1 << 63), Count: 9},
{Num: -(1 << 55) - 1, Count: 9},
{Num: -(1 << 55), Count: 8},
{Num: -(1 << 48) - 1, Count: 8},
{Num: -(1 << 48), Count: 7},
{Num: -(1 << 41) - 1, Count: 7},
{Num: -(1 << 41), Count: 6},
{Num: -(1 << 34) - 1, Count: 6},
{Num: -(1 << 34), Count: 5},
{Num: -(1 << 27) - 1, Count: 5},
{Num: -(1 << 27), Count: 4},
{Num: -(1 << 20) - 1, Count: 4},
{Num: -(1 << 20), Count: 3},
{Num: -(1 << 13) - 1, Count: 3},
{Num: -(1 << 13), Count: 2},
{Num: -(1 << 6) - 1, Count: 2},
{Num: -(1 << 6), Count: 1},
{Num: -1, Count: 1},
{Num: 0, Count: 1},
{Num: 1, Count: 1},
{Num: (1 << 6) - 1, Count: 1},
{Num: (1 << 6), Count: 2},
{Num: (1 << 13) - 1, Count: 2},
{Num: (1 << 13), Count: 3},
{Num: (1 << 20) - 1, Count: 3},
{Num: (1 << 20), Count: 4},
{Num: (1 << 27) - 1, Count: 4},
{Num: (1 << 27), Count: 5},
{Num: (1 << 34) - 1, Count: 5},
{Num: (1 << 34), Count: 6},
{Num: (1 << 41) - 1, Count: 6},
{Num: (1 << 41), Count: 7},
{Num: (1 << 48) - 1, Count: 7},
{Num: (1 << 48), Count: 8},
{Num: (1 << 55) - 1, Count: 8},
{Num: (1 << 55), Count: 9},
{Num: (1 << 63) - 1, Count: 9},
}
func TestCountVarInt64(t *testing.T) {
for _, testCase := range varInt64TestCases {
count := CountVarInt64(testCase.Num)
if count != testCase.Count {
t.Fatalf("calculated count %d is not equal %d for num %d\n",
count, testCase.Count, testCase.Num)
}
}
}
var varSizeTestCases = []struct {
Num int
Count int
}{
{Num: 0, Count: 1},
{Num: 1, Count: 1},
{Num: (1 << 7) - 1, Count: 1},
{Num: (1 << 7), Count: 2},
{Num: (1 << 14) - 1, Count: 2},
@@ -794,6 +853,62 @@ func TestCountVarSize(t *testing.T) {
}
}
// PUT & GET VAR INT64
func TestPutGetVarInt64(t *testing.T) {
for _, testCase := range varInt64TestCases {
a := make([]byte, 9)
putCount, err := PutVarInt64(a, testCase.Num)
if err != nil {
t.Fatalf("put num %d: %s", testCase.Num, err)
}
decoded, getCount, err := GetVarInt64(a)
if err != nil {
t.Fatalf("get num %d: %s", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
// PUT & GET VAR UINT64
func TestPutGetVarUint64(t *testing.T) {
for _, testCase := range varUint64TestCases {
a := make([]byte, 9)
putCount, err := PutVarUint64(a, testCase.Num)
if err != nil {
t.Fatalf("put %d: %s\n", testCase.Num, err)
}
decoded, getCount, err := GetVarUint64(a)
if err != nil {
t.Fatalf("get %d: %s\n", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
// WRITE & READ VAR SIZE
func TestWriteReadVarSize(t *testing.T) {
for _, testCase := range varSizeTestCases {
w := bytes.NewBuffer(nil)
@@ -821,6 +936,8 @@ func TestWriteReadVarSize(t *testing.T) {
}
}
// PUT & GET VAR SIZE
func TestPutGetVarSize(t *testing.T) {
for _, testCase := range varSizeTestCases {
a := make([]byte, 9)
@@ -848,6 +965,110 @@ func TestPutGetVarSize(t *testing.T) {
}
}
// PUT & GET REVERSE VAR UINT64 / INT64
func TestReversePutGetVarUint64(t *testing.T) {
for _, testCase := range varUint64TestCases {
a := make([]byte, 9)
putCount, err := ReversePutVarUint64(a, testCase.Num)
if err != nil {
t.Fatalf("put num %d: %s", testCase.Num, err)
}
decoded, getCount, err := ReverseGetVarUint64(a[:testCase.Count])
if err != nil {
t.Fatalf("get num %d: %s", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
func TestReversePutGetVarInt64(t *testing.T) {
for _, testCase := range varInt64TestCases {
a := make([]byte, 9)
putCount, err := ReversePutVarInt64(a, testCase.Num)
if err != nil {
t.Fatalf("put num %d: %s", testCase.Num, err)
}
decoded, getCount, err := ReverseGetVarInt64(a[:testCase.Count])
if err != nil {
t.Fatalf("get num %d: %s", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
// PUT TAIL VAR UINT64/INT64 & GET VAR UINT64/INT64
func TestTailPutGetVarInt64(t *testing.T) {
for _, testCase := range varInt64TestCases {
a := make([]byte, 9)
putCount, err := TailPutVarInt64(a, testCase.Num)
if err != nil {
t.Fatalf("put num %d: %s", testCase.Num, err)
}
decoded, getCount, err := GetVarInt64(a[9-testCase.Count:])
if err != nil {
t.Fatalf("get num %d: %s", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
func TestTailPutGetVarUint64(t *testing.T) {
for _, testCase := range varUint64TestCases {
a := make([]byte, 9)
putCount, err := TailPutVarUint64(a, testCase.Num)
if err != nil {
t.Fatalf("put num %d: %s", testCase.Num, err)
}
decoded, getCount, err := GetVarUint64(a[9-testCase.Count:])
if err != nil {
t.Fatalf("get num %d: %s", testCase.Num, err)
}
if decoded != testCase.Num {
t.Fatalf("num %d not equal decoded %d", testCase.Num, decoded)
}
if putCount != testCase.Count {
t.Fatalf("count %d not equal put count %d of num %d",
testCase.Count, putCount, testCase.Num)
}
if getCount != testCase.Count {
t.Fatalf("count %d not equal get count %d of num %d",
testCase.Count, getCount, testCase.Num)
}
}
}
// HELPERS
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"