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,114 @@
|
|
1
|
+
|
2
|
+
import java.io.ByteArrayInputStream;
|
3
|
+
import java.io.File;
|
4
|
+
import java.io.FileInputStream;
|
5
|
+
import java.io.FileOutputStream;
|
6
|
+
import java.io.IOException;
|
7
|
+
import java.util.ArrayList;
|
8
|
+
import java.util.Collections;
|
9
|
+
import java.util.HashMap;
|
10
|
+
import org.junit.*;
|
11
|
+
|
12
|
+
public class JavaTest {
|
13
|
+
|
14
|
+
@Test
|
15
|
+
public void testBinaryTree() throws IOException {
|
16
|
+
System.out.println("Test Binary Tree");
|
17
|
+
BinaryTree binartTree_ser = buildObj();
|
18
|
+
|
19
|
+
serialize(binartTree_ser);
|
20
|
+
byte[] read = deserialize();
|
21
|
+
|
22
|
+
BinaryTree binartTree_deser = new BinaryTree();
|
23
|
+
binartTree_deser.deserialize(new ByteArrayInputStream(read));
|
24
|
+
|
25
|
+
compareBinaryTree(binartTree_ser, binartTree_deser);
|
26
|
+
}
|
27
|
+
|
28
|
+
public BinaryTree buildObj() {
|
29
|
+
final Node root = new Node();
|
30
|
+
root.i32 = 0;
|
31
|
+
|
32
|
+
final Node n1_L = new Node();
|
33
|
+
n1_L.i32 = 1;
|
34
|
+
|
35
|
+
final Node n1_R = new Node();
|
36
|
+
n1_R.i32 = 2;
|
37
|
+
|
38
|
+
final Node n2_L_L = new Node();
|
39
|
+
n2_L_L.i32 = 3;
|
40
|
+
|
41
|
+
final Node n2_L_R = new Node();
|
42
|
+
n2_L_R.i32 = 4;
|
43
|
+
|
44
|
+
final Node n2_R_L = new Node();
|
45
|
+
n2_R_L.i32 = 5;
|
46
|
+
|
47
|
+
final Node n2_R_R = new Node();
|
48
|
+
n2_R_R.i32 = 6;
|
49
|
+
|
50
|
+
root.next_node = new ArrayList<Node>() {
|
51
|
+
{
|
52
|
+
add(n1_L);
|
53
|
+
add(n1_R);
|
54
|
+
}
|
55
|
+
};
|
56
|
+
|
57
|
+
n1_L.next_node = new ArrayList<Node>() {
|
58
|
+
{
|
59
|
+
add(n2_L_L);
|
60
|
+
add(n2_L_R);
|
61
|
+
}
|
62
|
+
};
|
63
|
+
|
64
|
+
n1_R.next_node = new ArrayList<Node>() {
|
65
|
+
{
|
66
|
+
add(n2_R_L);
|
67
|
+
add(n2_R_R);
|
68
|
+
}
|
69
|
+
};
|
70
|
+
|
71
|
+
BinaryTree bt = new BinaryTree();
|
72
|
+
bt.root_node = new ArrayList<Node>() {
|
73
|
+
{
|
74
|
+
add(root);
|
75
|
+
}
|
76
|
+
};
|
77
|
+
|
78
|
+
return bt;
|
79
|
+
}
|
80
|
+
|
81
|
+
public void compareBinaryTree(BinaryTree bt1, BinaryTree bt2) {
|
82
|
+
org.junit.Assert.assertEquals(bt1.root_node.size(), bt2.root_node
|
83
|
+
.size());
|
84
|
+
org.junit.Assert.assertEquals(bt1.root_node.get(0).i32, bt2.root_node
|
85
|
+
.get(0).i32);
|
86
|
+
org.junit.Assert.assertEquals(bt1.root_node.get(0).next_node.size(),
|
87
|
+
bt2.root_node.get(0).next_node.size());
|
88
|
+
org.junit.Assert.assertEquals(
|
89
|
+
bt1.root_node.get(0).next_node.get(0).next_node.get(0).i32,
|
90
|
+
bt2.root_node.get(0).next_node.get(0).next_node.get(0).i32);
|
91
|
+
}
|
92
|
+
|
93
|
+
public void serialize(BabelBase obj) throws IOException {
|
94
|
+
byte[] data = obj.serialize();
|
95
|
+
File file = new File("test/binaryTree_test/java_test/bin.babel");
|
96
|
+
try {
|
97
|
+
new FileOutputStream(file).write(data);
|
98
|
+
} catch (Exception e) {
|
99
|
+
e.printStackTrace();
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
public byte[] deserialize() throws IOException{
|
104
|
+
File file = new File("test/binaryTree_test/java_test/bin.babel");
|
105
|
+
byte[] data = new byte[(int) file.length()];
|
106
|
+
try {
|
107
|
+
new FileInputStream(file).read(data);
|
108
|
+
} catch (Exception e) {
|
109
|
+
e.printStackTrace();
|
110
|
+
}
|
111
|
+
return data;
|
112
|
+
}
|
113
|
+
|
114
|
+
}
|
@@ -0,0 +1,301 @@
|
|
1
|
+
import java.io.ByteArrayInputStream;
|
2
|
+
import java.io.ByteArrayOutputStream;
|
3
|
+
import java.io.IOException;
|
4
|
+
import java.util.ArrayList;
|
5
|
+
import java.util.HashMap;
|
6
|
+
import java.util.regex.Pattern;
|
7
|
+
import java.nio.charset.Charset;
|
8
|
+
|
9
|
+
abstract class BabelBase {
|
10
|
+
private static final Charset UTF8 = Charset.forName("UTF-8");
|
11
|
+
|
12
|
+
public byte[] serialize() throws IOException {
|
13
|
+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
14
|
+
serializeInternal(baos);
|
15
|
+
baos.close();
|
16
|
+
return baos.toByteArray();
|
17
|
+
}
|
18
|
+
|
19
|
+
abstract void serializeInternal(ByteArrayOutputStream baos) throws IOException;
|
20
|
+
|
21
|
+
abstract void deserialize(ByteArrayInputStream baos) throws IOException;
|
22
|
+
|
23
|
+
protected int readInt8(ByteArrayInputStream data) {
|
24
|
+
return data.read() & 0xff;
|
25
|
+
}
|
26
|
+
|
27
|
+
protected int readInt16(ByteArrayInputStream data) {
|
28
|
+
return (data.read() << 8) | readInt8(data);
|
29
|
+
}
|
30
|
+
|
31
|
+
protected int readInt24(ByteArrayInputStream data) {
|
32
|
+
return (data.read() << 16) | readInt16(data);
|
33
|
+
}
|
34
|
+
|
35
|
+
protected long readInt32(ByteArrayInputStream data) {
|
36
|
+
return (data.read() << 24) | readInt24(data);
|
37
|
+
}
|
38
|
+
|
39
|
+
protected boolean readBool(ByteArrayInputStream data) {
|
40
|
+
return readInt8(data) == 1;
|
41
|
+
}
|
42
|
+
|
43
|
+
protected String readString(ByteArrayInputStream data) throws IOException {
|
44
|
+
// Force utf8
|
45
|
+
return new String(readBytes(readInt16(data), data), UTF8);
|
46
|
+
}
|
47
|
+
|
48
|
+
private byte[] readBytes(int size, ByteArrayInputStream data) throws IOException {
|
49
|
+
byte[] bs = new byte[size];
|
50
|
+
data.read(bs);
|
51
|
+
return bs;
|
52
|
+
}
|
53
|
+
|
54
|
+
protected byte[] readBinary(ByteArrayInputStream data) throws IOException {
|
55
|
+
long c = readInt32(data);
|
56
|
+
if (c > Integer.MAX_VALUE) {
|
57
|
+
throw new IndexOutOfBoundsException("Binary data to big for java");
|
58
|
+
}
|
59
|
+
return readBytes((int) c, data);
|
60
|
+
}
|
61
|
+
|
62
|
+
protected byte[] readShortBinary(ByteArrayInputStream data) throws IOException {
|
63
|
+
return readBytes(readInt8(data), data);
|
64
|
+
}
|
65
|
+
|
66
|
+
protected String readIpNumber(ByteArrayInputStream data) throws IOException {
|
67
|
+
byte[] ips = readShortBinary(data);
|
68
|
+
if(ips.length == 4){
|
69
|
+
return readIpv4Number(ips);
|
70
|
+
} else{
|
71
|
+
return readIpv6Number(ips);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
protected String readIpv4Number(byte[] ips){
|
76
|
+
String ip = "";
|
77
|
+
for (byte b : ips) {
|
78
|
+
if (ip.length() > 0) {
|
79
|
+
ip += ".";
|
80
|
+
}
|
81
|
+
ip += (b & 0xFF);
|
82
|
+
}
|
83
|
+
return ip;
|
84
|
+
}
|
85
|
+
|
86
|
+
protected String readIpv6Number(byte[] ips) throws IOException {
|
87
|
+
String ip = "";
|
88
|
+
int part1, part2;
|
89
|
+
for (int i = 0; i < ips.length; i+=2) {
|
90
|
+
part1 = ips[i] & 0xFF;
|
91
|
+
part2 = ips[i+1] & 0xFF;
|
92
|
+
ip += part1 == 0? "" : Integer.toHexString(part1);
|
93
|
+
ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + Integer.toHexString(part2): Integer.toHexString(part2));
|
94
|
+
if (i < ips.length-2) {
|
95
|
+
ip += ":";
|
96
|
+
}
|
97
|
+
}
|
98
|
+
ip = ip.replaceAll(":{3,}", "::");
|
99
|
+
return ip;
|
100
|
+
}
|
101
|
+
|
102
|
+
protected void writeInt8(int v, ByteArrayOutputStream out) {
|
103
|
+
if (v > 0xFF) { // Max 255
|
104
|
+
raiseError("Too large int8 number: " + v);
|
105
|
+
}else if(v < 0){
|
106
|
+
raiseError("a negative number passed to int8 number: " + v);
|
107
|
+
}
|
108
|
+
out.write(v);
|
109
|
+
}
|
110
|
+
|
111
|
+
protected void writeInt16(int v, ByteArrayOutputStream out) {
|
112
|
+
if (v > 0xFFFF) { // Max 65.535
|
113
|
+
raiseError("Too large int16 number: " + v);
|
114
|
+
}else if(v < 0){
|
115
|
+
raiseError("a negative number passed to int16 number: " + v);
|
116
|
+
}
|
117
|
+
writeInt8(v >> 8 & 0xFF, out);
|
118
|
+
writeInt8(v & 0xFF, out);
|
119
|
+
}
|
120
|
+
|
121
|
+
protected void writeInt24(int v, ByteArrayOutputStream out) {
|
122
|
+
if (v > 0xFFFFFF) { // Max 16.777.215
|
123
|
+
raiseError("Too large int24 number: " + v);
|
124
|
+
}else if(v < 0){ // In Case added to Java declaration
|
125
|
+
raiseError("a negative number passed to int24 number: " + v);
|
126
|
+
}
|
127
|
+
writeInt8(v >> 16 & 0xFF, out);
|
128
|
+
writeInt16(v & 0xFFFF, out);
|
129
|
+
}
|
130
|
+
|
131
|
+
protected void writeInt32(long v, ByteArrayOutputStream out) {
|
132
|
+
if (v > 0xFFFFFFFFL) { // Max 4.294.967.295
|
133
|
+
raiseError("Too large int32 number: " + v);
|
134
|
+
}else if(v < 0){
|
135
|
+
raiseError("a negative number passed to int32 number: " + v);
|
136
|
+
}
|
137
|
+
writeInt8((int) ((v >> 24) & 0xFF), out);
|
138
|
+
writeInt24((int) (v & 0xFFFFFF), out);
|
139
|
+
}
|
140
|
+
|
141
|
+
protected void writeBool(boolean v, ByteArrayOutputStream out) {
|
142
|
+
writeInt8(v ? 1 : 0, out);
|
143
|
+
}
|
144
|
+
|
145
|
+
protected void writeString(String v, ByteArrayOutputStream out) throws IOException {
|
146
|
+
byte[] bs = v.getBytes(UTF8);
|
147
|
+
if (bs.length > 0xFFFF) {
|
148
|
+
raiseError("Too large string: " + bs.length + " bytes");
|
149
|
+
}
|
150
|
+
writeInt16(bs.length, out);
|
151
|
+
out.write(bs);
|
152
|
+
}
|
153
|
+
|
154
|
+
protected void writeBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
|
155
|
+
if (v.length > 0xFFFFFFFFL) {
|
156
|
+
raiseError("Too large binary: " + v.length + " bytes");
|
157
|
+
}
|
158
|
+
writeInt32(v.length, out);
|
159
|
+
out.write(v);
|
160
|
+
}
|
161
|
+
|
162
|
+
|
163
|
+
protected void write16Binary(int[] v, ByteArrayOutputStream out) throws IOException {
|
164
|
+
if (v.length > 0xFF) {
|
165
|
+
raiseError("Too large 16_binary: " + (v.length*2) + " bytes");
|
166
|
+
}
|
167
|
+
writeInt8(v.length*2, out);
|
168
|
+
for(int i = 0; i < v.length; i++){
|
169
|
+
this.writeInt16(v[i], out);
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
protected void writeShortBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
|
174
|
+
if (v.length > 0xFF) {
|
175
|
+
raiseError("Too large short_binary: " + v.length + " bytes");
|
176
|
+
}
|
177
|
+
writeInt8(v.length, out);
|
178
|
+
out.write(v);
|
179
|
+
}
|
180
|
+
|
181
|
+
protected void writeIpNumber(String v, ByteArrayOutputStream out) throws IOException {
|
182
|
+
if(v.contains(":")){
|
183
|
+
writeIpv6Number( v, out);
|
184
|
+
}else{
|
185
|
+
writeIpv4Number( v, out);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
protected void writeIpv4Number(String v, ByteArrayOutputStream out) throws IOException {
|
190
|
+
byte[] ss = new byte[0];
|
191
|
+
if(!v.isEmpty()){
|
192
|
+
String[] bs = v.split("\\.");
|
193
|
+
ss = new byte[bs.length];
|
194
|
+
for (int i = 0; i < bs.length; i++) {
|
195
|
+
ss[i] = (byte) (Integer.parseInt(bs[i]) & 0xFF);
|
196
|
+
}
|
197
|
+
}
|
198
|
+
if (ss.length == 0 || ss.length == 4) {
|
199
|
+
writeShortBinary(ss, out);
|
200
|
+
} else {
|
201
|
+
raiseError("Unknown IP v4 number " + v); // Only IPv4 for now
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
protected void writeIpv6Number(String v, ByteArrayOutputStream out)
|
206
|
+
throws IOException {
|
207
|
+
v = v.replaceAll(" ", "") + " "; // Temporary: To avoid the split problem when we have : at the
|
208
|
+
// end of "v"
|
209
|
+
int[] ss = new int[0];
|
210
|
+
boolean contains_ipv6_letters = Pattern.compile("[0-9a-f]+").matcher(
|
211
|
+
v.trim().toLowerCase()).find();
|
212
|
+
boolean contains_other_letters = Pattern.compile("[^:0-9a-f]+")
|
213
|
+
.matcher(v.trim().toLowerCase()).find();
|
214
|
+
// make sure of v must have only one "::" and no more than two of ":".
|
215
|
+
// e.g. 1::1::1 & 1:::1:205
|
216
|
+
if (!v.trim().isEmpty() && v.split(":{3,}").length == 1
|
217
|
+
&& v.split(":{2}").length <= 2 && !contains_other_letters
|
218
|
+
&& contains_ipv6_letters) {
|
219
|
+
String[] bs = v.split(":");
|
220
|
+
ss = new int[bs.length];
|
221
|
+
for (int i = 0; i < bs.length; i++) {
|
222
|
+
String s = bs[i].trim();
|
223
|
+
if (s.length() <= 4) // to avoid such number 0125f
|
224
|
+
ss[i] = Integer.parseInt(
|
225
|
+
(s.isEmpty() ? "0" : bs[i].trim()), 16);
|
226
|
+
else
|
227
|
+
raiseError("Unknown IPv6 Group " + i + " which is " + s);
|
228
|
+
}
|
229
|
+
}
|
230
|
+
// Check for make sure of the size of the IP groups in case "::" is used
|
231
|
+
// [> 2 & < 8]or not [must == 8]
|
232
|
+
if (!contains_other_letters
|
233
|
+
&& (!v.contains("::") && ss.length == 0 || ss.length == 8)
|
234
|
+
|| (v.contains("::") && ss.length > 2 && ss.length < 8)) {
|
235
|
+
write16Binary(ss, out);
|
236
|
+
} else {
|
237
|
+
raiseError("Unknown IP v6 number " + v);
|
238
|
+
}
|
239
|
+
}
|
240
|
+
|
241
|
+
protected void raiseError(String msg) {
|
242
|
+
throw new IllegalArgumentException("[" + this.getClass().getCanonicalName() + "] " + msg);
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
|
247
|
+
class BinaryTree extends BabelBase {
|
248
|
+
public ArrayList<Node> root_node = new ArrayList<Node>();
|
249
|
+
|
250
|
+
@Override
|
251
|
+
void serializeInternal(ByteArrayOutputStream baos) throws IOException {
|
252
|
+
// Serialize list 'root_node'
|
253
|
+
writeInt32(root_node.size(), baos);
|
254
|
+
for(int var_101=0; var_101<root_node.size(); var_101++) {
|
255
|
+
Node var_100 = root_node.get(var_101);
|
256
|
+
var_100.serializeInternal(baos);
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
@Override
|
261
|
+
public void deserialize(ByteArrayInputStream bais) throws IOException {
|
262
|
+
// Deserialize list 'root_node'
|
263
|
+
this.root_node = new ArrayList<Node>();
|
264
|
+
int var_102 = (int)this.readInt32(bais);
|
265
|
+
for(int var_104=0; var_104<var_102; var_104++) {
|
266
|
+
Node var_103 = new Node();
|
267
|
+
var_103.deserialize(bais);
|
268
|
+
this.root_node.add(var_103);
|
269
|
+
}
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
class Node extends BabelBase {
|
275
|
+
public long i32 = 0L;
|
276
|
+
public ArrayList<Node> next_node = new ArrayList<Node>();
|
277
|
+
|
278
|
+
@Override
|
279
|
+
void serializeInternal(ByteArrayOutputStream baos) throws IOException {
|
280
|
+
writeInt32(this.i32, baos);
|
281
|
+
// Serialize list 'next_node'
|
282
|
+
writeInt32(next_node.size(), baos);
|
283
|
+
for(int var_106=0; var_106<next_node.size(); var_106++) {
|
284
|
+
Node var_105 = next_node.get(var_106);
|
285
|
+
var_105.serializeInternal(baos);
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
@Override
|
290
|
+
public void deserialize(ByteArrayInputStream bais) throws IOException {
|
291
|
+
this.i32 = readInt32(bais);
|
292
|
+
// Deserialize list 'next_node'
|
293
|
+
this.next_node = new ArrayList<Node>();
|
294
|
+
int var_107 = (int)this.readInt32(bais);
|
295
|
+
for(int var_109=0; var_109<var_107; var_109++) {
|
296
|
+
Node var_108 = new Node();
|
297
|
+
var_108.deserialize(bais);
|
298
|
+
this.next_node.add(var_108);
|
299
|
+
}
|
300
|
+
}
|
301
|
+
}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
eval(require('fs').readFileSync('test/binaryTree_test/js_test/test_binaryTree.js', 'utf8'));
|
2
|
+
var assert = require('assert');
|
3
|
+
var fs = require('fs');
|
4
|
+
console.log("Test Binary Tree");
|
5
|
+
|
6
|
+
var binaryTree_ser = buildTree();
|
7
|
+
var ca = binaryTree_ser.serialize();
|
8
|
+
serialize(ca);
|
9
|
+
var read = deserialize();
|
10
|
+
var binaryTree_deser = new BinaryTree();
|
11
|
+
binaryTree_deser.deserialize(new BabelDataReader(read));
|
12
|
+
compareBinaryTree(binaryTree_ser, binaryTree_deser);
|
13
|
+
|
14
|
+
|
15
|
+
function buildTree(){
|
16
|
+
var root = new Node();
|
17
|
+
root.i32 = 0;
|
18
|
+
|
19
|
+
var n1_L = new Node();
|
20
|
+
n1_L.i32 = 1;
|
21
|
+
|
22
|
+
var n1_R = new Node();
|
23
|
+
n1_R.i32 = 2;
|
24
|
+
|
25
|
+
var n2_L_L = new Node();
|
26
|
+
n2_L_L.i32 = 3;
|
27
|
+
|
28
|
+
var n2_L_R = new Node();
|
29
|
+
n2_L_R.i32 = 4;
|
30
|
+
|
31
|
+
var n2_R_L = new Node();
|
32
|
+
n2_R_L.i32 = 5;
|
33
|
+
|
34
|
+
var n2_R_R = new Node();
|
35
|
+
n2_R_R.i32 = 6;
|
36
|
+
|
37
|
+
root.next_node = [n1_L, n1_R];
|
38
|
+
n1_L.next_node = [n2_L_L, n2_L_R];
|
39
|
+
n1_R.next_node = [n2_R_L, n2_R_R];
|
40
|
+
|
41
|
+
var bt = new BinaryTree();
|
42
|
+
bt.root_node = [root];
|
43
|
+
return bt;
|
44
|
+
}
|
45
|
+
|
46
|
+
function compareBinaryTree(bt1, bt2){
|
47
|
+
assert.equal( bt1.root_node.length, bt2.root_node.length);
|
48
|
+
assert.equal( bt1.root_node[0].i32, bt2.root_node[0].i32);
|
49
|
+
assert.equal( bt1.root_node[0].next_node.length, bt2.root_node[0].next_node.length);
|
50
|
+
assert.equal( bt1.root_node[0].next_node[0].next_node[0].i32, bt2.root_node[0].next_node[0].next_node[0].i32);
|
51
|
+
}
|
52
|
+
|
53
|
+
function serialize(obj){
|
54
|
+
var bBuffer = new Buffer(obj);
|
55
|
+
fs.writeFileSync(__dirname + '/bin.babel.js', bBuffer, function (err) {
|
56
|
+
if (err) {
|
57
|
+
return console.log(err);
|
58
|
+
}
|
59
|
+
});
|
60
|
+
}
|
61
|
+
|
62
|
+
function deserialize(){
|
63
|
+
var data = fs.readFileSync(__dirname + '/bin.babel.js');
|
64
|
+
data = toArray(data);
|
65
|
+
return data;
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
function toArray(buffer) {
|
70
|
+
var view = new Uint8Array(buffer.length);
|
71
|
+
for (var i = 0; i < buffer.length; ++i) {
|
72
|
+
view[i] = buffer[i];
|
73
|
+
}
|
74
|
+
return view;
|
75
|
+
}
|
76
|
+
|