1 /* 2 * Copyright 2016 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 FlatBufferBuilderTests 23 { CreateBuffer(bool forceDefaults = true)24 private FlatBufferBuilder CreateBuffer(bool forceDefaults = true) 25 { 26 var fbb = new FlatBufferBuilder(16) {ForceDefaults = forceDefaults}; 27 fbb.StartTable(1); 28 return fbb; 29 } 30 31 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddBool_AndDefaultValue_OffsetIncreasesBySize()32 public void FlatBufferBuilder_WithForceDefaults_WhenAddBool_AndDefaultValue_OffsetIncreasesBySize() 33 { 34 var fbb = CreateBuffer(); 35 var storedOffset = fbb.Offset; 36 fbb.AddBool(0, false, false); 37 var endOffset = fbb.Offset; 38 Assert.AreEqual(sizeof(bool), endOffset-storedOffset); 39 } 40 41 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddSByte_AndDefaultValue_OffsetIncreasesBySize()42 public void FlatBufferBuilder_WithForceDefaults_WhenAddSByte_AndDefaultValue_OffsetIncreasesBySize() 43 { 44 var fbb = CreateBuffer(); 45 var storedOffset = fbb.Offset; 46 fbb.AddSbyte(0, 0, 0); 47 var endOffset = fbb.Offset; 48 Assert.AreEqual(sizeof(sbyte), endOffset - storedOffset); 49 } 50 51 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddByte_AndDefaultValue_OffsetIncreasesBySize()52 public void FlatBufferBuilder_WithForceDefaults_WhenAddByte_AndDefaultValue_OffsetIncreasesBySize() 53 { 54 var fbb = CreateBuffer(); 55 var storedOffset = fbb.Offset; 56 fbb.AddByte(0, 0, 0); 57 var endOffset = fbb.Offset; 58 Assert.AreEqual(sizeof(byte), endOffset - storedOffset); 59 } 60 61 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddShort_AndDefaultValue_OffsetIncreasesBySize()62 public void FlatBufferBuilder_WithForceDefaults_WhenAddShort_AndDefaultValue_OffsetIncreasesBySize() 63 { 64 var fbb = CreateBuffer(); 65 var storedOffset = fbb.Offset; 66 fbb.AddShort(0, 0, 0); 67 var endOffset = fbb.Offset; 68 Assert.AreEqual(sizeof(short), endOffset - storedOffset); 69 } 70 71 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddUShort_AndDefaultValue_OffsetIncreasesBySize()72 public void FlatBufferBuilder_WithForceDefaults_WhenAddUShort_AndDefaultValue_OffsetIncreasesBySize() 73 { 74 var fbb = CreateBuffer(); 75 var storedOffset = fbb.Offset; 76 fbb.AddUshort(0, 0, 0); 77 var endOffset = fbb.Offset; 78 Assert.AreEqual(sizeof(ushort), endOffset - storedOffset); 79 } 80 81 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddInt_AndDefaultValue_OffsetIncreasesBySize()82 public void FlatBufferBuilder_WithForceDefaults_WhenAddInt_AndDefaultValue_OffsetIncreasesBySize() 83 { 84 var fbb = CreateBuffer(); 85 var storedOffset = fbb.Offset; 86 fbb.AddInt(0, 0, 0); 87 var endOffset = fbb.Offset; 88 Assert.AreEqual(sizeof(int), endOffset - storedOffset); 89 } 90 91 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddUInt_AndDefaultValue_OffsetIncreasesBySize()92 public void FlatBufferBuilder_WithForceDefaults_WhenAddUInt_AndDefaultValue_OffsetIncreasesBySize() 93 { 94 var fbb = CreateBuffer(); 95 var storedOffset = fbb.Offset; 96 fbb.AddUint(0, 0, 0); 97 var endOffset = fbb.Offset; 98 Assert.AreEqual(sizeof(uint), endOffset - storedOffset); 99 } 100 101 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddLong_AndDefaultValue_OffsetIncreasesBySize()102 public void FlatBufferBuilder_WithForceDefaults_WhenAddLong_AndDefaultValue_OffsetIncreasesBySize() 103 { 104 var fbb = CreateBuffer(); 105 var storedOffset = fbb.Offset; 106 fbb.AddLong(0, 0, 0); 107 var endOffset = fbb.Offset; 108 Assert.AreEqual(sizeof(long), endOffset - storedOffset); 109 } 110 111 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddULong_AndDefaultValue_OffsetIncreasesBySize()112 public void FlatBufferBuilder_WithForceDefaults_WhenAddULong_AndDefaultValue_OffsetIncreasesBySize() 113 { 114 var fbb = CreateBuffer(); 115 var storedOffset = fbb.Offset; 116 fbb.AddUlong(0, 0, 0); 117 var endOffset = fbb.Offset; 118 Assert.AreEqual(sizeof(ulong), endOffset - storedOffset); 119 } 120 121 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddFloat_AndDefaultValue_OffsetIncreasesBySize()122 public void FlatBufferBuilder_WithForceDefaults_WhenAddFloat_AndDefaultValue_OffsetIncreasesBySize() 123 { 124 var fbb = CreateBuffer(); 125 var storedOffset = fbb.Offset; 126 fbb.AddFloat(0, 0, 0); 127 var endOffset = fbb.Offset; 128 Assert.AreEqual(sizeof(float), endOffset - storedOffset); 129 } 130 131 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddDouble_AndDefaultValue_OffsetIncreasesBySize()132 public void FlatBufferBuilder_WithForceDefaults_WhenAddDouble_AndDefaultValue_OffsetIncreasesBySize() 133 { 134 var fbb = CreateBuffer(); 135 var storedOffset = fbb.Offset; 136 fbb.AddDouble(0, 0, 0); 137 var endOffset = fbb.Offset; 138 Assert.AreEqual(sizeof(double), endOffset - storedOffset); 139 } 140 141 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddBool_AndDefaultValue_OffsetIsUnchanged()142 public void FlatBufferBuilder_WhenAddBool_AndDefaultValue_OffsetIsUnchanged() 143 { 144 var fbb = CreateBuffer(false); 145 var storedOffset = fbb.Offset; 146 fbb.AddBool(0, false, false); 147 var endOffset = fbb.Offset; 148 Assert.AreEqual(endOffset, storedOffset); 149 } 150 151 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddSByte_AndDefaultValue_OffsetIsUnchanged()152 public void FlatBufferBuilder_WhenAddSByte_AndDefaultValue_OffsetIsUnchanged() 153 { 154 var fbb = CreateBuffer(false); 155 var storedOffset = fbb.Offset; 156 fbb.AddSbyte(0, 0, 0); 157 var endOffset = fbb.Offset; 158 Assert.AreEqual(endOffset, storedOffset); 159 } 160 161 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddByte_AndDefaultValue_OffsetIsUnchanged()162 public void FlatBufferBuilder_WhenAddByte_AndDefaultValue_OffsetIsUnchanged() 163 { 164 var fbb = CreateBuffer(false); 165 var storedOffset = fbb.Offset; 166 fbb.AddByte(0, 0, 0); 167 var endOffset = fbb.Offset; 168 Assert.AreEqual(endOffset, storedOffset); 169 } 170 171 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddShort_AndDefaultValue_OffsetIsUnchanged()172 public void FlatBufferBuilder_WhenAddShort_AndDefaultValue_OffsetIsUnchanged() 173 { 174 var fbb = CreateBuffer(false); 175 var storedOffset = fbb.Offset; 176 fbb.AddShort(0, 0, 0); 177 var endOffset = fbb.Offset; 178 Assert.AreEqual(endOffset, storedOffset); 179 } 180 181 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddUShort_AndDefaultValue_OffsetIsUnchanged()182 public void FlatBufferBuilder_WhenAddUShort_AndDefaultValue_OffsetIsUnchanged() 183 { 184 var fbb = CreateBuffer(false); 185 var storedOffset = fbb.Offset; 186 fbb.AddUshort(0, 0, 0); 187 var endOffset = fbb.Offset; 188 Assert.AreEqual(endOffset, storedOffset); 189 } 190 191 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddInt_AndDefaultValue_OffsetIsUnchanged()192 public void FlatBufferBuilder_WhenAddInt_AndDefaultValue_OffsetIsUnchanged() 193 { 194 var fbb = CreateBuffer(false); 195 var storedOffset = fbb.Offset; 196 fbb.AddInt(0, 0, 0); 197 var endOffset = fbb.Offset; 198 Assert.AreEqual(endOffset, storedOffset); 199 } 200 201 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddUInt_AndDefaultValue_OffsetIsUnchanged()202 public void FlatBufferBuilder_WhenAddUInt_AndDefaultValue_OffsetIsUnchanged() 203 { 204 var fbb = CreateBuffer(false); 205 var storedOffset = fbb.Offset; 206 fbb.AddUint(0, 0, 0); 207 var endOffset = fbb.Offset; 208 Assert.AreEqual(endOffset, storedOffset); 209 } 210 211 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddLong_AndDefaultValue_OffsetIsUnchanged()212 public void FlatBufferBuilder_WhenAddLong_AndDefaultValue_OffsetIsUnchanged() 213 { 214 var fbb = CreateBuffer(false); 215 var storedOffset = fbb.Offset; 216 fbb.AddLong(0, 0, 0); 217 var endOffset = fbb.Offset; 218 Assert.AreEqual(endOffset, storedOffset); 219 } 220 221 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddULong_AndDefaultValue_OffsetIsUnchanged()222 public void FlatBufferBuilder_WhenAddULong_AndDefaultValue_OffsetIsUnchanged() 223 { 224 var fbb = CreateBuffer(false); 225 var storedOffset = fbb.Offset; 226 fbb.AddUlong(0, 0, 0); 227 var endOffset = fbb.Offset; 228 Assert.AreEqual(endOffset, storedOffset); 229 } 230 231 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddFloat_AndDefaultValue_OffsetIsUnchanged()232 public void FlatBufferBuilder_WhenAddFloat_AndDefaultValue_OffsetIsUnchanged() 233 { 234 var fbb = CreateBuffer(false); 235 var storedOffset = fbb.Offset; 236 fbb.AddFloat(0, 0, 0); 237 var endOffset = fbb.Offset; 238 Assert.AreEqual(endOffset, storedOffset); 239 } 240 241 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddDouble_AndDefaultValue_OffsetIsUnchanged()242 public void FlatBufferBuilder_WhenAddDouble_AndDefaultValue_OffsetIsUnchanged() 243 { 244 var fbb = CreateBuffer(false); 245 var storedOffset = fbb.Offset; 246 fbb.AddDouble(0, 0, 0); 247 var endOffset = fbb.Offset; 248 Assert.AreEqual(endOffset, storedOffset); 249 } 250 251 [FlatBuffersTestMethod] FlatBufferBuilder_Add_Array_Float()252 public void FlatBufferBuilder_Add_Array_Float() 253 { 254 var fbb = CreateBuffer(false); 255 var storedOffset = fbb.Offset; 256 257 const int len = 9; 258 259 // Construct the data array 260 var data = new float[len]; 261 data[0] = 1.0079F; 262 data[1] = 4.0026F; 263 data[2] = 6.941F; 264 data[3] = 9.0122F; 265 data[4] = 10.811F; 266 data[5] = 12.0107F; 267 data[6] = 14.0067F; 268 data[7] = 15.9994F; 269 data[8] = 18.9984F; 270 271 fbb.Add(data); 272 var endOffset = fbb.Offset; 273 Assert.AreEqual(endOffset, storedOffset + sizeof(float) * data.Length); 274 } 275 276 [FlatBuffersTestMethod] FlatBufferBuilder_Add_Array_Bool()277 public void FlatBufferBuilder_Add_Array_Bool() 278 { 279 var fbb = CreateBuffer(false); 280 var storedOffset = fbb.Offset; 281 282 const int len = 9; 283 284 // Construct the data array 285 var data = new bool[len]; 286 data[0] = true; 287 data[1] = true; 288 data[2] = false; 289 data[3] = true; 290 data[4] = false; 291 data[5] = true; 292 data[6] = true; 293 data[7] = true; 294 data[8] = false; 295 296 fbb.Add(data); 297 var endOffset = fbb.Offset; 298 Assert.AreEqual(endOffset, storedOffset + sizeof(bool) * data.Length); 299 } 300 301 [FlatBuffersTestMethod] FlatBufferBuilder_Add_Array_Double()302 public void FlatBufferBuilder_Add_Array_Double() 303 { 304 var fbb = CreateBuffer(false); 305 var storedOffset = fbb.Offset; 306 307 const int len = 9; 308 309 // Construct the data array 310 var data = new double[len]; 311 data[0] = 1.0079; 312 data[1] = 4.0026; 313 data[2] = 6.941; 314 data[3] = 9.0122; 315 data[4] = 10.811; 316 data[5] = 12.0107; 317 data[6] = 14.0067; 318 data[7] = 15.9994; 319 data[8] = 18.9984; 320 321 fbb.Add(data); 322 var endOffset = fbb.Offset; 323 Assert.AreEqual(endOffset, storedOffset + sizeof(double) * data.Length); 324 } 325 326 [FlatBuffersTestMethod] FlatBufferBuilder_Add_Array_Null_Throws()327 public void FlatBufferBuilder_Add_Array_Null_Throws() 328 { 329 var fbb = CreateBuffer(false); 330 331 // Construct the data array 332 float[] data = null; 333 334 Assert.Throws<ArgumentNullException>(() => fbb.Add(data)); 335 } 336 337 [FlatBuffersTestMethod] FlatBufferBuilder_Add_Array_Empty_Noop()338 public void FlatBufferBuilder_Add_Array_Empty_Noop() 339 { 340 var fbb = CreateBuffer(false); 341 342 var storedOffset = fbb.Offset; 343 344 // Construct an empty data array 345 float[] data = new float[0]; 346 fbb.Add(data); 347 348 // Make sure the offset didn't change since nothing 349 // was really added 350 var endOffset = fbb.Offset; 351 Assert.AreEqual(endOffset, storedOffset); 352 } 353 } 354 } 355