File size: 10,436 Bytes
5dd7e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package byteops

import (
	"crypto/rand"
	"fmt"
	"math/big"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const MaxUint32 = ^uint32(0)

func mustRandIntn(max int64) int {
	randInt, err := rand.Int(rand.Reader, big.NewInt(max))
	if err != nil {
		panic(fmt.Sprintf("mustRandIntn error: %v", err))
	}
	return int(randInt.Int64())
}

// Create a buffer with space for several values and first write into it and then test that the values can be read again
func TestReadAnWrite(t *testing.T) {
	valuesNumbers := []uint64{234, 78, 23, 66, 8, 9, 2, 346745, 1}
	valuesByteArray := make([]byte, mustRandIntn(500))
	rand.Read(valuesByteArray)

	writeBuffer := make([]byte, 2*Uint64Len+2*Uint32Len+2*Uint16Len+len(valuesByteArray))
	byteOpsWrite := NewReadWriter(writeBuffer)

	byteOpsWrite.WriteUint64(valuesNumbers[0])
	byteOpsWrite.WriteUint32(uint32(valuesNumbers[1]))
	byteOpsWrite.WriteUint32(uint32(valuesNumbers[2]))
	assert.Equal(t, byteOpsWrite.CopyBytesToBuffer(valuesByteArray), nil)
	byteOpsWrite.WriteUint16(uint16(valuesNumbers[3]))
	byteOpsWrite.WriteUint64(valuesNumbers[4])
	byteOpsWrite.WriteUint16(uint16(valuesNumbers[5]))

	byteOpsRead := NewReadWriter(writeBuffer)

	require.Equal(t, byteOpsRead.ReadUint64(), valuesNumbers[0])
	require.Equal(t, byteOpsRead.ReadUint32(), uint32(valuesNumbers[1]))
	require.Equal(t, byteOpsRead.ReadUint32(), uint32(valuesNumbers[2]))

	// we are going to do the next op twice (once with copying, once without)
	// to be able to rewind the buffer, let's cache the current position
	posBeforeByteArray := byteOpsRead.Position

	returnBuf, err := byteOpsRead.CopyBytesFromBuffer(uint64(len(valuesByteArray)), nil)
	assert.Equal(t, returnBuf, valuesByteArray)
	assert.Equal(t, err, nil)

	// rewind the buffer to where it was before the read
	byteOpsRead.MoveBufferToAbsolutePosition(posBeforeByteArray)

	subSlice := byteOpsRead.ReadBytesFromBuffer(uint64(len(valuesByteArray)))
	assert.Equal(t, subSlice, valuesByteArray)

	// now read again using the other method

	require.Equal(t, byteOpsRead.ReadUint16(), uint16(valuesNumbers[3]))
	require.Equal(t, byteOpsRead.ReadUint64(), valuesNumbers[4])
	require.Equal(t, byteOpsRead.ReadUint16(), uint16(valuesNumbers[5]))
}

// create buffer that is larger than uint32 and write to the end and then try to reread it
func TestReadAnWriteLargeBuffer(t *testing.T) {
	writeBuffer := make([]byte, uint64(MaxUint32)+4)
	byteOpsWrite := NewReadWriter(writeBuffer)
	byteOpsWrite.MoveBufferPositionForward(uint64(MaxUint32))
	byteOpsWrite.WriteUint16(uint16(10))

	byteOpsRead := NewReadWriter(writeBuffer)
	byteOpsRead.MoveBufferPositionForward(uint64(MaxUint32))
	require.Equal(t, byteOpsRead.ReadUint16(), uint16(10))
}

func TestWritingAndReadingBufferOfDynamicLength(t *testing.T) {
	empty := []byte{}

	t.Run("uint64 length indicator", func(t *testing.T) {
		bufLen := uint64(mustRandIntn(1024))
		buf := make([]byte, bufLen)
		rand.Read(buf)

		// uint64 length indicator + buffer + unrelated data at end of buffer
		totalBuf := make([]byte, 8+bufLen+8+8)
		bo := NewReadWriter(totalBuf)

		// write
		assert.NoError(t, bo.CopyBytesToBufferWithUint64LengthIndicator(buf))
		assert.Equal(t, buf, totalBuf[8:8+bufLen])
		bo.WriteUint64(17)
		assert.NoError(t, bo.CopyBytesToBufferWithUint64LengthIndicator(empty))

		// read
		bo = NewReadWriter(totalBuf)
		bufRead := bo.ReadBytesFromBufferWithUint64LengthIndicator()
		assert.Len(t, bufRead, int(bufLen))
		assert.Equal(t, buf, bufRead)
		assert.Equal(t, uint64(17), bo.ReadUint64())
		bufRead = bo.ReadBytesFromBufferWithUint64LengthIndicator()
		assert.Len(t, bufRead, 0)
		assert.NotNil(t, bufRead)

		// discard
		bo = NewReadWriter(totalBuf)
		discarded := bo.DiscardBytesFromBufferWithUint64LengthIndicator()
		assert.Equal(t, bufLen, discarded)
		assert.Equal(t, uint64(17), bo.ReadUint64())
	})

	t.Run("uint32 length indicator", func(t *testing.T) {
		bufLen := uint32(mustRandIntn(1024))
		buf := make([]byte, bufLen)
		rand.Read(buf)

		// uint32 length indicator + buffer + unrelated data at end of buffer
		totalBuf := make([]byte, 4+bufLen+4+4)
		bo := NewReadWriter(totalBuf)

		// write
		assert.NoError(t, bo.CopyBytesToBufferWithUint32LengthIndicator(buf))
		assert.Equal(t, buf, totalBuf[4:4+bufLen])
		bo.WriteUint32(17)
		assert.NoError(t, bo.CopyBytesToBufferWithUint32LengthIndicator(empty))

		// read
		bo = NewReadWriter(totalBuf)
		bufRead := bo.ReadBytesFromBufferWithUint32LengthIndicator()
		assert.Len(t, bufRead, int(bufLen))
		assert.Equal(t, buf, bufRead)
		assert.Equal(t, uint32(17), bo.ReadUint32())
		bufRead = bo.ReadBytesFromBufferWithUint32LengthIndicator()
		assert.Len(t, bufRead, 0)
		assert.NotNil(t, bufRead)

		// discard
		bo = NewReadWriter(totalBuf)
		discarded := bo.DiscardBytesFromBufferWithUint32LengthIndicator()
		assert.Equal(t, bufLen, discarded)
		assert.Equal(t, uint32(17), bo.ReadUint32())
	})
}

func TestIntsToByteVector(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		bytes := IntsToByteVector([]float64{})
		assert.Equal(t, []byte{}, bytes)
	})

	t.Run("non-empty array of u8s", func(t *testing.T) {
		bytes := IntsToByteVector([]float64{1, 2, 3})
		assert.Equal(t, []byte{
			0o1, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
			0o2, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
			0o3, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
		}, bytes)
	})

	t.Run("non-empty array of u64", func(t *testing.T) {
		bytes := IntsToByteVector([]float64{
			9007199254740992, // MaxFloat64
			9007199254740991,
			9007199254740990,
		})
		assert.Equal(t, []byte{
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00,
			0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00,
		}, bytes)
	})
}

func TestIntsFromByteVector(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		ints := IntsFromByteVector([]byte{})
		assert.Equal(t, []int64{}, ints)
	})

	t.Run("non-empty array of u8s", func(t *testing.T) {
		ints := IntsFromByteVector([]byte{
			0o1, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
			0o2, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
			0o3, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0, 0o0,
		})
		assert.Equal(t, []int64{1, 2, 3}, ints)
	})

	t.Run("non-empty array of u64", func(t *testing.T) {
		ints := IntsFromByteVector([]byte{
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00,
			0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00,
		})
		assert.Equal(t, []int64{
			9007199254740992, // MaxFloat64
			9007199254740991,
			9007199254740990,
		}, ints)
	})
}

func TestFp32SliceToBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		bytes := Fp32SliceToBytes([]float32{})
		assert.Equal(t, []byte{}, bytes)
	})

	t.Run("non-empty array", func(t *testing.T) {
		bytes := Fp32SliceToBytes([]float32{1.1, 2.2, 3.3})
		assert.Equal(t, []byte{
			0xcd, 0xcc, 0x8c, 0x3f,
			0xcd, 0xcc, 0xc, 0x40,
			0x33, 0x33, 0x53, 0x40,
		}, bytes)
	})
}

func TestFp32SliceOfSlicesToBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		bytes := Fp32SliceOfSlicesToBytes([][]float32{})
		assert.Equal(t, []byte{}, bytes)
	})

	t.Run("empty subarrays", func(t *testing.T) {
		bytes := Fp32SliceOfSlicesToBytes([][]float32{{}, {}, {}})
		assert.Equal(t, []byte{}, bytes)
	})

	t.Run("non-empty array", func(t *testing.T) {
		bytes := Fp32SliceOfSlicesToBytes([][]float32{
			{1.1, 2.2, 3.3},
			{4.4, 5.5, 6.6},
			{7.7, 8.8, 9.9},
		})
		assert.Equal(t, []byte{
			0x3, 0x0,
			0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0xc, 0x40, 0x33, 0x33, 0x53, 0x40,
			0xcd, 0xcc, 0x8c, 0x40, 0x0, 0x0, 0xb0, 0x40, 0x33, 0x33, 0xd3, 0x40,
			0x66, 0x66, 0xf6, 0x40, 0xcd, 0xcc, 0xc, 0x41, 0x66, 0x66, 0x1e, 0x41,
		}, bytes)
	})
}

func TestFp32SliceFromBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		slice := Fp32SliceFromBytes([]byte{})
		assert.Equal(t, []float32{}, slice)
	})

	t.Run("non-empty array", func(t *testing.T) {
		slice := Fp32SliceFromBytes([]byte{
			0xcd, 0xcc, 0x8c, 0x3f,
			0xcd, 0xcc, 0xc, 0x40,
			0x33, 0x33, 0x53, 0x40,
		})
		assert.Equal(t, []float32{1.1, 2.2, 3.3}, slice)
	})
}

func TestFp32SliceOfSlicesFromBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		slices, err := Fp32SliceOfSlicesFromBytes([]byte{})
		assert.Nil(t, err)
		assert.Equal(t, [][]float32{}, slices)
	})

	t.Run("dimension is 0", func(t *testing.T) {
		_, err := Fp32SliceOfSlicesFromBytes([]byte{0x0, 0x0})
		assert.NotNil(t, err)
		assert.Equal(t, "dimension cannot be 0", err.Error())
	})

	t.Run("empty subarrays", func(t *testing.T) {
		slices, err := Fp32SliceOfSlicesFromBytes([]byte{0x3, 0x0})
		assert.Nil(t, err)
		assert.Equal(t, [][]float32{}, slices)
	})

	t.Run("non-empty array", func(t *testing.T) {
		slices, err := Fp32SliceOfSlicesFromBytes([]byte{
			0x3, 0x0,
			0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0xc, 0x40, 0x33, 0x33, 0x53, 0x40,
			0xcd, 0xcc, 0x8c, 0x40, 0x0, 0x0, 0xb0, 0x40, 0x33, 0x33, 0xd3, 0x40,
			0x66, 0x66, 0xf6, 0x40, 0xcd, 0xcc, 0xc, 0x41, 0x66, 0x66, 0x1e, 0x41,
		})
		assert.Nil(t, err)
		assert.Equal(t, [][]float32{
			{1.1, 2.2, 3.3},
			{4.4, 5.5, 6.6},
			{7.7, 8.8, 9.9},
		}, slices)
	})
}

func TestFp64SliceToBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		bytes := Fp32SliceToBytes([]float32{})
		assert.Equal(t, []byte{}, bytes)
	})

	t.Run("non-empty array", func(t *testing.T) {
		bytes := Fp64SliceToBytes([]float64{1.1, 2.2, 3.3})
		assert.Equal(t, []byte{
			0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f,
			0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x1, 0x40,
			0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xa, 0x40,
		}, bytes)
	})
}

func TestFp64SliceFromBytes(t *testing.T) {
	t.Run("empty array", func(t *testing.T) {
		slice := Fp64SliceFromBytes([]byte{})
		assert.Equal(t, []float64{}, slice)
	})

	t.Run("non-empty array", func(t *testing.T) {
		slice := Fp64SliceFromBytes([]byte{
			0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f,
			0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x1, 0x40,
			0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xa, 0x40,
		})
		assert.Equal(t, []float64{1.1, 2.2, 3.3}, slice)
	})
}