divine 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +2 -1
- data/Rakefile +49 -0
- data/lib/divine/code_generators/code_generator.rb +29 -6
- data/lib/divine/code_generators/java.rb +121 -14
- data/lib/divine/code_generators/javascript.rb +100 -11
- data/lib/divine/code_generators/ruby.rb +89 -4
- data/lib/divine/version.rb +1 -1
- data/test/basic_complex_test/basic_complex_test.rb +34 -0
- data/test/basic_complex_test/java_test/JavaTest.java +96 -0
- data/test/basic_complex_test/java_test/test_babel.java +368 -0
- data/test/basic_complex_test/js_test/js_testBasic.js +58 -0
- data/test/basic_complex_test/js_test/js_testComplex.js +68 -0
- data/test/basic_complex_test/js_test/test_babel.js +523 -0
- data/test/basic_complex_test/ruby_test/ruby_test.rb +68 -0
- data/test/basic_complex_test/ruby_test/test_babel.rb +368 -0
- data/test/binaryTree_test/binaryTree_test.rb +19 -0
- data/test/binaryTree_test/java_test/JavaTest.java +114 -0
- data/test/binaryTree_test/java_test/test_binaryTree.java +301 -0
- data/test/binaryTree_test/js_test/js_test.js +76 -0
- data/test/binaryTree_test/js_test/test_binaryTree.js +447 -0
- data/test/binaryTree_test/ruby_test/ruby_test.rb +68 -0
- data/test/binaryTree_test/ruby_test/test_binaryTree.rb +303 -0
- data/test/complex_test/complex_test.rb +23 -0
- data/test/complex_test/java_test/JavaTest.java +126 -0
- data/test/complex_test/java_test/test_complex.java +331 -0
- data/test/complex_test/js_test/js_test.js +69 -0
- data/test/complex_test/js_test/test_complex.js +478 -0
- data/test/complex_test/ruby_test/ruby_test.rb +55 -0
- data/test/complex_test/ruby_test/test_complex.rb +330 -0
- data/test/ipv6_test/ipv6_test.rb +14 -0
- data/test/ipv6_test/java_test/JavaTest.java +77 -0
- data/test/ipv6_test/java_test/junit.jar +0 -0
- data/test/ipv6_test/java_test/test_ipv6.java +270 -0
- data/test/ipv6_test/js_test/js_test.js +60 -0
- data/test/ipv6_test/js_test/test_ipv6.js +409 -0
- data/test/ipv6_test/ruby_test/ruby_test.rb +42 -0
- data/test/ipv6_test/ruby_test/test_ipv6.rb +267 -0
- data/test/java_lib/junit.jar +0 -0
- data/test/unify_test/java_test/test_unify.java +171 -0
- data/test/unify_test/js_test/js_test.js +56 -0
- data/test/unify_test/js_test/test_unify.js +326 -0
- data/test/unify_test/ruby_test/ruby_test.rb +35 -0
- data/test/unify_test/ruby_test/test_unify.rb +187 -0
- data/test/unify_test/unify_test.rb +17 -0
- metadata +77 -3
@@ -0,0 +1,447 @@
|
|
1
|
+
// ------------------------------------------------------------ BabelDataReader
|
2
|
+
function BabelDataReader(data) {
|
3
|
+
this.data = data;
|
4
|
+
this.index = 0;
|
5
|
+
}
|
6
|
+
|
7
|
+
BabelDataReader.prototype.getbyte = function () {
|
8
|
+
return this.data[this.index++];
|
9
|
+
};
|
10
|
+
|
11
|
+
BabelDataReader.prototype.read = function (items) {
|
12
|
+
var from = this.index;
|
13
|
+
this.index += items
|
14
|
+
return this.data.subarray(from, this.index)
|
15
|
+
};
|
16
|
+
|
17
|
+
|
18
|
+
// ------------------------------------------------------------ BabelDataWriter
|
19
|
+
function BabelDataWriter(data) {
|
20
|
+
this.data = data;
|
21
|
+
this.index = 0;
|
22
|
+
this.data = new Uint8Array(4096);
|
23
|
+
}
|
24
|
+
|
25
|
+
BabelDataWriter.prototype._realloc = function (size) {
|
26
|
+
size = size || 4096;
|
27
|
+
var old_data = this.data;
|
28
|
+
this.data = new Uint8Array(Math.max(size, 4096) + this.data.length);
|
29
|
+
this.data.set(old_data, 0);
|
30
|
+
};
|
31
|
+
|
32
|
+
BabelDataWriter.prototype.writeByte = function (a_byte) {
|
33
|
+
if (this.index + 1 >= this.data.length) this._realloc();
|
34
|
+
this.data[this.index++] = a_byte;
|
35
|
+
};
|
36
|
+
|
37
|
+
BabelDataWriter.prototype.write = function (bytes) {
|
38
|
+
if (this.index + bytes.length >= this.data.length) this._realloc(bytes.length);
|
39
|
+
this.data.set(bytes, this.index);
|
40
|
+
this.index += bytes.length;
|
41
|
+
};
|
42
|
+
|
43
|
+
BabelDataWriter.prototype.get_data = function () {
|
44
|
+
return this.data.subarray(0, this.index);
|
45
|
+
};
|
46
|
+
|
47
|
+
|
48
|
+
// ------------------------------------------------------------ BabelHelper
|
49
|
+
function BabelHelper() {}
|
50
|
+
|
51
|
+
BabelHelper.prototype.serialize = function () {
|
52
|
+
var out = new BabelDataWriter();
|
53
|
+
this.serialize_internal(out);
|
54
|
+
return out.get_data();
|
55
|
+
}
|
56
|
+
|
57
|
+
BabelHelper.prototype.read_int8 = function (data) {
|
58
|
+
return data.getbyte();
|
59
|
+
};
|
60
|
+
|
61
|
+
BabelHelper.prototype.read_int16 = function (data) {
|
62
|
+
return (data.getbyte() << 8) | this.read_int8(data);
|
63
|
+
};
|
64
|
+
|
65
|
+
BabelHelper.prototype.read_int24 = function (data) {
|
66
|
+
return (data.getbyte() << 16) | this.read_int16(data);
|
67
|
+
};
|
68
|
+
|
69
|
+
BabelHelper.prototype.read_int32 = function (data) {
|
70
|
+
// return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
|
71
|
+
return (data.getbyte() * (256*256*256)) + this.read_int24(data);
|
72
|
+
};
|
73
|
+
|
74
|
+
BabelHelper.prototype.read_binary = function (data) {
|
75
|
+
return data.read(this.read_int32(data));
|
76
|
+
};
|
77
|
+
|
78
|
+
BabelHelper.prototype.read_short_binary = function (data) {
|
79
|
+
return data.read(this.read_int8(data));
|
80
|
+
};
|
81
|
+
|
82
|
+
BabelHelper.prototype.read_ip_number = function (data) {
|
83
|
+
var ip_array = this.read_short_binary(data);
|
84
|
+
if(ip_array.length == 4){
|
85
|
+
return this.read_ipv4_number(ip_array);
|
86
|
+
}else{
|
87
|
+
return this.read_ipv6_number(ip_array);
|
88
|
+
}
|
89
|
+
};
|
90
|
+
|
91
|
+
BabelHelper.prototype.read_ipv4_number = function (ip_array) {
|
92
|
+
ip = "";
|
93
|
+
for (i = 0, len = ip_array.length; i < len; i++) {
|
94
|
+
b = ip_array[i];
|
95
|
+
if (ip.length > 0) {
|
96
|
+
ip += ".";
|
97
|
+
}
|
98
|
+
ip += b.toString();
|
99
|
+
}
|
100
|
+
return ip;
|
101
|
+
};
|
102
|
+
BabelHelper.prototype.read_ipv6_number = function (ip_array) {
|
103
|
+
var ip = "";
|
104
|
+
var part1, part2;
|
105
|
+
for (i = 0, len = ip_array.length; i < len; i+=2) {
|
106
|
+
part1 = ip_array[i];
|
107
|
+
part2 = ip_array[i+1];
|
108
|
+
ip += part1 == 0? "" : part1.toString(16);
|
109
|
+
ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + part2.toString(16): part2.toString(16));
|
110
|
+
if (i < ip_array.length-2)
|
111
|
+
ip += ":";
|
112
|
+
}
|
113
|
+
ip = ip.replace(/:{3,}/g, "::");
|
114
|
+
return ip;
|
115
|
+
};
|
116
|
+
|
117
|
+
BabelHelper.prototype.read_string = function (data) {
|
118
|
+
return this.decode_utf8(data.read(this.read_int16(data)))
|
119
|
+
};
|
120
|
+
|
121
|
+
BabelHelper.prototype.write_int8 = function (v, out) {
|
122
|
+
if (v > 0xFF) // Max 255
|
123
|
+
this.raise_error("Too large int8 number: " + v);
|
124
|
+
if(v < 0)
|
125
|
+
this.raise_error("a negative number passed to int8 number: " + v);
|
126
|
+
out.writeByte(v);
|
127
|
+
}
|
128
|
+
|
129
|
+
BabelHelper.prototype.write_int16 = function (v, out) {
|
130
|
+
if (v > 0xFFFF) // Max 65.535
|
131
|
+
this.raise_error("Too large int16 number: " + v);
|
132
|
+
if(v < 0)
|
133
|
+
this.raise_error("a negative number passed to int16 number: " + v);
|
134
|
+
this.write_int8(v >> 8 & 0xFF, out);
|
135
|
+
this.write_int8(v & 0xFF, out);
|
136
|
+
}
|
137
|
+
|
138
|
+
BabelHelper.prototype.write_int24 = function (v, out) {
|
139
|
+
if (v > 0xFFFFFF) // Max 16.777.215
|
140
|
+
this.raise_error("Too large int24 number: " + v);
|
141
|
+
if (v < 0) // In Case added to JavaScript declaration
|
142
|
+
this.raise_error("a negative number passed to int24 number: " + v);
|
143
|
+
this.write_int8(v >> 16 & 0xFF, out);
|
144
|
+
this.write_int16(v & 0xFFFF, out);
|
145
|
+
}
|
146
|
+
|
147
|
+
BabelHelper.prototype.write_int32 = function (v, out) {
|
148
|
+
if (v > 0xFFFFFFFF) // Max 4.294.967.295
|
149
|
+
this.raise_error("Too large int32 number: " + v);
|
150
|
+
if(v < 0)
|
151
|
+
this.raise_error("a negative number passed to int32 number: " + v);
|
152
|
+
this.write_int8(v >> 24 & 0xFF, out);
|
153
|
+
this.write_int24(v & 0xFFFFFF, out);
|
154
|
+
}
|
155
|
+
|
156
|
+
BabelHelper.prototype.write_bool = function (v, out) {
|
157
|
+
this.write_int8(v ? 1 : 0, out)
|
158
|
+
}
|
159
|
+
|
160
|
+
BabelHelper.prototype.write_string = function (v, out) {
|
161
|
+
var s = this.encode_utf8(v);
|
162
|
+
if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
|
163
|
+
this.write_int16(s.length, out);
|
164
|
+
out.write(s);
|
165
|
+
}
|
166
|
+
|
167
|
+
BabelHelper.prototype.write_binary = function (v, out) {
|
168
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
169
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
170
|
+
this.write_int32(v.length, out)
|
171
|
+
out.write(v);
|
172
|
+
} else if (v.constructor == String) {
|
173
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
174
|
+
this.write_int32(v.length, out)
|
175
|
+
out.write(v);
|
176
|
+
} else if (v == null) {
|
177
|
+
this.raise_error("Unsupported binary 'null'");
|
178
|
+
} else {
|
179
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
BabelHelper.prototype.write_16_binary = function (v, out) {
|
184
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
185
|
+
if (v.length > 0xFF) this.raise_error("Too large 16 binary: " + v.length*2 + " (" + v.constructor.name + ")");
|
186
|
+
this.write_int8(v.length * 2, out)
|
187
|
+
for (i = 0, len = v.length; i < len; i++) {
|
188
|
+
this.write_int16(v[i], out);
|
189
|
+
}
|
190
|
+
|
191
|
+
} else if (v == null) {
|
192
|
+
this.raise_error("Unsupported binary 'null'");
|
193
|
+
} else {
|
194
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
BabelHelper.prototype.write_short_binary = function (v, out) {
|
199
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
200
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
201
|
+
this.write_int8(v.length, out)
|
202
|
+
out.write(v);
|
203
|
+
} else if (v.constructor == String) {
|
204
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
205
|
+
this.write_int8(v.length, out)
|
206
|
+
out.write(v);
|
207
|
+
} else if (v == null) {
|
208
|
+
this.raise_error("Unsupported binary 'null'");
|
209
|
+
} else {
|
210
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
BabelHelper.prototype.write_ip_number = function (v, out) {
|
215
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)){
|
216
|
+
if(v.length == 4){
|
217
|
+
this.write_ipv4_number(v, out);
|
218
|
+
}else{
|
219
|
+
this.write_ipv6_number(v, out);
|
220
|
+
}
|
221
|
+
}else if(v.constructor == String){
|
222
|
+
if(/:/g.test(v)){
|
223
|
+
this.write_ipv6_number(v, out);
|
224
|
+
}else{
|
225
|
+
this.write_ipv4_number(v, out);
|
226
|
+
}
|
227
|
+
}else{
|
228
|
+
this.raise_error("Unknown IP number '" + v + "'");
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
BabelHelper.prototype.write_ipv4_number = function (v, out) {
|
233
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
234
|
+
if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
|
235
|
+
this.write_short_binary(v, out)
|
236
|
+
} else if (v.constructor == String) {
|
237
|
+
var ss = [];
|
238
|
+
if (v.length > 0) {
|
239
|
+
ss = v.split(".").map(Number);
|
240
|
+
}
|
241
|
+
this.write_ipv4_number(ss, out);
|
242
|
+
} else {
|
243
|
+
this.raise_error("Unknown IP number '" + v + "'");
|
244
|
+
}
|
245
|
+
};
|
246
|
+
|
247
|
+
BabelHelper.prototype.write_ipv6_number = function (v, out) {
|
248
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
249
|
+
this.write_16_binary(v, out)
|
250
|
+
} else if (v.constructor == String) {
|
251
|
+
var ss = [];
|
252
|
+
var contains_ipv6_letters = /[0-9a-f]+/gi.test(v);
|
253
|
+
var contains_other_letters = /[^:0-9a-f]+/gi.test(v);
|
254
|
+
if (v.length > 0 && v.split(/:{3,}/g).length == 1 && v.split(/:{2}/g).length <= 2 && !contains_other_letters &&
|
255
|
+
contains_ipv6_letters) {
|
256
|
+
v = v.replace(/ /g, "");
|
257
|
+
ss = v.split(":").map(function (t){
|
258
|
+
if (t.length > 4)
|
259
|
+
new BabelHelper().raise_error("Unknown IP Group number '" + t + "'");
|
260
|
+
if (t.length == 0)
|
261
|
+
t = "0";
|
262
|
+
return parseInt(t, 16);
|
263
|
+
});
|
264
|
+
}
|
265
|
+
if (!contains_other_letters &&
|
266
|
+
( !/::/g.test(v) && ss.length == 0 || ss.length == 8) || ( /::/g.test(v) && ss.length > 2 && ss.length < 8) ) {
|
267
|
+
this.write_ipv6_number(ss, out);
|
268
|
+
} else {
|
269
|
+
this.raise_error("Unknown IP v6 number '" + v + "'");
|
270
|
+
}
|
271
|
+
|
272
|
+
} else {
|
273
|
+
this.raise_error("Unknown IP v6 number '" + v + "'");
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
BabelHelper.prototype.encode_utf8 = function (str) {
|
278
|
+
var utf8 = [];
|
279
|
+
var chr, next_chr;
|
280
|
+
var x, y, z;
|
281
|
+
for (var i = 0; i < str.length; i++) {
|
282
|
+
chr = str.charCodeAt(i);
|
283
|
+
if ((chr & 0xFF80) == 0) {
|
284
|
+
utf8.push(chr);
|
285
|
+
} else {
|
286
|
+
if ((chr & 0xFC00) == 0xD800) {
|
287
|
+
next_chr = str.charCodeAt(i + 1);
|
288
|
+
if ((next_chr & 0xFC00) == 0xDC00) {
|
289
|
+
// UTF-16 surrogate pair
|
290
|
+
chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
|
291
|
+
i++;
|
292
|
+
} else {
|
293
|
+
this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
|
294
|
+
}
|
295
|
+
}
|
296
|
+
x = chr & 0xFF;
|
297
|
+
y = chr & 0xFF00;
|
298
|
+
z = chr & 0xFF0000;
|
299
|
+
|
300
|
+
if (chr <= 0x0007FF) {
|
301
|
+
utf8.push(0xC0 | (y >> 6) | (x >> 6));
|
302
|
+
utf8.push(0x80 | (x & 63));
|
303
|
+
} else if (chr <= 0x00FFFF) {
|
304
|
+
utf8.push(0xe0 | (y >> 12));
|
305
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
306
|
+
utf8.push(0x80 | (x & 63));
|
307
|
+
} else if (chr <= 0x10FFFF) {
|
308
|
+
utf8.push(0xF0 | (z >> 18));
|
309
|
+
utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
|
310
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
311
|
+
utf8.push(0x80 | (x & 63));
|
312
|
+
} else {
|
313
|
+
this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
return utf8;
|
318
|
+
}
|
319
|
+
|
320
|
+
BabelHelper.prototype.decode_utf8 = function (utf8_data) {
|
321
|
+
var str = "";
|
322
|
+
var chr, b2, b3, b4;
|
323
|
+
for (var i = 0; i < utf8_data.length; i++) {
|
324
|
+
chr = utf8_data[i];
|
325
|
+
if ((chr & 0x80) == 0x00) {}
|
326
|
+
else if ((chr & 0xF8) == 0xF0) {
|
327
|
+
// 4 bytes: U+10000 - U+10FFFF
|
328
|
+
b2 = utf8_data[i + 1];
|
329
|
+
b3 = utf8_data[i + 2];
|
330
|
+
b4 = utf8_data[i + 3];
|
331
|
+
if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
|
332
|
+
chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
|
333
|
+
i += 3;
|
334
|
+
} else {
|
335
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
|
336
|
+
}
|
337
|
+
} else if ((chr & 0xF0) == 0xE0) {
|
338
|
+
// 3 bytes: U+0800 - U+FFFF
|
339
|
+
b2 = utf8_data[i + 1];
|
340
|
+
b3 = utf8_data[i + 2];
|
341
|
+
if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
|
342
|
+
chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
|
343
|
+
i += 2;
|
344
|
+
} else {
|
345
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
|
346
|
+
}
|
347
|
+
} else if ((chr & 0xE0) == 0xC0) {
|
348
|
+
// 2 bytes: U+0080 - U+07FF
|
349
|
+
b2 = utf8_data[i + 1];
|
350
|
+
if ((b2 & 0xC0) == 0x80) {
|
351
|
+
chr = (chr & 31) << 6 | (b2 & 63);
|
352
|
+
i += 1;
|
353
|
+
} else {
|
354
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
|
355
|
+
}
|
356
|
+
} else {
|
357
|
+
// 80-BF: Second, third, or fourth byte of a multi-byte sequence
|
358
|
+
// F5-FF: Start of 4, 5, or 6 byte sequence
|
359
|
+
this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
|
360
|
+
}
|
361
|
+
if (chr <= 0xFFFF) {
|
362
|
+
str += String.fromCharCode(chr);
|
363
|
+
} else if (chr > 0xFFFF && chr <= 0x10FFFF) {
|
364
|
+
// Must be encoded into UTF-16 surrogate pair.
|
365
|
+
chr -= 0x10000;
|
366
|
+
str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
|
367
|
+
} else {
|
368
|
+
this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
|
369
|
+
}
|
370
|
+
}
|
371
|
+
return str;
|
372
|
+
}
|
373
|
+
|
374
|
+
BabelHelper.prototype.raise_error = function (msg) {
|
375
|
+
throw "[" + this.constructor.name + "] " + msg;
|
376
|
+
}
|
377
|
+
|
378
|
+
|
379
|
+
// ------------------------------------------------------------ BinaryTree
|
380
|
+
function BinaryTree() {
|
381
|
+
BabelHelper.call(this);
|
382
|
+
this.root_node = [];
|
383
|
+
}
|
384
|
+
|
385
|
+
// Inherit BabelHelper
|
386
|
+
BinaryTree.prototype = new BabelHelper();
|
387
|
+
|
388
|
+
// Correct the constructor pointer because it points to BabelHelper
|
389
|
+
BinaryTree.prototype.constructor = BinaryTree;
|
390
|
+
|
391
|
+
// Define the methods of BinaryTree
|
392
|
+
BinaryTree.prototype.deserialize = function (data) {
|
393
|
+
// Deserialize list 'root_node'
|
394
|
+
this.root_node = [];
|
395
|
+
var var_100 = this.read_int32(data);
|
396
|
+
for(var var_102=0; var_102<var_100; var_102++) {
|
397
|
+
var var_101 = new Node();
|
398
|
+
var_101.deserialize(data);
|
399
|
+
this.root_node.push(var_101);
|
400
|
+
}
|
401
|
+
}
|
402
|
+
|
403
|
+
BinaryTree.prototype.serialize_internal = function(out) {
|
404
|
+
// Serialize list 'root_node'
|
405
|
+
this.write_int32(this.root_node.length, out);
|
406
|
+
for(var var_104=0; var_104<this.root_node.length; var_104++) {
|
407
|
+
var var_103 = this.root_node[var_104];
|
408
|
+
var_103.serialize_internal(out)
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
|
413
|
+
// ------------------------------------------------------------ Node
|
414
|
+
function Node() {
|
415
|
+
BabelHelper.call(this);
|
416
|
+
this.i32 = 0;
|
417
|
+
this.next_node = [];
|
418
|
+
}
|
419
|
+
|
420
|
+
// Inherit BabelHelper
|
421
|
+
Node.prototype = new BabelHelper();
|
422
|
+
|
423
|
+
// Correct the constructor pointer because it points to BabelHelper
|
424
|
+
Node.prototype.constructor = Node;
|
425
|
+
|
426
|
+
// Define the methods of Node
|
427
|
+
Node.prototype.deserialize = function (data) {
|
428
|
+
this.i32 = this.read_int32(data);
|
429
|
+
// Deserialize list 'next_node'
|
430
|
+
this.next_node = [];
|
431
|
+
var var_105 = this.read_int32(data);
|
432
|
+
for(var var_107=0; var_107<var_105; var_107++) {
|
433
|
+
var var_106 = new Node();
|
434
|
+
var_106.deserialize(data);
|
435
|
+
this.next_node.push(var_106);
|
436
|
+
}
|
437
|
+
}
|
438
|
+
|
439
|
+
Node.prototype.serialize_internal = function(out) {
|
440
|
+
this.write_int32(this.i32, out);
|
441
|
+
// Serialize list 'next_node'
|
442
|
+
this.write_int32(this.next_node.length, out);
|
443
|
+
for(var var_109=0; var_109<this.next_node.length; var_109++) {
|
444
|
+
var var_108 = this.next_node[var_109];
|
445
|
+
var_108.serialize_internal(out)
|
446
|
+
}
|
447
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative 'test_binaryTree.rb'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestBabelTestBasic < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_BinaryTree
|
7
|
+
puts "Test Binary Tree"
|
8
|
+
binaryTree_ser = buildTree
|
9
|
+
|
10
|
+
ser = binaryTree_ser.serialize
|
11
|
+
serialize(ser)
|
12
|
+
res = deserialize()
|
13
|
+
|
14
|
+
binaryTree_deser = BabelTest::BinaryTree.new
|
15
|
+
binaryTree_deser.deserialize res
|
16
|
+
compareBinaryTree(binaryTree_ser, binaryTree_deser)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def buildTree()
|
21
|
+
root = BabelTest::Node.new
|
22
|
+
root.i32 = 0
|
23
|
+
|
24
|
+
n1_L = BabelTest::Node.new
|
25
|
+
n1_L.i32 = 1
|
26
|
+
|
27
|
+
n1_R = BabelTest::Node.new
|
28
|
+
n1_R.i32 = 2
|
29
|
+
|
30
|
+
n2_L_L = BabelTest::Node.new
|
31
|
+
n2_L_L.i32 = 3
|
32
|
+
|
33
|
+
n2_L_R = BabelTest::Node.new
|
34
|
+
n2_L_R.i32 = 4
|
35
|
+
|
36
|
+
n2_R_L = BabelTest::Node.new
|
37
|
+
n2_R_L.i32 = 5
|
38
|
+
|
39
|
+
n2_R_R = BabelTest::Node.new
|
40
|
+
n2_R_R.i32 = 6
|
41
|
+
|
42
|
+
root.next_node = [n1_L, n1_R]
|
43
|
+
n1_L.next_node = [n2_L_L, n2_L_R]
|
44
|
+
n1_R.next_node = [n2_R_L, n2_R_R]
|
45
|
+
|
46
|
+
bt = BabelTest::BinaryTree.new
|
47
|
+
bt.root_node = [root]
|
48
|
+
bt
|
49
|
+
end
|
50
|
+
|
51
|
+
def compareBinaryTree(bt1, bt2)
|
52
|
+
assert bt1.root_node.length == bt2.root_node.length
|
53
|
+
assert bt1.root_node[0].i32 == bt2.root_node[0].i32
|
54
|
+
assert bt1.root_node[0].next_node.length == bt2.root_node[0].next_node.length
|
55
|
+
assert bt1.root_node[0].next_node[0].next_node[0].i32 == bt2.root_node[0].next_node[0].next_node[0].i32
|
56
|
+
end
|
57
|
+
|
58
|
+
def serialize(data)
|
59
|
+
File.open("test/binaryTree_test/ruby_test/bin.babel.rb", "w+b") do |f|
|
60
|
+
f.write(data)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def deserialize()
|
65
|
+
mem_buf = File.new('test/binaryTree_test/ruby_test/bin.babel.rb').binmode
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|