-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk_internal_test.go
More file actions
109 lines (95 loc) · 3.36 KB
/
chunk_internal_test.go
File metadata and controls
109 lines (95 loc) · 3.36 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
package sqlitecloud
import (
"testing"
)
func TestProtocolBufferFromValue(t *testing.T) {
tests := []struct {
name string
value interface{}
wantLen int // expected number of []byte buffers returned
wantType byte
}{
// Basic types
{"nil", nil, 1, CMD_NULL},
{"string", "hello", 1, CMD_ZEROSTRING},
{"int", int(42), 1, CMD_INT},
{"int8", int8(8), 1, CMD_INT},
{"int16", int16(16), 1, CMD_INT},
{"int32", int32(32), 1, CMD_INT},
{"int64", int64(64), 1, CMD_INT},
{"float32", float32(3.14), 1, CMD_FLOAT},
{"float64", float64(2.71), 1, CMD_FLOAT},
{"[]byte", []byte("blob"), 2, CMD_BLOB}, // header + data
// Unsigned integers
{"uint", uint(1), 1, CMD_INT},
{"uint8", uint8(1), 1, CMD_INT},
{"uint16", uint16(1), 1, CMD_INT},
{"uint32", uint32(1), 1, CMD_INT},
{"uint64", uint64(1), 1, CMD_INT},
// Pointer types (dereferenced)
{"*int", intPtr(42), 1, CMD_INT},
{"*string", strPtr("hello"), 1, CMD_ZEROSTRING},
{"*int nil", (*int)(nil), 1, CMD_NULL},
{"*string nil", (*string)(nil), 1, CMD_NULL},
// Unsupported types still return empty buffers
{"bool", true, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buffers := protocolBufferFromValue(tt.value)
if len(buffers) != tt.wantLen {
t.Errorf("protocolBufferFromValue(%T(%v)): got %d buffers, want %d", tt.value, tt.value, len(buffers), tt.wantLen)
}
if tt.wantLen > 0 && len(buffers) > 0 {
if buffers[0][0] != tt.wantType {
t.Errorf("protocolBufferFromValue(%T(%v)): got type %c, want %c", tt.value, tt.value, buffers[0][0], tt.wantType)
}
}
})
}
}
func TestProtocolBufferFromValueMixedArray(t *testing.T) {
// Simulates the loop in sendArray: builds buffers from a mixed values slice
// and checks that the number of buffer groups matches the number of values.
pInt := intPtr(99)
values := []interface{}{
"hello", // string -> 1 buffer
int(42), // int -> 1 buffer
nil, // nil -> 1 buffer
pInt, // *int -> 1 buffer (dereferenced to int)
float64(3), // float64 -> 1 buffer
uint(7), // uint -> 1 buffer
[]byte("x"), // []byte -> 2 buffers (header+data)
}
// Count how many values produce at least one buffer
buffersPerValue := make([]int, len(values))
totalBuffers := 0
missingValues := 0
for i, v := range values {
bufs := protocolBufferFromValue(v)
buffersPerValue[i] = len(bufs)
totalBuffers += len(bufs)
if len(bufs) == 0 {
missingValues++
t.Errorf("value[%d] (%T = %v) produced 0 buffers — will be silently dropped", i, v, v)
}
}
if missingValues > 0 {
t.Errorf("%d out of %d values produced no buffers and will be missing from the protocol message", missingValues, len(values))
}
// Reproduce the exact loop from sendArray
buffers := [][]byte{}
for _, v := range values {
buffers = append(buffers, protocolBufferFromValue(v)...)
}
t.Logf("values count: %d, total buffers: %d, buffers per value: %v", len(values), len(buffers), buffersPerValue)
// Every value must produce at least 1 buffer ([]byte produces 2)
expectedMinBuffers := len(values)
if len(buffers) < expectedMinBuffers {
t.Errorf("buffers array has %d elements, expected at least %d (one per value). %d values were silently dropped.",
len(buffers), expectedMinBuffers, missingValues)
}
}
// helpers
func intPtr(v int) *int { return &v }
func strPtr(v string) *string { return &v }