add a Zeroing test and fix some bugs

This commit is contained in:
David Renshaw 2014-11-09 16:32:51 -05:00
parent a7d3175f60
commit fd3efb080e
3 changed files with 45 additions and 7 deletions

View file

@ -50,7 +50,7 @@ class EncodingSuite extends FunSuite {
TestUtil.initTestMessage(allTypes);
val message2 = new MessageBuilder();
val allTypes2 = message.initRoot(TestAllTypes.factory);
val allTypes2 = message2.initRoot(TestAllTypes.factory);
allTypes2.setStructField(allTypes.asReader());
TestUtil.checkTestMessage(allTypes2.getStructField());
@ -58,6 +58,34 @@ class EncodingSuite extends FunSuite {
TestUtil.checkTestMessage(reader);
}
test("Zeroing") {
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val structList = allTypes.initStructList(3);
TestUtil.initTestMessage(structList.get(0));
val structField = allTypes.initStructField();
TestUtil.initTestMessage(structField);
TestUtil.initTestMessage(structList.get(1));
TestUtil.initTestMessage(structList.get(2));
TestUtil.checkTestMessage(structList.get(0));
allTypes.initStructList(0);
TestUtil.checkTestMessage(allTypes.getStructField());
val allTypesReader = allTypes.asReader();
TestUtil.checkTestMessage(allTypesReader.getStructField());
val any = message.initRoot(AnyPointer.factory);
val segments = message.getSegmentsForOutput();
for (segment <- segments) {
for (jj <- 0 to segment.limit - 1) {
segment.get(jj) should equal (0);
}
}
}
test("Generics") {
val message = new MessageBuilder();
val factory = TestGenerics.newFactory(TestAllTypes.factory, Text.factory);

View file

@ -46,13 +46,18 @@ public final class MessageBuilder {
public <T> T initRoot(FromPointerBuilder<T> factory) {
SegmentBuilder rootSegment = this.arena.segments.get(0);
int location = rootSegment.allocate(1);
if (location == SegmentBuilder.FAILED_ALLOCATION) {
throw new Error("could not allocate root pointer");
if (rootSegment.currentSize() == 0) {
int location = rootSegment.allocate(1);
if (location == SegmentBuilder.FAILED_ALLOCATION) {
throw new Error("could not allocate root pointer");
}
if (location != 0) {
throw new Error("First allocated word of new segment was not at offset 0");
}
return factory.initFromPointerBuilder(rootSegment, location, 0);
} else {
return factory.initFromPointerBuilder(rootSegment, 0, 0);
}
AnyPointer.Builder ptr = new AnyPointer.Builder(rootSegment, location);
return ptr.initAs(factory);
}
public final java.nio.ByteBuffer[] getSegmentsForOutput() {

View file

@ -48,6 +48,11 @@ final class WireHelpers {
int amount, // in words
byte kind) {
long ref = segment.get(refOffset);
if (!WirePointer.isNull(ref)) {
zeroObject(segment, refOffset);
}
if (amount == 0 && kind == WirePointer.STRUCT) {
WirePointer.setKindAndTargetForEmptyStruct(segment.buffer, refOffset);
return new AllocateResult(refOffset, refOffset, segment);