cauterize 0.0.1.pre13 → 0.0.1.pre14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +33 -33
  3. data/.rspec +1 -1
  4. data/.travisci.yml +4 -4
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +21 -21
  7. data/README.md +177 -177
  8. data/Rakefile +54 -54
  9. data/bin/cauterize +61 -61
  10. data/cauterize.gemspec +27 -27
  11. data/example/Cauterize +68 -68
  12. data/example/build.sh +25 -25
  13. data/example/c_example_support/empty_main.c +1 -1
  14. data/example/c_example_support/example_project_config.c +11 -11
  15. data/example/c_example_support/example_project_config.h +15 -15
  16. data/example/ruby_ex.rb +14 -14
  17. data/lib/cauterize.rb +117 -117
  18. data/lib/cauterize/base_type.rb +78 -78
  19. data/lib/cauterize/builders.rb +41 -41
  20. data/lib/cauterize/builders/c/buildable.rb +91 -91
  21. data/lib/cauterize/builders/c/builtin.rb +43 -43
  22. data/lib/cauterize/builders/c/composite.rb +57 -57
  23. data/lib/cauterize/builders/c/enumeration.rb +60 -60
  24. data/lib/cauterize/builders/c/fixed_array.rb +74 -74
  25. data/lib/cauterize/builders/c/group.rb +143 -143
  26. data/lib/cauterize/builders/c/scalar.rb +34 -34
  27. data/lib/cauterize/builders/c/variable_array.rb +98 -98
  28. data/lib/cauterize/builders/cs/buildable.rb +59 -59
  29. data/lib/cauterize/builders/cs/builtin.rb +22 -22
  30. data/lib/cauterize/builders/cs/composite.rb +21 -21
  31. data/lib/cauterize/builders/cs/csarray.rb +32 -32
  32. data/lib/cauterize/builders/cs/enumeration.rb +21 -21
  33. data/lib/cauterize/builders/cs/fixed_array.rb +27 -27
  34. data/lib/cauterize/builders/cs/group.rb +32 -32
  35. data/lib/cauterize/builders/cs/scalar.rb +10 -10
  36. data/lib/cauterize/builders/cs/variable_array.rb +40 -34
  37. data/lib/cauterize/builders/doc/buildable.rb +30 -30
  38. data/lib/cauterize/builders/doc/builtin.rb +21 -21
  39. data/lib/cauterize/builders/doc/composite.rb +23 -23
  40. data/lib/cauterize/builders/doc/enumeration.rb +24 -24
  41. data/lib/cauterize/builders/doc/fixed_array.rb +26 -26
  42. data/lib/cauterize/builders/doc/group.rb +29 -29
  43. data/lib/cauterize/builders/doc/scalar.rb +21 -21
  44. data/lib/cauterize/builders/doc/variable_array.rb +26 -26
  45. data/lib/cauterize/builders/ruby/buildable.rb +11 -11
  46. data/lib/cauterize/builders/ruby/builtin.rb +25 -25
  47. data/lib/cauterize/builders/ruby/composite.rb +26 -26
  48. data/lib/cauterize/builders/ruby/enumeration.rb +27 -27
  49. data/lib/cauterize/builders/ruby/fixed_array.rb +20 -20
  50. data/lib/cauterize/builders/ruby/group.rb +35 -35
  51. data/lib/cauterize/builders/ruby/scalar.rb +19 -19
  52. data/lib/cauterize/builders/ruby/variable_array.rb +22 -22
  53. data/lib/cauterize/builtin.rb +62 -62
  54. data/lib/cauterize/c_builder.rb +103 -103
  55. data/lib/cauterize/cauterize.rb +76 -76
  56. data/lib/cauterize/composite.rb +64 -64
  57. data/lib/cauterize/cs_builder.rb +58 -58
  58. data/lib/cauterize/doc_builder.rb +34 -34
  59. data/lib/cauterize/enumeration.rb +98 -98
  60. data/lib/cauterize/fixed_array.rb +50 -50
  61. data/lib/cauterize/formatter.rb +67 -67
  62. data/lib/cauterize/group.rb +81 -81
  63. data/lib/cauterize/representation.rb +32 -32
  64. data/lib/cauterize/ruby_builder.rb +44 -44
  65. data/lib/cauterize/scalar.rb +53 -53
  66. data/lib/cauterize/snake_case.rb +21 -21
  67. data/lib/cauterize/variable_array.rb +55 -55
  68. data/lib/cauterize/version.rb +3 -3
  69. data/spec/base_type_spec.rb +251 -251
  70. data/spec/builders/c/buildable_spec.rb +25 -25
  71. data/spec/builders/c/builtin_spec.rb +22 -22
  72. data/spec/builders/c/composite_spec.rb +50 -50
  73. data/spec/builders/c/enumeration_spec.rb +95 -95
  74. data/spec/builders/c/fixed_array_spec.rb +36 -36
  75. data/spec/builders/c/group_spec.rb +140 -140
  76. data/spec/builders/c/scalar_spec.rb +26 -26
  77. data/spec/builders/c/variable_array_spec.rb +48 -48
  78. data/spec/builders/cs/buildable_spec.rb +8 -8
  79. data/spec/builders/cs/composite_spec.rb +32 -32
  80. data/spec/builders/cs/enumeration_spec.rb +33 -33
  81. data/spec/builders/cs/fixed_array_spec.rb +41 -41
  82. data/spec/builders/cs/group_spec.rb +56 -56
  83. data/spec/builders/cs/scalar_spec.rb +7 -7
  84. data/spec/builders/cs/variable_array_spec.rb +49 -45
  85. data/spec/builders/doc/buildable_spec.rb +25 -25
  86. data/spec/builders_spec.rb +57 -57
  87. data/spec/builtin_spec.rb +43 -43
  88. data/spec/c_builder_spec.rb +176 -176
  89. data/spec/cauterize_spec.rb +15 -15
  90. data/spec/composite_spec.rb +77 -77
  91. data/spec/cs_builder_spec.rb +115 -115
  92. data/spec/doc_builder_spec.rb +260 -260
  93. data/spec/enumeration_spec.rb +145 -145
  94. data/spec/fixed_array_spec.rb +61 -61
  95. data/spec/group_spec.rb +111 -111
  96. data/spec/ruby_builder_spec.rb +83 -83
  97. data/spec/ruby_generated_spec.rb +735 -735
  98. data/spec/scalar_spec.rb +44 -44
  99. data/spec/spec_helper.rb +122 -122
  100. data/spec/support/shared_examples_for_array_buildables.rb +22 -22
  101. data/spec/support/shared_examples_for_c_buildables.rb +103 -103
  102. data/spec/support/shared_examples_for_sane_c_buildables.rb +22 -22
  103. data/spec/support/shared_examples_for_stubbed_functions.rb +18 -18
  104. data/spec/support/spec_sample_model.rb +74 -74
  105. data/spec/test_main.c +13 -13
  106. data/spec/variable_array_spec.rb +73 -73
  107. data/support/c/src/cauterize.c +75 -75
  108. data/support/c/src/cauterize.h +60 -60
  109. data/support/c/src/cauterize_debug.h +29 -29
  110. data/support/c/src/cauterize_util.h +8 -8
  111. data/support/c/test/greatest.h +536 -536
  112. data/support/c/test/test.c +166 -166
  113. data/support/cs/src/CauterizeCompositeFormatter.cs +0 -0
  114. data/support/cs/src/CauterizeContainerFormatter.cs +0 -0
  115. data/support/cs/src/CauterizeEnumFormatter.cs +0 -0
  116. data/support/cs/src/CauterizeException.cs +15 -15
  117. data/support/cs/src/CauterizeFixedArrayFormatter.cs +2 -2
  118. data/support/cs/src/CauterizeFormatter.cs +0 -0
  119. data/support/cs/src/CauterizeGroupFormatter.cs +0 -0
  120. data/support/cs/src/CauterizeTypeFormatterFactory.cs +0 -0
  121. data/support/cs/src/CauterizeTypes.cs +46 -12
  122. data/support/cs/src/CauterizeVariableArrayFormatter.cs +1 -1
  123. data/support/cs/src/ICauterizeTypeFormatter.cs +0 -0
  124. data/support/cs/src/OrderAttribute.cs +0 -0
  125. data/support/cs/src/PrimitiveSupport.cs +12 -0
  126. data/support/cs/test/CauterizeCompositeFormatterTest.cs +0 -0
  127. data/support/cs/test/CauterizeEnumFormatterTest.cs +0 -0
  128. data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +0 -0
  129. data/support/cs/test/CauterizeFormatterTest.cs +0 -0
  130. data/support/cs/test/CauterizeGroupFormatterTest.cs +0 -0
  131. data/support/cs/test/CauterizeIntegrationTest.cs +0 -0
  132. data/support/cs/test/CauterizePrimitiveFormatterTest.cs +0 -0
  133. data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +0 -0
  134. data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +0 -0
  135. data/support/cs/test/OrderAttributeTest.cs +0 -0
  136. data/support/ruby/src/cauterize_ruby_baseclasses.rb +367 -367
  137. data/support/ruby/src/cauterize_ruby_builtins.rb +148 -148
  138. metadata +20 -20
@@ -1,83 +1,83 @@
1
- require 'tmpdir'
2
- require 'fileutils'
3
-
4
- module Cauterize
5
- describe Cauterize::RubyBuilder do
6
- before do
7
- @tempdir = Dir.mktmpdir
8
- @rb_path = File.join(@tempdir, "testing.rb")
9
-
10
- @rb = RubyBuilder.new(@rb_path, "testing")
11
- end
12
-
13
- describe :build do
14
- before do
15
- Cauterize.set_name("example_project")
16
- Cauterize.set_version("1.2.3")
17
-
18
- Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
19
-
20
- Cauterize.fixed_array(:mac_address) do |fa|
21
- fa.array_type :small_uint
22
- fa.array_size 6
23
- end
24
-
25
- Cauterize.variable_array(:mac_table) do |t|
26
- t.array_type :mac_address
27
- t.array_size 64
28
- end
29
-
30
- Cauterize.variable_array(:name) do |va|
31
- va.array_type :small_uint
32
- va.array_size 32
33
- end
34
-
35
- Cauterize.enumeration(:gender) do |e|
36
- e.value :male
37
- e.value :female
38
- end
39
-
40
- Cauterize.composite(:place) do |c|
41
- c.field :name, :name
42
- c.field :elevation, :uint32
43
- end
44
-
45
- Cauterize.composite(:person) do |c|
46
- c.field :first_name, :name
47
- c.field :last_name, :name
48
- c.field :gender, :gender
49
- end
50
-
51
- Cauterize.composite(:dog) do |c|
52
- c.field :name, :name
53
- c.field :gender, :gender
54
- c.field :leg_count, :small_uint
55
- end
56
-
57
- Cauterize.group(:creature) do |g|
58
- g.field :person, :person
59
- g.field :dog, :dog
60
- end
61
-
62
- @rb.build
63
- @ruby_text = File.read(@rb.rb)
64
- @ruby_lines = @ruby_text.lines.to_a
65
- end
66
-
67
- describe "header generation" do
68
- it "informs the user the code is generated" do
69
- @ruby_text.should include("generated code. Do not edit")
70
- end
71
-
72
- it "requires 'cauterize_ruby_baseclasses'" do
73
- @ruby_text.should include("require_relative './cauterize_ruby_builtins'")
74
- end
75
-
76
- it "The generated code is wrapped in a module" do
77
- @ruby_text.should include("module ExampleProject")
78
- end
79
-
80
- end
81
- end
82
- end
83
- end
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ module Cauterize
5
+ describe Cauterize::RubyBuilder do
6
+ before do
7
+ @tempdir = Dir.mktmpdir
8
+ @rb_path = File.join(@tempdir, "testing.rb")
9
+
10
+ @rb = RubyBuilder.new(@rb_path, "testing")
11
+ end
12
+
13
+ describe :build do
14
+ before do
15
+ Cauterize.set_name("example_project")
16
+ Cauterize.set_version("1.2.3")
17
+
18
+ Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
19
+
20
+ Cauterize.fixed_array(:mac_address) do |fa|
21
+ fa.array_type :small_uint
22
+ fa.array_size 6
23
+ end
24
+
25
+ Cauterize.variable_array(:mac_table) do |t|
26
+ t.array_type :mac_address
27
+ t.array_size 64
28
+ end
29
+
30
+ Cauterize.variable_array(:name) do |va|
31
+ va.array_type :small_uint
32
+ va.array_size 32
33
+ end
34
+
35
+ Cauterize.enumeration(:gender) do |e|
36
+ e.value :male
37
+ e.value :female
38
+ end
39
+
40
+ Cauterize.composite(:place) do |c|
41
+ c.field :name, :name
42
+ c.field :elevation, :uint32
43
+ end
44
+
45
+ Cauterize.composite(:person) do |c|
46
+ c.field :first_name, :name
47
+ c.field :last_name, :name
48
+ c.field :gender, :gender
49
+ end
50
+
51
+ Cauterize.composite(:dog) do |c|
52
+ c.field :name, :name
53
+ c.field :gender, :gender
54
+ c.field :leg_count, :small_uint
55
+ end
56
+
57
+ Cauterize.group(:creature) do |g|
58
+ g.field :person, :person
59
+ g.field :dog, :dog
60
+ end
61
+
62
+ @rb.build
63
+ @ruby_text = File.read(@rb.rb)
64
+ @ruby_lines = @ruby_text.lines.to_a
65
+ end
66
+
67
+ describe "header generation" do
68
+ it "informs the user the code is generated" do
69
+ @ruby_text.should include("generated code. Do not edit")
70
+ end
71
+
72
+ it "requires 'cauterize_ruby_baseclasses'" do
73
+ @ruby_text.should include("require_relative './cauterize_ruby_builtins'")
74
+ end
75
+
76
+ it "The generated code is wrapped in a module" do
77
+ @ruby_text.should include("module ExampleProject")
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,735 +1,735 @@
1
- require 'tmpdir'
2
- require 'fileutils'
3
-
4
- def range_test_type(s)
5
- ("range_test_" + s.to_s)
6
- end
7
-
8
- def integer_builtin_type_ranges
9
- [ [:uint8, 0, 2**8-1, 1],
10
- [:int8, -2**7, 2**7-1, 1],
11
- [:uint16, 0, 2**16-1, 2],
12
- [:int16, -2**15, 2**15-1, 2],
13
- [:uint32, 0, 2**32-1, 4],
14
- [:int32, -2**31, 2**31-1, 4],
15
- [:uint64, 0, 2**64-1, 8],
16
- [:int64, -2**63, 2**63-1, 8],
17
- ]
18
- end
19
-
20
- def floating_builtin_type_ranges
21
- [ [:float32, -3.4028234e38, 3.4028234e38, 4],
22
- [:float64, Float::MIN, Float::MAX, 8],
23
- ]
24
- end
25
-
26
- def integer_builtin_types
27
- integer_builtin_type_ranges.each do |t, min_val, max_val, num_bytes|
28
- t_class = ExampleProject.const_get(range_test_type(t).camel.to_sym)
29
- yield t_class, min_val, max_val, num_bytes
30
- end
31
- end
32
-
33
- def floating_builtin_types
34
- floating_builtin_type_ranges.each do |t, min_val, max_val, num_bytes|
35
- t_class = ExampleProject.const_get(range_test_type(t).camel.to_sym)
36
- yield t_class, min_val, max_val, num_bytes
37
- end
38
- end
39
-
40
- def numeric_builtin_types
41
- integer_builtin_types do |t, min_val, max_val, num_bytes|
42
- yield t, min_val, max_val, num_bytes
43
- end
44
- floating_builtin_types do |t, min_val, max_val, num_bytes|
45
- yield t, min_val, max_val, num_bytes
46
- end
47
- end
48
-
49
-
50
-
51
- Cauterize.set_name("example_project")
52
- Cauterize.set_version("1.2.3")
53
-
54
- integer_builtin_type_ranges.each do |builtin_type, min_val, max_val, num_bytes|
55
- Cauterize.scalar(range_test_type(builtin_type).to_sym) do |t|
56
- t.type_name(builtin_type)
57
- end
58
- end
59
-
60
- floating_builtin_type_ranges.each do |builtin_type, min_val, max_val, num_bytes|
61
- Cauterize.scalar(range_test_type(builtin_type).to_sym) do |t|
62
- t.type_name(builtin_type)
63
- end
64
- end
65
-
66
- Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
67
-
68
- Cauterize.scalar(:a_test_bool) {|t| t.type_name(:bool)}
69
- Cauterize.scalar(:a_test_float) {|t| t.type_name(:float32)}
70
-
71
- Cauterize.fixed_array(:simple_integer_fixed_array) do |fa|
72
- fa.array_type :uint8
73
- fa.array_size 5
74
- end
75
-
76
- Cauterize.enumeration(:color) do |e|
77
- e.value :red
78
- e.value :blue
79
- e.value :green
80
- end
81
-
82
- Cauterize.enumeration(:wacky_enum) do |e|
83
- e.value :negative, -500
84
- e.value :negative_plus_one
85
- e.value :positive, 500
86
- e.value :positive_plus_one
87
- end
88
-
89
- Cauterize.fixed_array(:mac_address) do |fa|
90
- fa.array_type :small_uint
91
- fa.array_size 6
92
- end
93
-
94
- Cauterize.variable_array(:mac_table) do |t|
95
- t.array_type :mac_address
96
- t.array_size 64
97
- end
98
-
99
- Cauterize.variable_array(:name) do |va|
100
- va.array_type :small_uint
101
- va.array_size 32
102
- end
103
-
104
- Cauterize.enumeration(:gender) do |e|
105
- e.value :male
106
- e.value :female
107
- end
108
-
109
- Cauterize.composite(:place) do |c|
110
- c.field :name, :name
111
- c.field :elevation, :uint32
112
- end
113
-
114
- Cauterize.composite(:person) do |c|
115
- c.field :first_name, :name
116
- c.field :last_name, :name
117
- c.field :gender, :gender
118
- end
119
-
120
- Cauterize.composite(:dog) do |c|
121
- c.field :name, :name
122
- c.field :gender, :gender
123
- c.field :leg_count, :small_uint
124
- end
125
-
126
- Cauterize.group(:creature) do |g|
127
- g.field :person, :person
128
- g.field :dog, :dog
129
- g.dataless :void
130
- end
131
-
132
- Dir.mktmpdir do |tmpdir|
133
- @rb_path = File.join(tmpdir, "testing.rb")
134
-
135
- # copy support files into temporary directory
136
- Dir["support/ruby/src/*"].each do |path|
137
- FileUtils.cp(path, tmpdir + "/")
138
- end
139
-
140
- @rb = Cauterize::RubyBuilder.new(@rb_path, "testing")
141
- @rb.build
142
- require @rb_path
143
-
144
- # puts File.read(@rb.rb)
145
- end
146
-
147
- # class FastName < CauterizeData
148
- # attr_reader :length
149
- # def initialize(raw_data)
150
- # @raw_data = raw_data.to_s
151
- # @length = ExampleProject::Name.size_type.construct(@raw_data.length)
152
- # raise "Invalid length: #{@raw_data.length}, max length is: #{ExampleProject::Name.max_length}" if @raw_data.length > ExampleProject::Name.max_length
153
- # end
154
-
155
- # def to_string
156
- # @raw_data
157
- # end
158
-
159
- # alias to_ruby to_string
160
- # alias pack to_string
161
-
162
- # def packio(x)
163
- # x << length.pack
164
- # x << @raw_data
165
- # end
166
-
167
- # def self.do_unpackio(x)
168
- # len = ExampleProject::Name.size_type.unpackio(x)
169
- # self.new(x.read(len.to_ruby))
170
- # end
171
- # end
172
-
173
- # ExampleProject::Name.set_specializer(FastName)
174
-
175
- module Cauterize
176
- describe Cauterize::RubyBuilder do
177
- before(:all) do
178
-
179
- end
180
-
181
- after(:all) do
182
- end
183
-
184
- describe CauterizeRuby::Scalar do
185
- numeric_builtin_types do |c, min_val, max_val, num_bytes|
186
- describe c do
187
- it "should be able to store its minimum value" do
188
- c.new(min_val).to_ruby.should == min_val
189
- end
190
- it "should be able to store its maximum value" do
191
- c.new(max_val).to_ruby.should == max_val
192
- end
193
- it "can construct from existing #{c}" do
194
- existing = c.new(max_val)
195
- c.construct(existing).to_ruby.should == max_val
196
- end
197
- it "pack and unpack should be inverses" do
198
- c.unpack(c.new(123).pack).to_ruby.should == 123
199
- end
200
- it "pack should return .num_bytes bytes" do
201
- x = c.new(123)
202
- x.pack.length.should == x.num_bytes
203
- # and for builtins these should also be the same:
204
- x.pack.length.should == c::max_size
205
- x.pack.length.should == c::min_size
206
- end
207
- it "to_i should result in an Integer" do
208
- c.new(max_val).to_i.is_a?(Integer).should be_true
209
- end
210
- it "to_f should result in a Float" do
211
- c.new(max_val).to_f.class.should == Float
212
- end
213
- it "min val should be smaller than max val" do
214
- (c.new(min_val) <=> max_val).should == -1
215
- (c.new(min_val) <=> c.new(max_val)).should == -1
216
- end
217
- it "max val should be bigger than min val" do
218
- (c.new(max_val) <=> min_val).should == 1
219
- (c.new(max_val) <=> min_val).should == 1
220
- end
221
- it "equal numbers should be equal" do
222
- (c.new(123) <=> 123).should == 0
223
- (c.new(123) <=> c.new(123)).should == 0
224
- (c.new(123) == c.new(123)).should be_true
225
- (c.new(123) == 123).should be_true
226
- (c.new(123) != 123).should be_false
227
- (c.new(123) != c.new(123)).should be_false
228
- (c.new(123) != c.new(124)).should be_true
229
- (c.new(123) != 124).should be_true
230
- end
231
- it "should raise exception if comparing to wrong cauterize type" do
232
- thing = ExampleProject::ATestBool.new(false)
233
- lambda { c.new(123) <=> thing }.should raise_error("Invalid Type: was #{ExampleProject::ATestBool}, expected #{c.name}")
234
- end
235
- it "num_bytes should match expected num_bytes" do
236
- c.new(max_val).num_bytes.should == num_bytes
237
- c::max_size.should == num_bytes
238
- c::min_size.should == num_bytes
239
- end
240
- end
241
- end
242
-
243
- integer_builtin_types do |c, min_val, max_val, num_byte|
244
- describe c do
245
- it "should not be able to store #{max_val + 1}" do
246
- lambda { c.new(max_val + 1)}.should raise_error("#{c}: Out of range value: #{max_val + 1}, for #{c}")
247
- end
248
- it "should not be able to store #{min_val - 1}" do
249
- lambda { c.new(min_val - 1)}.should raise_error("#{c}: Out of range value: #{min_val - 1}, for #{c}")
250
- end
251
- it "to_ruby and to_i and to_f should return the same number" do
252
- x = c.new(max_val)
253
- x.to_i.should == x.to_ruby
254
- x.to_f.should be_within(0.0001).of(x.to_ruby)
255
- end
256
- it "storing a float should truncate it to an Integer" do
257
- x = c.new(1.5)
258
- x.to_f.should == 1.0
259
- end
260
- it "to_ruby should result in an Integer" do
261
- c.new(max_val).to_ruby.is_a?(Integer).should be_true
262
- end
263
- end
264
- end
265
-
266
- floating_builtin_types do |c, min_val, max_val|
267
- describe c do
268
- it "to_ruby and to_f should return the same number" do
269
- x = c.new(max_val)
270
- x.to_f.should == x.to_ruby
271
- end
272
- it "to_ruby should result in a Float" do
273
- c.new(max_val).to_ruby.is_a?(Float).should be_true
274
- end
275
- it "to_i should return the same number, but truncated" do
276
- x = c.new(123.532)
277
- x.to_i.should == 123
278
- end
279
- end
280
- end
281
-
282
- describe ExampleProject::ATestFloat do
283
- max_plus = 3.402824e38
284
- it "should not be able to store #{max_plus}" do
285
- lambda { ExampleProject::ATestFloat.new(max_plus)}.should raise_error("ExampleProject::ATestFloat: Out of range value: #{max_plus}, for #{ExampleProject::ATestFloat}")
286
- end
287
- min_minus = -3.402824e38
288
- it "should not be able to store #{min_minus}" do
289
- lambda { ExampleProject::ATestFloat.new(min_minus)}.should raise_error("ExampleProject::ATestFloat: Out of range value: #{min_minus}, for #{ExampleProject::ATestFloat}")
290
- end
291
- end
292
-
293
- describe ExampleProject::ATestBool do
294
- it "pack and unpack should be inverses" do
295
- ExampleProject::ATestBool.unpack(ExampleProject::ATestBool.new(true).pack).to_ruby.should == true
296
- ExampleProject::ATestBool.unpack(ExampleProject::ATestBool.new(false).pack).to_ruby.should == false
297
- end
298
- it "should be construct from existing ATestBool" do
299
- existing = ExampleProject::ATestBool.new(false)
300
- ExampleProject::ATestBool.construct(existing).to_ruby.should == false
301
- end
302
- it "should be able to store true" do
303
- ExampleProject::ATestBool.new(true).to_ruby.should == true
304
- end
305
- it "should be able to store false" do
306
- ExampleProject::ATestBool.new(false).to_ruby.should == false
307
- end
308
- it "should convert a truthy true" do
309
- ExampleProject::ATestBool.new([]).to_ruby.should == true
310
- end
311
- it "should convert a falsy false" do
312
- ExampleProject::ATestBool.new(nil).to_ruby.should == false
313
- end
314
- it "true should equal true" do
315
- (ExampleProject::ATestBool.new(true) == true).should be_true
316
- (ExampleProject::ATestBool.new(true) != true).should be_false
317
- (ExampleProject::ATestBool.new(true) <=> true).should == 0
318
- end
319
- it "false should equal false" do
320
- (ExampleProject::ATestBool.new(false) == false).should be_true
321
- (ExampleProject::ATestBool.new(false) != false).should be_false
322
- (ExampleProject::ATestBool.new(false) <=> false).should == 0
323
- end
324
- it "false comes before than true" do
325
- (ExampleProject::ATestBool.new(false) <=> true).should == -1
326
- (ExampleProject::ATestBool.new(false) <=> ExampleProject::ATestBool.new(true)).should == -1
327
- end
328
- it "true comes after false" do
329
- (ExampleProject::ATestBool.new(true) <=> false).should == 1
330
- (ExampleProject::ATestBool.new(true) <=> ExampleProject::ATestBool.new(false)).should == 1
331
- end
332
- it "num_bytes == 1" do
333
- ExampleProject::ATestBool.new(true).pack.length.should == 1
334
- ExampleProject::ATestBool.new(true).num_bytes.should == 1
335
- ExampleProject::ATestBool::max_size.should == 1
336
- ExampleProject::ATestBool::min_size.should == 1
337
- end
338
- end
339
- end
340
-
341
- describe CauterizeRuby::Enumeration do
342
- describe ExampleProject::Color do
343
- example_colors = [:RED, :BLUE, :GREEN]
344
- example_not_colors = [:DIRT, :LASERS, :HUNGRY, 0, 1.0, true]
345
-
346
- example_colors.each_with_index do |color, i|
347
- it ".new and .to_ruby should be inverses" do
348
- ExampleProject::Color.new(color).to_ruby.should == color
349
- end
350
- it "pack and unpack should be inverses" do
351
- ExampleProject::Color.unpack(ExampleProject::Color.new(color).pack).to_ruby.should == color
352
- end
353
- it "should pack to .num_bytes bytes" do
354
- x = ExampleProject::Color.new(color)
355
- x.pack.length.should == x.num_bytes
356
- end
357
- it ".to_i should be the value index (in this case)" do
358
- ExampleProject::Color.new(color).to_i.should == i
359
- end
360
- it ".from_int and .to_i should be inverses" do
361
- ExampleProject::Color.from_int(i).to_i.should == i
362
- end
363
- end
364
- example_not_colors.each do |non_color|
365
- it "should not be able to construct from non-colors" do
366
- lambda { ExampleProject::Color.new(non_color)}.should raise_error("ExampleProject::Color: Invalid field name: #{non_color}, Valid field names are: [:RED, :BLUE, :GREEN]")
367
- end
368
- end
369
- it ".from_int should raise error on invalid input" do
370
- lambda { ExampleProject::Color.from_int(-1)}.should raise_error("ExampleProject::Color: Invalid enumeration value: -1")
371
- lambda { ExampleProject::Color.from_int(3)}.should raise_error("ExampleProject::Color: Invalid enumeration value: 3")
372
- end
373
- end
374
- describe ExampleProject::WackyEnum do
375
- example_wacky = { NEGATIVE: -500,
376
- NEGATIVE_PLUS_ONE: -499,
377
- POSITIVE: 500,
378
- POSITIVE_PLUS_ONE: 501}
379
- example_wacky.each do |k, v|
380
- it ".new and .to_ruby should be inverses" do
381
- ExampleProject::WackyEnum.new(k).to_ruby.should == k
382
- end
383
- it "pack and unpack should be inverses" do
384
- ExampleProject::WackyEnum.unpack(ExampleProject::WackyEnum.new(k).pack).to_ruby.should == k
385
- end
386
- it "should pack to .num_bytes bytes" do
387
- x = ExampleProject::WackyEnum.new(k)
388
- x.pack.length.should == x.num_bytes
389
- end
390
- it "max_size == min_size should be 2 for our wacky enum" do
391
- ExampleProject::WackyEnum::max_size.should == 2
392
- ExampleProject::WackyEnum::min_size.should == 2
393
- end
394
- it ".to_i should be the enum value" do
395
- ExampleProject::WackyEnum.new(k).to_i.should == v
396
- end
397
- it ".from_int and .to_i should be inverses" do
398
- ExampleProject::WackyEnum.from_int(v).to_i.should == v
399
- end
400
- end
401
- it ".from_int should raise error on invalid input" do
402
- lambda { ExampleProject::WackyEnum.from_int(0)}.should raise_error("ExampleProject::WackyEnum: Invalid enumeration value: 0")
403
- lambda { ExampleProject::WackyEnum.from_int(-498)}.should raise_error("ExampleProject::WackyEnum: Invalid enumeration value: -498")
404
- end
405
- describe "#<=>" do
406
- example_wacky.each do |k1, v1|
407
- example_wacky.each do |k2, v2|
408
- it "should have the same ordering as the enum values" do
409
- (ExampleProject::WackyEnum.new(k1) <=> ExampleProject::WackyEnum.new(k2)).should == (v1 <=> v2)
410
- end
411
- end
412
- end
413
- end
414
- describe "#==" do
415
- example_wacky.each do |k1, v1|
416
- example_wacky.each do |k2, v2|
417
- it "should be equal if enum values are equal" do
418
- if v1 == v2
419
- (ExampleProject::WackyEnum.new(k1) == ExampleProject::WackyEnum.new(k2)).should be_true
420
- (ExampleProject::WackyEnum.new(k1) != ExampleProject::WackyEnum.new(k2)).should be_false
421
- else
422
- (ExampleProject::WackyEnum.new(k1) == ExampleProject::WackyEnum.new(k2)).should be_false
423
- (ExampleProject::WackyEnum.new(k1) != ExampleProject::WackyEnum.new(k2)).should be_true
424
- end
425
- end
426
- end
427
- end
428
- end
429
- end
430
- describe "#<=>" do
431
- it "should automatically promote a symbol when compared against" do
432
- (ExampleProject::Color.new(:RED) <=> :RED).should == 0
433
- (ExampleProject::Color.new(:RED) <=> :BLUE).should == -1
434
- (ExampleProject::Color.new(:BLUE) <=> :RED).should == 1
435
- end
436
- it "should raise a type error if compared against a non-symbol or value of different type" do
437
- lambda { (ExampleProject::Color.new(:RED) <=> 0) }.should raise_error("ExampleProject::Color: Invalid field name: 0, Valid field names are: [:RED, :BLUE, :GREEN]")
438
- lambda { (ExampleProject::Color.new(:RED) <=> :ORANGE) }.should raise_error("ExampleProject::Color: Invalid field name: ORANGE, Valid field names are: [:RED, :BLUE, :GREEN]")
439
- lambda { (ExampleProject::WackyEnum.new(:NEGATIVE) <=> ExampleProject::Color.new(:RED)) }.should raise_error("Invalid Type: was ExampleProject::Color, expected ExampleProject::WackyEnum")
440
- end
441
- end
442
- end
443
-
444
- describe CauterizeRuby::VariableArray do
445
- it "can be constructed from an existing variable array of same type" do
446
- test_string = "this is a test"
447
- test_array = ExampleProject::Name.new(test_string.bytes)
448
- new_array = ExampleProject::Name.new(test_array)
449
- new_array.to_ruby.should == test_string.bytes.to_a
450
- end
451
-
452
- it "can pack, and unpack to its original value" do
453
- test_string = "this is a test"
454
- ExampleProject::Name.unpack(ExampleProject::Name.new(test_string.bytes).pack).to_string.should == test_string
455
- end
456
- end
457
-
458
- describe CauterizeRuby::FixedArray do
459
-
460
- it ".new and .to_ruby are inverses" do
461
- test_array = [1, 2, 6, 10, 100, 0]
462
- ExampleProject::MacAddress.new(test_array).to_ruby.should == test_array
463
- end
464
-
465
- it "can pack, and unpack to its original value" do
466
- test_array = [1, 2, 6, 10, 100, 0]
467
- ExampleProject::MacAddress.unpack(ExampleProject::MacAddress.new(test_array).pack).to_ruby.should == test_array
468
- end
469
-
470
- it "should pack to .num_bytes bytes" do
471
- test_array = [1, 2, 6, 10, 100, 0]
472
- x = ExampleProject::MacAddress.new(test_array)
473
- x.pack.length.should == x.num_bytes
474
- end
475
-
476
- it "raises an exception if the array length is wrong" do
477
- lambda { ExampleProject::MacAddress.new([1, 2, 6, 10, 100]) }.should raise_error("ExampleProject::MacAddress: Invalid length: 5, expected: 6")
478
- lambda { ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 1, 12]) }.should raise_error("ExampleProject::MacAddress: Invalid length: 7, expected: 6")
479
- end
480
-
481
- it "it has a valid enumerator interface" do
482
- test_array = [1, 2, 6, 10, 100, 0]
483
- x = ExampleProject::MacAddress.new(test_array)
484
- x.to_enum.to_a.should == test_array
485
- x.min.should == 0
486
- x.max.should == 100
487
- x.sort.should == test_array.sort
488
- end
489
-
490
- it "can be converted to a string if elements are in the right range" do
491
- test_array = "foobar".bytes
492
- ExampleProject::MacAddress.new(test_array).to_string.should == "foobar"
493
- ExampleProject::MacAddress.new("asdfas").to_string.should == "asdfas"
494
- end
495
-
496
- describe "#==" do
497
- it "should return true for equal arrays" do
498
- (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) == [1, 2, 6, 10, 100, 0]).should be_true
499
- (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) == ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_true
500
- (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) != ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_false
501
- end
502
- it "should return false for non-equal arrays" do
503
- (ExampleProject::MacAddress.new([1, 2, 10, 100, 5, 0]) == ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_false
504
- (ExampleProject::MacAddress.new([1, 2, 10, 100, 5, 0]) != ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_true
505
- end
506
- end
507
-
508
- # describe "#num_bytes" do
509
- # it "should match "
510
- # end
511
-
512
- it "num_bytes == max_size == min_size but _just for this particular fixed array_" do
513
- test_array = [1, 2, 6, 10, 100, 0]
514
- ExampleProject::MacAddress.new(test_array).num_bytes.should == 6
515
- ExampleProject::MacAddress::max_size.should == 6
516
- ExampleProject::MacAddress::min_size.should == 6
517
- end
518
-
519
- describe "#<=>" do
520
- sample_arrays = [[0, 0, 0, 0, 1, 2],
521
- [0, 1, 0, 0, 0, 0],
522
- [0, 1, 0, 1, 0, 0]]
523
- sample_arrays.each_with_index do |arr_a, i|
524
- sample_arrays.each_with_index do |arr_b, j|
525
- it "should give a lexicographical ordering" do
526
- (ExampleProject::MacAddress.new(arr_a) <=> ExampleProject::MacAddress.new(arr_b)).should == (i <=> j)
527
- end
528
- end
529
- end
530
- end
531
- end
532
-
533
- describe CauterizeRuby::VariableArray do
534
-
535
- test_arrays = ["Job Vranish".bytes.to_a,
536
- [] ]
537
- test_arrays.each do |test_array|
538
- it ".new and .to_ruby are inverses" do
539
- ExampleProject::Name.new(test_array).to_ruby.should == test_array
540
- end
541
-
542
- it "can pack, and unpack to its original value" do
543
- ExampleProject::Name.unpack(ExampleProject::Name.new(test_array).pack).to_ruby.should == test_array
544
- end
545
-
546
- it "should pack to .num_bytes bytes" do
547
- x = ExampleProject::Name.new(test_array)
548
- x.pack.length.should == x.num_bytes
549
- end
550
- end
551
-
552
- it "raises an exception if the array it too long" do
553
- lambda { ExampleProject::Name.new([1] * 33) }.should raise_error("ExampleProject::Name: Invalid length: 33, max length is: 32")
554
- end
555
-
556
- it "it has a valid enumerator interface" do
557
- test_array = [1, 2, 6, 10, 100, 0]
558
- x = ExampleProject::Name.new(test_array)
559
- x.to_enum.to_a.should == test_array
560
- x.min.should == 0
561
- x.max.should == 100
562
- x.sort.should == test_array.sort
563
- end
564
-
565
- it "can be converted to a string if elements are in the right range" do
566
- test_array = "Job Vranish".bytes
567
- ExampleProject::Name.new(test_array).to_string.should == "Job Vranish"
568
- ExampleProject::Name.new("asdfas").to_string.should == "asdfas"
569
- ExampleProject::Name.new("").to_string.should == ""
570
- end
571
-
572
- it "min size is just the size of the size type, max size is max size of all elements" do
573
- c = ExampleProject::Name
574
- test_array = [1, 2, 6, 10, 100, 0]
575
- c.new(test_array).num_bytes.should == 7
576
- c::max_size.should == c::size_type::max_size + (c::elem_type::max_size * c::max_length)
577
- c::min_size.should == c::size_type::min_size
578
- end
579
-
580
- describe "#==" do
581
- it "should return true for equal arrays" do
582
- (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) == [1, 2, 6, 10, 100, 0]).should be_true
583
- (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) == ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_true
584
- (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) != ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_false
585
- end
586
- it "should return false for non-equal arrays" do
587
- (ExampleProject::Name.new([1, 2, 10, 100, 0]) == ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_false
588
- (ExampleProject::Name.new([1, 2, 10, 100, 0]) != ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_true
589
- end
590
- end
591
-
592
- describe "#<=>" do
593
- sample_arrays = [[0, 1, 2],
594
- [0, 2, 2],
595
- [1, 0, 0]]
596
- sample_arrays.each_with_index do |arr_a, i|
597
- sample_arrays.each_with_index do |arr_b, j|
598
- it "should give a lexicographical ordering" do
599
- (ExampleProject::Name.new(arr_a) <=> ExampleProject::Name.new(arr_b)).should == (i <=> j)
600
- end
601
- end
602
- end
603
- end
604
-
605
- end
606
-
607
- describe CauterizeRuby::Composite do
608
- sample_persons = [ { first_name: "Jane", last_name: "Smith", gender: :MALE },
609
- { first_name: "Jill", last_name: "Smith", gender: :FEMALE},
610
- { first_name: "John", last_name: "Smit", gender: :MALE },
611
- { first_name: "John", last_name: "Smith", gender: :MALE }]
612
-
613
- sample_persons.each do |p|
614
- it ".new and .to_ruby are inverses" do
615
- ExampleProject::Person.new(p).should == p
616
- end
617
- it "can pack, and unpack to its original value" do
618
- ExampleProject::Person.unpack((ExampleProject::Person.new(p).pack)).should == p
619
- end
620
- it "pack should pack to .num_bytes bytes" do
621
- x = ExampleProject::Person.new(p)
622
- x.pack.length.should == x.num_bytes
623
- end
624
- end
625
-
626
- it "should raise an error if fields are missing during initialization" do
627
- lambda { ExampleProject::Person.new({ last_name: "Smith", gender: :MALE }) }.should raise_error("ExampleProject::Person: Invalid initialization params, missing fields: [:first_name], extra fields: []")
628
- end
629
-
630
- it "should raise an error if there are extra fields during initialization" do
631
- lambda { ExampleProject::Person.new({ first_name: "Jane", last_name: "Smith", age: 12, gender: :MALE }) }.should raise_error("ExampleProject::Person: Invalid initialization params, missing fields: [], extra fields: [:age]")
632
- end
633
-
634
- describe "#max_size" do
635
- it "should be the sum of the max sizes of the fields" do
636
- c = ExampleProject::Person
637
- c::max_size.should == c::fields.values.reduce(0) {|sum, v| sum + v::max_size}
638
- end
639
- end
640
- describe "#min_size" do
641
- it "should be the sum of the min sizes of the fields" do
642
- c = ExampleProject::Person
643
- c::min_size.should == c::fields.values.reduce(0) {|sum, v| sum + v::min_size}
644
- end
645
- end
646
-
647
- it "should have accessors for all the fields" do
648
- test_person = ExampleProject::Person.new({ first_name: "test", last_name: "thing", gender: :MALE})
649
- test_person.first_name.should == "test"
650
- test_person.last_name.should == "thing"
651
- test_person.gender.should == :MALE
652
- end
653
-
654
- describe "#<=>" do
655
- sample_persons.each_with_index do |p1, i|
656
- sample_persons.each_with_index do |p2, j|
657
- it "should give a lexicographical ordering" do
658
- (ExampleProject::Person.new(p1) <=> p2).should == (i <=> j)
659
- (ExampleProject::Person.new(p1) <=> ExampleProject::Person.new(p2)).should == (i <=> j)
660
- end
661
- end
662
- end
663
- end
664
-
665
- describe "#==" do
666
- sample_persons.each_with_index do |p1, i|
667
- sample_persons.each_with_index do |p2, j|
668
- it "should return true for equal things, false otherwise" do
669
- (ExampleProject::Person.new(p1) == p2).should == (i == j)
670
- (ExampleProject::Person.new(p1) == ExampleProject::Person.new(p2)).should == (i == j)
671
- (ExampleProject::Person.new(p1) != p2).should == (i != j)
672
- (ExampleProject::Person.new(p1) != ExampleProject::Person.new(p2)).should == (i != j)
673
- end
674
- end
675
- end
676
- end
677
- end
678
-
679
- describe CauterizeRuby::Group do
680
- test_groups = [ { tag: :PERSON, data: { first_name: "Jane", last_name: "Smith", gender: :MALE }},
681
- { tag: :DOG, data: { name: "Fido", leg_count: 3, gender: :MALE }}]
682
-
683
- test_groups.each do |g|
684
- it ".new and .to_ruby are inverses" do
685
- ExampleProject::Creature.new(g).should == g
686
- end
687
- it "can pack, and unpack to its original value" do
688
- ExampleProject::Creature.unpack((ExampleProject::Creature.new(g).pack)).should == g
689
- end
690
- it "pack should pack to .num_bytes bytes" do
691
- x = ExampleProject::Creature.new(g)
692
- x.pack.length.should == x.num_bytes
693
- end
694
- end
695
-
696
- describe "#max_size" do
697
- it "should be the max size of the tag + max of the max sizes of the fields" do
698
- c = ExampleProject::Creature
699
- c::max_size.should == c::tag_type::max_size + c::fields.values.map{|v| (v.nil?) ? 0 : v::max_size}.max
700
- end
701
- end
702
-
703
- describe "#min_size" do
704
- it "should be the max size of the tag + min of the min sizes of the fields" do
705
- c = ExampleProject::Creature
706
- c::min_size.should == c::tag_type::min_size + c::fields.values.map{|v| (v.nil?) ? 0 : v::min_size}.min
707
- end
708
- end
709
-
710
- describe "#<=>" do
711
- test_groups.each_with_index do |a, i|
712
- test_groups.each_with_index do |b, j|
713
- it "should give a lexicographical ordering" do
714
- (ExampleProject::Creature.new(a) <=> b).should == (i <=> j)
715
- (ExampleProject::Creature.new(a) <=> ExampleProject::Creature.new(b)).should == (i <=> j)
716
- end
717
- end
718
- end
719
- end
720
-
721
- describe "#==" do
722
- test_groups.each_with_index do |a, i|
723
- test_groups.each_with_index do |b, j|
724
- it "should return true for equal things, false otherwise" do
725
- (ExampleProject::Creature.new(a) == b).should == (i == j)
726
- (ExampleProject::Creature.new(a) == ExampleProject::Creature.new(b)).should == (i == j)
727
- (ExampleProject::Creature.new(a) != b).should == (i != j)
728
- (ExampleProject::Creature.new(a) != ExampleProject::Creature.new(b)).should == (i != j)
729
- end
730
- end
731
- end
732
- end
733
- end
734
- end
735
- end
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ def range_test_type(s)
5
+ ("range_test_" + s.to_s)
6
+ end
7
+
8
+ def integer_builtin_type_ranges
9
+ [ [:uint8, 0, 2**8-1, 1],
10
+ [:int8, -2**7, 2**7-1, 1],
11
+ [:uint16, 0, 2**16-1, 2],
12
+ [:int16, -2**15, 2**15-1, 2],
13
+ [:uint32, 0, 2**32-1, 4],
14
+ [:int32, -2**31, 2**31-1, 4],
15
+ [:uint64, 0, 2**64-1, 8],
16
+ [:int64, -2**63, 2**63-1, 8],
17
+ ]
18
+ end
19
+
20
+ def floating_builtin_type_ranges
21
+ [ [:float32, -3.4028234e38, 3.4028234e38, 4],
22
+ [:float64, Float::MIN, Float::MAX, 8],
23
+ ]
24
+ end
25
+
26
+ def integer_builtin_types
27
+ integer_builtin_type_ranges.each do |t, min_val, max_val, num_bytes|
28
+ t_class = ExampleProject.const_get(range_test_type(t).camel.to_sym)
29
+ yield t_class, min_val, max_val, num_bytes
30
+ end
31
+ end
32
+
33
+ def floating_builtin_types
34
+ floating_builtin_type_ranges.each do |t, min_val, max_val, num_bytes|
35
+ t_class = ExampleProject.const_get(range_test_type(t).camel.to_sym)
36
+ yield t_class, min_val, max_val, num_bytes
37
+ end
38
+ end
39
+
40
+ def numeric_builtin_types
41
+ integer_builtin_types do |t, min_val, max_val, num_bytes|
42
+ yield t, min_val, max_val, num_bytes
43
+ end
44
+ floating_builtin_types do |t, min_val, max_val, num_bytes|
45
+ yield t, min_val, max_val, num_bytes
46
+ end
47
+ end
48
+
49
+
50
+
51
+ Cauterize.set_name("example_project")
52
+ Cauterize.set_version("1.2.3")
53
+
54
+ integer_builtin_type_ranges.each do |builtin_type, min_val, max_val, num_bytes|
55
+ Cauterize.scalar(range_test_type(builtin_type).to_sym) do |t|
56
+ t.type_name(builtin_type)
57
+ end
58
+ end
59
+
60
+ floating_builtin_type_ranges.each do |builtin_type, min_val, max_val, num_bytes|
61
+ Cauterize.scalar(range_test_type(builtin_type).to_sym) do |t|
62
+ t.type_name(builtin_type)
63
+ end
64
+ end
65
+
66
+ Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
67
+
68
+ Cauterize.scalar(:a_test_bool) {|t| t.type_name(:bool)}
69
+ Cauterize.scalar(:a_test_float) {|t| t.type_name(:float32)}
70
+
71
+ Cauterize.fixed_array(:simple_integer_fixed_array) do |fa|
72
+ fa.array_type :uint8
73
+ fa.array_size 5
74
+ end
75
+
76
+ Cauterize.enumeration(:color) do |e|
77
+ e.value :red
78
+ e.value :blue
79
+ e.value :green
80
+ end
81
+
82
+ Cauterize.enumeration(:wacky_enum) do |e|
83
+ e.value :negative, -500
84
+ e.value :negative_plus_one
85
+ e.value :positive, 500
86
+ e.value :positive_plus_one
87
+ end
88
+
89
+ Cauterize.fixed_array(:mac_address) do |fa|
90
+ fa.array_type :small_uint
91
+ fa.array_size 6
92
+ end
93
+
94
+ Cauterize.variable_array(:mac_table) do |t|
95
+ t.array_type :mac_address
96
+ t.array_size 64
97
+ end
98
+
99
+ Cauterize.variable_array(:name) do |va|
100
+ va.array_type :small_uint
101
+ va.array_size 32
102
+ end
103
+
104
+ Cauterize.enumeration(:gender) do |e|
105
+ e.value :male
106
+ e.value :female
107
+ end
108
+
109
+ Cauterize.composite(:place) do |c|
110
+ c.field :name, :name
111
+ c.field :elevation, :uint32
112
+ end
113
+
114
+ Cauterize.composite(:person) do |c|
115
+ c.field :first_name, :name
116
+ c.field :last_name, :name
117
+ c.field :gender, :gender
118
+ end
119
+
120
+ Cauterize.composite(:dog) do |c|
121
+ c.field :name, :name
122
+ c.field :gender, :gender
123
+ c.field :leg_count, :small_uint
124
+ end
125
+
126
+ Cauterize.group(:creature) do |g|
127
+ g.field :person, :person
128
+ g.field :dog, :dog
129
+ g.dataless :void
130
+ end
131
+
132
+ Dir.mktmpdir do |tmpdir|
133
+ @rb_path = File.join(tmpdir, "testing.rb")
134
+
135
+ # copy support files into temporary directory
136
+ Dir["support/ruby/src/*"].each do |path|
137
+ FileUtils.cp(path, tmpdir + "/")
138
+ end
139
+
140
+ @rb = Cauterize::RubyBuilder.new(@rb_path, "testing")
141
+ @rb.build
142
+ require @rb_path
143
+
144
+ # puts File.read(@rb.rb)
145
+ end
146
+
147
+ # class FastName < CauterizeData
148
+ # attr_reader :length
149
+ # def initialize(raw_data)
150
+ # @raw_data = raw_data.to_s
151
+ # @length = ExampleProject::Name.size_type.construct(@raw_data.length)
152
+ # raise "Invalid length: #{@raw_data.length}, max length is: #{ExampleProject::Name.max_length}" if @raw_data.length > ExampleProject::Name.max_length
153
+ # end
154
+
155
+ # def to_string
156
+ # @raw_data
157
+ # end
158
+
159
+ # alias to_ruby to_string
160
+ # alias pack to_string
161
+
162
+ # def packio(x)
163
+ # x << length.pack
164
+ # x << @raw_data
165
+ # end
166
+
167
+ # def self.do_unpackio(x)
168
+ # len = ExampleProject::Name.size_type.unpackio(x)
169
+ # self.new(x.read(len.to_ruby))
170
+ # end
171
+ # end
172
+
173
+ # ExampleProject::Name.set_specializer(FastName)
174
+
175
+ module Cauterize
176
+ describe Cauterize::RubyBuilder do
177
+ before(:all) do
178
+
179
+ end
180
+
181
+ after(:all) do
182
+ end
183
+
184
+ describe CauterizeRuby::Scalar do
185
+ numeric_builtin_types do |c, min_val, max_val, num_bytes|
186
+ describe c do
187
+ it "should be able to store its minimum value" do
188
+ c.new(min_val).to_ruby.should == min_val
189
+ end
190
+ it "should be able to store its maximum value" do
191
+ c.new(max_val).to_ruby.should == max_val
192
+ end
193
+ it "can construct from existing #{c}" do
194
+ existing = c.new(max_val)
195
+ c.construct(existing).to_ruby.should == max_val
196
+ end
197
+ it "pack and unpack should be inverses" do
198
+ c.unpack(c.new(123).pack).to_ruby.should == 123
199
+ end
200
+ it "pack should return .num_bytes bytes" do
201
+ x = c.new(123)
202
+ x.pack.length.should == x.num_bytes
203
+ # and for builtins these should also be the same:
204
+ x.pack.length.should == c::max_size
205
+ x.pack.length.should == c::min_size
206
+ end
207
+ it "to_i should result in an Integer" do
208
+ c.new(max_val).to_i.is_a?(Integer).should be_true
209
+ end
210
+ it "to_f should result in a Float" do
211
+ c.new(max_val).to_f.class.should == Float
212
+ end
213
+ it "min val should be smaller than max val" do
214
+ (c.new(min_val) <=> max_val).should == -1
215
+ (c.new(min_val) <=> c.new(max_val)).should == -1
216
+ end
217
+ it "max val should be bigger than min val" do
218
+ (c.new(max_val) <=> min_val).should == 1
219
+ (c.new(max_val) <=> min_val).should == 1
220
+ end
221
+ it "equal numbers should be equal" do
222
+ (c.new(123) <=> 123).should == 0
223
+ (c.new(123) <=> c.new(123)).should == 0
224
+ (c.new(123) == c.new(123)).should be_true
225
+ (c.new(123) == 123).should be_true
226
+ (c.new(123) != 123).should be_false
227
+ (c.new(123) != c.new(123)).should be_false
228
+ (c.new(123) != c.new(124)).should be_true
229
+ (c.new(123) != 124).should be_true
230
+ end
231
+ it "should raise exception if comparing to wrong cauterize type" do
232
+ thing = ExampleProject::ATestBool.new(false)
233
+ lambda { c.new(123) <=> thing }.should raise_error("Invalid Type: was #{ExampleProject::ATestBool}, expected #{c.name}")
234
+ end
235
+ it "num_bytes should match expected num_bytes" do
236
+ c.new(max_val).num_bytes.should == num_bytes
237
+ c::max_size.should == num_bytes
238
+ c::min_size.should == num_bytes
239
+ end
240
+ end
241
+ end
242
+
243
+ integer_builtin_types do |c, min_val, max_val, num_byte|
244
+ describe c do
245
+ it "should not be able to store #{max_val + 1}" do
246
+ lambda { c.new(max_val + 1)}.should raise_error("#{c}: Out of range value: #{max_val + 1}, for #{c}")
247
+ end
248
+ it "should not be able to store #{min_val - 1}" do
249
+ lambda { c.new(min_val - 1)}.should raise_error("#{c}: Out of range value: #{min_val - 1}, for #{c}")
250
+ end
251
+ it "to_ruby and to_i and to_f should return the same number" do
252
+ x = c.new(max_val)
253
+ x.to_i.should == x.to_ruby
254
+ x.to_f.should be_within(0.0001).of(x.to_ruby)
255
+ end
256
+ it "storing a float should truncate it to an Integer" do
257
+ x = c.new(1.5)
258
+ x.to_f.should == 1.0
259
+ end
260
+ it "to_ruby should result in an Integer" do
261
+ c.new(max_val).to_ruby.is_a?(Integer).should be_true
262
+ end
263
+ end
264
+ end
265
+
266
+ floating_builtin_types do |c, min_val, max_val|
267
+ describe c do
268
+ it "to_ruby and to_f should return the same number" do
269
+ x = c.new(max_val)
270
+ x.to_f.should == x.to_ruby
271
+ end
272
+ it "to_ruby should result in a Float" do
273
+ c.new(max_val).to_ruby.is_a?(Float).should be_true
274
+ end
275
+ it "to_i should return the same number, but truncated" do
276
+ x = c.new(123.532)
277
+ x.to_i.should == 123
278
+ end
279
+ end
280
+ end
281
+
282
+ describe ExampleProject::ATestFloat do
283
+ max_plus = 3.402824e38
284
+ it "should not be able to store #{max_plus}" do
285
+ lambda { ExampleProject::ATestFloat.new(max_plus)}.should raise_error("ExampleProject::ATestFloat: Out of range value: #{max_plus}, for #{ExampleProject::ATestFloat}")
286
+ end
287
+ min_minus = -3.402824e38
288
+ it "should not be able to store #{min_minus}" do
289
+ lambda { ExampleProject::ATestFloat.new(min_minus)}.should raise_error("ExampleProject::ATestFloat: Out of range value: #{min_minus}, for #{ExampleProject::ATestFloat}")
290
+ end
291
+ end
292
+
293
+ describe ExampleProject::ATestBool do
294
+ it "pack and unpack should be inverses" do
295
+ ExampleProject::ATestBool.unpack(ExampleProject::ATestBool.new(true).pack).to_ruby.should == true
296
+ ExampleProject::ATestBool.unpack(ExampleProject::ATestBool.new(false).pack).to_ruby.should == false
297
+ end
298
+ it "should be construct from existing ATestBool" do
299
+ existing = ExampleProject::ATestBool.new(false)
300
+ ExampleProject::ATestBool.construct(existing).to_ruby.should == false
301
+ end
302
+ it "should be able to store true" do
303
+ ExampleProject::ATestBool.new(true).to_ruby.should == true
304
+ end
305
+ it "should be able to store false" do
306
+ ExampleProject::ATestBool.new(false).to_ruby.should == false
307
+ end
308
+ it "should convert a truthy true" do
309
+ ExampleProject::ATestBool.new([]).to_ruby.should == true
310
+ end
311
+ it "should convert a falsy false" do
312
+ ExampleProject::ATestBool.new(nil).to_ruby.should == false
313
+ end
314
+ it "true should equal true" do
315
+ (ExampleProject::ATestBool.new(true) == true).should be_true
316
+ (ExampleProject::ATestBool.new(true) != true).should be_false
317
+ (ExampleProject::ATestBool.new(true) <=> true).should == 0
318
+ end
319
+ it "false should equal false" do
320
+ (ExampleProject::ATestBool.new(false) == false).should be_true
321
+ (ExampleProject::ATestBool.new(false) != false).should be_false
322
+ (ExampleProject::ATestBool.new(false) <=> false).should == 0
323
+ end
324
+ it "false comes before than true" do
325
+ (ExampleProject::ATestBool.new(false) <=> true).should == -1
326
+ (ExampleProject::ATestBool.new(false) <=> ExampleProject::ATestBool.new(true)).should == -1
327
+ end
328
+ it "true comes after false" do
329
+ (ExampleProject::ATestBool.new(true) <=> false).should == 1
330
+ (ExampleProject::ATestBool.new(true) <=> ExampleProject::ATestBool.new(false)).should == 1
331
+ end
332
+ it "num_bytes == 1" do
333
+ ExampleProject::ATestBool.new(true).pack.length.should == 1
334
+ ExampleProject::ATestBool.new(true).num_bytes.should == 1
335
+ ExampleProject::ATestBool::max_size.should == 1
336
+ ExampleProject::ATestBool::min_size.should == 1
337
+ end
338
+ end
339
+ end
340
+
341
+ describe CauterizeRuby::Enumeration do
342
+ describe ExampleProject::Color do
343
+ example_colors = [:RED, :BLUE, :GREEN]
344
+ example_not_colors = [:DIRT, :LASERS, :HUNGRY, 0, 1.0, true]
345
+
346
+ example_colors.each_with_index do |color, i|
347
+ it ".new and .to_ruby should be inverses" do
348
+ ExampleProject::Color.new(color).to_ruby.should == color
349
+ end
350
+ it "pack and unpack should be inverses" do
351
+ ExampleProject::Color.unpack(ExampleProject::Color.new(color).pack).to_ruby.should == color
352
+ end
353
+ it "should pack to .num_bytes bytes" do
354
+ x = ExampleProject::Color.new(color)
355
+ x.pack.length.should == x.num_bytes
356
+ end
357
+ it ".to_i should be the value index (in this case)" do
358
+ ExampleProject::Color.new(color).to_i.should == i
359
+ end
360
+ it ".from_int and .to_i should be inverses" do
361
+ ExampleProject::Color.from_int(i).to_i.should == i
362
+ end
363
+ end
364
+ example_not_colors.each do |non_color|
365
+ it "should not be able to construct from non-colors" do
366
+ lambda { ExampleProject::Color.new(non_color)}.should raise_error("ExampleProject::Color: Invalid field name: #{non_color}, Valid field names are: [:RED, :BLUE, :GREEN]")
367
+ end
368
+ end
369
+ it ".from_int should raise error on invalid input" do
370
+ lambda { ExampleProject::Color.from_int(-1)}.should raise_error("ExampleProject::Color: Invalid enumeration value: -1")
371
+ lambda { ExampleProject::Color.from_int(3)}.should raise_error("ExampleProject::Color: Invalid enumeration value: 3")
372
+ end
373
+ end
374
+ describe ExampleProject::WackyEnum do
375
+ example_wacky = { NEGATIVE: -500,
376
+ NEGATIVE_PLUS_ONE: -499,
377
+ POSITIVE: 500,
378
+ POSITIVE_PLUS_ONE: 501}
379
+ example_wacky.each do |k, v|
380
+ it ".new and .to_ruby should be inverses" do
381
+ ExampleProject::WackyEnum.new(k).to_ruby.should == k
382
+ end
383
+ it "pack and unpack should be inverses" do
384
+ ExampleProject::WackyEnum.unpack(ExampleProject::WackyEnum.new(k).pack).to_ruby.should == k
385
+ end
386
+ it "should pack to .num_bytes bytes" do
387
+ x = ExampleProject::WackyEnum.new(k)
388
+ x.pack.length.should == x.num_bytes
389
+ end
390
+ it "max_size == min_size should be 2 for our wacky enum" do
391
+ ExampleProject::WackyEnum::max_size.should == 2
392
+ ExampleProject::WackyEnum::min_size.should == 2
393
+ end
394
+ it ".to_i should be the enum value" do
395
+ ExampleProject::WackyEnum.new(k).to_i.should == v
396
+ end
397
+ it ".from_int and .to_i should be inverses" do
398
+ ExampleProject::WackyEnum.from_int(v).to_i.should == v
399
+ end
400
+ end
401
+ it ".from_int should raise error on invalid input" do
402
+ lambda { ExampleProject::WackyEnum.from_int(0)}.should raise_error("ExampleProject::WackyEnum: Invalid enumeration value: 0")
403
+ lambda { ExampleProject::WackyEnum.from_int(-498)}.should raise_error("ExampleProject::WackyEnum: Invalid enumeration value: -498")
404
+ end
405
+ describe "#<=>" do
406
+ example_wacky.each do |k1, v1|
407
+ example_wacky.each do |k2, v2|
408
+ it "should have the same ordering as the enum values" do
409
+ (ExampleProject::WackyEnum.new(k1) <=> ExampleProject::WackyEnum.new(k2)).should == (v1 <=> v2)
410
+ end
411
+ end
412
+ end
413
+ end
414
+ describe "#==" do
415
+ example_wacky.each do |k1, v1|
416
+ example_wacky.each do |k2, v2|
417
+ it "should be equal if enum values are equal" do
418
+ if v1 == v2
419
+ (ExampleProject::WackyEnum.new(k1) == ExampleProject::WackyEnum.new(k2)).should be_true
420
+ (ExampleProject::WackyEnum.new(k1) != ExampleProject::WackyEnum.new(k2)).should be_false
421
+ else
422
+ (ExampleProject::WackyEnum.new(k1) == ExampleProject::WackyEnum.new(k2)).should be_false
423
+ (ExampleProject::WackyEnum.new(k1) != ExampleProject::WackyEnum.new(k2)).should be_true
424
+ end
425
+ end
426
+ end
427
+ end
428
+ end
429
+ end
430
+ describe "#<=>" do
431
+ it "should automatically promote a symbol when compared against" do
432
+ (ExampleProject::Color.new(:RED) <=> :RED).should == 0
433
+ (ExampleProject::Color.new(:RED) <=> :BLUE).should == -1
434
+ (ExampleProject::Color.new(:BLUE) <=> :RED).should == 1
435
+ end
436
+ it "should raise a type error if compared against a non-symbol or value of different type" do
437
+ lambda { (ExampleProject::Color.new(:RED) <=> 0) }.should raise_error("ExampleProject::Color: Invalid field name: 0, Valid field names are: [:RED, :BLUE, :GREEN]")
438
+ lambda { (ExampleProject::Color.new(:RED) <=> :ORANGE) }.should raise_error("ExampleProject::Color: Invalid field name: ORANGE, Valid field names are: [:RED, :BLUE, :GREEN]")
439
+ lambda { (ExampleProject::WackyEnum.new(:NEGATIVE) <=> ExampleProject::Color.new(:RED)) }.should raise_error("Invalid Type: was ExampleProject::Color, expected ExampleProject::WackyEnum")
440
+ end
441
+ end
442
+ end
443
+
444
+ describe CauterizeRuby::VariableArray do
445
+ it "can be constructed from an existing variable array of same type" do
446
+ test_string = "this is a test"
447
+ test_array = ExampleProject::Name.new(test_string.bytes)
448
+ new_array = ExampleProject::Name.new(test_array)
449
+ new_array.to_ruby.should == test_string.bytes.to_a
450
+ end
451
+
452
+ it "can pack, and unpack to its original value" do
453
+ test_string = "this is a test"
454
+ ExampleProject::Name.unpack(ExampleProject::Name.new(test_string.bytes).pack).to_string.should == test_string
455
+ end
456
+ end
457
+
458
+ describe CauterizeRuby::FixedArray do
459
+
460
+ it ".new and .to_ruby are inverses" do
461
+ test_array = [1, 2, 6, 10, 100, 0]
462
+ ExampleProject::MacAddress.new(test_array).to_ruby.should == test_array
463
+ end
464
+
465
+ it "can pack, and unpack to its original value" do
466
+ test_array = [1, 2, 6, 10, 100, 0]
467
+ ExampleProject::MacAddress.unpack(ExampleProject::MacAddress.new(test_array).pack).to_ruby.should == test_array
468
+ end
469
+
470
+ it "should pack to .num_bytes bytes" do
471
+ test_array = [1, 2, 6, 10, 100, 0]
472
+ x = ExampleProject::MacAddress.new(test_array)
473
+ x.pack.length.should == x.num_bytes
474
+ end
475
+
476
+ it "raises an exception if the array length is wrong" do
477
+ lambda { ExampleProject::MacAddress.new([1, 2, 6, 10, 100]) }.should raise_error("ExampleProject::MacAddress: Invalid length: 5, expected: 6")
478
+ lambda { ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 1, 12]) }.should raise_error("ExampleProject::MacAddress: Invalid length: 7, expected: 6")
479
+ end
480
+
481
+ it "it has a valid enumerator interface" do
482
+ test_array = [1, 2, 6, 10, 100, 0]
483
+ x = ExampleProject::MacAddress.new(test_array)
484
+ x.to_enum.to_a.should == test_array
485
+ x.min.should == 0
486
+ x.max.should == 100
487
+ x.sort.should == test_array.sort
488
+ end
489
+
490
+ it "can be converted to a string if elements are in the right range" do
491
+ test_array = "foobar".bytes
492
+ ExampleProject::MacAddress.new(test_array).to_string.should == "foobar"
493
+ ExampleProject::MacAddress.new("asdfas").to_string.should == "asdfas"
494
+ end
495
+
496
+ describe "#==" do
497
+ it "should return true for equal arrays" do
498
+ (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) == [1, 2, 6, 10, 100, 0]).should be_true
499
+ (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) == ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_true
500
+ (ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0]) != ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_false
501
+ end
502
+ it "should return false for non-equal arrays" do
503
+ (ExampleProject::MacAddress.new([1, 2, 10, 100, 5, 0]) == ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_false
504
+ (ExampleProject::MacAddress.new([1, 2, 10, 100, 5, 0]) != ExampleProject::MacAddress.new([1, 2, 6, 10, 100, 0])).should be_true
505
+ end
506
+ end
507
+
508
+ # describe "#num_bytes" do
509
+ # it "should match "
510
+ # end
511
+
512
+ it "num_bytes == max_size == min_size but _just for this particular fixed array_" do
513
+ test_array = [1, 2, 6, 10, 100, 0]
514
+ ExampleProject::MacAddress.new(test_array).num_bytes.should == 6
515
+ ExampleProject::MacAddress::max_size.should == 6
516
+ ExampleProject::MacAddress::min_size.should == 6
517
+ end
518
+
519
+ describe "#<=>" do
520
+ sample_arrays = [[0, 0, 0, 0, 1, 2],
521
+ [0, 1, 0, 0, 0, 0],
522
+ [0, 1, 0, 1, 0, 0]]
523
+ sample_arrays.each_with_index do |arr_a, i|
524
+ sample_arrays.each_with_index do |arr_b, j|
525
+ it "should give a lexicographical ordering" do
526
+ (ExampleProject::MacAddress.new(arr_a) <=> ExampleProject::MacAddress.new(arr_b)).should == (i <=> j)
527
+ end
528
+ end
529
+ end
530
+ end
531
+ end
532
+
533
+ describe CauterizeRuby::VariableArray do
534
+
535
+ test_arrays = ["Job Vranish".bytes.to_a,
536
+ [] ]
537
+ test_arrays.each do |test_array|
538
+ it ".new and .to_ruby are inverses" do
539
+ ExampleProject::Name.new(test_array).to_ruby.should == test_array
540
+ end
541
+
542
+ it "can pack, and unpack to its original value" do
543
+ ExampleProject::Name.unpack(ExampleProject::Name.new(test_array).pack).to_ruby.should == test_array
544
+ end
545
+
546
+ it "should pack to .num_bytes bytes" do
547
+ x = ExampleProject::Name.new(test_array)
548
+ x.pack.length.should == x.num_bytes
549
+ end
550
+ end
551
+
552
+ it "raises an exception if the array it too long" do
553
+ lambda { ExampleProject::Name.new([1] * 33) }.should raise_error("ExampleProject::Name: Invalid length: 33, max length is: 32")
554
+ end
555
+
556
+ it "it has a valid enumerator interface" do
557
+ test_array = [1, 2, 6, 10, 100, 0]
558
+ x = ExampleProject::Name.new(test_array)
559
+ x.to_enum.to_a.should == test_array
560
+ x.min.should == 0
561
+ x.max.should == 100
562
+ x.sort.should == test_array.sort
563
+ end
564
+
565
+ it "can be converted to a string if elements are in the right range" do
566
+ test_array = "Job Vranish".bytes
567
+ ExampleProject::Name.new(test_array).to_string.should == "Job Vranish"
568
+ ExampleProject::Name.new("asdfas").to_string.should == "asdfas"
569
+ ExampleProject::Name.new("").to_string.should == ""
570
+ end
571
+
572
+ it "min size is just the size of the size type, max size is max size of all elements" do
573
+ c = ExampleProject::Name
574
+ test_array = [1, 2, 6, 10, 100, 0]
575
+ c.new(test_array).num_bytes.should == 7
576
+ c::max_size.should == c::size_type::max_size + (c::elem_type::max_size * c::max_length)
577
+ c::min_size.should == c::size_type::min_size
578
+ end
579
+
580
+ describe "#==" do
581
+ it "should return true for equal arrays" do
582
+ (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) == [1, 2, 6, 10, 100, 0]).should be_true
583
+ (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) == ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_true
584
+ (ExampleProject::Name.new([1, 2, 6, 10, 100, 0]) != ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_false
585
+ end
586
+ it "should return false for non-equal arrays" do
587
+ (ExampleProject::Name.new([1, 2, 10, 100, 0]) == ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_false
588
+ (ExampleProject::Name.new([1, 2, 10, 100, 0]) != ExampleProject::Name.new([1, 2, 6, 10, 100, 0])).should be_true
589
+ end
590
+ end
591
+
592
+ describe "#<=>" do
593
+ sample_arrays = [[0, 1, 2],
594
+ [0, 2, 2],
595
+ [1, 0, 0]]
596
+ sample_arrays.each_with_index do |arr_a, i|
597
+ sample_arrays.each_with_index do |arr_b, j|
598
+ it "should give a lexicographical ordering" do
599
+ (ExampleProject::Name.new(arr_a) <=> ExampleProject::Name.new(arr_b)).should == (i <=> j)
600
+ end
601
+ end
602
+ end
603
+ end
604
+
605
+ end
606
+
607
+ describe CauterizeRuby::Composite do
608
+ sample_persons = [ { first_name: "Jane", last_name: "Smith", gender: :MALE },
609
+ { first_name: "Jill", last_name: "Smith", gender: :FEMALE},
610
+ { first_name: "John", last_name: "Smit", gender: :MALE },
611
+ { first_name: "John", last_name: "Smith", gender: :MALE }]
612
+
613
+ sample_persons.each do |p|
614
+ it ".new and .to_ruby are inverses" do
615
+ ExampleProject::Person.new(p).should == p
616
+ end
617
+ it "can pack, and unpack to its original value" do
618
+ ExampleProject::Person.unpack((ExampleProject::Person.new(p).pack)).should == p
619
+ end
620
+ it "pack should pack to .num_bytes bytes" do
621
+ x = ExampleProject::Person.new(p)
622
+ x.pack.length.should == x.num_bytes
623
+ end
624
+ end
625
+
626
+ it "should raise an error if fields are missing during initialization" do
627
+ lambda { ExampleProject::Person.new({ last_name: "Smith", gender: :MALE }) }.should raise_error("ExampleProject::Person: Invalid initialization params, missing fields: [:first_name], extra fields: []")
628
+ end
629
+
630
+ it "should raise an error if there are extra fields during initialization" do
631
+ lambda { ExampleProject::Person.new({ first_name: "Jane", last_name: "Smith", age: 12, gender: :MALE }) }.should raise_error("ExampleProject::Person: Invalid initialization params, missing fields: [], extra fields: [:age]")
632
+ end
633
+
634
+ describe "#max_size" do
635
+ it "should be the sum of the max sizes of the fields" do
636
+ c = ExampleProject::Person
637
+ c::max_size.should == c::fields.values.reduce(0) {|sum, v| sum + v::max_size}
638
+ end
639
+ end
640
+ describe "#min_size" do
641
+ it "should be the sum of the min sizes of the fields" do
642
+ c = ExampleProject::Person
643
+ c::min_size.should == c::fields.values.reduce(0) {|sum, v| sum + v::min_size}
644
+ end
645
+ end
646
+
647
+ it "should have accessors for all the fields" do
648
+ test_person = ExampleProject::Person.new({ first_name: "test", last_name: "thing", gender: :MALE})
649
+ test_person.first_name.should == "test"
650
+ test_person.last_name.should == "thing"
651
+ test_person.gender.should == :MALE
652
+ end
653
+
654
+ describe "#<=>" do
655
+ sample_persons.each_with_index do |p1, i|
656
+ sample_persons.each_with_index do |p2, j|
657
+ it "should give a lexicographical ordering" do
658
+ (ExampleProject::Person.new(p1) <=> p2).should == (i <=> j)
659
+ (ExampleProject::Person.new(p1) <=> ExampleProject::Person.new(p2)).should == (i <=> j)
660
+ end
661
+ end
662
+ end
663
+ end
664
+
665
+ describe "#==" do
666
+ sample_persons.each_with_index do |p1, i|
667
+ sample_persons.each_with_index do |p2, j|
668
+ it "should return true for equal things, false otherwise" do
669
+ (ExampleProject::Person.new(p1) == p2).should == (i == j)
670
+ (ExampleProject::Person.new(p1) == ExampleProject::Person.new(p2)).should == (i == j)
671
+ (ExampleProject::Person.new(p1) != p2).should == (i != j)
672
+ (ExampleProject::Person.new(p1) != ExampleProject::Person.new(p2)).should == (i != j)
673
+ end
674
+ end
675
+ end
676
+ end
677
+ end
678
+
679
+ describe CauterizeRuby::Group do
680
+ test_groups = [ { tag: :PERSON, data: { first_name: "Jane", last_name: "Smith", gender: :MALE }},
681
+ { tag: :DOG, data: { name: "Fido", leg_count: 3, gender: :MALE }}]
682
+
683
+ test_groups.each do |g|
684
+ it ".new and .to_ruby are inverses" do
685
+ ExampleProject::Creature.new(g).should == g
686
+ end
687
+ it "can pack, and unpack to its original value" do
688
+ ExampleProject::Creature.unpack((ExampleProject::Creature.new(g).pack)).should == g
689
+ end
690
+ it "pack should pack to .num_bytes bytes" do
691
+ x = ExampleProject::Creature.new(g)
692
+ x.pack.length.should == x.num_bytes
693
+ end
694
+ end
695
+
696
+ describe "#max_size" do
697
+ it "should be the max size of the tag + max of the max sizes of the fields" do
698
+ c = ExampleProject::Creature
699
+ c::max_size.should == c::tag_type::max_size + c::fields.values.map{|v| (v.nil?) ? 0 : v::max_size}.max
700
+ end
701
+ end
702
+
703
+ describe "#min_size" do
704
+ it "should be the max size of the tag + min of the min sizes of the fields" do
705
+ c = ExampleProject::Creature
706
+ c::min_size.should == c::tag_type::min_size + c::fields.values.map{|v| (v.nil?) ? 0 : v::min_size}.min
707
+ end
708
+ end
709
+
710
+ describe "#<=>" do
711
+ test_groups.each_with_index do |a, i|
712
+ test_groups.each_with_index do |b, j|
713
+ it "should give a lexicographical ordering" do
714
+ (ExampleProject::Creature.new(a) <=> b).should == (i <=> j)
715
+ (ExampleProject::Creature.new(a) <=> ExampleProject::Creature.new(b)).should == (i <=> j)
716
+ end
717
+ end
718
+ end
719
+ end
720
+
721
+ describe "#==" do
722
+ test_groups.each_with_index do |a, i|
723
+ test_groups.each_with_index do |b, j|
724
+ it "should return true for equal things, false otherwise" do
725
+ (ExampleProject::Creature.new(a) == b).should == (i == j)
726
+ (ExampleProject::Creature.new(a) == ExampleProject::Creature.new(b)).should == (i == j)
727
+ (ExampleProject::Creature.new(a) != b).should == (i != j)
728
+ (ExampleProject::Creature.new(a) != ExampleProject::Creature.new(b)).should == (i != j)
729
+ end
730
+ end
731
+ end
732
+ end
733
+ end
734
+ end
735
+ end