bindata 1.5.1 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- data/ChangeLog.rdoc +7 -0
- data/NEWS.rdoc +11 -0
- data/Rakefile +6 -1
- data/bindata.gemspec +2 -1
- data/doc/manual.md +17 -9
- data/examples/gzip.rb +2 -2
- data/examples/list.rb +2 -2
- data/lib/bindata/alignment.rb +4 -9
- data/lib/bindata/array.rb +57 -51
- data/lib/bindata/base.rb +13 -110
- data/lib/bindata/base_primitive.rb +130 -75
- data/lib/bindata/bits.rb +5 -7
- data/lib/bindata/choice.rb +24 -32
- data/lib/bindata/dsl.rb +1 -6
- data/lib/bindata/framework.rb +81 -0
- data/lib/bindata/int.rb +5 -7
- data/lib/bindata/name.rb +28 -0
- data/lib/bindata/offset.rb +42 -53
- data/lib/bindata/params.rb +33 -38
- data/lib/bindata/struct.rb +2 -6
- data/lib/bindata/trace.rb +16 -16
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata/virtual.rb +3 -3
- data/{spec/alignment_spec.rb → test/alignment_test.rb} +17 -16
- data/test/array_test.rb +371 -0
- data/test/base_primitive_test.rb +312 -0
- data/test/base_test.rb +183 -0
- data/{spec/bits_spec.rb → test/bits_test.rb} +59 -59
- data/test/choice_test.rb +260 -0
- data/{spec/spec_common.rb → test/common.rb} +33 -18
- data/test/count_bytes_remaining_test.rb +41 -0
- data/{spec/deprecated_spec.rb → test/deprecated_test.rb} +5 -7
- data/test/float_test.rb +72 -0
- data/{spec/int_spec.rb → test/int_test.rb} +34 -43
- data/{spec/io_spec.rb → test/io_test.rb} +70 -71
- data/{spec/lazy_spec.rb → test/lazy_test.rb} +38 -39
- data/test/offset_test.rb +93 -0
- data/test/params_test.rb +144 -0
- data/{spec/primitive_spec.rb → test/primitive_test.rb} +42 -54
- data/{spec/record_spec.rb → test/record_test.rb} +133 -154
- data/test/registry_test.rb +104 -0
- data/test/rest_test.rb +29 -0
- data/test/skip_test.rb +28 -0
- data/{spec/string_spec.rb → test/string_test.rb} +96 -97
- data/test/stringz_test.rb +127 -0
- data/{spec/struct_spec.rb → test/struct_test.rb} +119 -120
- data/{spec/system_spec.rb → test/system_test.rb} +66 -106
- metadata +39 -38
- data/lib/a.rb +0 -24
- data/spec/array_spec.rb +0 -331
- data/spec/base_primitive_spec.rb +0 -238
- data/spec/base_spec.rb +0 -376
- data/spec/choice_spec.rb +0 -263
- data/spec/count_bytes_remaining_spec.rb +0 -38
- data/spec/example.rb +0 -21
- data/spec/float_spec.rb +0 -37
- data/spec/registry_spec.rb +0 -108
- data/spec/rest_spec.rb +0 -26
- data/spec/skip_spec.rb +0 -27
- data/spec/stringz_spec.rb +0 -118
- data/tasks/rspec.rake +0 -17
@@ -1,9 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rspec'
|
4
|
-
require 'rspec/autorun'
|
1
|
+
require 'rubygems'
|
2
|
+
require 'minitest/autorun'
|
5
3
|
require 'stringio'
|
6
4
|
|
5
|
+
$LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
|
6
|
+
require 'bindata'
|
7
|
+
|
7
8
|
class Object
|
8
9
|
def expose_methods_for_testing
|
9
10
|
cls = (Class === self) ? self : (class << self ; self; end)
|
@@ -27,25 +28,23 @@ class StringIO
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
class ExampleSingle < BinData::BasePrimitive
|
32
|
+
def self.io_with_value(val)
|
33
|
+
BinData::IO.new([val].pack("V"))
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
line_num_regex = /.*:(\d+)(:.*|$)/
|
36
|
+
private
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
+
def value_to_binary_string(val)
|
39
|
+
[val].pack("V")
|
40
|
+
end
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
raise "Required calling pattern is lambda { xxx }.should raise_error_on_line(...)"
|
42
|
+
def read_and_return_value(io)
|
43
|
+
io.readbytes(4).unpack("V").at(0)
|
42
44
|
end
|
43
|
-
end
|
44
45
|
|
45
|
-
def
|
46
|
-
|
47
|
-
exception_line(err).should == line
|
48
|
-
block.call(err) if block_given?
|
46
|
+
def sensible_default
|
47
|
+
0
|
49
48
|
end
|
50
49
|
end
|
51
50
|
|
@@ -56,3 +55,19 @@ def binary(str)
|
|
56
55
|
str
|
57
56
|
end
|
58
57
|
end
|
58
|
+
|
59
|
+
module Kernel
|
60
|
+
def must_raise_on_line(exp, line, msg = nil)
|
61
|
+
ex = self.must_raise exp
|
62
|
+
ex.message.must_equal msg if msg
|
63
|
+
|
64
|
+
idx = ex.backtrace.find_index { |bt| /:in `must_raise_on_line'$/ =~ bt }
|
65
|
+
|
66
|
+
line_num_regex = /.*:(\d+)(:.*|$)/
|
67
|
+
err_line = line_num_regex.match(ex.backtrace[0])[1].to_i
|
68
|
+
ref_line = line_num_regex.match(ex.backtrace[idx + 1])[1].to_i
|
69
|
+
|
70
|
+
(err_line - ref_line).must_equal line
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "common"))
|
4
|
+
|
5
|
+
describe BinData::CountBytesRemaining do
|
6
|
+
let(:obj) { BinData::CountBytesRemaining.new }
|
7
|
+
|
8
|
+
it "initial state" do
|
9
|
+
obj.must_equal 0
|
10
|
+
obj.num_bytes.must_equal 0
|
11
|
+
end
|
12
|
+
|
13
|
+
it "counts till end of stream" do
|
14
|
+
data = "abcdefghij"
|
15
|
+
obj.read(data).must_equal 10
|
16
|
+
end
|
17
|
+
|
18
|
+
it "does not read any data" do
|
19
|
+
io = StringIO.new "abcdefghij"
|
20
|
+
obj.read(io)
|
21
|
+
|
22
|
+
io.pos.must_equal 0
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not write any data" do
|
26
|
+
obj.to_binary_s.must_equal ""
|
27
|
+
end
|
28
|
+
|
29
|
+
it "allows setting value for completeness" do
|
30
|
+
obj.assign("123")
|
31
|
+
obj.must_equal "123"
|
32
|
+
obj.to_binary_s.must_equal ""
|
33
|
+
end
|
34
|
+
|
35
|
+
it "accepts BinData::BasePrimitive parameters" do
|
36
|
+
count = BinData::CountBytesRemaining.new(:assert => 2)
|
37
|
+
lambda {
|
38
|
+
count.read("xyz")
|
39
|
+
}.must_raise BinData::ValidityError
|
40
|
+
end
|
41
|
+
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "
|
4
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
5
|
-
require 'bindata'
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "common"))
|
6
4
|
|
7
5
|
describe BinData::Base, "when defining" do
|
8
6
|
it "fails if #initialize is overridden" do
|
@@ -12,9 +10,9 @@ describe BinData::Base, "when defining" do
|
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
15
|
-
|
13
|
+
lambda {
|
16
14
|
BaseWithInitialize.new
|
17
|
-
}.
|
15
|
+
}.must_raise RuntimeError
|
18
16
|
end
|
19
17
|
|
20
18
|
it "handles if #initialize is naively renamed to #initialize_instance" do
|
@@ -24,8 +22,8 @@ describe BinData::Base, "when defining" do
|
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
27
|
-
|
25
|
+
lambda {
|
28
26
|
BaseWithInitializeInstance.new
|
29
|
-
}.
|
27
|
+
}.must_raise RuntimeError
|
30
28
|
end
|
31
29
|
end
|
data/test/float_test.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "common"))
|
4
|
+
|
5
|
+
module FloatTest
|
6
|
+
def test_num_bytes
|
7
|
+
@obj.num_bytes.must_equal 4
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_writing_then_reading
|
11
|
+
@obj.value_read_from_written.must_be_close_to Math::PI, 0.000001
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module DoubleTest
|
16
|
+
def test_num_bytes
|
17
|
+
@obj.num_bytes.must_equal 8
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_writing_then_reading
|
21
|
+
@obj.value_read_from_written.must_be_close_to Math::PI, 0.0000000000000001
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "A FloatLe" do
|
26
|
+
include FloatTest
|
27
|
+
|
28
|
+
before do
|
29
|
+
@obj = BinData::FloatLe.new(Math::PI)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "#to_binary_s" do
|
33
|
+
@obj.to_binary_s.must_equal [Math::PI].pack('e')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "A FloatBe" do
|
38
|
+
include FloatTest
|
39
|
+
|
40
|
+
before do
|
41
|
+
@obj = BinData::FloatBe.new(Math::PI)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "#to_binary_s" do
|
45
|
+
@obj.to_binary_s.must_equal [Math::PI].pack('g')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "A DoubleLe" do
|
50
|
+
include DoubleTest
|
51
|
+
|
52
|
+
before do
|
53
|
+
@obj = BinData::DoubleLe.new(Math::PI)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "#to_binary_s" do
|
57
|
+
@obj.to_binary_s.must_equal [Math::PI].pack('E')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
describe "A DoubleBe" do
|
63
|
+
include DoubleTest
|
64
|
+
|
65
|
+
before do
|
66
|
+
@obj = BinData::DoubleBe.new(Math::PI)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "#to_binary_s" do
|
70
|
+
@obj.to_binary_s.must_equal [Math::PI].pack('G')
|
71
|
+
end
|
72
|
+
end
|
@@ -1,93 +1,92 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "
|
4
|
-
require 'bindata'
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "common"))
|
5
4
|
|
6
|
-
|
5
|
+
module AllIntegers
|
7
6
|
|
8
|
-
|
7
|
+
def test_have_correct_num_bytes
|
9
8
|
all_classes do |int_class|
|
10
|
-
int_class.new.num_bytes.
|
9
|
+
int_class.new.num_bytes.must_equal @nbytes
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
|
-
|
13
|
+
def test_have_a_sensible_value_of_zero
|
15
14
|
all_classes do |int_class|
|
16
|
-
int_class.new.
|
15
|
+
int_class.new.must_equal 0
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
|
-
|
19
|
+
def test_avoid_underflow
|
21
20
|
all_classes do |int_class|
|
22
21
|
subject = int_class.new
|
23
22
|
subject.assign(min_value - 1)
|
24
23
|
|
25
|
-
subject.
|
24
|
+
subject.must_equal min_value
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
|
-
|
28
|
+
def test_avoid_overflow
|
30
29
|
all_classes do |int_class|
|
31
30
|
subject = int_class.new
|
32
31
|
subject.assign(max_value + 1)
|
33
32
|
|
34
|
-
subject.
|
33
|
+
subject.must_equal max_value
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
|
-
|
37
|
+
def test_assign_values
|
39
38
|
all_classes do |int_class|
|
40
39
|
subject = int_class.new
|
41
40
|
test_int = gen_test_int
|
42
41
|
subject.assign(test_int)
|
43
42
|
|
44
|
-
subject.
|
43
|
+
subject.must_equal test_int
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
|
-
|
47
|
+
def test_assign_values_from_other_int_objects
|
49
48
|
all_classes do |int_class|
|
50
49
|
src = int_class.new
|
51
50
|
src.assign(gen_test_int)
|
52
51
|
|
53
52
|
subject = int_class.new
|
54
53
|
subject.assign(src)
|
55
|
-
subject.
|
54
|
+
subject.must_equal src
|
56
55
|
end
|
57
56
|
end
|
58
57
|
|
59
|
-
|
58
|
+
def test_symmetrically_read_and_write_a_positive_number
|
60
59
|
all_classes do |int_class|
|
61
60
|
subject = int_class.new
|
62
61
|
subject.assign(gen_test_int)
|
63
62
|
|
64
|
-
subject.value_read_from_written.
|
63
|
+
subject.value_read_from_written.must_equal subject
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
68
|
-
|
67
|
+
def test_symmetrically_read_and_write_a_negative_number
|
69
68
|
all_classes do |int_class|
|
70
69
|
if @signed
|
71
70
|
subject = int_class.new
|
72
71
|
subject.assign(-gen_test_int)
|
73
72
|
|
74
|
-
subject.value_read_from_written.
|
73
|
+
subject.value_read_from_written.must_equal subject
|
75
74
|
end
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
79
|
-
|
78
|
+
def test_convert_a_positive_number_to_string
|
80
79
|
all_classes do |int_class|
|
81
80
|
val = gen_test_int
|
82
81
|
|
83
82
|
subject = int_class.new
|
84
83
|
subject.assign(val)
|
85
84
|
|
86
|
-
subject.to_binary_s.
|
85
|
+
subject.to_binary_s.must_equal int_to_binary_str(val)
|
87
86
|
end
|
88
87
|
end
|
89
88
|
|
90
|
-
|
89
|
+
def test_convert_a_negative_number_to_string
|
91
90
|
all_classes do |int_class|
|
92
91
|
if @signed
|
93
92
|
val = -gen_test_int
|
@@ -95,7 +94,7 @@ shared_examples "All Integers" do
|
|
95
94
|
subject = int_class.new
|
96
95
|
subject.assign(val)
|
97
96
|
|
98
|
-
subject.to_binary_s.
|
97
|
+
subject.to_binary_s.must_equal int_to_binary_str(val)
|
99
98
|
end
|
100
99
|
end
|
101
100
|
end
|
@@ -156,9 +155,9 @@ shared_examples "All Integers" do
|
|
156
155
|
end
|
157
156
|
|
158
157
|
describe "All signed big endian integers" do
|
159
|
-
|
158
|
+
include AllIntegers
|
160
159
|
|
161
|
-
before
|
160
|
+
before do
|
162
161
|
@endian = :big
|
163
162
|
@signed = true
|
164
163
|
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
@@ -166,9 +165,9 @@ describe "All signed big endian integers" do
|
|
166
165
|
end
|
167
166
|
|
168
167
|
describe "All unsigned big endian integers" do
|
169
|
-
|
168
|
+
include AllIntegers
|
170
169
|
|
171
|
-
before
|
170
|
+
before do
|
172
171
|
@endian = :big
|
173
172
|
@signed = false
|
174
173
|
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
@@ -176,9 +175,9 @@ describe "All unsigned big endian integers" do
|
|
176
175
|
end
|
177
176
|
|
178
177
|
describe "All signed little endian integers" do
|
179
|
-
|
178
|
+
include AllIntegers
|
180
179
|
|
181
|
-
before
|
180
|
+
before do
|
182
181
|
@endian = :little
|
183
182
|
@signed = true
|
184
183
|
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
@@ -186,9 +185,9 @@ describe "All signed little endian integers" do
|
|
186
185
|
end
|
187
186
|
|
188
187
|
describe "All unsigned little endian integers" do
|
189
|
-
|
188
|
+
include AllIntegers
|
190
189
|
|
191
|
-
before
|
190
|
+
before do
|
192
191
|
@endian = :little
|
193
192
|
@signed = false
|
194
193
|
@ints = create_mapping_of_class_to_nbits(@endian, @signed)
|
@@ -197,20 +196,12 @@ end
|
|
197
196
|
|
198
197
|
describe "Custom defined integers" do
|
199
198
|
it "fail unless bits are a multiple of 8" do
|
200
|
-
|
201
|
-
BinData::Uint7le
|
202
|
-
}.to raise_error
|
199
|
+
lambda { BinData::Uint7le }.must_raise NameError
|
203
200
|
|
204
|
-
|
205
|
-
BinData::Uint7be
|
206
|
-
}.to raise_error
|
201
|
+
lambda { BinData::Uint7be }.must_raise NameError
|
207
202
|
|
208
|
-
|
209
|
-
BinData::Int7le
|
210
|
-
}.to raise_error
|
203
|
+
lambda { BinData::Int7le }.must_raise NameError
|
211
204
|
|
212
|
-
|
213
|
-
BinData::Int7be
|
214
|
-
}.to raise_error
|
205
|
+
lambda { BinData::Int7be }.must_raise NameError
|
215
206
|
end
|
216
207
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "
|
4
|
-
require 'bindata/io'
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "common"))
|
5
4
|
|
6
5
|
describe BinData::IO, "reading from non seekable stream" do
|
7
|
-
before
|
6
|
+
before do
|
8
7
|
@rd, @wr = IO::pipe
|
9
8
|
if fork
|
10
9
|
# parent
|
@@ -25,23 +24,23 @@ describe BinData::IO, "reading from non seekable stream" do
|
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
|
-
after
|
27
|
+
after do
|
29
28
|
@rd.close
|
30
29
|
Process.wait
|
31
30
|
end
|
32
31
|
|
33
32
|
it "always has an offset of 0" do
|
34
33
|
@io.readbytes(10)
|
35
|
-
@io.offset.
|
34
|
+
@io.offset.must_equal 0
|
36
35
|
end
|
37
36
|
|
38
37
|
it "seeks correctly" do
|
39
38
|
@io.seekbytes(4999)
|
40
|
-
@io.readbytes(5).
|
39
|
+
@io.readbytes(5).must_equal "abbbb"
|
41
40
|
end
|
42
41
|
|
43
42
|
it "returns zero for num bytes remaining" do
|
44
|
-
@io.num_bytes_remaining.
|
43
|
+
@io.num_bytes_remaining.must_equal 0
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
@@ -50,54 +49,54 @@ describe BinData::IO, "when reading" do
|
|
50
49
|
let(:io) { BinData::IO.new(stream) }
|
51
50
|
|
52
51
|
it "wraps strings in StringIO" do
|
53
|
-
io.raw_io.class.
|
52
|
+
io.raw_io.class.must_equal StringIO
|
54
53
|
end
|
55
54
|
|
56
55
|
it "does not wrap IO objects" do
|
57
|
-
io.raw_io.
|
56
|
+
io.raw_io.must_equal stream
|
58
57
|
end
|
59
58
|
|
60
59
|
it "raises error when io is BinData::IO" do
|
61
|
-
|
60
|
+
lambda {
|
62
61
|
BinData::IO.new(BinData::IO.new(""))
|
63
|
-
}.
|
62
|
+
}.must_raise ArgumentError
|
64
63
|
end
|
65
64
|
|
66
65
|
it "returns correct offset" do
|
67
66
|
stream.seek(3, IO::SEEK_CUR)
|
68
67
|
|
69
|
-
io.offset.
|
70
|
-
io.readbytes(4).
|
71
|
-
io.offset.
|
68
|
+
io.offset.must_equal 0
|
69
|
+
io.readbytes(4).must_equal "defg"
|
70
|
+
io.offset.must_equal 4
|
72
71
|
end
|
73
72
|
|
74
73
|
it "seeks correctly" do
|
75
74
|
io.seekbytes(2)
|
76
|
-
io.readbytes(4).
|
75
|
+
io.readbytes(4).must_equal "cdef"
|
77
76
|
end
|
78
77
|
|
79
78
|
it "reads all bytes" do
|
80
|
-
io.read_all_bytes.
|
79
|
+
io.read_all_bytes.must_equal "abcdefghij"
|
81
80
|
end
|
82
81
|
|
83
82
|
it "returns number of bytes remaining" do
|
84
83
|
stream_length = io.num_bytes_remaining
|
85
84
|
|
86
85
|
io.readbytes(4)
|
87
|
-
io.num_bytes_remaining.
|
86
|
+
io.num_bytes_remaining.must_equal stream_length - 4
|
88
87
|
end
|
89
88
|
|
90
89
|
it "raises error when reading at eof" do
|
91
90
|
io.seekbytes(10)
|
92
|
-
|
91
|
+
lambda {
|
93
92
|
io.readbytes(3)
|
94
|
-
}.
|
93
|
+
}.must_raise EOFError
|
95
94
|
end
|
96
95
|
|
97
96
|
it "raises error on short reads" do
|
98
|
-
|
97
|
+
lambda {
|
99
98
|
io.readbytes(20)
|
100
|
-
}.
|
99
|
+
}.must_raise IOError
|
101
100
|
end
|
102
101
|
end
|
103
102
|
|
@@ -106,26 +105,26 @@ describe BinData::IO, "when writing" do
|
|
106
105
|
let(:io) { BinData::IO.new(stream) }
|
107
106
|
|
108
107
|
it "does not wrap IO objects" do
|
109
|
-
io.raw_io.
|
108
|
+
io.raw_io.must_equal stream
|
110
109
|
end
|
111
110
|
|
112
111
|
it "raises error when io is BinData::IO" do
|
113
|
-
|
112
|
+
lambda {
|
114
113
|
BinData::IO.new(BinData::IO.new(""))
|
115
|
-
}.
|
114
|
+
}.must_raise ArgumentError
|
116
115
|
end
|
117
116
|
|
118
117
|
it "writes correctly" do
|
119
118
|
io.writebytes("abcd")
|
120
119
|
|
121
|
-
stream.value.
|
120
|
+
stream.value.must_equal "abcd"
|
122
121
|
end
|
123
122
|
|
124
123
|
it "flushes" do
|
125
124
|
io.writebytes("abcd")
|
126
125
|
io.flush
|
127
126
|
|
128
|
-
stream.value.
|
127
|
+
stream.value.must_equal "abcd"
|
129
128
|
end
|
130
129
|
end
|
131
130
|
|
@@ -136,42 +135,42 @@ describe BinData::IO, "reading bits in big endian" do
|
|
136
135
|
let(:io) { BinData::IO.new([b1, b2, b3].pack("CCC")) }
|
137
136
|
|
138
137
|
it "reads a bitfield less than 1 byte" do
|
139
|
-
io.readbits(3, :big).
|
138
|
+
io.readbits(3, :big).must_equal 0b111
|
140
139
|
end
|
141
140
|
|
142
141
|
it "reads a bitfield more than 1 byte" do
|
143
|
-
io.readbits(10, :big).
|
142
|
+
io.readbits(10, :big).must_equal 0b1111_1010_11
|
144
143
|
end
|
145
144
|
|
146
145
|
it "reads a bitfield more than 2 bytes" do
|
147
|
-
io.readbits(17, :big).
|
146
|
+
io.readbits(17, :big).must_equal 0b1111_1010_1100_1110_0
|
148
147
|
end
|
149
148
|
|
150
149
|
it "reads two bitfields totalling less than 1 byte" do
|
151
|
-
io.readbits(5, :big).
|
152
|
-
io.readbits(2, :big).
|
150
|
+
io.readbits(5, :big).must_equal 0b1111_1
|
151
|
+
io.readbits(2, :big).must_equal 0b01
|
153
152
|
end
|
154
153
|
|
155
154
|
it "reads two bitfields totalling more than 1 byte" do
|
156
|
-
io.readbits(6, :big).
|
157
|
-
io.readbits(8, :big).
|
155
|
+
io.readbits(6, :big).must_equal 0b1111_10
|
156
|
+
io.readbits(8, :big).must_equal 0b10_1100_11
|
158
157
|
end
|
159
158
|
|
160
159
|
it "reads two bitfields totalling more than 2 bytes" do
|
161
|
-
io.readbits(7, :big).
|
162
|
-
io.readbits(12, :big).
|
160
|
+
io.readbits(7, :big).must_equal 0b1111_101
|
161
|
+
io.readbits(12, :big).must_equal 0b0_1100_1110_011
|
163
162
|
end
|
164
163
|
|
165
164
|
it "ignores unused bits when reading bytes" do
|
166
|
-
io.readbits(3, :big).
|
167
|
-
io.readbytes(1).
|
168
|
-
io.readbits(2, :big).
|
165
|
+
io.readbits(3, :big).must_equal 0b111
|
166
|
+
io.readbytes(1).must_equal [b2].pack("C")
|
167
|
+
io.readbits(2, :big).must_equal 0b01
|
169
168
|
end
|
170
169
|
|
171
170
|
it "resets read bits to realign stream to next byte" do
|
172
|
-
io.readbits(3, :big).
|
171
|
+
io.readbits(3, :big).must_equal 0b111
|
173
172
|
io.reset_read_bits
|
174
|
-
io.readbits(3, :big).
|
173
|
+
io.readbits(3, :big).must_equal 0b110
|
175
174
|
end
|
176
175
|
end
|
177
176
|
|
@@ -182,42 +181,42 @@ describe BinData::IO, "reading bits in little endian" do
|
|
182
181
|
let(:io) { BinData::IO.new([b1, b2, b3].pack("CCC")) }
|
183
182
|
|
184
183
|
it "reads a bitfield less than 1 byte" do
|
185
|
-
io.readbits(3, :little).
|
184
|
+
io.readbits(3, :little).must_equal 0b010
|
186
185
|
end
|
187
186
|
|
188
187
|
it "reads a bitfield more than 1 byte" do
|
189
|
-
io.readbits(10, :little).
|
188
|
+
io.readbits(10, :little).must_equal 0b10_1111_1010
|
190
189
|
end
|
191
190
|
|
192
191
|
it "reads a bitfield more than 2 bytes" do
|
193
|
-
io.readbits(17, :little).
|
192
|
+
io.readbits(17, :little).must_equal 0b0_1100_1110_1111_1010
|
194
193
|
end
|
195
194
|
|
196
195
|
it "reads two bitfields totalling less than 1 byte" do
|
197
|
-
io.readbits(5, :little).
|
198
|
-
io.readbits(2, :little).
|
196
|
+
io.readbits(5, :little).must_equal 0b1_1010
|
197
|
+
io.readbits(2, :little).must_equal 0b11
|
199
198
|
end
|
200
199
|
|
201
200
|
it "reads two bitfields totalling more than 1 byte" do
|
202
|
-
io.readbits(6, :little).
|
203
|
-
io.readbits(8, :little).
|
201
|
+
io.readbits(6, :little).must_equal 0b11_1010
|
202
|
+
io.readbits(8, :little).must_equal 0b00_1110_11
|
204
203
|
end
|
205
204
|
|
206
205
|
it "reads two bitfields totalling more than 2 bytes" do
|
207
|
-
io.readbits(7, :little).
|
208
|
-
io.readbits(12, :little).
|
206
|
+
io.readbits(7, :little).must_equal 0b111_1010
|
207
|
+
io.readbits(12, :little).must_equal 0b010_1100_1110_1
|
209
208
|
end
|
210
209
|
|
211
210
|
it "ignores unused bits when reading bytes" do
|
212
|
-
io.readbits(3, :little).
|
213
|
-
io.readbytes(1).
|
214
|
-
io.readbits(2, :little).
|
211
|
+
io.readbits(3, :little).must_equal 0b010
|
212
|
+
io.readbytes(1).must_equal [b2].pack("C")
|
213
|
+
io.readbits(2, :little).must_equal 0b10
|
215
214
|
end
|
216
215
|
|
217
216
|
it "resets read bits to realign stream to next byte" do
|
218
|
-
io.readbits(3, :little).
|
217
|
+
io.readbits(3, :little).must_equal 0b010
|
219
218
|
io.reset_read_bits
|
220
|
-
io.readbits(3, :little).
|
219
|
+
io.readbits(3, :little).must_equal 0b110
|
221
220
|
end
|
222
221
|
end
|
223
222
|
|
@@ -247,35 +246,35 @@ describe BinData::IO, "writing bits in big endian" do
|
|
247
246
|
|
248
247
|
it "writes a bitfield less than 1 byte" do
|
249
248
|
io.writebits(0b010, 3, :big)
|
250
|
-
io.value.
|
249
|
+
io.value.must_equal [0b0100_0000].pack("C")
|
251
250
|
end
|
252
251
|
|
253
252
|
it "writes a bitfield more than 1 byte" do
|
254
253
|
io.writebits(0b10_1001_1101, 10, :big)
|
255
|
-
io.value.
|
254
|
+
io.value.must_equal [0b1010_0111, 0b0100_0000].pack("CC")
|
256
255
|
end
|
257
256
|
|
258
257
|
it "writes a bitfield more than 2 bytes" do
|
259
258
|
io.writebits(0b101_1000_0010_1001_1101, 19, :big)
|
260
|
-
io.value.
|
259
|
+
io.value.must_equal [0b1011_0000, 0b0101_0011, 0b1010_0000].pack("CCC")
|
261
260
|
end
|
262
261
|
|
263
262
|
it "writes two bitfields totalling less than 1 byte" do
|
264
263
|
io.writebits(0b1_1001, 5, :big)
|
265
264
|
io.writebits(0b00, 2, :big)
|
266
|
-
io.value.
|
265
|
+
io.value.must_equal [0b1100_1000].pack("C")
|
267
266
|
end
|
268
267
|
|
269
268
|
it "writes two bitfields totalling more than 1 byte" do
|
270
269
|
io.writebits(0b01_0101, 6, :big)
|
271
270
|
io.writebits(0b001_1001, 7, :big)
|
272
|
-
io.value.
|
271
|
+
io.value.must_equal [0b0101_0100, 0b1100_1000].pack("CC")
|
273
272
|
end
|
274
273
|
|
275
274
|
it "writes two bitfields totalling more than 2 bytes" do
|
276
275
|
io.writebits(0b01_0111, 6, :big)
|
277
276
|
io.writebits(0b1_0010_1001_1001, 13, :big)
|
278
|
-
io.value.
|
277
|
+
io.value.must_equal [0b0101_1110, 0b0101_0011, 0b0010_0000].pack("CCC")
|
279
278
|
end
|
280
279
|
|
281
280
|
it "pads unused bits when writing bytes" do
|
@@ -283,7 +282,7 @@ describe BinData::IO, "writing bits in big endian" do
|
|
283
282
|
io.writebytes([0b1011_1111].pack("C"))
|
284
283
|
io.writebits(0b01, 2, :big)
|
285
284
|
|
286
|
-
io.value.
|
285
|
+
io.value.must_equal [0b1010_0000, 0b1011_1111, 0b0100_0000].pack("CCC")
|
287
286
|
end
|
288
287
|
end
|
289
288
|
|
@@ -292,35 +291,35 @@ describe BinData::IO, "writing bits in little endian" do
|
|
292
291
|
|
293
292
|
it "writes a bitfield less than 1 byte" do
|
294
293
|
io.writebits(0b010, 3, :little)
|
295
|
-
io.value.
|
294
|
+
io.value.must_equal [0b0000_0010].pack("C")
|
296
295
|
end
|
297
296
|
|
298
297
|
it "writes a bitfield more than 1 byte" do
|
299
298
|
io.writebits(0b10_1001_1101, 10, :little)
|
300
|
-
io.value.
|
299
|
+
io.value.must_equal [0b1001_1101, 0b0000_0010].pack("CC")
|
301
300
|
end
|
302
301
|
|
303
302
|
it "writes a bitfield more than 2 bytes" do
|
304
303
|
io.writebits(0b101_1000_0010_1001_1101, 19, :little)
|
305
|
-
io.value.
|
304
|
+
io.value.must_equal [0b1001_1101, 0b1000_0010, 0b0000_0101].pack("CCC")
|
306
305
|
end
|
307
306
|
|
308
307
|
it "writes two bitfields totalling less than 1 byte" do
|
309
308
|
io.writebits(0b1_1001, 5, :little)
|
310
309
|
io.writebits(0b00, 2, :little)
|
311
|
-
io.value.
|
310
|
+
io.value.must_equal [0b0001_1001].pack("C")
|
312
311
|
end
|
313
312
|
|
314
313
|
it "writes two bitfields totalling more than 1 byte" do
|
315
314
|
io.writebits(0b01_0101, 6, :little)
|
316
315
|
io.writebits(0b001_1001, 7, :little)
|
317
|
-
io.value.
|
316
|
+
io.value.must_equal [0b0101_0101, 0b0000_0110].pack("CC")
|
318
317
|
end
|
319
318
|
|
320
319
|
it "writes two bitfields totalling more than 2 bytes" do
|
321
320
|
io.writebits(0b01_0111, 6, :little)
|
322
321
|
io.writebits(0b1_0010_1001_1001, 13, :little)
|
323
|
-
io.value.
|
322
|
+
io.value.must_equal [0b0101_0111, 0b1010_0110, 0b0000_0100].pack("CCC")
|
324
323
|
end
|
325
324
|
|
326
325
|
it "pads unused bits when writing bytes" do
|
@@ -328,7 +327,7 @@ describe BinData::IO, "writing bits in little endian" do
|
|
328
327
|
io.writebytes([0b1011_1111].pack("C"))
|
329
328
|
io.writebits(0b01, 2, :little)
|
330
329
|
|
331
|
-
io.value.
|
330
|
+
io.value.must_equal [0b0000_0101, 0b1011_1111, 0b0000_0001].pack("CCC")
|
332
331
|
end
|
333
332
|
end
|
334
333
|
|
@@ -339,14 +338,14 @@ describe BinData::IO, "with changing endian" do
|
|
339
338
|
str = [b1, b2].pack("CC")
|
340
339
|
io = BinData::IO.new(str)
|
341
340
|
|
342
|
-
io.readbits(3, :big).
|
343
|
-
io.readbits(4, :little).
|
341
|
+
io.readbits(3, :big).must_equal 0b011
|
342
|
+
io.readbits(4, :little).must_equal 0b0010
|
344
343
|
end
|
345
344
|
|
346
345
|
it "does not mix different endianess when writing" do
|
347
346
|
io = BitWriterHelper.new
|
348
347
|
io.writebits(0b110, 3, :big)
|
349
348
|
io.writebits(0b010, 3, :little)
|
350
|
-
io.value.
|
349
|
+
io.value.must_equal [0b1100_0000, 0b0000_0010].pack("CC")
|
351
350
|
end
|
352
351
|
end
|