File: array_test.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (395 lines) | stat: -rwxr-xr-x 10,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

describe BinData::Array, "when instantiating" do
  describe "with no mandatory parameters supplied" do
    it "raises an error" do
      args = {}
      _ { BinData::Array.new(args) }.must_raise ArgumentError
    end
  end

  describe "with some but not all mandatory parameters supplied" do
    it "raises an error" do
      args = {initial_length: 3}
      _ { BinData::Array.new(args) }.must_raise ArgumentError
    end
  end

  it "warns about :length" do
    Kernel.must_warn ":length is not used with BinData::Array.  You probably want to change this to :initial_length" do
    obj = BinData::Array.new(type: :uint8, length: 3)
      obj.read "123"
    end
  end

  it "warns about :read_length" do
    Kernel.must_warn ":read_length is not used with BinData::Array.  You probably want to change this to :initial_length" do
    obj = BinData::Array.new(type: :uint8, read_length: 3)
      obj.read "123"
    end
  end

  it "fails if a given type is unknown" do
    args = {type: :does_not_exist, initial_length: 3}
    _ { BinData::Array.new(args) }.must_raise BinData::UnRegisteredTypeError
  end

  it "fails if :initial_length is not an integer" do
    args = {type: :uint8, initial_length: "3"}
    _ { BinData::Array.new(args) }.must_raise ArgumentError
  end

  it "does not allow both :initial_length and :read_until" do
    args = {initial_length: 3, read_until: -> { false } }
    _ { BinData::Array.new(args) }.must_raise ArgumentError
  end

  it "accepts BinData::Base as :type" do
    obj = BinData::Int8.new(initial_value: 5)
    array = BinData::Array.new(type: obj, initial_length: 1)
    _(array).must_equal [5]
  end
end

describe BinData::Array, "with no elements" do
  let(:obj) { BinData::Array.new(type: :uint32le) }

  it "initial state" do
    assert obj.clear?
    _(obj).must_be_empty
    _(obj.length).must_equal 0
    _(obj.first).must_be_nil
    _(obj.last).must_be_nil
  end

  it "returns [] for the first n elements" do
    _(obj.first(3)).must_equal []
  end

  it "returns [] for the last n elements" do
    _(obj.last(3)).must_equal []
  end
end

describe BinData::Array, "with several elements" do
  let(:obj) {
    type = [:uint32le, {initial_value: -> { index + 1 }}]
    BinData::Array.new(type: type, initial_length: 5)
  }

  it "initial state" do
    assert obj.clear?
    _(obj).wont_be_empty
    _(obj.size).must_equal 5
    _(obj.length).must_equal 5
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5]
    _(obj.inspect).must_equal "[1, 2, 3, 4, 5]"
  end

  it "coerces to ::Array if required" do
    _([0].concat(obj)).must_equal [0, 1, 2, 3, 4, 5]
  end

  it "uses methods from Enumerable" do
    _(obj.select { |x| (x % 2) == 0 }).must_equal [2, 4]
  end

  it "assigns primitive values" do
    obj.assign([4, 5, 6])
    _(obj).must_equal [4, 5, 6]
  end

  it "assigns bindata objects" do
    obj.assign([BinData::Uint32le.new(4), BinData::Uint32le.new(5), BinData::Uint32le.new(6)])
    _(obj).must_equal [4, 5, 6]
  end

  it "assigns a bindata array" do
    array = BinData::Array.new([4, 5, 6], type: :uint32le)
    obj.assign(array)
    _(obj).must_equal [4, 5, 6]
  end

  it "returns the first element" do
    _(obj.first).must_equal 1
  end

  it "returns the first n elements" do
    _(obj[0...3]).must_equal [1, 2, 3]
    _(obj.first(3)).must_equal [1, 2, 3]
    _(obj.first(99)).must_equal [1, 2, 3, 4, 5]
  end

  it "returns the last element" do
    _(obj.last).must_equal 5
    _(obj[-1]).must_equal 5
  end

  it "returns the last n elements" do
    _(obj.last(3)).must_equal [3, 4, 5]
    _(obj.last(99)).must_equal [1, 2, 3, 4, 5]

    _(obj[-3, 100]).must_equal [3, 4, 5]
  end

  it "clears all" do
    obj[1] = 8
    obj.clear
    _(obj).must_equal [1, 2, 3, 4, 5]
  end

  it "clears a single element" do
    obj[1] = 8
    obj[1].clear
    _(obj[1]).must_equal 2
  end

  it "is clear if all elements are clear" do
    obj[1] = 8
    obj[1].clear
    assert obj.clear?
  end

  it "tests clear status of individual elements" do
    obj[1] = 8
    assert obj[0].clear?
    refute obj[1].clear?
  end

  it "directly accesses elements" do
    obj[1] = 8
    _(obj[1]).must_equal 8
  end

  it "symmetrically reads and writes" do
    obj[1] = 8
    str = obj.to_binary_s

    obj.clear
    _(obj[1]).must_equal 2

    obj.read(str)
    _(obj[1]).must_equal 8
  end

  it "identifies index of elements" do
    _(obj.index(3)).must_equal 2
  end

  it "returns nil for index of non existent element" do
    _(obj.index(42)).must_be_nil
  end

  it "has correct debug name" do
    _(obj[2].debug_name).must_equal "obj[2]"
  end

  it "has correct offset" do
    _(obj[2].rel_offset).must_equal 2 * 4
  end

  it "has correct num_bytes" do
    _(obj.num_bytes).must_equal 5 * 4
  end

  it "has correct num_bytes for individual elements" do
    _(obj[0].num_bytes).must_equal 4
  end
end

describe BinData::Array, "when accessing elements" do
  let(:obj) {
    type = [:uint32le, {initial_value: -> { index + 1 }}]
    data = BinData::Array.new(type: type, initial_length: 5)
    data.assign([1, 2, 3, 4, 5])
    data
  }

  it "inserts with positive indexes" do
    obj.insert(2, 30, 40)
    _(obj.snapshot).must_equal [1, 2, 30, 40, 3, 4, 5]
  end

  it "inserts with negative indexes" do
    obj.insert(-2, 30, 40)
    _(obj.snapshot).must_equal [1, 2, 3, 4, 30, 40, 5]
  end

  it "pushes" do
    obj.push(30, 40)
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5, 30, 40]
  end

  it "concats" do
    obj.concat([30, 40])
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5, 30, 40]
  end

  it "unshifts" do
    obj.unshift(30, 40)
    _(obj.snapshot).must_equal [30, 40, 1, 2, 3, 4, 5]
  end

  it "automatically extends on [index]" do
    _(obj[9]).must_equal 10
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  end

  it "automatically extends on []=" do
    obj[9] = 30
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 30]
  end

  it "automatically extends on insert" do
    obj.insert(7, 30, 40)
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5, 6, 7, 30, 40]
  end

  it "does not extend on at" do
    _(obj.at(9)).must_be_nil
    _(obj.length).must_equal 5
  end

  it "does not extend on [start, length]" do
    _(obj[9, 2]).must_be_nil
    _(obj.length).must_equal 5
  end

  it "does not extend on [range]" do
    _(obj[9 .. 10]).must_be_nil
    _(obj.length).must_equal 5
  end

  it "raises error on bad input to []" do
    _ { obj["a"] }.must_raise TypeError
    _ { obj[1, "a"] }.must_raise TypeError
  end

  it "is unaffected by self assignment" do
    obj.assign(obj)
    _(obj.snapshot).must_equal [1, 2, 3, 4, 5]
  end
end

describe BinData::Array, "with :read_until" do

  describe "containing +element+" do
    it "reads until the sentinel is reached" do
      read_until = lambda { element == 5 }
      obj = BinData::Array.new(type: :int8, read_until: read_until)

      obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
      _(obj).must_equal [1, 2, 3, 4, 5]
    end
  end

  describe "containing +array+ and +index+" do
    it "reads until the sentinel is reached" do
      read_until = lambda { index >= 2 and array[index - 2] == 5 }
      obj = BinData::Array.new(type: :int8, read_until: read_until)

      obj.read "\x01\x02\x03\x04\x05\x06\x07\x08"
      _(obj).must_equal [1, 2, 3, 4, 5, 6, 7]
    end
  end

  describe ":eof" do
    it "reads records until eof" do
      obj = BinData::Array.new(type: :int8, read_until: :eof)

      obj.read "\x01\x02\x03"
      _(obj).must_equal [1, 2, 3]
    end

    it "reads records until eof, ignoring partial records" do
      obj = BinData::Array.new(type: :int16be, read_until: :eof)

      obj.read "\x00\x01\x00\x02\x03"
      _(obj).must_equal [1, 2]
    end

    it "reports exceptions" do
      array_type = [:string, {read_length: -> { unknown_variable }}]
      obj = BinData::Array.new(type: array_type, read_until: :eof)
      _ { obj.read "\x00\x01\x00\x02\x03" }.must_raise NoMethodError
    end
  end
end

describe BinData::Array, "nested within an Array" do
  let(:obj) {
    nested_array_params = { type: [:int8, { initial_value: :index }],
                            initial_length: -> { index + 1 } }
    BinData::Array.new(type: [:array, nested_array_params],
                       initial_length: 3)
  }

  it "#snapshot" do
    _(obj.snapshot).must_equal [ [0], [0, 1], [0, 1, 2] ]
  end

  it "maintains structure when reading" do
    obj.read "\x04\x05\x06\x07\x08\x09"
    _(obj).must_equal [ [4], [5, 6], [7, 8, 9] ]
  end
end

describe BinData::Array, "subclassed" do
  class IntArray < BinData::Array
    endian :big
    default_parameter initial_element_value: 0

    uint16 initial_value: :initial_element_value
  end

  it "forwards parameters" do
    obj = IntArray.new(initial_length: 7)
    _(obj.length).must_equal 7
  end

  it "overrides default parameters" do
    obj = IntArray.new(initial_length: 3, initial_element_value: 5)
    _(obj.to_binary_s).must_equal_binary "\x00\x05\x00\x05\x00\x05"
  end
end

describe BinData::Array, "of bits" do
  let(:obj) { BinData::Array.new(type: :bit1, initial_length: 15) }

  it "reads" do
    str = [0b0001_0100, 0b1000_1000].pack("CC")
    obj.read(str)
    _(obj[0]).must_equal  0
    _(obj[1]).must_equal  0
    _(obj[2]).must_equal  0
    _(obj[3]).must_equal  1
    _(obj[4]).must_equal  0
    _(obj[5]).must_equal  1
    _(obj[6]).must_equal  0
    _(obj[7]).must_equal  0
    _(obj[8]).must_equal  1
    _(obj[9]).must_equal  0
    _(obj[10]).must_equal 0
    _(obj[11]).must_equal 0
    _(obj[12]).must_equal 1
    _(obj[13]).must_equal 0
    _(obj[14]).must_equal 0
  end

  it "writes" do
    obj[3] = 1
    _(obj.to_binary_s).must_equal_binary [0b0001_0000, 0b0000_0000].pack("CC")
  end

  it "returns num_bytes" do
    _(obj.num_bytes).must_equal 2
  end

  it "has correct offset" do
    _(obj[7].rel_offset).must_equal 0
    _(obj[8].rel_offset).must_equal 1
  end
end