@@ -81,8 +81,8 @@ local function checkReadBuffer(buf, offset, sizePrefix)
81
81
assert (mon :Testempty () == nil )
82
82
end
83
83
84
- local function generateMonster (sizePrefix )
85
- local b = flatbuffers .Builder (0 )
84
+ local function generateMonster (sizePrefix , b )
85
+ b = b or flatbuffers .Builder (0 )
86
86
local str = b :CreateString (" MyMonster" )
87
87
local test1 = b :CreateString (" test1" )
88
88
local test2 = b :CreateString (" test2" )
@@ -156,6 +156,51 @@ local function sizePrefix(sizePrefix)
156
156
checkReadBuffer (buf , offset , sizePrefix )
157
157
end
158
158
159
+ local function fbbClear ()
160
+ -- Generate a builder that will be 'cleared' and reused to create two different objects.
161
+ local fbb = flatbuffers .Builder (0 )
162
+
163
+ -- First use the builder to read the normal monster data and verify it works
164
+ local buf , offset = generateMonster (false , fbb )
165
+ checkReadBuffer (buf , offset , false )
166
+
167
+ -- Then clear the builder to be used again
168
+ fbb :Clear ()
169
+
170
+ -- Storage for the built monsters
171
+ local monsters = {}
172
+ local lastBuf
173
+
174
+ -- Make another builder that will be use identically to the 'cleared' one so outputs can be compared. Build both the
175
+ -- Cleared builder and new builder in the exact same way, so we can compare their results
176
+ for i , builder in ipairs ({fbb , flatbuffers .Builder (0 )}) do
177
+ local strOffset = builder :CreateString (" Hi there" )
178
+ monster .Start (builder )
179
+ monster .AddPos (builder , vec3 .CreateVec3 (builder , 3.0 , 2.0 , 1.0 , 17.0 , 3 , 100 , 123 ))
180
+ monster .AddName (builder , strOffset )
181
+ monster .AddMana (builder , 123 )
182
+ builder :Finish (monster .End (builder ))
183
+ local buf = builder :Output (false )
184
+ if not lastBuf then
185
+ lastBuf = buf
186
+ else
187
+ -- the output, sized-buffer should be identical
188
+ assert (lastBuf == buf , " Monster output buffers are not identical" )
189
+ end
190
+ monsters [i ] = monster .GetRootAsMonster (flatbuffers .binaryArray .New (buf ), 0 )
191
+ end
192
+
193
+ -- Check that all the fields for the generated monsters are as we expect
194
+ for i , monster in ipairs (monsters ) do
195
+ assert (monster :Name () == " Hi there" , " Monster Name is not 'Hi There' for monster " .. i )
196
+ -- HP is default to 100 in the schema, but we change it in generateMonster to 80, so this is a good test to
197
+ -- see if the cleared builder really clears the data.
198
+ assert (monster :Hp () == 100 , " HP doesn't equal the default value for monster " .. i )
199
+ assert (monster :Mana () == 123 , " Monster Mana is not '123' for monster " .. i )
200
+ assert (monster :Pos ():X () == 3.0 , " Monster vec3.X is not '3' for monster " .. i )
201
+ end
202
+ end
203
+
159
204
local function testCanonicalData ()
160
205
local f = assert (io.open (' monsterdata_test.mon' , ' rb' ))
161
206
local wireData = f :read (" *a" )
@@ -219,6 +264,10 @@ local tests =
219
264
d = " Test size prefix" ,
220
265
args = {{true }, {false }}
221
266
},
267
+ {
268
+ f = fbbClear ,
269
+ d = " FlatBufferBuilder Clear" ,
270
+ },
222
271
{
223
272
f = testCanonicalData ,
224
273
d = " Tests Canonical flatbuffer file included in repo"
0 commit comments