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,68 @@
|
|
1
|
+
require_relative 'test_babel.rb'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestBabelTestBasic < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_basic
|
11
|
+
puts "Basic Test"
|
12
|
+
testbasic_ser = BabelTest::TestBasic.new
|
13
|
+
testbasic_ser.i8 = 0x0F
|
14
|
+
testbasic_ser.i16 = 0X1234
|
15
|
+
testbasic_ser.i32 = 0x567890AB
|
16
|
+
testbasic_ser.str = "abc\n123\t\""
|
17
|
+
testbasic_ser.ip = "192.168.0.1"
|
18
|
+
testbasic_ser.guid = (300..302).to_s
|
19
|
+
|
20
|
+
ser = testbasic_ser.serialize
|
21
|
+
serialize(ser)
|
22
|
+
res = deserialize()
|
23
|
+
|
24
|
+
testbasic_deser = BabelTest::TestBasic.new
|
25
|
+
testbasic_deser.deserialize res
|
26
|
+
|
27
|
+
assert testbasic_ser.i8 == testbasic_deser.i8
|
28
|
+
assert testbasic_ser.i16 == testbasic_deser.i16
|
29
|
+
assert testbasic_ser.i32 == testbasic_deser.i32
|
30
|
+
assert testbasic_ser.str == testbasic_deser.str
|
31
|
+
assert testbasic_ser.ip == testbasic_deser.ip
|
32
|
+
assert testbasic_ser.guid == testbasic_deser.guid
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_complex
|
36
|
+
testcomplex_ser = BabelTest::TestComplex.new
|
37
|
+
testcomplex_ser.list1 = [0, 1, 255, 0x7FFFFFFF, 0x7FFFFFFF+1, 0xFFFFFFFE, 0xFFFFFFFF]
|
38
|
+
testcomplex_ser.list2 = [0,1, 15,16, 127,128, 254,255]
|
39
|
+
testcomplex_ser.map1[0] = 10
|
40
|
+
testcomplex_ser.map1[10] = 100
|
41
|
+
testcomplex_ser.map2["Hello_1"] = [BabelTest::Entry.new, BabelTest::Entry.new, BabelTest::Entry.new]
|
42
|
+
testcomplex_ser.map2["Hello_2"] = [BabelTest::Entry.new, BabelTest::Entry.new]
|
43
|
+
|
44
|
+
ser = testcomplex_ser.serialize
|
45
|
+
serialize(ser)
|
46
|
+
res = deserialize()
|
47
|
+
|
48
|
+
testcomplex_deser = BabelTest::TestComplex.new
|
49
|
+
testcomplex_deser.deserialize res
|
50
|
+
|
51
|
+
assert testcomplex_ser.list1.sort == testcomplex_deser.list1.sort
|
52
|
+
assert testcomplex_ser.list2.sort == testcomplex_deser.list2.sort
|
53
|
+
assert testcomplex_ser.map1 == testcomplex_deser.map1
|
54
|
+
testcomplex_ser.map2.keys.each do |k|
|
55
|
+
assert testcomplex_ser.map2[k].length == testcomplex_deser.map2[k].length
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def serialize(data)
|
60
|
+
File.open("test/basic_complex_test/ruby_test/bin.babel.rb", "w+b") do |f|
|
61
|
+
f.write(data)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def deserialize()
|
66
|
+
mem_buf = File.new('test/basic_complex_test/ruby_test/bin.babel.rb').binmode
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,368 @@
|
|
1
|
+
module BabelTest
|
2
|
+
|
3
|
+
|
4
|
+
class BabelBase < Object
|
5
|
+
public
|
6
|
+
def serialize
|
7
|
+
out = []
|
8
|
+
serialize_internal(out)
|
9
|
+
out.flatten.pack("C*")
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
### Read methods ###
|
14
|
+
def read_int8(data)
|
15
|
+
data.getbyte
|
16
|
+
end
|
17
|
+
|
18
|
+
def read_int16(data)
|
19
|
+
(data.getbyte << 8) | read_int8(data)
|
20
|
+
end
|
21
|
+
|
22
|
+
def read_int24(data)
|
23
|
+
(data.getbyte << 16) | read_int16(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def read_int32(data)
|
27
|
+
(data.getbyte << 24) | read_int24(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def read_bool(data)
|
31
|
+
read_int8(data) == 1
|
32
|
+
end
|
33
|
+
|
34
|
+
def read_string(data)
|
35
|
+
data.read(read_int16(data)).force_encoding('UTF-8')
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_binary(data)
|
39
|
+
data.read(read_int32(data))
|
40
|
+
end
|
41
|
+
|
42
|
+
def read_short_binary(data)
|
43
|
+
data.read(read_int8(data))
|
44
|
+
end
|
45
|
+
|
46
|
+
def read_ip_number(data)
|
47
|
+
ips = read_short_binary(data)
|
48
|
+
if ips.size == 4
|
49
|
+
read_ipv4_number(ips)
|
50
|
+
else
|
51
|
+
read_ipv6_number(ips)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def read_ipv4_number(ips)
|
56
|
+
ips.bytes.to_a.join('.')
|
57
|
+
end
|
58
|
+
|
59
|
+
def read_ipv6_number(ips)
|
60
|
+
ipv6 = []
|
61
|
+
ips.bytes.each_slice(2) do |t|
|
62
|
+
fst = t[0]
|
63
|
+
lst = t[1]
|
64
|
+
tmp = ""
|
65
|
+
tmp = fst.to_s 16 if fst != 0
|
66
|
+
if fst != 0 and lst < 10
|
67
|
+
tmp << "0#{lst.to_s 16}"
|
68
|
+
elsif fst != 0 and lst > 10 or fst == 0 and lst > 0
|
69
|
+
tmp << lst.to_s(16)
|
70
|
+
end
|
71
|
+
ipv6.push(tmp)
|
72
|
+
end
|
73
|
+
ipv6.join(':').gsub(/:{2,}/, "::")
|
74
|
+
end
|
75
|
+
|
76
|
+
### Write methods ###
|
77
|
+
def write_int8(v, out)
|
78
|
+
v = v.to_i
|
79
|
+
raise_error "Too large int8 number: #{v}" if v > 0xFF # Max 255
|
80
|
+
raise_error "a negative number passed to int8 number: #{v}" if v < 0
|
81
|
+
out << v
|
82
|
+
end
|
83
|
+
|
84
|
+
def write_int16(v, out)
|
85
|
+
v = v.to_i
|
86
|
+
raise_error "Too large int16 number: #{v}" if v > 0xFFFF # Max 65.535
|
87
|
+
raise_error "a negative number passed to int16 number: #{v}" if v < 0
|
88
|
+
write_int8( v >> 8 & 0xFF, out)
|
89
|
+
write_int8( v & 0xFF, out)
|
90
|
+
end
|
91
|
+
|
92
|
+
def write_int24(v, out)
|
93
|
+
v = v.to_i
|
94
|
+
raise_error "Too large int24 number: #{v}" if v > 0xFFFFFF # Max 16.777.215
|
95
|
+
raise_error "a negative number passed to int24 number: #{v}" if v < 0 # In Case added to ruby declaration
|
96
|
+
write_int8( v >> 16 & 0xFF, out)
|
97
|
+
write_int16( v & 0xFFFF, out)
|
98
|
+
end
|
99
|
+
|
100
|
+
def write_int32(v, out)
|
101
|
+
v = v.to_i
|
102
|
+
raise_error "Too large int32 number: #{v}" if v > 0xFFFFFFFF # Max 4.294.967.295
|
103
|
+
raise_error "a negative number passed to int32 number: #{v}" if v < 0
|
104
|
+
write_int8( v >> 24 & 0xFF, out)
|
105
|
+
write_int24( v & 0xFFFFFF, out)
|
106
|
+
end
|
107
|
+
|
108
|
+
def write_bool(v, out)
|
109
|
+
write_int8(v ? 1 : 0, out)
|
110
|
+
end
|
111
|
+
|
112
|
+
def write_string(v, out)
|
113
|
+
s = force_to_utf8_string(v)
|
114
|
+
raise_error "Too large string: #{s.bytesize} bytes" if s.bytesize > 0xFFFF
|
115
|
+
write_int16(s.bytesize, out)
|
116
|
+
out << s.bytes.to_a
|
117
|
+
end
|
118
|
+
|
119
|
+
def write_binary(v, out)
|
120
|
+
if v.is_a?(Array)
|
121
|
+
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
122
|
+
write_int32(v.size, out)
|
123
|
+
v.each do |x|
|
124
|
+
write_int8(x, out)
|
125
|
+
end
|
126
|
+
elsif v.is_a?(String)
|
127
|
+
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
128
|
+
write_int32(v.size, out)
|
129
|
+
out << v.bytes.to_a
|
130
|
+
else
|
131
|
+
raise_error "Unsupported binary 'nil'" if v == nil
|
132
|
+
raise_error "Unsupported binary of type '#{v.class.name}'"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def write_16_binary(v, out)
|
137
|
+
if v.is_a?(Array)
|
138
|
+
raise_error "Too large 16 binary: #{v.size} (#{v.class.name})" unless v.size*2 < 0xFF
|
139
|
+
write_int8(v.size * 2, out) # IPv6 consists of 8 parts each of them has zise of 2 bytes
|
140
|
+
v.each do |x|
|
141
|
+
write_int16(x, out)
|
142
|
+
end
|
143
|
+
else
|
144
|
+
raise_error "Unsupported binary 'nil'" if v == nil
|
145
|
+
raise_error "Unsupported binary of type '#{v.class.name}'"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def write_short_binary(v, out)
|
150
|
+
if v.is_a?(Array)
|
151
|
+
raise_error "Too large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
152
|
+
write_int8(v.size, out)
|
153
|
+
v.each do |x|
|
154
|
+
write_int8(x, out)
|
155
|
+
end
|
156
|
+
elsif v.is_a?(String)
|
157
|
+
raise_error "To large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
158
|
+
write_int8(v.size, out)
|
159
|
+
out << v.bytes.to_a
|
160
|
+
else
|
161
|
+
raise_error "Unsupported binary 'nil'" if v == nil
|
162
|
+
raise_error "Unsupported binary of type '#{v.class.name}'"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def write_ip_number(v, out)
|
167
|
+
if v.is_a?(Array)
|
168
|
+
if v.size == 4
|
169
|
+
write_ipv4_number(v, out);
|
170
|
+
else
|
171
|
+
write_ipv6_number(v, out);
|
172
|
+
end
|
173
|
+
elsif v.is_a?(String)
|
174
|
+
if v.include?":"
|
175
|
+
write_ipv6_number(v, out);
|
176
|
+
else
|
177
|
+
write_ipv4_number(v, out);
|
178
|
+
end
|
179
|
+
else
|
180
|
+
raise_error "Unknown IP number '#{v}'"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def write_ipv4_number(v,out)
|
185
|
+
if v.is_a?(Array)
|
186
|
+
raise_error "Unknown IP v4 number #{v}" unless v.size == 0 || v.size == 4 # Only IPv4 for now
|
187
|
+
write_short_binary(v, out)
|
188
|
+
elsif v.is_a?(String)
|
189
|
+
ss = v.split(/\./).map do |s|
|
190
|
+
s.to_i
|
191
|
+
end
|
192
|
+
write_ipv4_number(ss, out)
|
193
|
+
else
|
194
|
+
raise_error "Unknown IP number '#{v}'"
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def write_ipv6_number(v, out)
|
199
|
+
if v.is_a?(Array)
|
200
|
+
write_16_binary(v, out)
|
201
|
+
elsif v.is_a?(String)
|
202
|
+
v = v.gsub(" ","") + " " # Temporary: To avoid the split problem when we have : at the end of "v"
|
203
|
+
raise_error "Unknown IPv6 number #{v}" unless v.strip.empty? ||
|
204
|
+
v.strip.match(/[^:0-9a-f]+/i) == nil && #Should not contains numbers or letters 0-9a-f
|
205
|
+
v.strip.match(/[0-9a-f]+/i) != nil && #Should contains numbers or letters 0-9a-f
|
206
|
+
v.match(":{3,}") == nil &&
|
207
|
+
v.split("::").size <= 2
|
208
|
+
ss = v.split(/:/).map do |s|
|
209
|
+
s = s.strip
|
210
|
+
raise_error "Unknown IPv6 Group #{s}" unless s.size <= 4
|
211
|
+
s.to_i 16
|
212
|
+
end
|
213
|
+
ss = [] if v.strip.empty?
|
214
|
+
raise_error "Unknown IPv6 number #{v}" unless (!v.include?("::") && ss.size == 0 || ss.size == 8) ||
|
215
|
+
(v.include?("::") && ss.size > 2 && ss.size < 8)
|
216
|
+
write_ipv6_number(ss, out)
|
217
|
+
else
|
218
|
+
raise_error "Unknown IPv6 number '#{v}'"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
private
|
223
|
+
def raise_error(msg)
|
224
|
+
raise "[#{self.class.name}] #{msg}"
|
225
|
+
end
|
226
|
+
|
227
|
+
def force_to_utf8_string(string)
|
228
|
+
if string.encoding != Encoding::UTF_8
|
229
|
+
string = string.encode(Encoding::UTF_8)
|
230
|
+
end
|
231
|
+
return string
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
class Entry < BabelBase
|
238
|
+
|
239
|
+
def serialize_internal(out)
|
240
|
+
print "+"
|
241
|
+
end
|
242
|
+
|
243
|
+
def deserialize(data)
|
244
|
+
print "-"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
class TestBasic < BabelBase
|
251
|
+
attr_accessor :i8, :i16, :i32, :str, :ip, :guid
|
252
|
+
|
253
|
+
def initialize()
|
254
|
+
super
|
255
|
+
@i8 ||= 0
|
256
|
+
@i16 ||= 0
|
257
|
+
@i32 ||= 0
|
258
|
+
@str ||= ""
|
259
|
+
@ip ||= ""
|
260
|
+
@guid ||= []
|
261
|
+
end
|
262
|
+
|
263
|
+
def serialize_internal(out)
|
264
|
+
print "+"
|
265
|
+
write_int8(i8, out)
|
266
|
+
write_int16(i16, out)
|
267
|
+
write_int32(i32, out)
|
268
|
+
write_string(str, out)
|
269
|
+
write_ip_number(ip, out)
|
270
|
+
write_binary(guid, out)
|
271
|
+
end
|
272
|
+
|
273
|
+
def deserialize(data)
|
274
|
+
print "-"
|
275
|
+
@i8 = read_int8(data)
|
276
|
+
@i16 = read_int16(data)
|
277
|
+
@i32 = read_int32(data)
|
278
|
+
@str = read_string(data)
|
279
|
+
@ip = read_ip_number(data)
|
280
|
+
@guid = read_binary(data)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
class TestComplex < BabelBase
|
287
|
+
attr_accessor :list1, :list2, :map1, :map2
|
288
|
+
|
289
|
+
def initialize()
|
290
|
+
super
|
291
|
+
@list1 ||= []
|
292
|
+
@list2 ||= []
|
293
|
+
@map1 ||= {}
|
294
|
+
@map2 ||= {}
|
295
|
+
end
|
296
|
+
|
297
|
+
def serialize_internal(out)
|
298
|
+
print "+"
|
299
|
+
# Serialize list 'list1'
|
300
|
+
write_int32(list1.size, out)
|
301
|
+
list1.each do |var_100|
|
302
|
+
write_int32(var_100, out)
|
303
|
+
end
|
304
|
+
# Serialize list 'list2'
|
305
|
+
write_int32(list2.size, out)
|
306
|
+
list2.each do |var_101|
|
307
|
+
write_int8(var_101, out)
|
308
|
+
end
|
309
|
+
# Serialize map 'map1'
|
310
|
+
write_int32(map1.size, out)
|
311
|
+
map1.each_pair do |var_102, var_103|
|
312
|
+
write_int8(var_102, out)
|
313
|
+
write_int32(var_103, out)
|
314
|
+
end
|
315
|
+
# Serialize map 'map2'
|
316
|
+
write_int32(map2.size, out)
|
317
|
+
map2.each_pair do |var_104, var_105|
|
318
|
+
write_string(var_104, out)
|
319
|
+
write_int32(var_105.size, out)
|
320
|
+
var_105.each do |var_106|
|
321
|
+
var_106.serialize_internal(out)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def deserialize(data)
|
327
|
+
print "-"
|
328
|
+
# Deserialize list 'list1'
|
329
|
+
@list1 = []
|
330
|
+
var_107 = read_int32(data)
|
331
|
+
(1..var_107).each do
|
332
|
+
var_108 = read_int32(data)
|
333
|
+
@list1 << var_108
|
334
|
+
end
|
335
|
+
# Deserialize list 'list2'
|
336
|
+
@list2 = []
|
337
|
+
var_109 = read_int32(data)
|
338
|
+
(1..var_109).each do
|
339
|
+
var_10a = read_int8(data)
|
340
|
+
@list2 << var_10a
|
341
|
+
end
|
342
|
+
# Deserialize map 'map1'
|
343
|
+
@map1 = {}
|
344
|
+
var_10b = read_int32(data)
|
345
|
+
(1..var_10b).each do
|
346
|
+
var_10c = read_int8(data)
|
347
|
+
var_10d = read_int32(data)
|
348
|
+
@map1[var_10c] = var_10d
|
349
|
+
end
|
350
|
+
# Deserialize map 'map2'
|
351
|
+
@map2 = {}
|
352
|
+
var_10e = read_int32(data)
|
353
|
+
(1..var_10e).each do
|
354
|
+
var_10f = read_string(data)
|
355
|
+
var_110 = []
|
356
|
+
var_111 = read_int32(data)
|
357
|
+
(1..var_111).each do
|
358
|
+
var_112 = Entry.new
|
359
|
+
var_112.deserialize(data)
|
360
|
+
var_110 << var_112
|
361
|
+
end
|
362
|
+
@map2[var_10f] = var_110
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
|
368
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
require 'divine'
|
3
|
+
|
4
|
+
struct 'Node' do
|
5
|
+
int32 :i32
|
6
|
+
list :next_node, :Node # List of nodes with size equals to 2, just to refer to the next left and right nodes in the tree
|
7
|
+
end
|
8
|
+
|
9
|
+
struct 'BinaryTree' do
|
10
|
+
list :root_node, :Node # List of root node of size equals to 1.
|
11
|
+
end
|
12
|
+
|
13
|
+
if ARGV[0] == "ruby"
|
14
|
+
Divine::CodeGenerator.new.generate(:ruby, file: 'test_binaryTree.rb', module: 'BabelTest', parent_class: "Object", target_dir: 'test/binaryTree_test/ruby_test')
|
15
|
+
elsif ARGV[0] == "js"
|
16
|
+
Divine::CodeGenerator.new.generate(:javascript, file: 'test_binaryTree.js', target_dir: 'test/binaryTree_test/js_test')
|
17
|
+
elsif ARGV[0] == "java"
|
18
|
+
Divine::CodeGenerator.new.generate(:java, file: 'test_binaryTree.java', target_dir: 'test/binaryTree_test/java_test')
|
19
|
+
end
|