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 buffer = new byte[1]; 48 var uut = new ByteBuffer(buffer); 49 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutByte(1, 99)); 50 } 51 #endif 52 53 [FlatBuffersTestMethod] ByteBuffer_PutShortPopulatesBufferCorrectly()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] ByteBuffer_PutShortCannotPutAtOffsetPastLength()67 public void ByteBuffer_PutShortCannotPutAtOffsetPastLength() 68 { 69 var buffer = new byte[2]; 70 var uut = new ByteBuffer(buffer); 71 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(2, 99)); 72 } 73 #endif 74 75 #if !BYTEBUFFER_NO_BOUNDS_CHECK 76 [FlatBuffersTestMethod] ByteBuffer_PutShortChecksLength()77 public void ByteBuffer_PutShortChecksLength() 78 { 79 var buffer = new byte[1]; 80 var uut = new ByteBuffer(buffer); 81 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(0, 99)); 82 } 83 84 [FlatBuffersTestMethod] ByteBuffer_PutShortChecksLengthAndOffset()85 public void ByteBuffer_PutShortChecksLengthAndOffset() 86 { 87 var buffer = new byte[2]; 88 var uut = new ByteBuffer(buffer); 89 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutShort(1, 99)); 90 } 91 #endif 92 93 [FlatBuffersTestMethod] ByteBuffer_PutIntPopulatesBufferCorrectly()94 public void ByteBuffer_PutIntPopulatesBufferCorrectly() 95 { 96 var buffer = new byte[4]; 97 var uut = new ByteBuffer(buffer); 98 uut.PutInt(0, 0x0A0B0C0D); 99 100 // Ensure Endianness was written correctly 101 Assert.AreEqual(0x0D, buffer[0]); 102 Assert.AreEqual(0x0C, buffer[1]); 103 Assert.AreEqual(0x0B, buffer[2]); 104 Assert.AreEqual(0x0A, buffer[3]); 105 } 106 107 #if !BYTEBUFFER_NO_BOUNDS_CHECK 108 [FlatBuffersTestMethod] ByteBuffer_PutIntCannotPutAtOffsetPastLength()109 public void ByteBuffer_PutIntCannotPutAtOffsetPastLength() 110 { 111 var buffer = new byte[4]; 112 var uut = new ByteBuffer(buffer); 113 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); 114 } 115 116 [FlatBuffersTestMethod] ByteBuffer_PutIntChecksLength()117 public void ByteBuffer_PutIntChecksLength() 118 { 119 var buffer = new byte[1]; 120 var uut = new ByteBuffer(buffer); 121 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(0, 0x0A0B0C0D)); 122 } 123 124 [FlatBuffersTestMethod] ByteBuffer_PutIntChecksLengthAndOffset()125 public void ByteBuffer_PutIntChecksLengthAndOffset() 126 { 127 var buffer = new byte[4]; 128 var uut = new ByteBuffer(buffer); 129 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutInt(2, 0x0A0B0C0D)); 130 } 131 #endif 132 133 [FlatBuffersTestMethod] ByteBuffer_PutLongPopulatesBufferCorrectly()134 public void ByteBuffer_PutLongPopulatesBufferCorrectly() 135 { 136 var buffer = new byte[8]; 137 var uut = new ByteBuffer(buffer); 138 uut.PutLong(0, 0x010203040A0B0C0D); 139 140 // Ensure Endianness was written correctly 141 Assert.AreEqual(0x0D, buffer[0]); 142 Assert.AreEqual(0x0C, buffer[1]); 143 Assert.AreEqual(0x0B, buffer[2]); 144 Assert.AreEqual(0x0A, buffer[3]); 145 Assert.AreEqual(0x04, buffer[4]); 146 Assert.AreEqual(0x03, buffer[5]); 147 Assert.AreEqual(0x02, buffer[6]); 148 Assert.AreEqual(0x01, buffer[7]); 149 } 150 151 #if !BYTEBUFFER_NO_BOUNDS_CHECK 152 [FlatBuffersTestMethod] ByteBuffer_PutLongCannotPutAtOffsetPastLength()153 public void ByteBuffer_PutLongCannotPutAtOffsetPastLength() 154 { 155 var buffer = new byte[8]; 156 var uut = new ByteBuffer(buffer); 157 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); 158 } 159 160 [FlatBuffersTestMethod] ByteBuffer_PutLongChecksLength()161 public void ByteBuffer_PutLongChecksLength() 162 { 163 var buffer = new byte[1]; 164 var uut = new ByteBuffer(buffer); 165 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(0, 0x010203040A0B0C0D)); 166 } 167 168 [FlatBuffersTestMethod] ByteBuffer_PutLongChecksLengthAndOffset()169 public void ByteBuffer_PutLongChecksLengthAndOffset() 170 { 171 var buffer = new byte[8]; 172 var uut = new ByteBuffer(buffer); 173 Assert.Throws<ArgumentOutOfRangeException>(() => uut.PutLong(2, 0x010203040A0B0C0D)); 174 } 175 #endif 176 177 [FlatBuffersTestMethod] ByteBuffer_GetByteReturnsCorrectData()178 public void ByteBuffer_GetByteReturnsCorrectData() 179 { 180 var buffer = new byte[1]; 181 buffer[0] = 99; 182 var uut = new ByteBuffer(buffer); 183 Assert.AreEqual((byte)99, uut.Get(0)); 184 } 185 186 #if !BYTEBUFFER_NO_BOUNDS_CHECK 187 [FlatBuffersTestMethod] ByteBuffer_GetByteChecksOffset()188 public void ByteBuffer_GetByteChecksOffset() 189 { 190 var buffer = new byte[1]; 191 var uut = new ByteBuffer(buffer); 192 Assert.Throws<ArgumentOutOfRangeException>(()=>uut.Get(1)); 193 } 194 #endif 195 196 [FlatBuffersTestMethod] ByteBuffer_GetShortReturnsCorrectData()197 public void ByteBuffer_GetShortReturnsCorrectData() 198 { 199 var buffer = new byte[2]; 200 buffer[0] = 1; 201 buffer[1] = 0; 202 var uut = new ByteBuffer(buffer); 203 Assert.AreEqual(1, uut.GetShort(0)); 204 } 205 206 #if !BYTEBUFFER_NO_BOUNDS_CHECK 207 [FlatBuffersTestMethod] ByteBuffer_GetShortChecksOffset()208 public void ByteBuffer_GetShortChecksOffset() 209 { 210 var buffer = new byte[2]; 211 var uut = new ByteBuffer(buffer); 212 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(2)); 213 } 214 215 [FlatBuffersTestMethod] ByteBuffer_GetShortChecksLength()216 public void ByteBuffer_GetShortChecksLength() 217 { 218 var buffer = new byte[2]; 219 var uut = new ByteBuffer(buffer); 220 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetShort(1)); 221 } 222 #endif 223 224 [FlatBuffersTestMethod] ByteBuffer_GetIntReturnsCorrectData()225 public void ByteBuffer_GetIntReturnsCorrectData() 226 { 227 var buffer = new byte[4]; 228 buffer[0] = 0x0D; 229 buffer[1] = 0x0C; 230 buffer[2] = 0x0B; 231 buffer[3] = 0x0A; 232 var uut = new ByteBuffer(buffer); 233 Assert.AreEqual(0x0A0B0C0D, uut.GetInt(0)); 234 } 235 236 #if !BYTEBUFFER_NO_BOUNDS_CHECK 237 [FlatBuffersTestMethod] ByteBuffer_GetIntChecksOffset()238 public void ByteBuffer_GetIntChecksOffset() 239 { 240 var buffer = new byte[4]; 241 var uut = new ByteBuffer(buffer); 242 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(4)); 243 } 244 245 [FlatBuffersTestMethod] ByteBuffer_GetIntChecksLength()246 public void ByteBuffer_GetIntChecksLength() 247 { 248 var buffer = new byte[2]; 249 var uut = new ByteBuffer(buffer); 250 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetInt(0)); 251 } 252 #endif 253 254 [FlatBuffersTestMethod] ByteBuffer_GetLongReturnsCorrectData()255 public void ByteBuffer_GetLongReturnsCorrectData() 256 { 257 var buffer = new byte[8]; 258 buffer[0] = 0x0D; 259 buffer[1] = 0x0C; 260 buffer[2] = 0x0B; 261 buffer[3] = 0x0A; 262 buffer[4] = 0x04; 263 buffer[5] = 0x03; 264 buffer[6] = 0x02; 265 buffer[7] = 0x01; 266 var uut = new ByteBuffer(buffer); 267 Assert.AreEqual(0x010203040A0B0C0D, uut.GetLong(0)); 268 } 269 270 #if !BYTEBUFFER_NO_BOUNDS_CHECK 271 [FlatBuffersTestMethod] ByteBuffer_GetLongChecksOffset()272 public void ByteBuffer_GetLongChecksOffset() 273 { 274 var buffer = new byte[8]; 275 var uut = new ByteBuffer(buffer); 276 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(8)); 277 } 278 279 [FlatBuffersTestMethod] ByteBuffer_GetLongChecksLength()280 public void ByteBuffer_GetLongChecksLength() 281 { 282 var buffer = new byte[7]; 283 var uut = new ByteBuffer(buffer); 284 Assert.Throws<ArgumentOutOfRangeException>(() => uut.GetLong(0)); 285 } 286 #endif 287 288 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUshort()289 public void ByteBuffer_ReverseBytesUshort() 290 { 291 const ushort original = (ushort)0x1234U; 292 var reverse = ByteBuffer.ReverseBytes(original); 293 Assert.AreEqual(0x3412U, reverse); 294 295 var rereverse = ByteBuffer.ReverseBytes(reverse); 296 Assert.AreEqual(original, rereverse); 297 } 298 299 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUint()300 public void ByteBuffer_ReverseBytesUint() 301 { 302 const uint original = 0x12345678; 303 var reverse = ByteBuffer.ReverseBytes(original); 304 Assert.AreEqual(0x78563412U, reverse); 305 306 var rereverse = ByteBuffer.ReverseBytes(reverse); 307 Assert.AreEqual(original, rereverse); 308 } 309 310 [FlatBuffersTestMethod] ByteBuffer_ReverseBytesUlong()311 public void ByteBuffer_ReverseBytesUlong() 312 { 313 const ulong original = 0x1234567890ABCDEFUL; 314 var reverse = ByteBuffer.ReverseBytes(original); 315 Assert.AreEqual(0xEFCDAB9078563412UL, reverse); 316 317 var rereverse = ByteBuffer.ReverseBytes(reverse); 318 Assert.AreEqual(original, rereverse); 319 } 320 } 321 } 322