This commit is contained in:
Wouter van Oortmerssen
2016-03-07 15:04:18 -08:00
8 changed files with 173 additions and 42 deletions

View File

@@ -67,6 +67,7 @@ function main() {
// Test it:
testBuffer(fbb.dataBuffer());
test64bit();
testUnicode();
fuzzTest1();
@@ -105,6 +106,13 @@ function testBuffer(bb) {
}
assert.strictEqual(invsum, 10);
var invsum2 = 0;
var invArr = monster.inventoryArray();
for (var i = 0; i < invArr.length; i++) {
invsum2 += invArr[i];
}
assert.strictEqual(invsum2, 10);
var test_0 = monster.test4(0);
var test_1 = monster.test4(1);
assert.strictEqual(monster.test4Length(), 2);
@@ -117,26 +125,74 @@ function testBuffer(bb) {
assert.strictEqual(monster.testbool(), false);
}
function test64bit() {
var fbb = new flatbuffers.Builder();
var required = fbb.createString('required');
MyGame.Example.Stat.startStat(fbb);
var stat2 = MyGame.Example.Stat.endStat(fbb);
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addName(fbb, required);
MyGame.Example.Monster.addTestempty(fbb, stat2);
var mon2 = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Stat.startStat(fbb);
MyGame.Example.Stat.addVal(fbb, new flatbuffers.Long(0x12345678, 0x23456789));
var stat = MyGame.Example.Stat.endStat(fbb);
MyGame.Example.Monster.startMonster(fbb);
MyGame.Example.Monster.addName(fbb, required);
MyGame.Example.Monster.addEnemy(fbb, mon2);
MyGame.Example.Monster.addTestempty(fbb, stat);
var mon = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Monster.finishMonsterBuffer(fbb, mon);
var bytes = fbb.asUint8Array();
////////////////////////////////////////////////////////////////
var bb = new flatbuffers.ByteBuffer(bytes);
assert.ok(MyGame.Example.Monster.bufferHasIdentifier(bb));
var mon = MyGame.Example.Monster.getRootAsMonster(bb);
var stat = mon.testempty();
assert.strictEqual(stat != null, true);
assert.strictEqual(stat.val() != null, true);
assert.strictEqual(stat.val().low, 0x12345678);
assert.strictEqual(stat.val().high, 0x23456789);
var mon2 = mon.enemy();
assert.strictEqual(mon2 != null, true);
stat = mon2.testempty();
assert.strictEqual(stat != null, true);
assert.strictEqual(stat.val() != null, true);
assert.strictEqual(stat.val().low, 0); // default value
assert.strictEqual(stat.val().high, 0);
}
function testUnicode() {
var correct = fs.readFileSync('unicode_test.mon');
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
// Test reading
var bb = new flatbuffers.ByteBuffer(new Uint8Array(correct));
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
assert.strictEqual(monster.name(), json.name);
assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length);
json.testarrayoftables.forEach(function(table, i) {
var value = monster.testarrayoftables(i);
assert.strictEqual(value.name(), table.name);
assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name));
});
assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length);
json.testarrayofstring.forEach(function(string, i) {
assert.strictEqual(monster.testarrayofstring(i), string);
assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
});
function testReadingUnicode(bb) {
var monster = MyGame.Example.Monster.getRootAsMonster(bb);
assert.strictEqual(monster.name(), json.name);
assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name));
assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length);
json.testarrayoftables.forEach(function(table, i) {
var value = monster.testarrayoftables(i);
assert.strictEqual(value.name(), table.name);
assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name));
});
assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length);
json.testarrayofstring.forEach(function(string, i) {
assert.strictEqual(monster.testarrayofstring(i), string);
assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string));
});
}
testReadingUnicode(new flatbuffers.ByteBuffer(new Uint8Array(correct)));
// Test writing
var fbb = new flatbuffers.Builder();
@@ -156,7 +212,7 @@ function testUnicode() {
MyGame.Example.Monster.addTestarrayoftables(fbb, testarrayoftablesOffset);
MyGame.Example.Monster.addName(fbb, name);
MyGame.Example.Monster.finishMonsterBuffer(fbb, MyGame.Example.Monster.endMonster(fbb));
assert.deepEqual(new Buffer(fbb.asUint8Array()), correct);
testReadingUnicode(new flatbuffers.ByteBuffer(fbb.asUint8Array()));
}
var __imul = Math.imul ? Math.imul : function(a, b) {

View File

@@ -300,7 +300,7 @@ MyGame.Example.Stat.prototype.id = function(optionalEncoding) {
*/
MyGame.Example.Stat.prototype.val = function() {
var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO;
return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
};
/**
@@ -331,7 +331,7 @@ MyGame.Example.Stat.addId = function(builder, idOffset) {
* @param {flatbuffers.Long} val
*/
MyGame.Example.Stat.addVal = function(builder, val) {
builder.addFieldInt64(1, val, flatbuffers.Long.ZERO);
builder.addFieldInt64(1, val, builder.createLong(0, 0));
};
/**
@@ -447,6 +447,14 @@ MyGame.Example.Monster.prototype.inventoryLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Uint8Array}
*/
MyGame.Example.Monster.prototype.inventoryArray = function() {
var offset = this.bb.__offset(this.bb_pos, 14);
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @returns {MyGame.Example.Color}
*/
@@ -555,6 +563,14 @@ MyGame.Example.Monster.prototype.testnestedflatbufferLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Uint8Array}
*/
MyGame.Example.Monster.prototype.testnestedflatbufferArray = function() {
var offset = this.bb.__offset(this.bb_pos, 30);
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @param {MyGame.Example.Stat=} obj
* @returns {MyGame.Example.Stat}
@@ -593,7 +609,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1 = function() {
*/
MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() {
var offset = this.bb.__offset(this.bb_pos, 40);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO;
return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
};
/**
@@ -601,7 +617,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1 = function() {
*/
MyGame.Example.Monster.prototype.testhashu64Fnv1 = function() {
var offset = this.bb.__offset(this.bb_pos, 42);
return offset ? this.bb.readUint64(this.bb_pos + offset) : flatbuffers.Long.ZERO;
return offset ? this.bb.readUint64(this.bb_pos + offset) : this.bb.createLong(0, 0);
};
/**
@@ -625,7 +641,7 @@ MyGame.Example.Monster.prototype.testhashu32Fnv1a = function() {
*/
MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() {
var offset = this.bb.__offset(this.bb_pos, 48);
return offset ? this.bb.readInt64(this.bb_pos + offset) : flatbuffers.Long.ZERO;
return offset ? this.bb.readInt64(this.bb_pos + offset) : this.bb.createLong(0, 0);
};
/**
@@ -633,7 +649,7 @@ MyGame.Example.Monster.prototype.testhashs64Fnv1a = function() {
*/
MyGame.Example.Monster.prototype.testhashu64Fnv1a = function() {
var offset = this.bb.__offset(this.bb_pos, 50);
return offset ? this.bb.readUint64(this.bb_pos + offset) : flatbuffers.Long.ZERO;
return offset ? this.bb.readUint64(this.bb_pos + offset) : this.bb.createLong(0, 0);
};
/**
@@ -653,6 +669,14 @@ MyGame.Example.Monster.prototype.testarrayofboolsLength = function() {
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Int8Array}
*/
MyGame.Example.Monster.prototype.testarrayofboolsArray = function() {
var offset = this.bb.__offset(this.bb_pos, 52);
return offset ? new Int8Array(this.bb.bytes().buffer, this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @param {flatbuffers.Builder} builder
*/
@@ -893,7 +917,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1 = function(builder, testhashu32Fnv1) {
* @param {flatbuffers.Long} testhashs64Fnv1
*/
MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) {
builder.addFieldInt64(18, testhashs64Fnv1, flatbuffers.Long.ZERO);
builder.addFieldInt64(18, testhashs64Fnv1, builder.createLong(0, 0));
};
/**
@@ -901,7 +925,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1 = function(builder, testhashs64Fnv1) {
* @param {flatbuffers.Long} testhashu64Fnv1
*/
MyGame.Example.Monster.addTesthashu64Fnv1 = function(builder, testhashu64Fnv1) {
builder.addFieldInt64(19, testhashu64Fnv1, flatbuffers.Long.ZERO);
builder.addFieldInt64(19, testhashu64Fnv1, builder.createLong(0, 0));
};
/**
@@ -925,7 +949,7 @@ MyGame.Example.Monster.addTesthashu32Fnv1a = function(builder, testhashu32Fnv1a)
* @param {flatbuffers.Long} testhashs64Fnv1a
*/
MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a) {
builder.addFieldInt64(22, testhashs64Fnv1a, flatbuffers.Long.ZERO);
builder.addFieldInt64(22, testhashs64Fnv1a, builder.createLong(0, 0));
};
/**
@@ -933,7 +957,7 @@ MyGame.Example.Monster.addTesthashs64Fnv1a = function(builder, testhashs64Fnv1a)
* @param {flatbuffers.Long} testhashu64Fnv1a
*/
MyGame.Example.Monster.addTesthashu64Fnv1a = function(builder, testhashu64Fnv1a) {
builder.addFieldInt64(23, testhashu64Fnv1a, flatbuffers.Long.ZERO);
builder.addFieldInt64(23, testhashu64Fnv1a, builder.createLong(0, 0));
};
/**

View File

@@ -836,10 +836,13 @@ void UnknownFieldsTest() {
TEST_EQ(parser.Parse("table T { str:string; i:int;}"
"root_type T;"
"{ str:\"test\","
"unknown_string:\"test\","
"\"unknown_string\":\"test\","
"unknown_int:10,"
"unknown_float:1.0,"
"unknown_array: [ 1, 2, 3, 4],"
"unknown_object: { i: 10 },"
"\"unknown_object\": { \"i\": 10 },"
"i:10}"), true);
std::string jsongen;