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 namespace FlatBuffers.Test 18 { 19 [FlatBuffersTestClass] 20 public class FlatBufferBuilderTests 21 { CreateBuffer(bool forceDefaults = true)22 private FlatBufferBuilder CreateBuffer(bool forceDefaults = true) 23 { 24 var fbb = new FlatBufferBuilder(16) {ForceDefaults = forceDefaults}; 25 fbb.StartObject(1); 26 return fbb; 27 } 28 29 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddBool_AndDefaultValue_OffsetIncreasesBySize()30 public void FlatBufferBuilder_WithForceDefaults_WhenAddBool_AndDefaultValue_OffsetIncreasesBySize() 31 { 32 var fbb = CreateBuffer(); 33 var storedOffset = fbb.Offset; 34 fbb.AddBool(0, false, false); 35 var endOffset = fbb.Offset; 36 Assert.AreEqual(sizeof(bool), endOffset-storedOffset); 37 } 38 39 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddSByte_AndDefaultValue_OffsetIncreasesBySize()40 public void FlatBufferBuilder_WithForceDefaults_WhenAddSByte_AndDefaultValue_OffsetIncreasesBySize() 41 { 42 var fbb = CreateBuffer(); 43 var storedOffset = fbb.Offset; 44 fbb.AddSbyte(0, 0, 0); 45 var endOffset = fbb.Offset; 46 Assert.AreEqual(sizeof(sbyte), endOffset - storedOffset); 47 } 48 49 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddByte_AndDefaultValue_OffsetIncreasesBySize()50 public void FlatBufferBuilder_WithForceDefaults_WhenAddByte_AndDefaultValue_OffsetIncreasesBySize() 51 { 52 var fbb = CreateBuffer(); 53 var storedOffset = fbb.Offset; 54 fbb.AddByte(0, 0, 0); 55 var endOffset = fbb.Offset; 56 Assert.AreEqual(sizeof(byte), endOffset - storedOffset); 57 } 58 59 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddShort_AndDefaultValue_OffsetIncreasesBySize()60 public void FlatBufferBuilder_WithForceDefaults_WhenAddShort_AndDefaultValue_OffsetIncreasesBySize() 61 { 62 var fbb = CreateBuffer(); 63 var storedOffset = fbb.Offset; 64 fbb.AddShort(0, 0, 0); 65 var endOffset = fbb.Offset; 66 Assert.AreEqual(sizeof(short), endOffset - storedOffset); 67 } 68 69 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddUShort_AndDefaultValue_OffsetIncreasesBySize()70 public void FlatBufferBuilder_WithForceDefaults_WhenAddUShort_AndDefaultValue_OffsetIncreasesBySize() 71 { 72 var fbb = CreateBuffer(); 73 var storedOffset = fbb.Offset; 74 fbb.AddUshort(0, 0, 0); 75 var endOffset = fbb.Offset; 76 Assert.AreEqual(sizeof(ushort), endOffset - storedOffset); 77 } 78 79 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddInt_AndDefaultValue_OffsetIncreasesBySize()80 public void FlatBufferBuilder_WithForceDefaults_WhenAddInt_AndDefaultValue_OffsetIncreasesBySize() 81 { 82 var fbb = CreateBuffer(); 83 var storedOffset = fbb.Offset; 84 fbb.AddInt(0, 0, 0); 85 var endOffset = fbb.Offset; 86 Assert.AreEqual(sizeof(int), endOffset - storedOffset); 87 } 88 89 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddUInt_AndDefaultValue_OffsetIncreasesBySize()90 public void FlatBufferBuilder_WithForceDefaults_WhenAddUInt_AndDefaultValue_OffsetIncreasesBySize() 91 { 92 var fbb = CreateBuffer(); 93 var storedOffset = fbb.Offset; 94 fbb.AddUint(0, 0, 0); 95 var endOffset = fbb.Offset; 96 Assert.AreEqual(sizeof(uint), endOffset - storedOffset); 97 } 98 99 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddLong_AndDefaultValue_OffsetIncreasesBySize()100 public void FlatBufferBuilder_WithForceDefaults_WhenAddLong_AndDefaultValue_OffsetIncreasesBySize() 101 { 102 var fbb = CreateBuffer(); 103 var storedOffset = fbb.Offset; 104 fbb.AddLong(0, 0, 0); 105 var endOffset = fbb.Offset; 106 Assert.AreEqual(sizeof(long), endOffset - storedOffset); 107 } 108 109 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddULong_AndDefaultValue_OffsetIncreasesBySize()110 public void FlatBufferBuilder_WithForceDefaults_WhenAddULong_AndDefaultValue_OffsetIncreasesBySize() 111 { 112 var fbb = CreateBuffer(); 113 var storedOffset = fbb.Offset; 114 fbb.AddUlong(0, 0, 0); 115 var endOffset = fbb.Offset; 116 Assert.AreEqual(sizeof(ulong), endOffset - storedOffset); 117 } 118 119 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddFloat_AndDefaultValue_OffsetIncreasesBySize()120 public void FlatBufferBuilder_WithForceDefaults_WhenAddFloat_AndDefaultValue_OffsetIncreasesBySize() 121 { 122 var fbb = CreateBuffer(); 123 var storedOffset = fbb.Offset; 124 fbb.AddFloat(0, 0, 0); 125 var endOffset = fbb.Offset; 126 Assert.AreEqual(sizeof(float), endOffset - storedOffset); 127 } 128 129 [FlatBuffersTestMethod] FlatBufferBuilder_WithForceDefaults_WhenAddDouble_AndDefaultValue_OffsetIncreasesBySize()130 public void FlatBufferBuilder_WithForceDefaults_WhenAddDouble_AndDefaultValue_OffsetIncreasesBySize() 131 { 132 var fbb = CreateBuffer(); 133 var storedOffset = fbb.Offset; 134 fbb.AddDouble(0, 0, 0); 135 var endOffset = fbb.Offset; 136 Assert.AreEqual(sizeof(double), endOffset - storedOffset); 137 } 138 139 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddBool_AndDefaultValue_OffsetIsUnchanged()140 public void FlatBufferBuilder_WhenAddBool_AndDefaultValue_OffsetIsUnchanged() 141 { 142 var fbb = CreateBuffer(false); 143 var storedOffset = fbb.Offset; 144 fbb.AddBool(0, false, false); 145 var endOffset = fbb.Offset; 146 Assert.AreEqual(endOffset, storedOffset); 147 } 148 149 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddSByte_AndDefaultValue_OffsetIsUnchanged()150 public void FlatBufferBuilder_WhenAddSByte_AndDefaultValue_OffsetIsUnchanged() 151 { 152 var fbb = CreateBuffer(false); 153 var storedOffset = fbb.Offset; 154 fbb.AddSbyte(0, 0, 0); 155 var endOffset = fbb.Offset; 156 Assert.AreEqual(endOffset, storedOffset); 157 } 158 159 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddByte_AndDefaultValue_OffsetIsUnchanged()160 public void FlatBufferBuilder_WhenAddByte_AndDefaultValue_OffsetIsUnchanged() 161 { 162 var fbb = CreateBuffer(false); 163 var storedOffset = fbb.Offset; 164 fbb.AddByte(0, 0, 0); 165 var endOffset = fbb.Offset; 166 Assert.AreEqual(endOffset, storedOffset); 167 } 168 169 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddShort_AndDefaultValue_OffsetIsUnchanged()170 public void FlatBufferBuilder_WhenAddShort_AndDefaultValue_OffsetIsUnchanged() 171 { 172 var fbb = CreateBuffer(false); 173 var storedOffset = fbb.Offset; 174 fbb.AddShort(0, 0, 0); 175 var endOffset = fbb.Offset; 176 Assert.AreEqual(endOffset, storedOffset); 177 } 178 179 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddUShort_AndDefaultValue_OffsetIsUnchanged()180 public void FlatBufferBuilder_WhenAddUShort_AndDefaultValue_OffsetIsUnchanged() 181 { 182 var fbb = CreateBuffer(false); 183 var storedOffset = fbb.Offset; 184 fbb.AddUshort(0, 0, 0); 185 var endOffset = fbb.Offset; 186 Assert.AreEqual(endOffset, storedOffset); 187 } 188 189 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddInt_AndDefaultValue_OffsetIsUnchanged()190 public void FlatBufferBuilder_WhenAddInt_AndDefaultValue_OffsetIsUnchanged() 191 { 192 var fbb = CreateBuffer(false); 193 var storedOffset = fbb.Offset; 194 fbb.AddInt(0, 0, 0); 195 var endOffset = fbb.Offset; 196 Assert.AreEqual(endOffset, storedOffset); 197 } 198 199 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddUInt_AndDefaultValue_OffsetIsUnchanged()200 public void FlatBufferBuilder_WhenAddUInt_AndDefaultValue_OffsetIsUnchanged() 201 { 202 var fbb = CreateBuffer(false); 203 var storedOffset = fbb.Offset; 204 fbb.AddUint(0, 0, 0); 205 var endOffset = fbb.Offset; 206 Assert.AreEqual(endOffset, storedOffset); 207 } 208 209 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddLong_AndDefaultValue_OffsetIsUnchanged()210 public void FlatBufferBuilder_WhenAddLong_AndDefaultValue_OffsetIsUnchanged() 211 { 212 var fbb = CreateBuffer(false); 213 var storedOffset = fbb.Offset; 214 fbb.AddLong(0, 0, 0); 215 var endOffset = fbb.Offset; 216 Assert.AreEqual(endOffset, storedOffset); 217 } 218 219 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddULong_AndDefaultValue_OffsetIsUnchanged()220 public void FlatBufferBuilder_WhenAddULong_AndDefaultValue_OffsetIsUnchanged() 221 { 222 var fbb = CreateBuffer(false); 223 var storedOffset = fbb.Offset; 224 fbb.AddUlong(0, 0, 0); 225 var endOffset = fbb.Offset; 226 Assert.AreEqual(endOffset, storedOffset); 227 } 228 229 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddFloat_AndDefaultValue_OffsetIsUnchanged()230 public void FlatBufferBuilder_WhenAddFloat_AndDefaultValue_OffsetIsUnchanged() 231 { 232 var fbb = CreateBuffer(false); 233 var storedOffset = fbb.Offset; 234 fbb.AddFloat(0, 0, 0); 235 var endOffset = fbb.Offset; 236 Assert.AreEqual(endOffset, storedOffset); 237 } 238 239 [FlatBuffersTestMethod] FlatBufferBuilder_WhenAddDouble_AndDefaultValue_OffsetIsUnchanged()240 public void FlatBufferBuilder_WhenAddDouble_AndDefaultValue_OffsetIsUnchanged() 241 { 242 var fbb = CreateBuffer(false); 243 var storedOffset = fbb.Offset; 244 fbb.AddDouble(0, 0, 0); 245 var endOffset = fbb.Offset; 246 Assert.AreEqual(endOffset, storedOffset); 247 } 248 } 249 } 250