MCPcopy Create free account
hub / github.com/apache/fory / TestUnsafePutVarUint32PhysicalWriteWidth

Function TestUnsafePutVarUint32PhysicalWriteWidth

go/fory/buffer_test.go:89–114  ·  view source on GitHub ↗

TestUnsafePutVarUint32PhysicalWriteWidth verifies that UnsafePutVarUint32 performs an 8-byte physical write for 5-byte varints and that Reserve(8) (as required by the contract) keeps those 8 bytes within the backing array.

(t *testing.T)

Source from the content-addressed store, hash-verified

87// an 8-byte physical write for 5-byte varints and that Reserve(8) (as required by
88// the contract) keeps those 8 bytes within the backing array.
89func TestUnsafePutVarUint32PhysicalWriteWidth(t *testing.T) {
90 const sentinelByte = byte(0xAB)
91 const totalCap = 16
92 backing := make([]byte, totalCap, totalCap)
93
94 // Fill [8, totalCap) with sentinels; [0, 8) is the reserved window.
95 for i := 8; i < totalCap; i++ {
96 backing[i] = sentinelByte
97 }
98
99 // Expose 8 bytes of len, matching Reserve(8) contract.
100 buf := NewByteBuffer(backing[:8])
101
102 // Reserve(8) should return immediately as len(data) is already 8.
103 buf.Reserve(8)
104
105 // Encode value >= 2^28 (5 varint bytes) which triggers 8-byte bulk write.
106 written := buf.UnsafePutVarUint32(0, 1<<28)
107 require.Equal(t, 5, written, "expected 5 logical bytes written")
108
109 // Verify bytes [8, totalCap) remain untouched by the 8-byte bulk write.
110 for i := 8; i < totalCap; i++ {
111 require.Equal(t, sentinelByte, backing[i],
112 "byte at index %d is outside the 8-byte reserved window and must not be written", i)
113 }
114}
115
116func TestReadVarUint32RejectsOverflowFifthByte(t *testing.T) {
117 for _, data := range [][]byte{

Callers

nothing calls this directly

Calls 4

ReserveMethod · 0.95
UnsafePutVarUint32Method · 0.95
NewByteBufferFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected