1 /* 2 * Copyright 2014 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 ByteBufferTests 23 { 24 25 [FlatBuffersTestMethod] ByteBuffer_Length_MatchesBufferLength()26 public void ByteBuffer_Length_MatchesBufferLength() 27 { 28 var buffer = new byte[1000]; 29 var uut = new ByteBuffer(buffer); 30 Assert.AreEqual(buffer.Length, uut.Length); 31 } 32 33 [FlatBuffersTestMethod] ByteBuffer_PutBytePopulatesBufferAtZeroOffset()34 public void ByteBuffer_PutBytePopulatesBufferAtZeroOffset() 35 { 36 var buffer = new byte[1]; 37 var uut = new ByteBuffer(buffer); 38 uut.PutByte(0, (byte)99); 39 40 Assert.AreEqual((byte)99, buffer[0]); 41 } 42 43 #if !BYTEBUFFER_NO_BOUNDS_CHECK 44 [FlatBuffersTestMethod] ByteBuffer_PutByteCannotPutAtOffsetPastLength()45 public void ByteBuffer_PutByteCannotPutAtOffsetPastLength() 46 { 47 var uut = new ByteBuffer(1); 48 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99)); 49 } 50 #endif 51 52 [FlatBuffersTestMethod] ByteBuffer_PutShortPopulatesBufferCorrectly()53 public void ByteBuffer_PutShortPopulatesBufferCorrectly() 54 { 55 var buffer = new byte[2]; 56 var uut = new ByteBuffer(buffer); 57 uut.PutShort(0, (short)1); 58 59 // Ensure Endianness was written correctly 60 Assert.AreEqual((byte)1, buffer[0]); 61 Assert.AreEqual((byte)0, buffer[1]); 62 } 63 64 #if !BYTEBUFFER_NO_BOUNDS_CHECK 65 [FlatBuffersTestMethod] ByteBuffer_PutShortCannotPutAtOffsetPastLength()66 public void ByteBuffer_PutShortCannotPutAtOffsetPastLength() 67 { 68 var uut = new ByteBuffer(2); 69 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99)); 70 } 71 #endif 72 73 #if !BYTEBUFFER_NO_BOUNDS_CHECK 74 [FlatBuffersTestMethod] ByteBuffer_PutShortChecksLength()75 public void ByteBuffer_PutShortChecksLength() 76 { 77 var uut = new ByteBuffer(1); 78 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99)); 79 } 80 81 [FlatBuffersTestMethod] ByteBuffer_PutShortChecksLengthAndOffset()82 public void ByteBuffer_PutShortChecksLengthAndOffset() 83 { 84 var uut = new ByteBuffer(2); 85 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99)); 86 } 87 #endif 88 89 [FlatBuffersTestMethod] ByteBuffer_PutIntPopulatesBufferCorrectly()90 public void ByteBuffer_PutIntPopulatesBufferCorrectly() 91 { 92 var buffer = new byte[4]; 93 var uut = new ByteBuffer(buffer); 94 uut.PutInt(0, 0x0A0B0C0D); 95 96 // Ensure Endianness was written correctly 97 Assert.AreEqual(0x0D, buffer[0]); 98 Assert.AreEqual(0x0C, buffer[1]); 99 Assert.AreEqual(0x0B, buffer[2]); 100 Assert.AreEqual(0x0A, buffer[3]); 101 } 102 103 #if !BYTEBUFFER_NO_BOUNDS_CHECK 104 [FlatBuffersTestMethod] ByteBuffer_PutIntCannotPutAtOffsetPastLength()105 public void ByteBuffer_PutIntCannotPutAtOffsetPastLength() 106 { 107 var uut = new ByteBuffer(4); 108 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); 109 } 110 111 [FlatBuffersTestMethod] ByteBuffer_PutIntChecksLength()112 public void ByteBuffer_PutIntChecksLength() 113 { 114 var uut = new ByteBuffer(1); 115 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D)); 116 } 117 118 [FlatBuffersTestMethod] ByteBuffer_PutIntChecksLengthAndOffset()119 public void ByteBuffer_PutIntChecksLengthAndOffset() 120 { 121 var uut = new ByteBuffer(4); 122 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); 123 } 124 #endif 125 126 [FlatBuffersTestMethod] ByteBuffer_PutLongPopulatesBufferCorrectly()127 public void ByteBuffer_PutLongPopulatesBufferCorrectly() 128 { 129 var buffer = new byte[8]; 130 var uut = new ByteBuffer(buffer); 131 uut.PutLong(0, 0x010203040A0B0C0D); 132 133 // Ensure Endianness was written correctly 134 Assert.AreEqual(0x0D, buffer[0]); 135 Assert.AreEqual(0x0C, buffer[1]); 136 Assert.AreEqual(0x0B, buffer[2]); 137 Assert.AreEqual(0x0A, buffer[3]); 138 Assert.AreEqual(0x04, buffer[4]); 139 Assert.AreEqual(0x03, buffer[5]); 140 Assert.AreEqual(0x02, buffer[6]); 141 Assert.AreEqual(0x01, buffer[7]); 142 } 143 144 #if !BYTEBUFFER_NO_BOUNDS_CHECK 145 [FlatBuffersTestMethod] ByteBuffer_PutLongCannotPutAtOffsetPastLength()146 public void ByteBuffer_PutLongCannotPutAtOffsetPastLength() 147 { 148 var uut = new ByteBuffer(8); 149 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); 150 } 151 152 [FlatBuffersTestMethod] ByteBuffer_PutLongChecksLength()153 public void ByteBuffer_PutLongChecksLength() 154 { 155 var uut = new ByteBuffer(1); 156 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D)); 157 } 158 159 [FlatBuffersTestMethod] ByteBuffer_PutLongChecksLengthAndOffset()160 public void ByteBuffer_PutLongChecksLengthAndOffset() 161 { 162 var uut = new ByteBuffer(8); 163 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); 164 } 165 #endif 166 167 [FlatBuffersTestMethod] ByteBuffer_GetByteReturnsCorrectData()168 public void ByteBuffer_GetByteReturnsCorrectData() 169 { 170 var buffer = new byte[1]; 171 buffer[0] = 99; 172 var uut = new ByteBuffer(buffer); 173 Assert.AreEqual((byte)99, uut.Get(0)); 174 } 175 176 #if !BYTEBUFFER_NO_BOUNDS_CHECK 177 [FlatBuffersTestMethod] ByteBuffer_GetByteChecksOffset()178 public void ByteBuffer_GetByteChecksOffset() 179 { 180 var uut = new ByteBuffer(1); 181 Assert.Throws<ArgumentOutOfRangeException>(() => uut.Get(1)); 182 } 183 #endif 184 185 [FlatBuffersTestMethod] ByteBuffer_GetShortReturnsCorrectData()186 public void ByteBuffer_GetShortReturnsCorrectData() 187 { 188 var buffer = new byte[2]; 189 buffer[0] = 1; 190 buffer[1] = 0; 191 var uut = new ByteBuffer(buffer); 192 Assert.AreEqual(1, uut.GetShort(0)); 193 } 194 195 #if !BYTEBUFFER_NO_BOUNDS_CHECK 196 [FlatBuffersTestMethod] ByteBuffer_GetShortChecksOffset()197 public void ByteBuffer_GetShortChecksOffset() 198 { 199 var uut = new ByteBuffer(2); 200 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2)); 201 } 202 203 [FlatBuffersTestMethod] ByteBuffer_GetShortChecksLength()204 public void ByteBuffer_GetShortChecksLength() 205 { 206 var uut = new ByteBuffer(2); 207 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1)); 208 } 209 #endif 210 211 [FlatBuffersTestMethod] ByteBuffer_GetIntReturnsCorrectData()212 public void ByteBuffer_GetIntReturnsCorrectData() 213 { 214 var buffer = new byte[4]; 215 buffer[0] = 0x0D; 216 buffer[1] = 0x0C; 217 buffer[2] = 0x0B; 218 buffer[3] = 0x0A; 219 var uut = new ByteBuffer(buffer); 220 Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0)); 221 } 222 223 #if !BYTEBUFFER_NO_BOUNDS_CHECK 224 [FlatBuffersTestMethod] ByteBuffer_GetIntChecksOffset()225 public void ByteBuffer_GetIntChecksOffset() 226 { 227 var uut = new ByteBuffer(4); 228 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4)); 229 } 230 231 [FlatBuffersTestMethod] ByteBuffer_GetIntChecksLength()232 public void ByteBuffer_GetIntChecksLength() 233 { 234 var uut = new ByteBuffer(2); 235 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0)); 236 } 237 #endif 238 239 [FlatBuffersTestMethod] ByteBuffer_GetLongReturnsCorrectData()240 public void ByteBuffer_GetLongReturnsCorrectData() 241 { 242 var buffer = new byte[8]; 243 buffer[0] = 0x0D; 244 buffer[1] = 0x0C; 245 buffer[2] = 0x0B; 246 buffer[3] = 0x0A; 247 buffer[4] = 0x04; 248 buffer[5] = 0x03; 249 buffer[6] = 0x02; 250 buffer[7] = 0x01; 251 var uut = new ByteBuffer(buffer); 252 Assert.AreEqual(0x010203040A0B0C0D, uut.GetLong(0)); 253 } 254 255 #if !BYTEBUFFER_NO_BOUNDS_CHECK 256 [FlatBuffersTestMethod] ByteBuffer_GetLongChecksOffset()257 public void ByteBuffer_GetLongChecksOffset() 258 { 259 var uut = new ByteBuffer(8); 260 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8)); 261 } 262 263 [FlatBuffersTestMethod] ByteBuffer_GetLongChecksLength()264 public void ByteBuffer_GetLongChecksLength() 265 { 266 var uut = new ByteBuffer(7); 267 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0)); 268 } 269 #endif 270 271 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUshort()272 public void ByteBuffer_ReverseBytesUshort() 273 { 274 const ushort original = (ushort)0x1234U; 275 var reverse = ByteBuffer.ReverseBytes(original); 276 Assert.AreEqual(0x3412U, reverse); 277 278 var rereverse = ByteBuffer.ReverseBytes(reverse); 279 Assert.AreEqual(original, rereverse); 280 } 281 282 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUint()283 public void ByteBuffer_ReverseBytesUint() 284 { 285 const uint original = 0x12345678; 286 var reverse = ByteBuffer.ReverseBytes(original); 287 Assert.AreEqual(0x78563412U, reverse); 288 289 var rereverse = ByteBuffer.ReverseBytes(reverse); 290 Assert.AreEqual(original, rereverse); 291 } 292 293 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUlong()294 public void ByteBuffer_ReverseBytesUlong() 295 { 296 const ulong original = 0x1234567890ABCDEFUL; 297 var reverse = ByteBuffer.ReverseBytes(original); 298 Assert.AreEqual(0xEFCDAB9078563412UL, reverse); 299 300 var rereverse = ByteBuffer.ReverseBytes(reverse); 301 Assert.AreEqual(original, rereverse); 302 } 303 304 [FlatBuffersTestMethod] ByteBuffer_ToFullArray_MatchesBuffer()305 public void ByteBuffer_ToFullArray_MatchesBuffer() 306 { 307 var buffer = new byte[4]; 308 buffer[0] = 0x0D; 309 buffer[1] = 0x0C; 310 buffer[2] = 0x0B; 311 buffer[3] = 0x0A; 312 var uut = new ByteBuffer(buffer); 313 Assert.ArrayEqual(buffer, uut.ToFullArray()); 314 } 315 316 [FlatBuffersTestMethod] ByteBuffer_ToSizedArray_MatchesBuffer()317 public void ByteBuffer_ToSizedArray_MatchesBuffer() 318 { 319 var buffer = new byte[4]; 320 buffer[0] = 0x0D; 321 buffer[1] = 0x0C; 322 buffer[2] = 0x0B; 323 buffer[3] = 0x0A; 324 var uut = new ByteBuffer(buffer); 325 Assert.ArrayEqual(buffer, uut.ToFullArray()); 326 } 327 328 [FlatBuffersTestMethod] ByteBuffer_Duplicate_MatchesBuffer()329 public void ByteBuffer_Duplicate_MatchesBuffer() 330 { 331 var buffer = new byte[4]; 332 buffer[0] = 0x0D; 333 buffer[1] = 0x0C; 334 buffer[2] = 0x0B; 335 buffer[3] = 0x0A; 336 var uut = new ByteBuffer(buffer); 337 Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0)); 338 339 // Advance by two bytes 340 uut.Position = 2; uut = uut.Duplicate(); 341 Assert.AreEqual(0x0A0B, uut.GetShort(2)); 342 343 // Advance by one more byte 344 uut.Position = 1; uut = uut.Duplicate(); 345 Assert.AreEqual(0x0A, uut.Get(3)); 346 } 347 348 [FlatBuffersTestMethod] ByteBuffer_To_Array_Float()349 public void ByteBuffer_To_Array_Float() 350 { 351 const int len = 9; 352 353 // Construct the data array 354 var fData = new float[len]; 355 fData[0] = 1.0079F; 356 fData[1] = 4.0026F; 357 fData[2] = 6.941F; 358 fData[3] = 9.0122F; 359 fData[4] = 10.811F; 360 fData[5] = 12.0107F; 361 fData[6] = 14.0067F; 362 fData[7] = 15.9994F; 363 fData[8] = 18.9984F; 364 365 // Tranfer it to a byte array 366 var buffer = new byte[sizeof(float) * fData.Length]; 367 Buffer.BlockCopy(fData, 0, buffer, 0, buffer.Length); 368 369 // Create the Byte Buffer from byte array 370 var uut = new ByteBuffer(buffer); 371 372 // Get the full array back out and ensure they are equivalent 373 var bbArray = uut.ToArray<float>(0, len); 374 Assert.ArrayEqual(fData, bbArray); 375 376 // Get a portion of the full array back out and ensure the 377 // subrange agrees 378 var bbArray2 = uut.ToArray<float>(4, len - 1); 379 Assert.AreEqual(bbArray2.Length, len - 1); 380 for (int i = 1; i < len - 1; i++) 381 { 382 Assert.AreEqual(fData[i], bbArray2[i - 1]); 383 } 384 385 // Get a sub portion of the full array back out and ensure the 386 // subrange agrees 387 var bbArray3 = uut.ToArray<float>(8, len - 4); 388 Assert.AreEqual(bbArray3.Length, len - 4); 389 for (int i = 2; i < len - 4; i++) 390 { 391 Assert.AreEqual(fData[i], bbArray3[i - 2]); 392 } 393 } 394 395 public void ByteBuffer_Put_Array_Helper<T>(T[] data, int typeSize) 396 where T : struct 397 { 398 // Create the Byte Buffer 399 var uut = new ByteBuffer(1024); 400 401 // Put the data into the buffer and make sure the offset is 402 // calculated correctly 403 int nOffset = uut.Put(1024, data); 404 Assert.AreEqual(1024 - typeSize * data.Length, nOffset); 405 406 // Get the full array back out and ensure they are equivalent 407 var bbArray = uut.ToArray<T>(nOffset, data.Length); Assert.ArrayEqualFlatBuffers.Test.ByteBufferTests.__anon1408 Assert.ArrayEqual(data, bbArray); 409 } 410 411 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Float()412 public void ByteBuffer_Put_Array_Float() 413 { 414 const int len = 9; 415 416 // Construct the data array 417 var data = new float[len]; 418 data[0] = 1.0079F; 419 data[1] = 4.0026F; 420 data[2] = 6.941F; 421 data[3] = 9.0122F; 422 data[4] = 10.811F; 423 data[5] = 12.0107F; 424 data[6] = 14.0067F; 425 data[7] = 15.9994F; 426 data[8] = 18.9984F; 427 428 ByteBuffer_Put_Array_Helper(data, sizeof(float)); 429 } 430 431 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Double()432 public void ByteBuffer_Put_Array_Double() 433 { 434 const int len = 9; 435 436 // Construct the data array 437 var data = new double[len]; 438 data[0] = 1.0079; 439 data[1] = 4.0026; 440 data[2] = 6.941; 441 data[3] = 9.0122; 442 data[4] = 10.811; 443 data[5] = 12.0107; 444 data[6] = 14.0067; 445 data[7] = 15.9994; 446 data[8] = 18.9984; 447 448 ByteBuffer_Put_Array_Helper(data, sizeof(double)); 449 } 450 451 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Int()452 public void ByteBuffer_Put_Array_Int() 453 { 454 const int len = 9; 455 456 // Construct the data array 457 var data = new int[len]; 458 data[0] = 1; 459 data[1] = 4; 460 data[2] = 6; 461 data[3] = 9; 462 data[4] = 10; 463 data[5] = 12; 464 data[6] = 14; 465 data[7] = 15; 466 data[8] = 18; 467 468 ByteBuffer_Put_Array_Helper(data, sizeof(int)); 469 } 470 471 472 [FlatBuffersTestMethod] ByteBuffer_Put_Array_UInt()473 public void ByteBuffer_Put_Array_UInt() 474 { 475 const int len = 9; 476 477 // Construct the data array 478 var data = new uint[len]; 479 data[0] = 1; 480 data[1] = 4; 481 data[2] = 6; 482 data[3] = 9; 483 data[4] = 10; 484 data[5] = 12; 485 data[6] = 14; 486 data[7] = 15; 487 data[8] = 18; 488 489 ByteBuffer_Put_Array_Helper(data, sizeof(uint)); 490 } 491 492 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Bool()493 public void ByteBuffer_Put_Array_Bool() 494 { 495 const int len = 9; 496 497 // Construct the data array 498 var data = new bool[len]; 499 data[0] = true; 500 data[1] = true; 501 data[2] = false; 502 data[3] = true; 503 data[4] = false; 504 data[5] = true; 505 data[6] = true; 506 data[7] = true; 507 data[8] = false; 508 509 ByteBuffer_Put_Array_Helper(data, sizeof(bool)); 510 } 511 512 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Long()513 public void ByteBuffer_Put_Array_Long() 514 { 515 const int len = 9; 516 517 // Construct the data array 518 var data = new long[len]; 519 data[0] = 1; 520 data[1] = 4; 521 data[2] = 6; 522 data[3] = 9; 523 data[4] = 10; 524 data[5] = 12; 525 data[6] = 14; 526 data[7] = 15; 527 data[8] = 18; 528 529 ByteBuffer_Put_Array_Helper(data, sizeof(long)); 530 } 531 532 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Byte()533 public void ByteBuffer_Put_Array_Byte() 534 { 535 const int len = 9; 536 537 // Construct the data array 538 var data = new byte[len]; 539 data[0] = 1; 540 data[1] = 4; 541 data[2] = 6; 542 data[3] = 9; 543 data[4] = 10; 544 data[5] = 12; 545 data[6] = 14; 546 data[7] = 15; 547 data[8] = 18; 548 549 ByteBuffer_Put_Array_Helper(data, sizeof(byte)); 550 } 551 552 [FlatBuffersTestMethod] ByteBuffer_Put_Array_SByte()553 public void ByteBuffer_Put_Array_SByte() 554 { 555 const int len = 9; 556 557 // Construct the data array 558 var data = new sbyte[len]; 559 data[0] = 1; 560 data[1] = 4; 561 data[2] = 6; 562 data[3] = 9; 563 data[4] = 10; 564 data[5] = 12; 565 data[6] = 14; 566 data[7] = 15; 567 data[8] = 18; 568 569 ByteBuffer_Put_Array_Helper(data, sizeof(sbyte)); 570 } 571 572 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Null_Throws()573 public void ByteBuffer_Put_Array_Null_Throws() 574 { 575 // Create the Byte Buffer 576 var uut = new ByteBuffer(1024); 577 578 // create a null array and try to put it into the buffer 579 float[] data = null; 580 Assert.Throws<ArgumentNullException>(() => uut.Put(1024, data)); 581 } 582 583 [FlatBuffersTestMethod] ByteBuffer_Put_Array_Empty_Throws()584 public void ByteBuffer_Put_Array_Empty_Throws() 585 { 586 // Create the Byte Buffer 587 var uut = new ByteBuffer(1024); 588 589 // create an array of length == 0, and try to put it into the buffer 590 float[] data = new float[0]; 591 Assert.Throws<ArgumentException>(() => uut.Put(1024, data)); 592 } 593 594 private struct dummyStruct 595 { 596 int a; 597 float b; 598 } 599 600 [FlatBuffersTestMethod] ByteBuffer_Put_Array_IncorrectType_Throws()601 public void ByteBuffer_Put_Array_IncorrectType_Throws() 602 { 603 // Create the Byte Buffer 604 var uut = new ByteBuffer(1024); 605 606 // Create an array of dummy structures that shouldn't be 607 // able to be put into the buffer 608 var data = new dummyStruct[10]; 609 Assert.Throws<ArgumentException>(() => uut.Put(1024, data)); 610 } 611 } 612 } 613