bindata 1.2.2 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- data/ChangeLog +12 -0
- data/NEWS +53 -0
- data/Rakefile +2 -1
- data/examples/NBT.txt +149 -0
- data/examples/ip_address.rb +1 -2
- data/examples/list.rb +124 -0
- data/examples/nbt.rb +195 -0
- data/lib/bindata.rb +4 -3
- data/lib/bindata/alignment.rb +86 -0
- data/lib/bindata/array.rb +21 -29
- data/lib/bindata/base.rb +82 -81
- data/lib/bindata/base_primitive.rb +66 -48
- data/lib/bindata/choice.rb +18 -28
- data/lib/bindata/deprecated.rb +17 -0
- data/lib/bindata/dsl.rb +25 -15
- data/lib/bindata/int.rb +2 -2
- data/lib/bindata/io.rb +8 -6
- data/lib/bindata/offset.rb +91 -0
- data/lib/bindata/primitive.rb +22 -11
- data/lib/bindata/record.rb +40 -10
- data/lib/bindata/sanitize.rb +15 -30
- data/lib/bindata/string.rb +16 -17
- data/lib/bindata/stringz.rb +0 -1
- data/lib/bindata/struct.rb +17 -6
- data/lib/bindata/trace.rb +52 -0
- data/lib/bindata/wrapper.rb +28 -6
- data/manual.haml +56 -10
- data/manual.md +318 -113
- data/spec/alignment_spec.rb +61 -0
- data/spec/array_spec.rb +139 -178
- data/spec/base_primitive_spec.rb +86 -111
- data/spec/base_spec.rb +200 -172
- data/spec/bits_spec.rb +45 -53
- data/spec/choice_spec.rb +91 -87
- data/spec/deprecated_spec.rb +36 -14
- data/spec/float_spec.rb +16 -68
- data/spec/int_spec.rb +26 -27
- data/spec/io_spec.rb +105 -105
- data/spec/lazy_spec.rb +50 -50
- data/spec/primitive_spec.rb +36 -36
- data/spec/record_spec.rb +134 -134
- data/spec/registry_spec.rb +34 -38
- data/spec/rest_spec.rb +8 -11
- data/spec/skip_spec.rb +9 -17
- data/spec/spec_common.rb +4 -0
- data/spec/string_spec.rb +92 -115
- data/spec/stringz_spec.rb +41 -74
- data/spec/struct_spec.rb +132 -153
- data/spec/system_spec.rb +115 -60
- data/spec/wrapper_spec.rb +63 -31
- data/tasks/pkg.rake +1 -1
- metadata +15 -7
data/spec/deprecated_spec.rb
CHANGED
@@ -23,6 +23,30 @@ describe BinData::MultiValue, "when defining" do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
describe BinData::Base, "when defining" do
|
26
|
+
it "should fail if #initialize is overridden" do
|
27
|
+
class BaseWithInitialize < BinData::Base
|
28
|
+
def initialize(params = {}, parent = nil)
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
lambda {
|
34
|
+
BaseWithInitialize.new
|
35
|
+
}.should raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should handle if #initialize is naively renamed to #initialize_instance" do
|
39
|
+
class BaseWithInitializeInstance < BinData::Base
|
40
|
+
def initialize_instance(params = {}, parent = nil)
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
lambda {
|
46
|
+
BaseWithInitializeInstance.new
|
47
|
+
}.should_not raise_error
|
48
|
+
end
|
49
|
+
|
26
50
|
it "should handle deprecated #register method" do
|
27
51
|
lambda {
|
28
52
|
class DeprecatedRegisterBase < BinData::Base
|
@@ -57,34 +81,32 @@ describe BinData::Base do
|
|
57
81
|
class DeprecatedBase < BinData::Base
|
58
82
|
end
|
59
83
|
|
60
|
-
|
61
|
-
|
62
|
-
@io = "abcde"
|
63
|
-
end
|
84
|
+
subject { DeprecatedBase.new }
|
85
|
+
let(:io) { "abcde" }
|
64
86
|
|
65
87
|
it "should forward _do_read to do_read" do
|
66
|
-
|
67
|
-
|
88
|
+
subject.should_receive(:do_read).with(io)
|
89
|
+
subject._do_read(io)
|
68
90
|
end
|
69
91
|
|
70
92
|
it "should forward _do_write to do_write" do
|
71
|
-
|
72
|
-
|
93
|
+
subject.should_receive(:do_write).with(io)
|
94
|
+
subject._do_write(io)
|
73
95
|
end
|
74
96
|
|
75
97
|
it "should forward _do_num_bytes to do_num_bytes" do
|
76
|
-
|
77
|
-
|
98
|
+
subject.should_receive(:do_num_bytes)
|
99
|
+
subject._do_num_bytes
|
78
100
|
end
|
79
101
|
|
80
102
|
it "should forward _assign to assign" do
|
81
103
|
val = 3
|
82
|
-
|
83
|
-
|
104
|
+
subject.should_receive(:assign).with(val)
|
105
|
+
subject._assign(val)
|
84
106
|
end
|
85
107
|
|
86
108
|
it "should forward _snapshot to snapshot" do
|
87
|
-
|
88
|
-
|
109
|
+
subject.should_receive(:snapshot)
|
110
|
+
subject._snapshot
|
89
111
|
end
|
90
112
|
end
|
data/spec/float_spec.rb
CHANGED
@@ -4,86 +4,34 @@ require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
|
4
4
|
require 'bindata/float'
|
5
5
|
|
6
6
|
describe "A FloatLe" do
|
7
|
-
|
8
|
-
@obj = BinData::FloatLe.new
|
9
|
-
@obj.value = Math::PI
|
10
|
-
end
|
7
|
+
subject { BinData::FloatLe.new(Math::PI) }
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
it "should write the expected value" do
|
17
|
-
written_value(@obj).should == [Math::PI].pack('e')
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should read the same value as written" do
|
21
|
-
value_read_from_written(@obj).should be_close(Math::PI, 0.000001)
|
22
|
-
end
|
9
|
+
its(:num_bytes) { should == 4 }
|
10
|
+
its(:to_binary_s) { should == [Math::PI].pack('e') }
|
11
|
+
its(:value_read_from_written) { should be_close(Math::PI, 0.000001) }
|
23
12
|
end
|
24
13
|
|
25
14
|
describe "A FloatBe" do
|
26
|
-
|
27
|
-
@obj = BinData::FloatBe.new
|
28
|
-
@obj.value = Math::PI
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should be 4 bytes in size" do
|
32
|
-
@obj.num_bytes.should == 4
|
33
|
-
end
|
15
|
+
subject { BinData::FloatBe.new(Math::PI) }
|
34
16
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
it "should read the same value as written" do
|
40
|
-
value_read_from_written(@obj).should be_close(Math::PI, 0.000001)
|
41
|
-
end
|
17
|
+
its(:num_bytes) { should == 4 }
|
18
|
+
its(:to_binary_s) { should == [Math::PI].pack('g') }
|
19
|
+
its(:value_read_from_written) { should be_close(Math::PI, 0.000001) }
|
42
20
|
end
|
43
21
|
|
44
22
|
describe "A DoubleLe" do
|
45
|
-
|
46
|
-
@obj = BinData::DoubleLe.new
|
47
|
-
@obj.value = Math::PI
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should be 8 bytes in size" do
|
51
|
-
@obj.num_bytes.should == 8
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should write the expected value" do
|
55
|
-
written_value(@obj).should == [Math::PI].pack('E')
|
56
|
-
end
|
23
|
+
subject { BinData::DoubleLe.new(Math::PI) }
|
57
24
|
|
58
|
-
|
59
|
-
|
60
|
-
|
25
|
+
its(:num_bytes) { should == 8 }
|
26
|
+
its(:to_binary_s) { should == [Math::PI].pack('E') }
|
27
|
+
its(:value_read_from_written) { should be_close(Math::PI, 0.0000000000000001) }
|
61
28
|
end
|
62
29
|
|
63
30
|
|
64
31
|
describe "A DoubleBe" do
|
65
|
-
|
66
|
-
@obj = BinData::DoubleBe.new
|
67
|
-
@obj.value = Math::PI
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should be 8 bytes in size" do
|
71
|
-
@obj.num_bytes.should == 8
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should write the expected value" do
|
75
|
-
written_value(@obj).should == [Math::PI].pack('G')
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should read the same value as written" do
|
79
|
-
value_read_from_written(@obj).should be_close(Math::PI, 0.0000000000000001)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def written_value(obj)
|
84
|
-
obj.to_binary_s
|
85
|
-
end
|
32
|
+
subject { BinData::DoubleBe.new(Math::PI) }
|
86
33
|
|
87
|
-
|
88
|
-
|
34
|
+
its(:num_bytes) { should == 8 }
|
35
|
+
its(:to_binary_s) { should == [Math::PI].pack('G') }
|
36
|
+
its(:value_read_from_written) { should be_close(Math::PI, 0.0000000000000001) }
|
89
37
|
end
|
data/spec/int_spec.rb
CHANGED
@@ -13,34 +13,35 @@ share_examples_for "All Integers" do
|
|
13
13
|
|
14
14
|
it "should have a sensible value of zero" do
|
15
15
|
all_classes do |int_class|
|
16
|
-
int_class.new.
|
16
|
+
int_class.new.should be_zero
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should avoid underflow" do
|
21
21
|
all_classes do |int_class|
|
22
|
-
|
23
|
-
|
22
|
+
subject = int_class.new
|
23
|
+
subject.assign(min_value - 1)
|
24
24
|
|
25
|
-
|
25
|
+
subject.should == min_value
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should avoid overflow" do
|
30
30
|
all_classes do |int_class|
|
31
|
-
|
32
|
-
|
31
|
+
subject = int_class.new
|
32
|
+
subject.assign(max_value + 1)
|
33
33
|
|
34
|
-
|
34
|
+
subject.should == max_value
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should assign values" do
|
39
39
|
all_classes do |int_class|
|
40
|
-
|
40
|
+
subject = int_class.new
|
41
41
|
test_int = gen_test_int
|
42
|
-
|
43
|
-
|
42
|
+
subject.assign(test_int)
|
43
|
+
|
44
|
+
subject.should == test_int
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
@@ -49,30 +50,28 @@ share_examples_for "All Integers" do
|
|
49
50
|
src = int_class.new
|
50
51
|
src.assign(gen_test_int)
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
subject = int_class.new
|
54
|
+
subject.assign(src)
|
55
|
+
subject.should == src
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
58
59
|
it "should symmetrically read and write a +ve number" do
|
59
60
|
all_classes do |int_class|
|
60
|
-
|
61
|
-
|
61
|
+
subject = int_class.new
|
62
|
+
subject.assign(gen_test_int)
|
62
63
|
|
63
|
-
|
64
|
-
int_class.read(str).should == obj.value
|
64
|
+
subject.value_read_from_written.should == subject
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should symmetrically read and write a -ve number" do
|
69
69
|
all_classes do |int_class|
|
70
70
|
if @signed
|
71
|
-
|
72
|
-
|
71
|
+
subject = int_class.new
|
72
|
+
subject.assign(-gen_test_int)
|
73
73
|
|
74
|
-
|
75
|
-
int_class.read(str).should == obj.value
|
74
|
+
subject.value_read_from_written.should == subject
|
76
75
|
end
|
77
76
|
end
|
78
77
|
end
|
@@ -81,10 +80,10 @@ share_examples_for "All Integers" do
|
|
81
80
|
all_classes do |int_class|
|
82
81
|
val = gen_test_int
|
83
82
|
|
84
|
-
|
85
|
-
|
83
|
+
subject = int_class.new
|
84
|
+
subject.assign(val)
|
86
85
|
|
87
|
-
|
86
|
+
subject.to_binary_s.should == int_to_binary_str(val)
|
88
87
|
end
|
89
88
|
end
|
90
89
|
|
@@ -93,10 +92,10 @@ share_examples_for "All Integers" do
|
|
93
92
|
if @signed
|
94
93
|
val = -gen_test_int
|
95
94
|
|
96
|
-
|
97
|
-
|
95
|
+
subject = int_class.new
|
96
|
+
subject.assign(val)
|
98
97
|
|
99
|
-
|
98
|
+
subject.to_binary_s.should == int_to_binary_str(val)
|
100
99
|
end
|
101
100
|
end
|
102
101
|
end
|
data/spec/io_spec.rb
CHANGED
@@ -41,52 +41,42 @@ describe BinData::IO, "reading from non seekable stream" do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
describe BinData::IO do
|
44
|
+
describe BinData::IO, "when reading" do
|
45
|
+
let(:stream) { StringIO.new "abcdefghij" }
|
46
|
+
let(:io) { BinData::IO.new(stream) }
|
47
|
+
|
45
48
|
it "should wrap strings in StringIO" do
|
46
|
-
io = BinData::IO.new("abcd")
|
47
49
|
io.raw_io.class.should == StringIO
|
48
50
|
end
|
49
51
|
|
50
52
|
it "should not wrap IO objects" do
|
51
|
-
stream = StringIO.new
|
52
|
-
io = BinData::IO.new(stream)
|
53
53
|
io.raw_io.should == stream
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should raise error when io is BinData::IO" do
|
57
57
|
lambda {
|
58
|
-
|
58
|
+
BinData::IO.new(BinData::IO.new(""))
|
59
59
|
}.should raise_error(ArgumentError)
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should return correct offset" do
|
63
|
-
stream = StringIO.new("abcdefghij")
|
64
63
|
stream.seek(3, IO::SEEK_CUR)
|
65
64
|
|
66
|
-
io = BinData::IO.new(stream)
|
67
65
|
io.offset.should == 0
|
68
|
-
io.readbytes(4)
|
66
|
+
io.readbytes(4).should == "defg"
|
69
67
|
io.offset.should == 4
|
70
68
|
end
|
71
69
|
|
72
70
|
it "should seek" do
|
73
|
-
stream = StringIO.new("abcdefghij")
|
74
|
-
io = BinData::IO.new(stream)
|
75
|
-
|
76
71
|
io.seekbytes(2)
|
77
72
|
io.readbytes(4).should == "cdef"
|
78
73
|
end
|
79
74
|
|
80
75
|
it "should read all bytes" do
|
81
|
-
stream = StringIO.new("abcdefghij")
|
82
|
-
io = BinData::IO.new(stream)
|
83
|
-
|
84
76
|
io.read_all_bytes.should == "abcdefghij"
|
85
77
|
end
|
86
78
|
|
87
79
|
it "should raise error when reading at eof" do
|
88
|
-
stream = StringIO.new("abcdefghij")
|
89
|
-
io = BinData::IO.new(stream)
|
90
80
|
io.seekbytes(10)
|
91
81
|
lambda {
|
92
82
|
io.readbytes(3)
|
@@ -94,115 +84,129 @@ describe BinData::IO do
|
|
94
84
|
end
|
95
85
|
|
96
86
|
it "should raise error on short reads" do
|
97
|
-
stream = StringIO.new("abcdefghij")
|
98
|
-
io = BinData::IO.new(stream)
|
99
87
|
lambda {
|
100
88
|
io.readbytes(20)
|
101
89
|
}.should raise_error(IOError)
|
102
90
|
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe BinData::IO, "when writing" do
|
94
|
+
let(:stream) { StringIO.new }
|
95
|
+
let(:io) { BinData::IO.new(stream) }
|
96
|
+
|
97
|
+
it "should not wrap IO objects" do
|
98
|
+
io.raw_io.should == stream
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should raise error when io is BinData::IO" do
|
102
|
+
lambda {
|
103
|
+
BinData::IO.new(BinData::IO.new(""))
|
104
|
+
}.should raise_error(ArgumentError)
|
105
|
+
end
|
103
106
|
|
104
107
|
it "should write" do
|
105
|
-
stream = StringIO.new
|
106
|
-
io = BinData::IO.new(stream)
|
107
108
|
io.writebytes("abcd")
|
108
109
|
|
109
110
|
stream.value.should == "abcd"
|
110
111
|
end
|
111
112
|
|
112
113
|
it "should flush" do
|
113
|
-
stream = StringIO.new
|
114
|
-
io = BinData::IO.new(stream)
|
115
114
|
io.writebytes("abcd")
|
116
115
|
io.flush
|
117
116
|
|
118
117
|
stream.value.should == "abcd"
|
119
118
|
end
|
120
|
-
|
121
119
|
end
|
122
120
|
|
123
121
|
describe BinData::IO, "reading bits in big endian" do
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
str = [@b1, @b2, @b3].pack("CCC")
|
129
|
-
@io = BinData::IO.new(str)
|
130
|
-
end
|
122
|
+
let(:b1) { 0b1111_1010 }
|
123
|
+
let(:b2) { 0b1100_1110 }
|
124
|
+
let(:b3) { 0b0110_1010 }
|
125
|
+
let(:io) { BinData::IO.new([b1, b2, b3].pack("CCC")) }
|
131
126
|
|
132
127
|
it "should read a bitfield less than 1 byte" do
|
133
|
-
|
128
|
+
io.readbits(3, :big).should == 0b111
|
134
129
|
end
|
135
130
|
|
136
131
|
it "should read a bitfield more than 1 byte" do
|
137
|
-
|
132
|
+
io.readbits(10, :big).should == 0b1111_1010_11
|
138
133
|
end
|
139
134
|
|
140
135
|
it "should read a bitfield more than 2 bytes" do
|
141
|
-
|
136
|
+
io.readbits(17, :big).should == 0b1111_1010_1100_1110_0
|
142
137
|
end
|
143
138
|
|
144
139
|
it "should read two bitfields totalling less than 1 byte" do
|
145
|
-
|
146
|
-
|
140
|
+
io.readbits(5, :big).should == 0b1111_1
|
141
|
+
io.readbits(2, :big).should == 0b01
|
147
142
|
end
|
148
143
|
|
149
144
|
it "should read two bitfields totalling more than 1 byte" do
|
150
|
-
|
151
|
-
|
145
|
+
io.readbits(6, :big).should == 0b1111_10
|
146
|
+
io.readbits(8, :big).should == 0b10_1100_11
|
152
147
|
end
|
153
148
|
|
154
149
|
it "should read two bitfields totalling more than 2 bytes" do
|
155
|
-
|
156
|
-
|
150
|
+
io.readbits(7, :big).should == 0b1111_101
|
151
|
+
io.readbits(12, :big).should == 0b0_1100_1110_011
|
157
152
|
end
|
158
153
|
|
159
154
|
it "should ignore unused bits when reading bytes" do
|
160
|
-
|
161
|
-
|
162
|
-
|
155
|
+
io.readbits(3, :big).should == 0b111
|
156
|
+
io.readbytes(1).should == [b2].pack("C")
|
157
|
+
io.readbits(2, :big).should == 0b01
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should reset read bits to realign stream to next byte" do
|
161
|
+
io.readbits(3, :big).should == 0b111
|
162
|
+
io.reset_read_bits
|
163
|
+
io.readbits(3, :big).should == 0b110
|
163
164
|
end
|
164
165
|
end
|
165
166
|
|
166
167
|
describe BinData::IO, "reading bits in little endian" do
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
str = [@b1, @b2, @b3].pack("CCC")
|
172
|
-
@io = BinData::IO.new(str)
|
173
|
-
end
|
168
|
+
let(:b1) { 0b1111_1010 }
|
169
|
+
let(:b2) { 0b1100_1110 }
|
170
|
+
let(:b3) { 0b0110_1010 }
|
171
|
+
let(:io) { BinData::IO.new([b1, b2, b3].pack("CCC")) }
|
174
172
|
|
175
173
|
it "should read a bitfield less than 1 byte" do
|
176
|
-
|
174
|
+
io.readbits(3, :little).should == 0b010
|
177
175
|
end
|
178
176
|
|
179
177
|
it "should read a bitfield more than 1 byte" do
|
180
|
-
|
178
|
+
io.readbits(10, :little).should == 0b10_1111_1010
|
181
179
|
end
|
182
180
|
|
183
181
|
it "should read a bitfield more than 2 bytes" do
|
184
|
-
|
182
|
+
io.readbits(17, :little).should == 0b0_1100_1110_1111_1010
|
185
183
|
end
|
186
184
|
|
187
185
|
it "should read two bitfields totalling less than 1 byte" do
|
188
|
-
|
189
|
-
|
186
|
+
io.readbits(5, :little).should == 0b1_1010
|
187
|
+
io.readbits(2, :little).should == 0b11
|
190
188
|
end
|
191
189
|
|
192
190
|
it "should read two bitfields totalling more than 1 byte" do
|
193
|
-
|
194
|
-
|
191
|
+
io.readbits(6, :little).should == 0b11_1010
|
192
|
+
io.readbits(8, :little).should == 0b00_1110_11
|
195
193
|
end
|
196
194
|
|
197
195
|
it "should read two bitfields totalling more than 2 bytes" do
|
198
|
-
|
199
|
-
|
196
|
+
io.readbits(7, :little).should == 0b111_1010
|
197
|
+
io.readbits(12, :little).should == 0b010_1100_1110_1
|
200
198
|
end
|
201
199
|
|
202
200
|
it "should ignore unused bits when reading bytes" do
|
203
|
-
|
204
|
-
|
205
|
-
|
201
|
+
io.readbits(3, :little).should == 0b010
|
202
|
+
io.readbytes(1).should == [b2].pack("C")
|
203
|
+
io.readbits(2, :little).should == 0b10
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should reset read bits to realign stream to next byte" do
|
207
|
+
io.readbits(3, :little).should == 0b010
|
208
|
+
io.reset_read_bits
|
209
|
+
io.readbits(3, :little).should == 0b110
|
206
210
|
end
|
207
211
|
end
|
208
212
|
|
@@ -228,96 +232,92 @@ class BitWriterHelper
|
|
228
232
|
end
|
229
233
|
|
230
234
|
describe BinData::IO, "writing bits in big endian" do
|
231
|
-
|
232
|
-
@io = BitWriterHelper.new
|
233
|
-
end
|
235
|
+
let(:io) { BitWriterHelper.new }
|
234
236
|
|
235
237
|
it "should write a bitfield less than 1 byte" do
|
236
|
-
|
237
|
-
|
238
|
+
io.writebits(0b010, 3, :big)
|
239
|
+
io.value.should == [0b0100_0000].pack("C")
|
238
240
|
end
|
239
241
|
|
240
242
|
it "should write a bitfield more than 1 byte" do
|
241
|
-
|
242
|
-
|
243
|
+
io.writebits(0b10_1001_1101, 10, :big)
|
244
|
+
io.value.should == [0b1010_0111, 0b0100_0000].pack("CC")
|
243
245
|
end
|
244
246
|
|
245
247
|
it "should write a bitfield more than 2 bytes" do
|
246
|
-
|
247
|
-
|
248
|
+
io.writebits(0b101_1000_0010_1001_1101, 19, :big)
|
249
|
+
io.value.should == [0b1011_0000, 0b0101_0011, 0b1010_0000].pack("CCC")
|
248
250
|
end
|
249
251
|
|
250
252
|
it "should write two bitfields totalling less than 1 byte" do
|
251
|
-
|
252
|
-
|
253
|
-
|
253
|
+
io.writebits(0b1_1001, 5, :big)
|
254
|
+
io.writebits(0b00, 2, :big)
|
255
|
+
io.value.should == [0b1100_1000].pack("C")
|
254
256
|
end
|
255
257
|
|
256
258
|
it "should write two bitfields totalling more than 1 byte" do
|
257
|
-
|
258
|
-
|
259
|
-
|
259
|
+
io.writebits(0b01_0101, 6, :big)
|
260
|
+
io.writebits(0b001_1001, 7, :big)
|
261
|
+
io.value.should == [0b0101_0100, 0b1100_1000].pack("CC")
|
260
262
|
end
|
261
263
|
|
262
264
|
it "should write two bitfields totalling more than 2 bytes" do
|
263
|
-
|
264
|
-
|
265
|
-
|
265
|
+
io.writebits(0b01_0111, 6, :big)
|
266
|
+
io.writebits(0b1_0010_1001_1001, 13, :big)
|
267
|
+
io.value.should == [0b0101_1110, 0b0101_0011, 0b0010_0000].pack("CCC")
|
266
268
|
end
|
267
269
|
|
268
270
|
it "should pad unused bits when writing bytes" do
|
269
|
-
|
270
|
-
|
271
|
-
|
271
|
+
io.writebits(0b101, 3, :big)
|
272
|
+
io.writebytes([0b1011_1111].pack("C"))
|
273
|
+
io.writebits(0b01, 2, :big)
|
272
274
|
|
273
|
-
|
275
|
+
io.value.should == [0b1010_0000, 0b1011_1111, 0b0100_0000].pack("CCC")
|
274
276
|
end
|
275
277
|
end
|
276
278
|
|
277
279
|
describe BinData::IO, "writing bits in little endian" do
|
278
|
-
|
279
|
-
@io = BitWriterHelper.new
|
280
|
-
end
|
280
|
+
let(:io) { BitWriterHelper.new }
|
281
281
|
|
282
282
|
it "should write a bitfield less than 1 byte" do
|
283
|
-
|
284
|
-
|
283
|
+
io.writebits(0b010, 3, :little)
|
284
|
+
io.value.should == [0b0000_0010].pack("C")
|
285
285
|
end
|
286
286
|
|
287
287
|
it "should write a bitfield more than 1 byte" do
|
288
|
-
|
289
|
-
|
288
|
+
io.writebits(0b10_1001_1101, 10, :little)
|
289
|
+
io.value.should == [0b1001_1101, 0b0000_0010].pack("CC")
|
290
290
|
end
|
291
291
|
|
292
292
|
it "should write a bitfield more than 2 bytes" do
|
293
|
-
|
294
|
-
|
293
|
+
io.writebits(0b101_1000_0010_1001_1101, 19, :little)
|
294
|
+
io.value.should == [0b1001_1101, 0b1000_0010, 0b0000_0101].pack("CCC")
|
295
295
|
end
|
296
296
|
|
297
297
|
it "should write two bitfields totalling less than 1 byte" do
|
298
|
-
|
299
|
-
|
300
|
-
|
298
|
+
io.writebits(0b1_1001, 5, :little)
|
299
|
+
io.writebits(0b00, 2, :little)
|
300
|
+
io.value.should == [0b0001_1001].pack("C")
|
301
301
|
end
|
302
302
|
|
303
303
|
it "should write two bitfields totalling more than 1 byte" do
|
304
|
-
|
305
|
-
|
306
|
-
|
304
|
+
io.writebits(0b01_0101, 6, :little)
|
305
|
+
io.writebits(0b001_1001, 7, :little)
|
306
|
+
io.value.should == [0b0101_0101, 0b0000_0110].pack("CC")
|
307
307
|
end
|
308
308
|
|
309
309
|
it "should write two bitfields totalling more than 2 bytes" do
|
310
|
-
|
311
|
-
|
312
|
-
|
310
|
+
io.writebits(0b01_0111, 6, :little)
|
311
|
+
io.writebits(0b1_0010_1001_1001, 13, :little)
|
312
|
+
io.value.should == [0b0101_0111, 0b1010_0110, 0b0000_0100].pack("CCC")
|
313
313
|
end
|
314
314
|
|
315
315
|
it "should pad unused bits when writing bytes" do
|
316
|
-
|
317
|
-
|
318
|
-
|
316
|
+
io.writebits(0b101, 3, :little)
|
317
|
+
io.writebytes([0b1011_1111].pack("C"))
|
318
|
+
io.writebits(0b01, 2, :little)
|
319
319
|
|
320
|
-
|
320
|
+
io.value.should == [0b0000_0101, 0b1011_1111, 0b0000_0001].pack("CCC")
|
321
321
|
end
|
322
322
|
end
|
323
323
|
|