divine 0.0.3 → 0.0.4

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.
Files changed (49) hide show
  1. data/.gitignore +4 -14
  2. data/README.md +25 -0
  3. data/Rakefile +34 -19
  4. data/divine.gemspec +1 -0
  5. data/lib/divine.rb +3 -1
  6. data/lib/divine/code_generators/code_generator.rb +51 -1
  7. data/lib/divine/code_generators/csharp.rb +898 -0
  8. data/lib/divine/code_generators/java.rb +125 -13
  9. data/lib/divine/code_generators/javascript.rb +111 -4
  10. data/lib/divine/code_generators/ruby.rb +103 -9
  11. data/lib/divine/dsl.rb +95 -6
  12. data/lib/divine/graph_generator/graph_generator.rb +81 -0
  13. data/lib/divine/version.rb +2 -1
  14. data/test/basic_complex_test/basic_complex_test.rb +5 -0
  15. data/test/basic_complex_test/graph.jpg +0 -0
  16. data/test/basic_complex_test/java_test/JavaTest.java +1 -1
  17. data/test/basic_complex_test/ruby_test/ruby_test.rb +17 -4
  18. data/test/binaryTree_test/binaryTree_test.rb +5 -0
  19. data/test/binaryTree_test/csharp_test/csharp_test.cs +99 -0
  20. data/test/binaryTree_test/graph.png +0 -0
  21. data/test/binaryTree_test/java_test/JavaTest.java +1 -1
  22. data/test/binaryTree_test/ruby_test/ruby_test.rb +26 -3
  23. data/test/complex_test/complex_test.rb +5 -0
  24. data/test/complex_test/csharp_test/csharp_test.cs +109 -0
  25. data/test/complex_test/graph.png +0 -0
  26. data/test/complex_test/java_test/JavaTest.java +1 -1
  27. data/test/complex_test/ruby_test/ruby_test.rb +24 -1
  28. data/test/dynamic_int_test/csharp_test/csharp_test.cs +76 -0
  29. data/test/dynamic_int_test/dynamic_int_test.rb +20 -0
  30. data/test/dynamic_int_test/graph.jpg +0 -0
  31. data/test/dynamic_int_test/java_test/JavaTest.java +72 -0
  32. data/test/dynamic_int_test/js_test/js_test.js +54 -0
  33. data/test/dynamic_int_test/ruby_test/ruby_test.rb +55 -0
  34. data/test/ipv6_test/csharp_test/csharp_test.cs +73 -0
  35. data/test/ipv6_test/graph.jpg +0 -0
  36. data/test/ipv6_test/ipv6_test.rb +5 -0
  37. data/test/ipv6_test/java_test/JavaTest.java +1 -1
  38. data/test/ipv6_test/ruby_test/ruby_test.rb +24 -4
  39. data/test/lib/csharp/nunit.framework.dll +0 -0
  40. data/test/{java_lib → lib/java}/junit.jar +0 -0
  41. data/test/signed_int_test/csharp_test/csharp_test.cs +86 -0
  42. data/test/signed_int_test/graph.jpg +0 -0
  43. data/test/signed_int_test/java_test/JavaTest.java +1 -1
  44. data/test/signed_int_test/ruby_test/ruby_test.rb +21 -1
  45. data/test/signed_int_test/signed_int_test.rb +6 -0
  46. data/test/unify_test/unify_test.rb +17 -4
  47. metadata +54 -8
  48. data/test/signed_float_test/ruby_test/ruby_test.rb +0 -36
  49. data/test/signed_float_test/signed_float_test.rb +0 -14
@@ -1,39 +1,73 @@
1
1
  module Divine
2
+ # Contains all defined structs
2
3
  $all_structs = {}
3
4
 
5
+ #
6
+ # Encapsulation basic struct information
7
+ #
4
8
  class StructDefinition
9
+
5
10
  def initialize(owner, type, args)
6
11
  @owner = owner
7
12
  @type = type
8
13
  @args = args
9
14
  end
10
15
 
16
+ #
17
+ # Get struct's name
18
+ #
11
19
  def name
12
20
  @args.first
13
21
  end
14
-
22
+
15
23
  def type
16
24
  @type
17
25
  end
18
-
26
+
27
+ #
28
+ # Get struct's version
29
+ #
19
30
  def version
20
31
  @owner.version
21
32
  end
22
-
33
+
34
+ #
35
+ # Represents strut as string
36
+ #
23
37
  def to_s
24
38
  "#{@owner.name}: #{self.type} #{self.name} (#{self.class.name}, #{@args.inspect})"
25
39
  end
26
40
  end
27
41
 
42
+ #
43
+ # Encapsulation simple struct information
44
+ #
28
45
  class SimpleDefinition < StructDefinition
46
+ #
47
+ # Ask if the struct is simple.
48
+ # * +Return+ : True
29
49
  def simple?; true; end
50
+
51
+ #
52
+ # Get types used in the struct
53
+ #
30
54
  def referenced_types
31
55
  [@type]
32
56
  end
33
57
  end
34
58
 
59
+ #
60
+ # Encapsulation complex struct information
61
+ #
35
62
  class ComplexDefinition < StructDefinition
63
+ #
64
+ # Ask if the struct is simple.
65
+ # * *Return* : False
36
66
  def simple?; false; end
67
+
68
+ #
69
+ # Get types used in the struct
70
+ #
37
71
  def referenced_types
38
72
  fs = referenced_types_internal.map do |t|
39
73
  if t.is_a? StructBuilder
@@ -48,48 +82,100 @@ module Divine
48
82
  end
49
83
  end
50
84
 
85
+ #
86
+ # Encapsulation for dynamic integer date type
87
+ #
88
+ class DynamicInteger63Definition < SimpleDefinition
89
+ end
90
+
91
+ #
92
+ # Encapsulation for signed integer 64-bit date type
93
+ #
51
94
  class SInteger64Definition < SimpleDefinition
52
95
  end
53
96
 
97
+ #
98
+ # Encapsulation for signed integer 32-bit date type
99
+ #
54
100
  class SInteger32Definition < SimpleDefinition
55
101
  end
56
102
 
103
+ #
104
+ # Encapsulation for unsigned integer 32-bit date type
105
+ #
57
106
  class Integer32Definition < SimpleDefinition
58
107
  end
59
108
 
109
+ #
110
+ # Encapsulation for unsigned integer 24-bit
111
+ #
60
112
  class Integer24Definition < SimpleDefinition
61
113
  end
62
114
 
115
+ #
116
+ # Encapsulation for unsigned integer 16-bit data type
117
+ #
63
118
  class Integer16Definition < SimpleDefinition
64
119
  end
65
120
 
121
+ #
122
+ # Encapsulation for unsigned integer 8-bit data type
123
+ #
66
124
  class Integer8Definition < SimpleDefinition
67
125
  end
68
126
 
127
+ #
128
+ # Encapsulation for binary data type
129
+ #
69
130
  class BinaryDefinition < SimpleDefinition
70
131
  end
71
132
 
133
+ #
134
+ # Encapsulation for short binary data type
135
+ #
72
136
  class ShortBinaryDefinition < SimpleDefinition # Shorted than 256 bytes
73
137
  end
74
138
 
139
+ #
140
+ # Encapsulation for string data type
141
+ #
75
142
  class StringDefinition < SimpleDefinition
76
143
  end
77
144
 
145
+ #
146
+ # Encapsulation for boolean data type
147
+ #
78
148
  class BooleanDefinition < SimpleDefinition
79
149
  end
80
150
 
151
+ #
152
+ # Encapsulation for IP(v4 & v6) data type
153
+ #
81
154
  class IpNumberDefinition < SimpleDefinition
82
155
  end
83
156
 
84
-
157
+ #
158
+ # Encapsulation for list data type
159
+ #
85
160
  class ListDefinition < ComplexDefinition
161
+
162
+ #
163
+ # Return internal types contained in current list
164
+ #
86
165
  protected
87
166
  def referenced_types_internal
88
167
  [@args[1]]
89
168
  end
90
169
  end
91
170
 
171
+ #
172
+ # Encapsulation for map[dictionary] data type
173
+ #
92
174
  class MapDefinition < ComplexDefinition
175
+
176
+ #
177
+ # Return internal types contained in current map
178
+ #
93
179
  protected
94
180
  def referenced_types_internal
95
181
  [@args[1], @args[2]]
@@ -97,8 +183,9 @@ module Divine
97
183
  end
98
184
 
99
185
 
100
-
186
+ # Contains all supported data type
101
187
  $available_types = {
188
+ dint63: DynamicInteger63Definition,
102
189
  sint64: SInteger64Definition,
103
190
  sint32: SInteger32Definition,
104
191
  int32: Integer32Definition,
@@ -114,7 +201,9 @@ module Divine
114
201
  ip_number: IpNumberDefinition
115
202
  }
116
203
 
117
-
204
+ #
205
+ # Responsible for building structs
206
+ #
118
207
  class StructBuilder
119
208
  # Name = name of the struct
120
209
  # Version = struct version (not currently used)
@@ -0,0 +1,81 @@
1
+ require 'graphviz'
2
+
3
+ module Divine
4
+
5
+ #
6
+ # Responsible for drawing ERD diagram corresponding to structs
7
+ #
8
+ class GraphGenerator
9
+
10
+ #
11
+ # Draw Graph for current structs on $all_structs
12
+ # * *Args* :
13
+ # -path- -> target path
14
+ def draw(path = "", file_name = "graph", format = "jpg")
15
+ @relations = {}
16
+ @nodes_map = {}
17
+ @graph = GraphViz.new( :G, :type => :digraph )
18
+ $all_structs.keys.each do |k|
19
+ name = "Struct: #{k}"
20
+ content = "Temp Contents"
21
+ simple_fields = ""
22
+ complex_fields = ""
23
+ $all_structs[k][0].simple_fields.each { |f|
24
+ simple_fields << "#{f.name}: #{f.type}\n"
25
+ }
26
+ $all_structs[k][0].complex_fields.each { |f|
27
+ type = f.referenced_types.to_s.gsub(':','').gsub("[list,", "list[").gsub("[map,", "map[").gsub(/[\s]*/, "").gsub("[", "&lt;").gsub("]", "&gt;")
28
+ $all_structs.keys.each do |l|
29
+ if type.include?(l.to_s)
30
+ addRelation(l, k)
31
+ end
32
+ end
33
+ complex_fields << "#{f.name}: #{type}\n"
34
+ }
35
+ content = simple_fields + complex_fields
36
+ @nodes_map[k] = addNode(name, content)
37
+ end
38
+
39
+ addLinks()
40
+
41
+ # Generate output image
42
+ @graph.output( format.to_sym => File.join(path, "#{file_name}.#{format}") )
43
+ end
44
+
45
+
46
+ #
47
+ # Add node to the graph
48
+ # * *Args* :
49
+ # -struct_name- -> struct name
50
+ # -struct_content- -> struct contents "Fields"
51
+ def addNode(struct_name, struct_content)
52
+ @graph.add_nodes( "{ #{struct_name} | #{struct_content} }", "shape" => "record", "style" => "rounded")
53
+ end
54
+
55
+
56
+ #
57
+ # Build links from relation map
58
+ #
59
+ def addLinks()
60
+ @relations.each_pair do |src, trgs|
61
+ trgs.each { |trg|
62
+ @graph.add_edges( @nodes_map[trg], @nodes_map[src] )
63
+ }
64
+ end
65
+ end
66
+
67
+ #
68
+ # Add relation between nodes
69
+ # * *Args* :
70
+ # -src- -> the source struct
71
+ # -trg- -> the target struct
72
+ def addRelation(src, trg)
73
+ if @relations.keys.include?(src)
74
+ @relations[src].push(trg)
75
+ else
76
+ @relations[src] = [trg]
77
+ end
78
+ end
79
+ end
80
+
81
+ end
@@ -1,3 +1,4 @@
1
1
  module Divine
2
- VERSION = "0.0.3"
2
+ # Divine Version
3
+ VERSION = "0.0.4"
3
4
  end
@@ -24,11 +24,16 @@ struct(:TestComplex) {
24
24
  }
25
25
  }
26
26
 
27
+ # Draw ERD for prev. structs
28
+ Divine::GraphGenerator.new.draw("test/basic_complex_test/")
29
+
27
30
  if ARGV[0] == "ruby"
28
31
  Divine::CodeGenerator.new.generate(:ruby, file: 'test_babel.rb', module: 'BabelTest', parent_class: "Object", target_dir: 'test/basic_complex_test/ruby_test')
29
32
  elsif ARGV[0] == "js"
30
33
  Divine::CodeGenerator.new.generate(:javascript, file: 'test_babel.js', target_dir: 'test/basic_complex_test/js_test')
31
34
  elsif ARGV[0] == "java"
32
35
  Divine::CodeGenerator.new.generate(:java, file: 'test_babel.java', target_dir: 'test/basic_complex_test/java_test')
36
+ elsif ARGV[0] == "csharp"
37
+ #Divine::CodeGenerator.new.generate(:csharp, file: 'test_babel.cs', target_dir: "test/basic_complex_test/csharp_test")
33
38
  end
34
39
 
@@ -72,7 +72,7 @@ public class JavaTest {
72
72
 
73
73
  }
74
74
 
75
- public void serialize(BabelBase obj) throws IOException {
75
+ public void serialize(Divine obj) throws IOException {
76
76
  byte[] data = obj.serialize();
77
77
  File file = new File("test/basic_complex_test/java_test/bin.babel");
78
78
  try {
@@ -1,12 +1,14 @@
1
1
  require_relative 'test_babel.rb'
2
2
  require 'minitest/autorun'
3
3
 
4
+ #
5
+ # Responsible for testing basic and complex structure
6
+ #
4
7
  class TestBabelTestBasic < MiniTest::Unit::TestCase
5
8
 
6
- def setup
7
-
8
- end
9
-
9
+ #
10
+ # Compare objects after serialize and deserialize operations "simple structure"
11
+ #
10
12
  def test_basic
11
13
  puts "Basic Test"
12
14
  testbasic_ser = BabelTest::TestBasic.new
@@ -32,6 +34,9 @@ class TestBabelTestBasic < MiniTest::Unit::TestCase
32
34
  assert_equal testbasic_ser.guid , testbasic_deser.guid
33
35
  end
34
36
 
37
+ #
38
+ # Compare objects after serialize and deserialize operations "complex structure"
39
+ #
35
40
  def test_complex
36
41
  testcomplex_ser = BabelTest::TestComplex.new
37
42
  testcomplex_ser.list1 = [0, 1, 255, 0x7FFFFFFF, 0x7FFFFFFF+1, 0xFFFFFFFE, 0xFFFFFFFF]
@@ -56,12 +61,20 @@ class TestBabelTestBasic < MiniTest::Unit::TestCase
56
61
  end
57
62
  end
58
63
 
64
+ #
65
+ # Write binary data to file
66
+ # * *Args* :
67
+ # -data- --> bytes to be written
68
+ #
59
69
  def serialize(data)
60
70
  File.open("test/basic_complex_test/ruby_test/bin.babel.rb", "w+b") do |f|
61
71
  f.write(data)
62
72
  end
63
73
  end
64
74
 
75
+ #
76
+ # Read file in binary mode and return its bytes content
77
+ #
65
78
  def deserialize()
66
79
  mem_buf = File.new('test/basic_complex_test/ruby_test/bin.babel.rb').binmode
67
80
  end
@@ -10,10 +10,15 @@ struct 'BinaryTree' do
10
10
  list :root_node, :Node # List of root node of size equals to 1.
11
11
  end
12
12
 
13
+ # Draw ERD for prev. structs
14
+ Divine::GraphGenerator.new.draw("test/binaryTree_test/", "graph", "png")
15
+
13
16
  if ARGV[0] == "ruby"
14
17
  Divine::CodeGenerator.new.generate(:ruby, file: 'test_binaryTree.rb', module: 'BabelTest', parent_class: "Object", target_dir: 'test/binaryTree_test/ruby_test')
15
18
  elsif ARGV[0] == "js"
16
19
  Divine::CodeGenerator.new.generate(:javascript, file: 'test_binaryTree.js', target_dir: 'test/binaryTree_test/js_test')
17
20
  elsif ARGV[0] == "java"
18
21
  Divine::CodeGenerator.new.generate(:java, file: 'test_binaryTree.java', target_dir: 'test/binaryTree_test/java_test')
22
+ elsif ARGV[0] == "csharp"
23
+ Divine::CodeGenerator.new.generate(:csharp, file: 'test_binaryTree.cs', target_dir: "test/binaryTree_test/csharp_test")
19
24
  end
@@ -0,0 +1,99 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Text;
4
+ using System.IO;
5
+ using divine;
6
+ using NUnit.Framework;
7
+
8
+ namespace test_signed_int
9
+ {
10
+ [TestFixture]
11
+ class divine
12
+ {
13
+ [Test]
14
+ public static void Main(String[] args)
15
+ {
16
+ System.Console.Write("Test Binary Tree \n");
17
+ BinaryTree ser = buildObj();
18
+ serialize(ser);
19
+ byte[] res = deserialize();
20
+
21
+ BinaryTree deser = new BinaryTree();
22
+ deser.deserialize(new MemoryStream(res));
23
+
24
+ compare(ser, deser);
25
+ }
26
+
27
+ public static BinaryTree buildObj() {
28
+ Node root = new Node();
29
+ root.i32 = 0;
30
+
31
+ Node n1_L = new Node();
32
+ n1_L.i32 = 1;
33
+
34
+ Node n1_R = new Node();
35
+ n1_R.i32 = 2;
36
+
37
+ Node n2_L_L = new Node();
38
+ n2_L_L.i32 = 3;
39
+
40
+ Node n2_L_R = new Node();
41
+ n2_L_R.i32 = 4;
42
+
43
+ Node n2_R_L = new Node();
44
+ n2_R_L.i32 = 5;
45
+
46
+ Node n2_R_R = new Node();
47
+ n2_R_R.i32 = 6;
48
+
49
+ root.next_node.Add(n1_L);
50
+ root.next_node.Add(n1_R);
51
+
52
+ n1_L.next_node.Add(n2_L_L);
53
+ n1_L.next_node.Add(n2_L_R);
54
+
55
+ n1_R.next_node.Add(n2_R_L);
56
+ n1_R.next_node.Add(n2_R_R);
57
+
58
+ BinaryTree bt = new BinaryTree();
59
+ bt.root_node.Add(root);
60
+
61
+ return bt;
62
+ }
63
+
64
+ public static void compare(BinaryTree bt1, BinaryTree bt2)
65
+ {
66
+ Assert.AreEqual(bt1.root_node.Count, bt2.root_node.Count);
67
+ Assert.AreEqual(bt1.root_node[0].i32, bt2.root_node[0].i32);
68
+ Assert.AreEqual(bt1.root_node[0].next_node.Count, bt2.root_node[0].next_node.Count);
69
+ Assert.AreEqual(bt1.root_node[0].next_node[0].next_node[0].i32,
70
+ bt2.root_node[0].next_node[0].next_node[0].i32);
71
+ }
72
+
73
+ public static void serialize(Divine obj)
74
+ {
75
+ try
76
+ {
77
+ byte[] data = obj.serialize();
78
+ File.WriteAllBytes("test/binaryTree_test/csharp_test/bin.babel.csharp", data);
79
+ }
80
+ catch (System.IO.IOException ex)
81
+ {
82
+ throw ex;
83
+ }
84
+ }
85
+
86
+ public static byte[] deserialize(){
87
+ try
88
+ {
89
+ byte[] data = File.ReadAllBytes("test/binaryTree_test/csharp_test/bin.babel.csharp");
90
+ return data;
91
+ }
92
+ catch (System.IO.IOException ex)
93
+ {
94
+ throw ex;
95
+ }
96
+ }
97
+
98
+ }
99
+ }