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

Function TestVarint

go/fory/buffer_test.go:27–65  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestVarint(t *testing.T) {
28 err := &Error{}
29 for i := 1; i <= 32; i++ {
30 buf := NewByteBuffer(nil)
31 for j := 0; j < i; j++ {
32 buf.WriteByte_(1) // make address unaligned.
33 buf.ReadByte(err)
34 }
35 // Zigzag encoding doubles positive values: zigzag(n) = n * 2 for positive n
36 // So boundary values for positive numbers are:
37 // 1 byte: 0-63 (zigzag 0-126, fits in 7 bits)
38 // 2 bytes: 64-8191 (zigzag 128-16382, fits in 14 bits)
39 // 3 bytes: 8192-1048575 (zigzag fits in 21 bits)
40 // 4 bytes: 1048576-134217727 (zigzag fits in 28 bits)
41 // 5 bytes: 134217728+ (zigzag fits in 32 bits)
42 checkVarint(t, buf, 1, 1)
43 checkVarint(t, buf, 63, 1) // max 1-byte positive: zigzag(63)=126
44 checkVarint(t, buf, 64, 2) // min 2-byte positive: zigzag(64)=128
45 checkVarint(t, buf, 8191, 2) // max 2-byte positive: zigzag(8191)=16382
46 checkVarint(t, buf, 8192, 3) // min 3-byte positive: zigzag(8192)=16384
47 checkVarint(t, buf, 1048575, 3) // max 3-byte positive
48 checkVarint(t, buf, 1048576, 4) // min 4-byte positive
49 checkVarint(t, buf, 134217727, 4) // max 4-byte positive
50 checkVarint(t, buf, 134217728, 5) // min 5-byte positive
51 checkVarint(t, buf, MaxInt32, 5)
52 checkVarintWrite(t, buf, -1)
53 checkVarintWrite(t, buf, -1<<6)
54 checkVarintWrite(t, buf, -1<<7)
55 checkVarintWrite(t, buf, -1<<13)
56 checkVarintWrite(t, buf, -1<<14)
57 checkVarintWrite(t, buf, -1<<20)
58 checkVarintWrite(t, buf, -1<<21)
59 checkVarintWrite(t, buf, -1<<27)
60 checkVarintWrite(t, buf, -1<<28)
61 checkVarintWrite(t, buf, MinInt8)
62 checkVarintWrite(t, buf, MinInt16)
63 checkVarintWrite(t, buf, MinInt32)
64 }
65}
66
67func checkVarint(t *testing.T, buf *ByteBuffer, value int32, bytesWritten int8) {
68 err := &Error{}

Callers

nothing calls this directly

Calls 5

WriteByte_Method · 0.95
ReadByteMethod · 0.95
NewByteBufferFunction · 0.85
checkVarintFunction · 0.85
checkVarintWriteFunction · 0.85

Tested by

no test coverage detected