1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
import assert from 'assert'
import * as flatbuffers from 'flatbuffers'
import { Character } from './union_vector/character'
import { BookReader, BookReaderT } from './union_vector/book-reader'
import { Attacker, AttackerT } from './union_vector/attacker'
import { Movie, MovieT } from './union_vector/movie'
var charTypes = [
Character.Belle,
Character.MuLan,
Character.BookFan,
Character.Other
];
function testMovieBuf(movie) {
assert.strictEqual(movie.charactersTypeLength(), charTypes.length);
assert.strictEqual(movie.charactersLength(), movie.charactersTypeLength());
for (var i = 0; i < charTypes.length; ++i) {
assert.strictEqual(movie.charactersType(i), charTypes[i]);
}
var bookReader7 = movie.characters(0, new BookReader());
assert.strictEqual(bookReader7.booksRead(), 7);
var attacker = movie.characters(1, new Attacker());
assert.strictEqual(attacker.swordAttackDamage(), 5);
var bookReader2 = movie.characters(2, new BookReader());
assert.strictEqual(bookReader2.booksRead(), 2);
var other = movie.characters(3, '');
assert.strictEqual(other, "I am other");
}
function testMovieUnpack(movie) {
assert.strictEqual(movie.charactersType.length, charTypes.length);
assert.strictEqual(movie.characters.length, movie.charactersType.length);
for (var i = 0; i < charTypes.length; ++i) {
assert.strictEqual(movie.charactersType[i], charTypes[i]);
}
var bookReader7 = movie.characters[0];
assert.strictEqual(bookReader7 instanceof BookReaderT, true);
assert.strictEqual(bookReader7.booksRead, 7);
var attacker = movie.characters[1];
assert.strictEqual(attacker instanceof AttackerT, true);
assert.strictEqual(attacker.swordAttackDamage, 5);
var bookReader2 = movie.characters[2];
assert.strictEqual(bookReader2 instanceof BookReaderT, true);
assert.strictEqual(bookReader2.booksRead, 2);
var other = movie.characters[3];
assert.strictEqual(other, "I am other");
}
function createMovie(fbb) {
Attacker.startAttacker(fbb);
Attacker.addSwordAttackDamage(fbb, 5);
var attackerOffset = Attacker.endAttacker(fbb);
var charTypesOffset = Movie.createCharactersTypeVector(fbb, charTypes);
var charsOffset = 0;
let otherOffset = fbb.createString("I am other");
charsOffset = Movie.createCharactersVector(
fbb,
[
BookReader.createBookReader(fbb, 7),
attackerOffset,
BookReader.createBookReader(fbb, 2),
otherOffset
]
);
Movie.startMovie(fbb);
Movie.addCharactersType(fbb, charTypesOffset);
Movie.addCharacters(fbb, charsOffset);
Movie.finishMovieBuffer(fbb, Movie.endMovie(fbb))
}
function main() {
var fbb = new flatbuffers.Builder();
createMovie(fbb);
var buf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
var movie = Movie.getRootAsMovie(buf);
testMovieBuf(movie);
testMovieUnpack(movie.unpack());
var movie_to = new MovieT();
movie.unpackTo(movie_to);
testMovieUnpack(movie_to);
fbb.clear();
Movie.finishMovieBuffer(fbb, movie_to.pack(fbb));
var unpackBuf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
testMovieBuf(Movie.getRootAsMovie(unpackBuf));
console.log('FlatBuffers union vector test: completed successfully');
}
main();
|