Java/C#/Python prefixed size support (#4445)

* initial changes to support size prefixed buffers in Java

* add slice equivalent to CSharp ByteBuffer

* resolve TODO for slicing in CSharp code generation

* add newly generated Java and CSharp test sources

* fix typo in comment

* add FinishSizePrefixed methods to CSharp FlatBufferBuilder as well

* add option to allow writing the prefix as well

* generate size-prefixed monster binary as well

* extend JavaTest to test the size prefixed binary as well

* use constants for size prefix length

* fuse common code for getRootAs and getSizePrefixedRootAs

* pulled file identifier out of if

* add FinishSizePrefixed, GetSizePrefixedRootAs support for Python

* Revert "extend JavaTest to test the size prefixed binary as well"

This reverts commit 68be4420dd.

* Revert "generate size-prefixed monster binary as well"

This reverts commit 2939516fdf.

* fix ByteBuffer.cs Slice() method; add proper CSharp and Java tests

* fix unused parameter

* increment version number

* pulled out generated methods into separate utility class

* pulled out generated methods into separate utility class for Python

* fix indentation

* remove unnecessary comment

* fix newline and copyright

* add ByteBufferUtil to csproj compilation

* hide ByteBuffer's internal data; track offset into parent's array

* test unsafe versions as well; compile and run in debug mode

* clarify help text for size prefix

* move ByteBuffer slicing behavior to subclass

* fix protection levels

* add size prefix support for text generation

* add ByteBufferSlice to csproj compilation

* revert size prefix handling for nested buffers

* use duplicate instead of slice for removing size prefix

* remove slice subclass and use duplicate for removing size prefix

* remove slice specific tests

* remove superfluous command line option
This commit is contained in:
Robert Schmidtke
2018-03-12 19:30:46 +01:00
committed by Wouter van Oortmerssen
parent 6b3f057bdc
commit 08cf50c54a
24 changed files with 663 additions and 201 deletions

View File

@@ -32,6 +32,12 @@ namespace FlatBuffers.Test
[FlatBuffersTestMethod]
public void CanCreateNewFlatBufferFromScratch()
{
CanCreateNewFlatBufferFromScratch(true);
CanCreateNewFlatBufferFromScratch(false);
}
private void CanCreateNewFlatBufferFromScratch(bool sizePrefix)
{
// Second, let's create a FlatBuffer from scratch in C#, and test it also.
// We use an initial size of 1 to exercise the reallocation algorithm,
@@ -95,22 +101,40 @@ namespace FlatBuffers.Test
Monster.AddTestarrayoftables(fbb, sortMons);
var mon = Monster.EndMonster(fbb);
Monster.FinishMonsterBuffer(fbb, mon);
if (sizePrefix)
{
Monster.FinishSizePrefixedMonsterBuffer(fbb, mon);
}
else
{
Monster.FinishMonsterBuffer(fbb, mon);
}
// Dump to output directory so we can inspect later, if needed
using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset))
using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
{
var data = ms.ToArray();
File.WriteAllBytes(@"Resources/monsterdata_cstest.mon",data);
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
File.WriteAllBytes(filename, data);
}
// Remove the size prefix if necessary for further testing
ByteBuffer dataBuffer = fbb.DataBuffer;
if (sizePrefix)
{
Assert.AreEqual(ByteBufferUtil.GetSizePrefix(dataBuffer) + FlatBufferConstants.SizePrefixLength,
dataBuffer.Length - dataBuffer.Position);
dataBuffer = ByteBufferUtil.RemoveSizePrefix(dataBuffer);
}
// Now assert the buffer
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
//Attempt to mutate Monster fields and check whether the buffer has been mutated properly
// revert to original values after testing
Monster monster = Monster.GetRootAsMonster(fbb.DataBuffer);
Monster monster = Monster.GetRootAsMonster(dataBuffer);
// mana is optional and does not exist in the buffer so the mutation should fail
// the mana field should retain its default value
@@ -161,12 +185,12 @@ namespace FlatBuffers.Test
pos.MutateX(1.0f);
Assert.AreEqual(pos.X, 1.0f);
TestBuffer(fbb.DataBuffer);
TestBuffer(dataBuffer);
}
private void TestBuffer(ByteBuffer bb)
{
var monster = Monster.GetRootAsMonster(bb);
Monster monster = Monster.GetRootAsMonster(bb);
Assert.AreEqual(80, monster.Hp);
Assert.AreEqual(150, monster.Mana);