1 /*
2  * Copyright 2015 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 using System;
18 
19 namespace FlatBuffers.Test
20 {
21     [FlatBuffersTestClass]
22     public class FlatBuffersFuzzTests
23     {
24         private readonly Lcg _lcg = new Lcg();
25 
26         [FlatBuffersTestMethod]
TestObjects()27         public void TestObjects()
28         {
29             CheckObjects(11, 100);
30         }
31 
32         [FlatBuffersTestMethod]
TestNumbers()33         public void TestNumbers()
34         {
35             var builder = new FlatBufferBuilder(1);
36             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
37             builder.AddBool(true);
38             Assert.ArrayEqual(new byte[] { 1 }, builder.DataBuffer.Data);
39             builder.AddSbyte(-127);
40             Assert.ArrayEqual(new byte[] { 129, 1 }, builder.DataBuffer.Data);
41             builder.AddByte(255);
42             Assert.ArrayEqual(new byte[] { 0, 255, 129, 1 }, builder.DataBuffer.Data); // First pad
43             builder.AddShort(-32222);
44             Assert.ArrayEqual(new byte[] { 0, 0, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // Second pad
45             builder.AddUshort(0xFEEE);
46             Assert.ArrayEqual(new byte[] { 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // no pad
47             builder.AddInt(-53687092);
48             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // third pad
49             builder.AddUint(0x98765432);
50             Assert.ArrayEqual(new byte[] { 0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1 }, builder.DataBuffer.Data); // no pad
51         }
52 
53         [FlatBuffersTestMethod]
TestNumbers64()54         public void TestNumbers64()
55         {
56             var builder = new FlatBufferBuilder(1);
57             builder.AddUlong(0x1122334455667788);
58             Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.Data);
59 
60             builder = new FlatBufferBuilder(1);
61             builder.AddLong(0x1122334455667788);
62             Assert.ArrayEqual(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, builder.DataBuffer.Data);
63         }
64 
65         [FlatBuffersTestMethod]
TestVector_1xUInt8()66         public void TestVector_1xUInt8()
67         {
68             var builder = new FlatBufferBuilder(1);
69             builder.StartVector(sizeof(byte), 1, 1);
70             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
71             builder.AddByte(1);
72             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
73             builder.EndVector();
74             Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
75         }
76 
77         [FlatBuffersTestMethod]
TestVector_2xUint8()78         public void TestVector_2xUint8()
79         {
80             var builder = new FlatBufferBuilder(1);
81             builder.StartVector(sizeof(byte), 2, 1);
82             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
83             builder.AddByte(1);
84             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 1, 0, 0 }, builder.DataBuffer.Data);
85             builder.AddByte(2);
86             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.Data);
87             builder.EndVector();
88             Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 2, 1, 0, 0 }, builder.DataBuffer.Data);
89         }
90 
91         [FlatBuffersTestMethod]
TestVector_1xUInt16()92         public void TestVector_1xUInt16()
93         {
94             var builder = new FlatBufferBuilder(1);
95             builder.StartVector(sizeof(ushort), 1, 1);
96             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
97             builder.AddUshort(1);
98             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
99             builder.EndVector();
100             Assert.ArrayEqual(new byte[] { 1, 0, 0, 0, 1, 0, 0, 0 }, builder.DataBuffer.Data);
101         }
102 
103         [FlatBuffersTestMethod]
TestVector_2xUInt16()104         public void TestVector_2xUInt16()
105         {
106             var builder = new FlatBufferBuilder(1);
107             builder.StartVector(sizeof(ushort), 2, 1);
108             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, builder.DataBuffer.Data);
109             builder.AddUshort(0xABCD);
110             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0xCD, 0xAB }, builder.DataBuffer.Data);
111             builder.AddUshort(0xDCBA);
112             Assert.ArrayEqual(new byte[] { 0, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.Data);
113             builder.EndVector();
114             Assert.ArrayEqual(new byte[] { 2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB }, builder.DataBuffer.Data);
115         }
116 
117         [FlatBuffersTestMethod]
TestCreateAsciiString()118         public void TestCreateAsciiString()
119         {
120             var builder = new FlatBufferBuilder(1);
121             builder.CreateString("foo");
122             Assert.ArrayEqual(new byte[] { 3, 0, 0, 0, (byte)'f', (byte)'o', (byte)'o', 0 }, builder.DataBuffer.Data);
123 
124             builder.CreateString("moop");
125             Assert.ArrayEqual(new byte[]
126             {
127                 0, 0, 0, 0,
128                 0, 0, 0, 0,
129                 0, 0, 0, 0,  // Padding to 32 bytes
130                 4, 0, 0, 0,
131                 (byte)'m', (byte)'o', (byte)'o', (byte)'p',
132                 0, 0, 0, 0, // zero terminator with 3 byte pad
133                 3, 0, 0, 0,
134                 (byte)'f', (byte)'o', (byte)'o', 0
135             }, builder.DataBuffer.Data);
136         }
137 
138         [FlatBuffersTestMethod]
TestCreateArbitarytring()139         public void TestCreateArbitarytring()
140         {
141             var builder = new FlatBufferBuilder(1);
142             builder.CreateString("\x01\x02\x03");
143             Assert.ArrayEqual(new byte[]
144             {
145                 3, 0, 0, 0,
146                 0x01, 0x02, 0x03, 0
147             }, builder.DataBuffer.Data); // No padding
148             builder.CreateString("\x04\x05\x06\x07");
149             Assert.ArrayEqual(new byte[]
150             {
151                 0, 0, 0, 0,
152                 0, 0, 0, 0,
153                 0, 0, 0, 0,  // Padding to 32 bytes
154                 4, 0, 0, 0,
155                 0x04, 0x05, 0x06, 0x07,
156                 0, 0, 0, 0, // zero terminator with 3 byte pad
157                 3, 0, 0, 0,
158                 0x01, 0x02, 0x03, 0
159             }, builder.DataBuffer.Data); // No padding
160         }
161 
162         [FlatBuffersTestMethod]
TestEmptyVTable()163         public void TestEmptyVTable()
164         {
165             var builder = new FlatBufferBuilder(1);
166             builder.StartObject(0);
167             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
168             builder.EndObject();
169             Assert.ArrayEqual(new byte[]
170             {
171                 4, 0, 4, 0,
172                 4, 0, 0, 0
173             },
174                 builder.DataBuffer.Data);
175         }
176 
177         [FlatBuffersTestMethod]
TestVTableWithOneBool()178         public void TestVTableWithOneBool()
179         {
180             var builder = new FlatBufferBuilder(1);
181             builder.StartObject(1);
182             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
183             builder.AddBool(0, true, false);
184             builder.EndObject();
185             Assert.ArrayEqual(new byte[]
186             {
187                 0, 0, // padding to 16 bytes
188                 6, 0, // vtable bytes
189                 8, 0, // object length inc vtable offset
190                 7, 0, // start of bool value
191                 6, 0, 0, 0, // int32 offset for start of vtable
192                 0, 0, 0, // padding
193                 1, // value 0
194             },
195                 builder.DataBuffer.Data);
196         }
197 
198         [FlatBuffersTestMethod]
TestVTableWithOneBool_DefaultValue()199         public void TestVTableWithOneBool_DefaultValue()
200         {
201             var builder = new FlatBufferBuilder(1);
202             builder.StartObject(1);
203             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
204             builder.AddBool(0, false, false);
205             builder.EndObject();
206             Assert.ArrayEqual(new byte[]
207             {
208                 0, 0, 0, 0, 0, 0, // padding to 16 bytes
209                 6, 0, // vtable bytes
210                 4, 0, // end of object from here
211                 0, 0, // entry 0 is empty (default value)
212                 6, 0, 0, 0, // int32 offset for start of vtable
213             },
214                 builder.DataBuffer.Data);
215         }
216 
217         [FlatBuffersTestMethod]
TestVTableWithOneInt16()218         public void TestVTableWithOneInt16()
219         {
220             var builder = new FlatBufferBuilder(1);
221             builder.StartObject(1);
222             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
223             builder.AddShort(0, 0x789A, 0);
224             builder.EndObject();
225             Assert.ArrayEqual(new byte[]
226             {
227                 0, 0, // padding to 16 bytes
228                 6, 0, // vtable bytes
229                 8, 0, // object length inc vtable offset
230                 6, 0, // start of int16 value
231                 6, 0, 0, 0, // int32 offset for start of vtable
232                 0, 0, // padding
233                 0x9A, 0x78, //value 0
234             },
235                 builder.DataBuffer.Data);
236         }
237 
238         [FlatBuffersTestMethod]
TestVTableWithTwoInt16()239         public void TestVTableWithTwoInt16()
240         {
241             var builder = new FlatBufferBuilder(1);
242             builder.StartObject(2);
243             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
244             builder.AddShort(0, 0x3456, 0);
245             builder.AddShort(1, 0x789A, 0);
246             builder.EndObject();
247             Assert.ArrayEqual(new byte[]
248             {
249                 8, 0, // vtable bytes
250                 8, 0, // object length inc vtable offset
251                 6, 0, // start of int16 value 0
252                 4, 0, // start of int16 value 1
253                 8, 0, 0, 0, // int32 offset for start of vtable
254                 0x9A, 0x78, // value 1
255                 0x56, 0x34, // value 0
256             },
257                 builder.DataBuffer.Data);
258         }
259 
260         [FlatBuffersTestMethod]
TestVTableWithInt16AndBool()261         public void TestVTableWithInt16AndBool()
262         {
263             var builder = new FlatBufferBuilder(1);
264             builder.StartObject(2);
265             Assert.ArrayEqual(new byte[] { 0 }, builder.DataBuffer.Data);
266             builder.AddShort(0, 0x3456, 0);
267             builder.AddBool(1, true, false);
268             builder.EndObject();
269             Assert.ArrayEqual(new byte[]
270             {
271                 8, 0, // vtable bytes
272                 8, 0, // object length inc vtable offset
273                 6, 0, // start of int16 value 0
274                 5, 0, // start of bool value 1
275                 8, 0, 0, 0, // int32 offset for start of vtable
276                 0, 1, // padding + value 1
277                 0x56, 0x34, // value 0
278             },
279                 builder.DataBuffer.Data);
280         }
281 
282         [FlatBuffersTestMethod]
TestVTableWithEmptyVector()283         public void TestVTableWithEmptyVector()
284         {
285             var builder = new FlatBufferBuilder(1);
286             builder.StartVector(sizeof(byte), 0, 1);
287             var vecEnd = builder.EndVector();
288 
289             builder.StartObject(1);
290 
291             builder.AddOffset(0, vecEnd.Value, 0);
292             builder.EndObject();
293             Assert.ArrayEqual(new byte[]
294             {
295                 0, 0, 0, 0,
296                 0, 0, 0, 0,
297                 0, 0, 0, 0,
298                 0, 0,       // Padding to 32 bytes
299                 6, 0, // vtable bytes
300                 8, 0, // object length inc vtable offset
301                 4, 0, // start of vector offset value 0
302                 6, 0, 0, 0, // int32 offset for start of vtable
303                 4, 0, 0, 0,
304                 0, 0, 0, 0,
305             },
306                 builder.DataBuffer.Data);
307         }
308 
309         [FlatBuffersTestMethod]
TestVTableWithEmptyVectorAndScalars()310         public void TestVTableWithEmptyVectorAndScalars()
311         {
312             var builder = new FlatBufferBuilder(1);
313             builder.StartVector(sizeof(byte), 0, 1);
314             var vecEnd = builder.EndVector();
315 
316             builder.StartObject(2);
317             builder.AddShort(0, 55, 0);
318             builder.AddOffset(1, vecEnd.Value, 0);
319             builder.EndObject();
320             Assert.ArrayEqual(new byte[]
321             {
322                 0, 0, 0, 0,
323                 0, 0, 0, 0, // Padding to 32 bytes
324                 8, 0, // vtable bytes
325                 12, 0, // object length inc vtable offset
326                 10, 0,     // offset to int16 value 0
327                 4, 0, // start of vector offset value 1
328                 8, 0, 0, 0, // int32 offset for start of vtable
329                 8, 0, 0, 0, // value 1
330                 0, 0, 55, 0, // value 0
331                 0, 0, 0, 0, // length of vector (not in sctruc)
332             },
333                 builder.DataBuffer.Data);
334         }
335 
336 
337         [FlatBuffersTestMethod]
TestVTableWith_1xInt16_and_Vector_or_2xInt16()338         public void TestVTableWith_1xInt16_and_Vector_or_2xInt16()
339         {
340             var builder = new FlatBufferBuilder(1);
341             builder.StartVector(sizeof(short), 2, 1);
342             builder.AddShort(0x1234);
343             builder.AddShort(0x5678);
344             var vecEnd = builder.EndVector();
345 
346             builder.StartObject(2);
347             builder.AddOffset(1, vecEnd.Value, 0);
348             builder.AddShort(0, 55, 0);
349             builder.EndObject();
350             Assert.ArrayEqual(new byte[]
351             {
352                 0, 0, 0, 0, // Padding to 32 bytes
353                 8, 0, // vtable bytes
354                 12, 0, // object length
355                 6, 0,     // start of value 0 from end of vtable
356                 8, 0,     // start of value 1 from end of buffer
357                 8, 0, 0, 0, // int32 offset for start of vtable
358                 0, 0, 55, 0,    // padding + value 0
359                 4, 0, 0, 0, // position of vector from here
360                 2, 0, 0, 0, // length of vector
361                 0x78, 0x56,       // vector value 0
362                 0x34, 0x12,       // vector value 1
363             },
364                 builder.DataBuffer.Data);
365         }
366 
367         [FlatBuffersTestMethod]
TestVTableWithAStruct_of_int8_int16_int32()368         public void TestVTableWithAStruct_of_int8_int16_int32()
369         {
370             var builder = new FlatBufferBuilder(1);
371             builder.StartObject(1);
372             builder.Prep(4+4+4, 0);
373             builder.AddSbyte(55);
374             builder.Pad(3);
375             builder.AddShort(0x1234);
376             builder.Pad(2);
377             builder.AddInt(0x12345678);
378             var structStart = builder.Offset;
379             builder.AddStruct(0, structStart, 0);
380             builder.EndObject();
381             Assert.ArrayEqual(new byte[]
382             {
383                 0, 0, 0, 0,
384                 0, 0, 0, 0,
385                 0, 0, // Padding to 32 bytes
386                 6, 0, // vtable bytes
387                 16, 0, // object length
388                 4, 0,     // start of struct from here
389                 6, 0, 0, 0, // int32 offset for start of vtable
390                 0x78, 0x56, 0x34, 0x12,  // struct value 2
391                 0x00, 0x00, 0x34, 0x12, // struct value 1
392                 0x00, 0x00, 0x00, 55, // struct value 0
393             },
394                 builder.DataBuffer.Data);
395         }
396 
397         [FlatBuffersTestMethod]
TestVTableWithAVectorOf_2xStructOf_2xInt8()398         public void TestVTableWithAVectorOf_2xStructOf_2xInt8()
399         {
400             var builder = new FlatBufferBuilder(1);
401             builder.StartVector(sizeof(byte)*2, 2, 1);
402             builder.AddByte(33);
403             builder.AddByte(44);
404             builder.AddByte(55);
405             builder.AddByte(66);
406             var vecEnd = builder.EndVector();
407 
408             builder.StartObject(1);
409             builder.AddOffset(0, vecEnd.Value, 0);
410             builder.EndObject();
411 
412             Assert.ArrayEqual(new byte[]
413             {
414                 0, 0, 0, 0,
415                 0, 0, 0, 0,
416                 0, 0, // Padding to 32 bytes
417                 6, 0, // vtable bytes
418                 8, 0, // object length
419                 4, 0,     // offset of vector offset
420                 6, 0, 0, 0, // int32 offset for start of vtable
421                 4, 0, 0, 0, // Vector start offset
422                 2, 0, 0, 0, // Vector len
423                 66, // vector 1, 1
424                 55, // vector 1, 0
425                 44, // vector 0, 1
426                 33, // vector 0, 0
427             },
428                 builder.DataBuffer.Data);
429         }
430 
431         [FlatBuffersTestMethod]
TestVTableWithSomeElements()432         public void TestVTableWithSomeElements()
433         {
434             var builder = new FlatBufferBuilder(1);
435             builder.StartObject(2);
436             builder.AddByte(0, 33, 0);
437             builder.AddShort(1, 66, 0);
438             var off = builder.EndObject();
439             builder.Finish(off);
440 
441             Assert.ArrayEqual(new byte[]
442             {
443                 0, 0, 0, 0,
444                 0, 0, 0, 0,
445                 0, 0, 0, 0, //Padding to 32 bytes
446                 12, 0, 0, 0,     // root of table, pointing to vtable offset
447                 8, 0, // vtable bytes
448                 8, 0, // object length
449                 7, 0, // start of value 0
450                 4, 0, // start of value 1
451                 8, 0, 0, 0, // int32 offset for start of vtable
452                 66, 0, // value 1
453                 0, 33, // value 0
454 
455             },
456                 builder.DataBuffer.Data);
457         }
458 
459         [FlatBuffersTestMethod]
TestTwoFinishTable()460         public void TestTwoFinishTable()
461         {
462             var builder = new FlatBufferBuilder(1);
463             builder.StartObject(2);
464             builder.AddByte(0, 33, 0);
465             builder.AddByte(1, 44, 0);
466             var off0 = builder.EndObject();
467             builder.Finish(off0);
468 
469             builder.StartObject(3);
470             builder.AddByte(0, 55, 0);
471             builder.AddByte(1, 66, 0);
472             builder.AddByte(2, 77, 0);
473             var off1 = builder.EndObject();
474             builder.Finish(off1);
475 
476             Assert.ArrayEqual(new byte[]
477             {
478                 0, 0, 0, 0,
479                 0, 0, 0, 0,
480                 0, 0, 0, 0,
481                 0, 0, 0, 0,
482                 0, 0, 0, 0,       // padding to 64 bytes
483                 16, 0, 0, 0,     // root of table, pointing to vtable offset (obj1)
484                 0, 0, // padding
485 
486                 10, 0, // vtable bytes
487                 8, 0, // object length
488                 7, 0, // start of value 0
489                 6, 0, // start of value 1
490                 5, 0, // start of value 2
491                 10, 0, 0, 0, // int32 offset for start of vtable
492                 0, // pad
493                 77, // values 2, 1, 0
494                 66,
495                 55,
496 
497                 12, 0, 0, 0,     // root of table, pointing to vtable offset (obj0)
498                 8, 0, // vtable bytes
499                 8, 0, // object length
500                 7, 0, // start of value 0
501                 6, 0, // start of value 1
502                 8, 0, 0, 0, // int32 offset for start of vtable
503                 0, 0, // pad
504                 44, // value 1, 0
505                 33,
506             },
507                 builder.DataBuffer.Data);
508         }
509 
510         [FlatBuffersTestMethod]
TestBunchOfBools()511         public void TestBunchOfBools()
512         {
513             var builder = new FlatBufferBuilder(1);
514             builder.StartObject(8);
515             for (var i = 0; i < 8; i++)
516             {
517                 builder.AddBool(i, true, false);
518             }
519             var off = builder.EndObject();
520             builder.Finish(off);
521 
522             Assert.ArrayEqual(new byte[]
523             {
524                 0, 0, 0, 0,
525                 0, 0, 0, 0,
526                 0, 0, 0, 0,
527                 0, 0, 0, 0,
528                 0, 0, 0, 0,
529                 0, 0, 0, 0,
530                 0, 0, 0, 0,       // padding to 64 bytes
531 
532                 24, 0, 0, 0,     // root of table, pointing to vtable offset (obj0)
533                 20, 0, // vtable bytes
534                 12, 0, // object length
535                 11, 0, // start of value 0
536                 10, 0, // start of value 1
537                 9, 0, // start of value 2
538                 8, 0, // start of value 3
539                 7, 0, // start of value 4
540                 6, 0, // start of value 5
541                 5, 0, // start of value 6
542                 4, 0, // start of value 7
543 
544                 20, 0, 0, 0, // int32 offset for start of vtable
545 
546                 1, 1, 1, 1,  // values
547                 1, 1, 1, 1,
548 
549             },
550                 builder.DataBuffer.Data);
551         }
552 
553         [FlatBuffersTestMethod]
TestWithFloat()554         public void TestWithFloat()
555         {
556             var builder = new FlatBufferBuilder(1);
557             builder.StartObject(1);
558             builder.AddFloat(0, 1, 0);
559             builder.EndObject();
560 
561 
562             Assert.ArrayEqual(new byte[]
563             {
564                 0, 0,
565                 6, 0, // vtable bytes
566                 8, 0, // object length
567                 4, 0, // start of value 0
568                 6, 0, 0, 0, // int32 offset for start of vtable
569                 0, 0, 128, 63,  // value
570 
571             },
572                 builder.DataBuffer.Data);
573         }
574 
CheckObjects(int fieldCount, int objectCount)575         private void CheckObjects(int fieldCount, int objectCount)
576         {
577             _lcg.Reset();
578 
579             const int testValuesMax = 11;
580 
581             var builder = new FlatBufferBuilder(1);
582 
583             var objects = new int[objectCount];
584 
585             for (var i = 0; i < objectCount; ++i)
586             {
587                 builder.StartObject(fieldCount);
588 
589                 for (var j = 0; j < fieldCount; ++j)
590                 {
591                     var fieldType = _lcg.Next()%testValuesMax;
592 
593                     switch (fieldType)
594                     {
595                         case 0:
596                         {
597                             builder.AddBool(j, FuzzTestData.BoolValue, false);
598                             break;
599                         }
600                         case 1:
601                         {
602                             builder.AddSbyte(j, FuzzTestData.Int8Value, 0);
603                             break;
604                         }
605                         case 2:
606                         {
607                             builder.AddByte(j, FuzzTestData.UInt8Value, 0);
608                             break;
609                         }
610                         case 3:
611                         {
612                             builder.AddShort(j, FuzzTestData.Int16Value, 0);
613                             break;
614                         }
615                         case 4:
616                         {
617                             builder.AddUshort(j, FuzzTestData.UInt16Value, 0);
618                             break;
619                         }
620                         case 5:
621                         {
622                             builder.AddInt(j, FuzzTestData.Int32Value, 0);
623                             break;
624                         }
625                         case 6:
626                         {
627                             builder.AddUint(j, FuzzTestData.UInt32Value, 0);
628                             break;
629                         }
630                         case 7:
631                         {
632                             builder.AddLong(j, FuzzTestData.Int64Value, 0);
633                             break;
634                         }
635                         case 8:
636                         {
637                             builder.AddUlong(j, FuzzTestData.UInt64Value, 0);
638                             break;
639                         }
640                         case 9:
641                         {
642                             builder.AddFloat(j, FuzzTestData.Float32Value, 0);
643                             break;
644                         }
645                         case 10:
646                         {
647                             builder.AddDouble(j, FuzzTestData.Float64Value, 0);
648                             break;
649                         }
650                         default:
651                             throw new Exception("Unreachable");
652                     }
653 
654                 }
655 
656                 var offset = builder.EndObject();
657 
658                 // Store the object offset
659                 objects[i] = offset;
660             }
661 
662             _lcg.Reset();
663 
664             // Test all objects are readable and return expected values...
665             for (var i = 0; i < objectCount; ++i)
666             {
667                 var table = new TestTable(builder.DataBuffer, builder.DataBuffer.Length - objects[i]);
668 
669                 for (var j = 0; j < fieldCount; ++j)
670                 {
671                     var fieldType = _lcg.Next() % testValuesMax;
672                     var fc = 2 + j; // 2 == VtableMetadataFields
673                     var f = fc * 2;
674 
675                     switch (fieldType)
676                     {
677                         case 0:
678                         {
679                             Assert.AreEqual(FuzzTestData.BoolValue, table.GetSlot(f, false));
680                             break;
681                         }
682                         case 1:
683                         {
684                             Assert.AreEqual(FuzzTestData.Int8Value, table.GetSlot(f, (sbyte)0));
685                             break;
686                         }
687                         case 2:
688                         {
689                             Assert.AreEqual(FuzzTestData.UInt8Value, table.GetSlot(f, (byte)0));
690                             break;
691                         }
692                         case 3:
693                         {
694                             Assert.AreEqual(FuzzTestData.Int16Value, table.GetSlot(f, (short)0));
695                             break;
696                         }
697                         case 4:
698                         {
699                             Assert.AreEqual(FuzzTestData.UInt16Value, table.GetSlot(f, (ushort)0));
700                             break;
701                         }
702                         case 5:
703                         {
704                             Assert.AreEqual(FuzzTestData.Int32Value, table.GetSlot(f, (int)0));
705                             break;
706                         }
707                         case 6:
708                         {
709                             Assert.AreEqual(FuzzTestData.UInt32Value, table.GetSlot(f, (uint)0));
710                             break;
711                         }
712                         case 7:
713                         {
714                             Assert.AreEqual(FuzzTestData.Int64Value, table.GetSlot(f, (long)0));
715                             break;
716                         }
717                         case 8:
718                         {
719                             Assert.AreEqual(FuzzTestData.UInt64Value, table.GetSlot(f, (ulong)0));
720                             break;
721                         }
722                         case 9:
723                         {
724                             Assert.AreEqual(FuzzTestData.Float32Value, table.GetSlot(f, (float)0));
725                             break;
726                         }
727                         case 10:
728                         {
729                             Assert.AreEqual(FuzzTestData.Float64Value, table.GetSlot(f, (double)0));
730                             break;
731                         }
732                         default:
733                             throw new Exception("Unreachable");
734                     }
735 
736                 }
737 
738             }
739 
740         }
741     }
742 }
743