forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteBufferTests.cs
635 lines (548 loc) · 19.2 KB
/
ByteBufferTests.cs
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
namespace FlatBuffers.Test
{
[FlatBuffersTestClass]
public class ByteBufferTests
{
[FlatBuffersTestMethod]
public void ByteBuffer_Length_MatchesBufferLength()
{
var buffer = new byte[1000];
var uut = new ByteBuffer(buffer);
Assert.AreEqual(buffer.Length, uut.Length);
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutBytePopulatesBufferAtZeroOffset()
{
var buffer = new byte[1];
var uut = new ByteBuffer(buffer);
uut.PutByte(0, (byte)99);
Assert.AreEqual((byte)99, buffer[0]);
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutByteCannotPutAtOffsetPastLength()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortPopulatesBufferCorrectly()
{
var buffer = new byte[2];
var uut = new ByteBuffer(buffer);
uut.PutShort(0, (short)1);
// Ensure Endianness was written correctly
Assert.AreEqual((byte)1, buffer[0]);
Assert.AreEqual((byte)0, buffer[1]);
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortCannotPutAtOffsetPastLength()
{
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99));
}
#endif
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLength()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutShortChecksLengthAndOffset()
{
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntPopulatesBufferCorrectly()
{
var buffer = new byte[4];
var uut = new ByteBuffer(buffer);
uut.PutInt(0, 0x0A0B0C0D);
// Ensure Endianness was written correctly
Assert.AreEqual(0x0D, buffer[0]);
Assert.AreEqual(0x0C, buffer[1]);
Assert.AreEqual(0x0B, buffer[2]);
Assert.AreEqual(0x0A, buffer[3]);
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntCannotPutAtOffsetPastLength()
{
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLength()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntChecksLengthAndOffset()
{
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongPopulatesBufferCorrectly()
{
var buffer = new byte[8];
var uut = new ByteBuffer(buffer);
uut.PutLong(0, 0x010203040A0B0C0D);
// Ensure Endianness was written correctly
Assert.AreEqual(0x0D, buffer[0]);
Assert.AreEqual(0x0C, buffer[1]);
Assert.AreEqual(0x0B, buffer[2]);
Assert.AreEqual(0x0A, buffer[3]);
Assert.AreEqual(0x04, buffer[4]);
Assert.AreEqual(0x03, buffer[5]);
Assert.AreEqual(0x02, buffer[6]);
Assert.AreEqual(0x01, buffer[7]);
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongCannotPutAtOffsetPastLength()
{
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLength()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D));
}
[FlatBuffersTestMethod]
public void ByteBuffer_PutLongChecksLengthAndOffset()
{
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteReturnsCorrectData()
{
var buffer = new byte[1];
buffer[0] = 99;
var uut = new ByteBuffer(buffer);
Assert.AreEqual((byte)99, uut.Get(0));
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_GetByteChecksOffset()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.Get(1));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortReturnsCorrectData()
{
var buffer = new byte[2];
buffer[0] = 1;
buffer[1] = 0;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(1, uut.GetShort(0));
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksOffset()
{
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetShortChecksLength()
{
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntReturnsCorrectData()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0));
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksOffset()
{
var uut = new ByteBuffer(4);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetIntChecksLength()
{
var uut = new ByteBuffer(2);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongReturnsCorrectData()
{
var buffer = new byte[8];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
buffer[4] = 0x04;
buffer[5] = 0x03;
buffer[6] = 0x02;
buffer[7] = 0x01;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(0x010203040A0B0C0D, uut.GetLong(0));
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksOffset()
{
var uut = new ByteBuffer(8);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8));
}
[FlatBuffersTestMethod]
public void ByteBuffer_GetLongChecksLength()
{
var uut = new ByteBuffer(7);
Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0));
}
#endif
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUshort()
{
const ushort original = (ushort)0x1234U;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0x3412U, reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUint()
{
const uint original = 0x12345678;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0x78563412U, reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ReverseBytesUlong()
{
const ulong original = 0x1234567890ABCDEFUL;
var reverse = ByteBuffer.ReverseBytes(original);
Assert.AreEqual(0xEFCDAB9078563412UL, reverse);
var rereverse = ByteBuffer.ReverseBytes(reverse);
Assert.AreEqual(original, rereverse);
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToFullArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_ToSizedArray_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.ArrayEqual(buffer, uut.ToFullArray());
}
[FlatBuffersTestMethod]
public void ByteBuffer_Duplicate_MatchesBuffer()
{
var buffer = new byte[4];
buffer[0] = 0x0D;
buffer[1] = 0x0C;
buffer[2] = 0x0B;
buffer[3] = 0x0A;
var uut = new ByteBuffer(buffer);
Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0));
// Advance by two bytes
uut.Position = 2; uut = uut.Duplicate();
Assert.AreEqual(0x0A0B, uut.GetShort(2));
// Advance by one more byte
uut.Position = 1; uut = uut.Duplicate();
Assert.AreEqual(0x0A, uut.Get(3));
}
[FlatBuffersTestMethod]
public void ByteBuffer_To_Array_Float()
{
const int len = 9;
// Construct the data array
var fData = new float[len];
fData[0] = 1.0079F;
fData[1] = 4.0026F;
fData[2] = 6.941F;
fData[3] = 9.0122F;
fData[4] = 10.811F;
fData[5] = 12.0107F;
fData[6] = 14.0067F;
fData[7] = 15.9994F;
fData[8] = 18.9984F;
// Tranfer it to a byte array
var buffer = new byte[sizeof(float) * fData.Length];
Buffer.BlockCopy(fData, 0, buffer, 0, buffer.Length);
// Create the Byte Buffer from byte array
var uut = new ByteBuffer(buffer);
// Get the full array back out and ensure they are equivalent
var bbArray = uut.ToArray<float>(0, len);
Assert.ArrayEqual(fData, bbArray);
// Get a portion of the full array back out and ensure the
// subrange agrees
var bbArray2 = uut.ToArray<float>(4, len - 1);
Assert.AreEqual(bbArray2.Length, len - 1);
for (int i = 1; i < len - 1; i++)
{
Assert.AreEqual(fData[i], bbArray2[i - 1]);
}
// Get a sub portion of the full array back out and ensure the
// subrange agrees
var bbArray3 = uut.ToArray<float>(8, len - 4);
Assert.AreEqual(bbArray3.Length, len - 4);
for (int i = 2; i < len - 4; i++)
{
Assert.AreEqual(fData[i], bbArray3[i - 2]);
}
}
public void ByteBuffer_Put_Array_Helper<T>(T[] data, int typeSize)
where T : struct
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// Put the data into the buffer and make sure the offset is
// calculated correctly
int nOffset = uut.Put(1024, data);
Assert.AreEqual(1024 - typeSize * data.Length, nOffset);
// Get the full array back out and ensure they are equivalent
var bbArray = uut.ToArray<T>(nOffset, data.Length);
Assert.ArrayEqual(data, bbArray);
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Float()
{
const int len = 9;
// Construct the data array
var data = new float[len];
data[0] = 1.0079F;
data[1] = 4.0026F;
data[2] = 6.941F;
data[3] = 9.0122F;
data[4] = 10.811F;
data[5] = 12.0107F;
data[6] = 14.0067F;
data[7] = 15.9994F;
data[8] = 18.9984F;
ByteBuffer_Put_Array_Helper(data, sizeof(float));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Double()
{
const int len = 9;
// Construct the data array
var data = new double[len];
data[0] = 1.0079;
data[1] = 4.0026;
data[2] = 6.941;
data[3] = 9.0122;
data[4] = 10.811;
data[5] = 12.0107;
data[6] = 14.0067;
data[7] = 15.9994;
data[8] = 18.9984;
ByteBuffer_Put_Array_Helper(data, sizeof(double));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Int()
{
const int len = 9;
// Construct the data array
var data = new int[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(int));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_UInt()
{
const int len = 9;
// Construct the data array
var data = new uint[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(uint));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Bool()
{
const int len = 9;
// Construct the data array
var data = new bool[len];
data[0] = true;
data[1] = true;
data[2] = false;
data[3] = true;
data[4] = false;
data[5] = true;
data[6] = true;
data[7] = true;
data[8] = false;
ByteBuffer_Put_Array_Helper(data, sizeof(bool));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Long()
{
const int len = 9;
// Construct the data array
var data = new long[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(long));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Byte()
{
const int len = 9;
// Construct the data array
var data = new byte[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(byte));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_SByte()
{
const int len = 9;
// Construct the data array
var data = new sbyte[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(sbyte));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Null_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// create a null array and try to put it into the buffer
float[] data = null;
Assert.Throws<ArgumentNullException>(() => uut.Put(1024, data));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Empty_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// create an array of length == 0, and try to put it into the buffer
float[] data = new float[0];
Assert.Throws<ArgumentException>(() => uut.Put(1024, data));
}
#pragma warning disable 0169
// These are purposely not used and the warning is suppress
private struct dummyStruct
{
int a;
float b;
}
#pragma warning restore 0169
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_IncorrectType_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// Create an array of dummy structures that shouldn't be
// able to be put into the buffer
var data = new dummyStruct[10];
Assert.Throws<ArgumentException>(() => uut.Put(1024, data));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Get_Double()
{
var uut = new ByteBuffer(1024);
double value = 3.14159265;
uut.PutDouble(900, value);
double getValue = uut.GetDouble(900);
Assert.AreEqual(value, getValue);
}
[FlatBuffersTestMethod]
public void ByteBuffer_Get_Float()
{
var uut = new ByteBuffer(1024);
float value = 3.14159265F;
uut.PutFloat(900, value);
double getValue = uut.GetFloat(900);
Assert.AreEqual(value, getValue);
}
}
}