Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 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; |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 18 | using System.Runtime.InteropServices; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 19 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 20 | namespace Google.FlatBuffers.Test |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 21 | { |
| 22 | [FlatBuffersTestClass] |
| 23 | public class ByteBufferTests |
| 24 | { |
| 25 | |
| 26 | [FlatBuffersTestMethod] |
| 27 | public void ByteBuffer_Length_MatchesBufferLength() |
| 28 | { |
| 29 | var buffer = new byte[1000]; |
| 30 | var uut = new ByteBuffer(buffer); |
| 31 | Assert.AreEqual(buffer.Length, uut.Length); |
| 32 | } |
| 33 | |
| 34 | [FlatBuffersTestMethod] |
| 35 | public void ByteBuffer_PutBytePopulatesBufferAtZeroOffset() |
| 36 | { |
| 37 | var buffer = new byte[1]; |
| 38 | var uut = new ByteBuffer(buffer); |
| 39 | uut.PutByte(0, (byte)99); |
| 40 | |
| 41 | Assert.AreEqual((byte)99, buffer[0]); |
| 42 | } |
| 43 | |
| 44 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 45 | [FlatBuffersTestMethod] |
| 46 | public void ByteBuffer_PutByteCannotPutAtOffsetPastLength() |
| 47 | { |
| 48 | var uut = new ByteBuffer(1); |
| 49 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99)); |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | [FlatBuffersTestMethod] |
| 54 | public void ByteBuffer_PutShortPopulatesBufferCorrectly() |
| 55 | { |
| 56 | var buffer = new byte[2]; |
| 57 | var uut = new ByteBuffer(buffer); |
| 58 | uut.PutShort(0, (short)1); |
| 59 | |
| 60 | // Ensure Endianness was written correctly |
| 61 | Assert.AreEqual((byte)1, buffer[0]); |
| 62 | Assert.AreEqual((byte)0, buffer[1]); |
| 63 | } |
| 64 | |
| 65 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 66 | [FlatBuffersTestMethod] |
| 67 | public void ByteBuffer_PutShortCannotPutAtOffsetPastLength() |
| 68 | { |
| 69 | var uut = new ByteBuffer(2); |
| 70 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99)); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 75 | [FlatBuffersTestMethod] |
| 76 | public void ByteBuffer_PutShortChecksLength() |
| 77 | { |
| 78 | var uut = new ByteBuffer(1); |
| 79 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99)); |
| 80 | } |
| 81 | |
| 82 | [FlatBuffersTestMethod] |
| 83 | public void ByteBuffer_PutShortChecksLengthAndOffset() |
| 84 | { |
| 85 | var uut = new ByteBuffer(2); |
| 86 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99)); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | [FlatBuffersTestMethod] |
| 91 | public void ByteBuffer_PutIntPopulatesBufferCorrectly() |
| 92 | { |
| 93 | var buffer = new byte[4]; |
| 94 | var uut = new ByteBuffer(buffer); |
| 95 | uut.PutInt(0, 0x0A0B0C0D); |
| 96 | |
| 97 | // Ensure Endianness was written correctly |
| 98 | Assert.AreEqual(0x0D, buffer[0]); |
| 99 | Assert.AreEqual(0x0C, buffer[1]); |
| 100 | Assert.AreEqual(0x0B, buffer[2]); |
| 101 | Assert.AreEqual(0x0A, buffer[3]); |
| 102 | } |
| 103 | |
| 104 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 105 | [FlatBuffersTestMethod] |
| 106 | public void ByteBuffer_PutIntCannotPutAtOffsetPastLength() |
| 107 | { |
| 108 | var uut = new ByteBuffer(4); |
| 109 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); |
| 110 | } |
| 111 | |
| 112 | [FlatBuffersTestMethod] |
| 113 | public void ByteBuffer_PutIntChecksLength() |
| 114 | { |
| 115 | var uut = new ByteBuffer(1); |
| 116 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D)); |
| 117 | } |
| 118 | |
| 119 | [FlatBuffersTestMethod] |
| 120 | public void ByteBuffer_PutIntChecksLengthAndOffset() |
| 121 | { |
| 122 | var uut = new ByteBuffer(4); |
| 123 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | [FlatBuffersTestMethod] |
| 128 | public void ByteBuffer_PutLongPopulatesBufferCorrectly() |
| 129 | { |
| 130 | var buffer = new byte[8]; |
| 131 | var uut = new ByteBuffer(buffer); |
| 132 | uut.PutLong(0, 0x010203040A0B0C0D); |
| 133 | |
| 134 | // Ensure Endianness was written correctly |
| 135 | Assert.AreEqual(0x0D, buffer[0]); |
| 136 | Assert.AreEqual(0x0C, buffer[1]); |
| 137 | Assert.AreEqual(0x0B, buffer[2]); |
| 138 | Assert.AreEqual(0x0A, buffer[3]); |
| 139 | Assert.AreEqual(0x04, buffer[4]); |
| 140 | Assert.AreEqual(0x03, buffer[5]); |
| 141 | Assert.AreEqual(0x02, buffer[6]); |
| 142 | Assert.AreEqual(0x01, buffer[7]); |
| 143 | } |
| 144 | |
| 145 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 146 | [FlatBuffersTestMethod] |
| 147 | public void ByteBuffer_PutLongCannotPutAtOffsetPastLength() |
| 148 | { |
| 149 | var uut = new ByteBuffer(8); |
| 150 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); |
| 151 | } |
| 152 | |
| 153 | [FlatBuffersTestMethod] |
| 154 | public void ByteBuffer_PutLongChecksLength() |
| 155 | { |
| 156 | var uut = new ByteBuffer(1); |
| 157 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D)); |
| 158 | } |
| 159 | |
| 160 | [FlatBuffersTestMethod] |
| 161 | public void ByteBuffer_PutLongChecksLengthAndOffset() |
| 162 | { |
| 163 | var uut = new ByteBuffer(8); |
| 164 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | [FlatBuffersTestMethod] |
| 169 | public void ByteBuffer_GetByteReturnsCorrectData() |
| 170 | { |
| 171 | var buffer = new byte[1]; |
| 172 | buffer[0] = 99; |
| 173 | var uut = new ByteBuffer(buffer); |
| 174 | Assert.AreEqual((byte)99, uut.Get(0)); |
| 175 | } |
| 176 | |
| 177 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 178 | [FlatBuffersTestMethod] |
| 179 | public void ByteBuffer_GetByteChecksOffset() |
| 180 | { |
| 181 | var uut = new ByteBuffer(1); |
| 182 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.Get(1)); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | [FlatBuffersTestMethod] |
| 187 | public void ByteBuffer_GetShortReturnsCorrectData() |
| 188 | { |
| 189 | var buffer = new byte[2]; |
| 190 | buffer[0] = 1; |
| 191 | buffer[1] = 0; |
| 192 | var uut = new ByteBuffer(buffer); |
| 193 | Assert.AreEqual(1, uut.GetShort(0)); |
| 194 | } |
| 195 | |
| 196 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 197 | [FlatBuffersTestMethod] |
| 198 | public void ByteBuffer_GetShortChecksOffset() |
| 199 | { |
| 200 | var uut = new ByteBuffer(2); |
| 201 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2)); |
| 202 | } |
| 203 | |
| 204 | [FlatBuffersTestMethod] |
| 205 | public void ByteBuffer_GetShortChecksLength() |
| 206 | { |
| 207 | var uut = new ByteBuffer(2); |
| 208 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1)); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | [FlatBuffersTestMethod] |
| 213 | public void ByteBuffer_GetIntReturnsCorrectData() |
| 214 | { |
| 215 | var buffer = new byte[4]; |
| 216 | buffer[0] = 0x0D; |
| 217 | buffer[1] = 0x0C; |
| 218 | buffer[2] = 0x0B; |
| 219 | buffer[3] = 0x0A; |
| 220 | var uut = new ByteBuffer(buffer); |
| 221 | Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0)); |
| 222 | } |
| 223 | |
| 224 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 225 | [FlatBuffersTestMethod] |
| 226 | public void ByteBuffer_GetIntChecksOffset() |
| 227 | { |
| 228 | var uut = new ByteBuffer(4); |
| 229 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4)); |
| 230 | } |
| 231 | |
| 232 | [FlatBuffersTestMethod] |
| 233 | public void ByteBuffer_GetIntChecksLength() |
| 234 | { |
| 235 | var uut = new ByteBuffer(2); |
| 236 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0)); |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | [FlatBuffersTestMethod] |
| 241 | public void ByteBuffer_GetLongReturnsCorrectData() |
| 242 | { |
| 243 | var buffer = new byte[8]; |
| 244 | buffer[0] = 0x0D; |
| 245 | buffer[1] = 0x0C; |
| 246 | buffer[2] = 0x0B; |
| 247 | buffer[3] = 0x0A; |
| 248 | buffer[4] = 0x04; |
| 249 | buffer[5] = 0x03; |
| 250 | buffer[6] = 0x02; |
| 251 | buffer[7] = 0x01; |
| 252 | var uut = new ByteBuffer(buffer); |
| 253 | Assert.AreEqual(0x010203040A0B0C0D, uut.GetLong(0)); |
| 254 | } |
| 255 | |
| 256 | #if !BYTEBUFFER_NO_BOUNDS_CHECK |
| 257 | [FlatBuffersTestMethod] |
| 258 | public void ByteBuffer_GetLongChecksOffset() |
| 259 | { |
| 260 | var uut = new ByteBuffer(8); |
| 261 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8)); |
| 262 | } |
| 263 | |
| 264 | [FlatBuffersTestMethod] |
| 265 | public void ByteBuffer_GetLongChecksLength() |
| 266 | { |
| 267 | var uut = new ByteBuffer(7); |
| 268 | Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0)); |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | [FlatBuffersTestMethod] |
| 273 | public void ByteBuffer_ReverseBytesUshort() |
| 274 | { |
| 275 | const ushort original = (ushort)0x1234U; |
| 276 | var reverse = ByteBuffer.ReverseBytes(original); |
| 277 | Assert.AreEqual(0x3412U, reverse); |
| 278 | |
| 279 | var rereverse = ByteBuffer.ReverseBytes(reverse); |
| 280 | Assert.AreEqual(original, rereverse); |
| 281 | } |
| 282 | |
| 283 | [FlatBuffersTestMethod] |
| 284 | public void ByteBuffer_ReverseBytesUint() |
| 285 | { |
| 286 | const uint original = 0x12345678; |
| 287 | var reverse = ByteBuffer.ReverseBytes(original); |
| 288 | Assert.AreEqual(0x78563412U, reverse); |
| 289 | |
| 290 | var rereverse = ByteBuffer.ReverseBytes(reverse); |
| 291 | Assert.AreEqual(original, rereverse); |
| 292 | } |
| 293 | |
| 294 | [FlatBuffersTestMethod] |
| 295 | public void ByteBuffer_ReverseBytesUlong() |
| 296 | { |
| 297 | const ulong original = 0x1234567890ABCDEFUL; |
| 298 | var reverse = ByteBuffer.ReverseBytes(original); |
| 299 | Assert.AreEqual(0xEFCDAB9078563412UL, reverse); |
| 300 | |
| 301 | var rereverse = ByteBuffer.ReverseBytes(reverse); |
| 302 | Assert.AreEqual(original, rereverse); |
| 303 | } |
| 304 | |
| 305 | [FlatBuffersTestMethod] |
| 306 | public void ByteBuffer_ToFullArray_MatchesBuffer() |
| 307 | { |
| 308 | var buffer = new byte[4]; |
| 309 | buffer[0] = 0x0D; |
| 310 | buffer[1] = 0x0C; |
| 311 | buffer[2] = 0x0B; |
| 312 | buffer[3] = 0x0A; |
| 313 | var uut = new ByteBuffer(buffer); |
| 314 | Assert.ArrayEqual(buffer, uut.ToFullArray()); |
| 315 | } |
| 316 | |
| 317 | [FlatBuffersTestMethod] |
| 318 | public void ByteBuffer_ToSizedArray_MatchesBuffer() |
| 319 | { |
| 320 | var buffer = new byte[4]; |
| 321 | buffer[0] = 0x0D; |
| 322 | buffer[1] = 0x0C; |
| 323 | buffer[2] = 0x0B; |
| 324 | buffer[3] = 0x0A; |
| 325 | var uut = new ByteBuffer(buffer); |
| 326 | Assert.ArrayEqual(buffer, uut.ToFullArray()); |
| 327 | } |
| 328 | |
| 329 | [FlatBuffersTestMethod] |
| 330 | public void ByteBuffer_Duplicate_MatchesBuffer() |
| 331 | { |
| 332 | var buffer = new byte[4]; |
| 333 | buffer[0] = 0x0D; |
| 334 | buffer[1] = 0x0C; |
| 335 | buffer[2] = 0x0B; |
| 336 | buffer[3] = 0x0A; |
| 337 | var uut = new ByteBuffer(buffer); |
| 338 | Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0)); |
| 339 | |
| 340 | // Advance by two bytes |
| 341 | uut.Position = 2; uut = uut.Duplicate(); |
| 342 | Assert.AreEqual(0x0A0B, uut.GetShort(2)); |
| 343 | |
| 344 | // Advance by one more byte |
| 345 | uut.Position = 1; uut = uut.Duplicate(); |
| 346 | Assert.AreEqual(0x0A, uut.Get(3)); |
| 347 | } |
| 348 | |
| 349 | [FlatBuffersTestMethod] |
| 350 | public void ByteBuffer_To_Array_Float() |
| 351 | { |
| 352 | const int len = 9; |
| 353 | |
| 354 | // Construct the data array |
| 355 | var fData = new float[len]; |
| 356 | fData[0] = 1.0079F; |
| 357 | fData[1] = 4.0026F; |
| 358 | fData[2] = 6.941F; |
| 359 | fData[3] = 9.0122F; |
| 360 | fData[4] = 10.811F; |
| 361 | fData[5] = 12.0107F; |
| 362 | fData[6] = 14.0067F; |
| 363 | fData[7] = 15.9994F; |
| 364 | fData[8] = 18.9984F; |
| 365 | |
| 366 | // Tranfer it to a byte array |
| 367 | var buffer = new byte[sizeof(float) * fData.Length]; |
| 368 | Buffer.BlockCopy(fData, 0, buffer, 0, buffer.Length); |
| 369 | |
| 370 | // Create the Byte Buffer from byte array |
| 371 | var uut = new ByteBuffer(buffer); |
| 372 | |
| 373 | // Get the full array back out and ensure they are equivalent |
| 374 | var bbArray = uut.ToArray<float>(0, len); |
| 375 | Assert.ArrayEqual(fData, bbArray); |
| 376 | |
| 377 | // Get a portion of the full array back out and ensure the |
| 378 | // subrange agrees |
| 379 | var bbArray2 = uut.ToArray<float>(4, len - 1); |
| 380 | Assert.AreEqual(bbArray2.Length, len - 1); |
| 381 | for (int i = 1; i < len - 1; i++) |
| 382 | { |
| 383 | Assert.AreEqual(fData[i], bbArray2[i - 1]); |
| 384 | } |
| 385 | |
| 386 | // Get a sub portion of the full array back out and ensure the |
| 387 | // subrange agrees |
| 388 | var bbArray3 = uut.ToArray<float>(8, len - 4); |
| 389 | Assert.AreEqual(bbArray3.Length, len - 4); |
| 390 | for (int i = 2; i < len - 4; i++) |
| 391 | { |
| 392 | Assert.AreEqual(fData[i], bbArray3[i - 2]); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | public void ByteBuffer_Put_Array_Helper<T>(T[] data, int typeSize) |
| 397 | where T : struct |
| 398 | { |
| 399 | // Create the Byte Buffer |
| 400 | var uut = new ByteBuffer(1024); |
| 401 | |
| 402 | // Put the data into the buffer and make sure the offset is |
| 403 | // calculated correctly |
| 404 | int nOffset = uut.Put(1024, data); |
| 405 | Assert.AreEqual(1024 - typeSize * data.Length, nOffset); |
| 406 | |
| 407 | // Get the full array back out and ensure they are equivalent |
| 408 | var bbArray = uut.ToArray<T>(nOffset, data.Length); |
| 409 | Assert.ArrayEqual(data, bbArray); |
| 410 | } |
| 411 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 412 | public void ByteBuffer_Put_ArraySegment_Helper<T>(ArraySegment<T> data, int typeSize) |
| 413 | where T : struct |
| 414 | { |
| 415 | // Create the Byte Buffer |
| 416 | var uut = new ByteBuffer(1024); |
| 417 | |
| 418 | // Put the data into the buffer and make sure the offset is |
| 419 | // calculated correctly |
| 420 | int nOffset = uut.Put(1024, data); |
| 421 | Assert.AreEqual(1024 - typeSize * data.Count, nOffset); |
| 422 | |
| 423 | // Get the full array back out and ensure they are equivalent |
| 424 | var bbArray = uut.ToArray<T>(nOffset, data.Count); |
| 425 | Assert.ArrayEqual(data.ToArray(), bbArray); |
| 426 | } |
| 427 | |
| 428 | public unsafe void ByteBuffer_Put_IntPtr_Helper<T>(T[] data, int typeSize) |
| 429 | where T : struct |
| 430 | { |
| 431 | GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); |
| 432 | try |
| 433 | { |
| 434 | var dataPtr = handle.AddrOfPinnedObject(); |
| 435 | var sizeInBytes = data.Length * typeSize; |
| 436 | |
| 437 | // Create the Byte Buffer |
| 438 | var uut = new ByteBuffer(1024); |
| 439 | |
| 440 | // Put the data into the buffer and make sure the offset is |
| 441 | // calculated correctly |
| 442 | int nOffset = uut.Put<T>(1024, dataPtr, sizeInBytes); |
| 443 | Assert.AreEqual(1024 - sizeInBytes, nOffset); |
| 444 | |
| 445 | // Get the full array back out and ensure they are equivalent |
| 446 | var bbArray = uut.ToArray<T>(nOffset, data.Length); |
| 447 | Assert.ArrayEqual(data, bbArray); |
| 448 | } |
| 449 | finally |
| 450 | { |
| 451 | handle.Free(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | public void ByteBuffer_Put_ArrayTypes_Helper<T>(T[] data, int typeSize) |
| 456 | where T : struct |
| 457 | { |
| 458 | ByteBuffer_Put_Array_Helper(data, typeSize); |
| 459 | |
| 460 | var arraySegment = CreateArraySegment(data); |
| 461 | ByteBuffer_Put_ArraySegment_Helper(arraySegment, typeSize); |
| 462 | |
| 463 | ByteBuffer_Put_IntPtr_Helper(data, typeSize); |
| 464 | } |
| 465 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 466 | [FlatBuffersTestMethod] |
| 467 | public void ByteBuffer_Put_Array_Float() |
| 468 | { |
| 469 | const int len = 9; |
| 470 | |
| 471 | // Construct the data array |
| 472 | var data = new float[len]; |
| 473 | data[0] = 1.0079F; |
| 474 | data[1] = 4.0026F; |
| 475 | data[2] = 6.941F; |
| 476 | data[3] = 9.0122F; |
| 477 | data[4] = 10.811F; |
| 478 | data[5] = 12.0107F; |
| 479 | data[6] = 14.0067F; |
| 480 | data[7] = 15.9994F; |
| 481 | data[8] = 18.9984F; |
| 482 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 483 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(float)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | [FlatBuffersTestMethod] |
| 487 | public void ByteBuffer_Put_Array_Double() |
| 488 | { |
| 489 | const int len = 9; |
| 490 | |
| 491 | // Construct the data array |
| 492 | var data = new double[len]; |
| 493 | data[0] = 1.0079; |
| 494 | data[1] = 4.0026; |
| 495 | data[2] = 6.941; |
| 496 | data[3] = 9.0122; |
| 497 | data[4] = 10.811; |
| 498 | data[5] = 12.0107; |
| 499 | data[6] = 14.0067; |
| 500 | data[7] = 15.9994; |
| 501 | data[8] = 18.9984; |
| 502 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 503 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(double)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | [FlatBuffersTestMethod] |
| 507 | public void ByteBuffer_Put_Array_Int() |
| 508 | { |
| 509 | const int len = 9; |
| 510 | |
| 511 | // Construct the data array |
| 512 | var data = new int[len]; |
| 513 | data[0] = 1; |
| 514 | data[1] = 4; |
| 515 | data[2] = 6; |
| 516 | data[3] = 9; |
| 517 | data[4] = 10; |
| 518 | data[5] = 12; |
| 519 | data[6] = 14; |
| 520 | data[7] = 15; |
| 521 | data[8] = 18; |
| 522 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 523 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(int)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | |
| 527 | [FlatBuffersTestMethod] |
| 528 | public void ByteBuffer_Put_Array_UInt() |
| 529 | { |
| 530 | const int len = 9; |
| 531 | |
| 532 | // Construct the data array |
| 533 | var data = new uint[len]; |
| 534 | data[0] = 1; |
| 535 | data[1] = 4; |
| 536 | data[2] = 6; |
| 537 | data[3] = 9; |
| 538 | data[4] = 10; |
| 539 | data[5] = 12; |
| 540 | data[6] = 14; |
| 541 | data[7] = 15; |
| 542 | data[8] = 18; |
| 543 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 544 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(uint)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | [FlatBuffersTestMethod] |
| 548 | public void ByteBuffer_Put_Array_Bool() |
| 549 | { |
| 550 | const int len = 9; |
| 551 | |
| 552 | // Construct the data array |
| 553 | var data = new bool[len]; |
| 554 | data[0] = true; |
| 555 | data[1] = true; |
| 556 | data[2] = false; |
| 557 | data[3] = true; |
| 558 | data[4] = false; |
| 559 | data[5] = true; |
| 560 | data[6] = true; |
| 561 | data[7] = true; |
| 562 | data[8] = false; |
| 563 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 564 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(bool)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | [FlatBuffersTestMethod] |
| 568 | public void ByteBuffer_Put_Array_Long() |
| 569 | { |
| 570 | const int len = 9; |
| 571 | |
| 572 | // Construct the data array |
| 573 | var data = new long[len]; |
| 574 | data[0] = 1; |
| 575 | data[1] = 4; |
| 576 | data[2] = 6; |
| 577 | data[3] = 9; |
| 578 | data[4] = 10; |
| 579 | data[5] = 12; |
| 580 | data[6] = 14; |
| 581 | data[7] = 15; |
| 582 | data[8] = 18; |
| 583 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 584 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(long)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | [FlatBuffersTestMethod] |
| 588 | public void ByteBuffer_Put_Array_Byte() |
| 589 | { |
| 590 | const int len = 9; |
| 591 | |
| 592 | // Construct the data array |
| 593 | var data = new byte[len]; |
| 594 | data[0] = 1; |
| 595 | data[1] = 4; |
| 596 | data[2] = 6; |
| 597 | data[3] = 9; |
| 598 | data[4] = 10; |
| 599 | data[5] = 12; |
| 600 | data[6] = 14; |
| 601 | data[7] = 15; |
| 602 | data[8] = 18; |
| 603 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 604 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(byte)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | [FlatBuffersTestMethod] |
| 608 | public void ByteBuffer_Put_Array_SByte() |
| 609 | { |
| 610 | const int len = 9; |
| 611 | |
| 612 | // Construct the data array |
| 613 | var data = new sbyte[len]; |
| 614 | data[0] = 1; |
| 615 | data[1] = 4; |
| 616 | data[2] = 6; |
| 617 | data[3] = 9; |
| 618 | data[4] = 10; |
| 619 | data[5] = 12; |
| 620 | data[6] = 14; |
| 621 | data[7] = 15; |
| 622 | data[8] = 18; |
| 623 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 624 | ByteBuffer_Put_ArrayTypes_Helper(data, sizeof(sbyte)); |
| 625 | } |
| 626 | |
| 627 | private static ArraySegment<T> CreateArraySegment<T>(T[] data) |
| 628 | where T : struct |
| 629 | { |
| 630 | const int arraySegmentPadding = 7; |
| 631 | var newData = new T[data.Length + 2 * arraySegmentPadding]; |
| 632 | Array.Copy(data, 0, newData, arraySegmentPadding, data.Length); |
| 633 | return new ArraySegment<T>(newData, arraySegmentPadding, data.Length); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | [FlatBuffersTestMethod] |
| 637 | public void ByteBuffer_Put_Array_Null_Throws() |
| 638 | { |
| 639 | // Create the Byte Buffer |
| 640 | var uut = new ByteBuffer(1024); |
| 641 | |
| 642 | // create a null array and try to put it into the buffer |
| 643 | float[] data = null; |
| 644 | Assert.Throws<ArgumentNullException>(() => uut.Put(1024, data)); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 645 | |
| 646 | ArraySegment<float> dataArraySegment = default; |
| 647 | Assert.Throws<ArgumentNullException>(() => uut.Put(1024, dataArraySegment)); |
| 648 | |
| 649 | IntPtr dataPtr = IntPtr.Zero; |
| 650 | int dataPtrLength = 100; |
| 651 | Assert.Throws<ArgumentNullException>(() => uut.Put<float>(1024, dataPtr, dataPtrLength)); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | [FlatBuffersTestMethod] |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 655 | public unsafe void ByteBuffer_Put_Array_Empty_Throws() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 656 | { |
| 657 | // Create the Byte Buffer |
| 658 | var uut = new ByteBuffer(1024); |
| 659 | |
| 660 | // create an array of length == 0, and try to put it into the buffer |
| 661 | float[] data = new float[0]; |
| 662 | Assert.Throws<ArgumentException>(() => uut.Put(1024, data)); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 663 | |
| 664 | var dataArraySegment = new ArraySegment<float>(new float[10], 5, 0); |
| 665 | Assert.Throws<ArgumentException>(() => uut.Put(1024, dataArraySegment)); |
| 666 | |
| 667 | fixed(float* floatPtr = data) |
| 668 | { |
| 669 | var dataPtr = (IntPtr)floatPtr; |
| 670 | var dataPtrLength = 0; |
| 671 | Assert.Throws<ArgumentException>(() => uut.Put<float>(1024, dataPtr, dataPtrLength)); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | [FlatBuffersTestMethod] |
| 676 | public unsafe void ByteBuffer_Put_IntPtr_NegativeSize_Throws() |
| 677 | { |
| 678 | // Create the Byte Buffer |
| 679 | var uut = new ByteBuffer(1024); |
| 680 | |
| 681 | // create an array of length == 0, and try to put it into the buffer |
| 682 | float[] data = new float[10]; |
| 683 | fixed(float* floatPtr = data) |
| 684 | { |
| 685 | var dataPtr = (IntPtr)floatPtr; |
| 686 | var dataPtrLength = -1; |
| 687 | Assert.Throws<ArgumentException>(() => uut.Put<float>(1024, dataPtr, dataPtrLength)); |
| 688 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 691 | #pragma warning disable 0169 |
| 692 | // These are purposely not used and the warning is suppress |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 693 | private struct dummyStruct |
| 694 | { |
| 695 | int a; |
| 696 | float b; |
| 697 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 698 | #pragma warning restore 0169 |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 699 | |
| 700 | [FlatBuffersTestMethod] |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 701 | public unsafe void ByteBuffer_Put_Array_IncorrectType_Throws() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 702 | { |
| 703 | // Create the Byte Buffer |
| 704 | var uut = new ByteBuffer(1024); |
| 705 | |
| 706 | // Create an array of dummy structures that shouldn't be |
| 707 | // able to be put into the buffer |
| 708 | var data = new dummyStruct[10]; |
| 709 | Assert.Throws<ArgumentException>(() => uut.Put(1024, data)); |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 710 | |
| 711 | var dataArraySegment = new ArraySegment<dummyStruct>(data); |
| 712 | Assert.Throws<ArgumentException>(() => uut.Put(1024, dataArraySegment)); |
| 713 | |
| 714 | fixed(dummyStruct* floatPtr = data) |
| 715 | { |
| 716 | var dataPtr = (IntPtr)floatPtr; |
| 717 | var dataPtrLength = data.Length * sizeof(dummyStruct); |
| 718 | Assert.Throws<ArgumentException>(() => uut.Put<dummyStruct>(1024, dataPtr, dataPtrLength)); |
| 719 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 720 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 721 | |
| 722 | [FlatBuffersTestMethod] |
| 723 | public void ByteBuffer_Get_Double() |
| 724 | { |
| 725 | var uut = new ByteBuffer(1024); |
| 726 | double value = 3.14159265; |
| 727 | uut.PutDouble(900, value); |
| 728 | double getValue = uut.GetDouble(900); |
| 729 | Assert.AreEqual(value, getValue); |
| 730 | } |
| 731 | |
| 732 | [FlatBuffersTestMethod] |
| 733 | public void ByteBuffer_Get_Float() |
| 734 | { |
| 735 | var uut = new ByteBuffer(1024); |
| 736 | float value = 3.14159265F; |
| 737 | uut.PutFloat(900, value); |
| 738 | double getValue = uut.GetFloat(900); |
| 739 | Assert.AreEqual(value, getValue); |
| 740 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 741 | } |
| 742 | } |