summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/unpack/shared/integer.rb
blob: cbaa743683afb5d72f866eaaef6f45ce722d8e1b (plain)
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
# -*- encoding: binary -*-

describe :string_unpack_16bit_le, shared: true do
  it "decodes one short for a single format character" do
    "ab".unpack(unpack_format).should == [25185]
  end

  it "decodes two shorts for two format characters" do
    "abcd".unpack(unpack_format(nil, 2)).should == [25185, 25699]
  end

  it "decodes the number of shorts requested by the count modifier" do
    "abcdef".unpack(unpack_format(3)).should == [25185, 25699, 26213]
  end

  it "decodes the remaining shorts when passed the '*' modifier" do
    "abcd".unpack(unpack_format('*')).should == [25185, 25699]
  end

  it "decodes the remaining shorts when passed the '*' modifier after another directive" do
    "abcd".unpack(unpack_format()+unpack_format('*')).should == [25185, 25699]
  end

  it "does not decode a short when fewer bytes than a short remain and the '*' modifier is passed" do
    "\xff".unpack(unpack_format('*')).should == []
  end

  it "adds nil for each element requested beyond the end of the String" do
    [ ["",     [nil, nil, nil]],
      ["abc",  [25185, nil, nil]],
      ["abcd", [25185, 25699, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end

  it "ignores NULL bytes between directives" do
    "abcd".unpack(unpack_format("\000", 2)).should == [25185, 25699]
  end

  it "ignores spaces between directives" do
    "abcd".unpack(unpack_format(' ', 2)).should == [25185, 25699]
  end
end

describe :string_unpack_16bit_le_signed, shared: true do
  it "decodes a short with most significant bit set as a negative number" do
    "\x00\xff".unpack(unpack_format()).should == [-256]
  end
end

describe :string_unpack_16bit_le_unsigned, shared: true do
  it "decodes a short with most significant bit set as a positive number" do
    "\x00\xff".unpack(unpack_format()).should == [65280]
  end
end

describe :string_unpack_16bit_be, shared: true do
  it "decodes one short for a single format character" do
    "ba".unpack(unpack_format).should == [25185]
  end

  it "decodes two shorts for two format characters" do
    "badc".unpack(unpack_format(nil, 2)).should == [25185, 25699]
  end

  it "decodes the number of shorts requested by the count modifier" do
    "badcfe".unpack(unpack_format(3)).should == [25185, 25699, 26213]
  end

  it "decodes the remaining shorts when passed the '*' modifier" do
    "badc".unpack(unpack_format('*')).should == [25185, 25699]
  end

  it "decodes the remaining shorts when passed the '*' modifier after another directive" do
    "badc".unpack(unpack_format()+unpack_format('*')).should == [25185, 25699]
  end

  it "does not decode a short when fewer bytes than a short remain and the '*' modifier is passed" do
    "\xff".unpack(unpack_format('*')).should == []
  end

  it "adds nil for each element requested beyond the end of the String" do
    [ ["",     [nil, nil, nil]],
      ["bac",  [25185, nil, nil]],
      ["badc", [25185, 25699, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end

  it "ignores NULL bytes between directives" do
    "badc".unpack(unpack_format("\000", 2)).should == [25185, 25699]
  end

  it "ignores spaces between directives" do
    "badc".unpack(unpack_format(' ', 2)).should == [25185, 25699]
  end
end

describe :string_unpack_16bit_be_signed, shared: true do
  it "decodes a short with most significant bit set as a negative number" do
    "\xff\x00".unpack(unpack_format()).should == [-256]
  end
end

describe :string_unpack_16bit_be_unsigned, shared: true do
  it "decodes a short with most significant bit set as a positive number" do
    "\xff\x00".unpack(unpack_format()).should == [65280]
  end
end

describe :string_unpack_32bit_le, shared: true do
  it "decodes one int for a single format character" do
    "abcd".unpack(unpack_format).should == [1684234849]
  end

  it "decodes two ints for two format characters" do
    "abghefcd".unpack(unpack_format(nil, 2)).should == [1751605857, 1684235877]
  end

  it "decodes the number of ints requested by the count modifier" do
    "abcedfgh".unpack(unpack_format(2)).should == [1701012065, 1751606884]
  end

  it "decodes the remaining ints when passed the '*' modifier" do
    "acbdegfh".unpack(unpack_format('*')).should == [1684169569, 1751541605]
  end

  it "decodes the remaining ints when passed the '*' modifier after another directive" do
    "abcdefgh".unpack(unpack_format()+unpack_format('*')).should == [1684234849, 1751606885]
  end

  it "does not decode an int when fewer bytes than an int remain and the '*' modifier is passed" do
    "abc".unpack(unpack_format('*')).should == []
  end

  it "adds nil for each element requested beyond the end of the String" do
    [ ["",          [nil, nil, nil]],
      ["abcde",     [1684234849, nil, nil]],
      ["abcdefg",   [1684234849, nil, nil]],
      ["abcdefgh",  [1684234849, 1751606885, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end

  it "ignores NULL bytes between directives" do
    "abcdefgh".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885]
  end

  it "ignores spaces between directives" do
    "abcdefgh".unpack(unpack_format(' ', 2)).should == [1684234849, 1751606885]
  end
end

describe :string_unpack_32bit_le_signed, shared: true do
  it "decodes an int with most significant bit set as a negative number" do
    "\x00\xaa\x00\xff".unpack(unpack_format()).should == [-16733696]
  end
end

describe :string_unpack_32bit_le_unsigned, shared: true do
  it "decodes an int with most significant bit set as a positive number" do
    "\x00\xaa\x00\xff".unpack(unpack_format()).should == [4278233600]
  end
end

describe :string_unpack_32bit_be, shared: true do
  it "decodes one int for a single format character" do
    "dcba".unpack(unpack_format).should == [1684234849]
  end

  it "decodes two ints for two format characters" do
    "hgbadcfe".unpack(unpack_format(nil, 2)).should == [1751605857, 1684235877]
  end

  it "decodes the number of ints requested by the count modifier" do
    "ecbahgfd".unpack(unpack_format(2)).should == [1701012065, 1751606884]
  end

  it "decodes the remaining ints when passed the '*' modifier" do
    "dbcahfge".unpack(unpack_format('*')).should == [1684169569, 1751541605]
  end

  it "decodes the remaining ints when passed the '*' modifier after another directive" do
    "dcbahgfe".unpack(unpack_format()+unpack_format('*')).should == [1684234849, 1751606885]
  end

  it "does not decode an int when fewer bytes than an int remain and the '*' modifier is passed" do
    "abc".unpack(unpack_format('*')).should == []
  end

  it "adds nil for each element requested beyond the end of the String" do
    [ ["",          [nil, nil, nil]],
      ["dcbae",     [1684234849, nil, nil]],
      ["dcbaefg",   [1684234849, nil, nil]],
      ["dcbahgfe",  [1684234849, 1751606885, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end

  it "ignores NULL bytes between directives" do
    "dcbahgfe".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885]
  end

  it "ignores spaces between directives" do
    "dcbahgfe".unpack(unpack_format(' ', 2)).should == [1684234849, 1751606885]
  end
end

describe :string_unpack_32bit_be_signed, shared: true do
  it "decodes an int with most significant bit set as a negative number" do
    "\xff\x00\xaa\x00".unpack(unpack_format()).should == [-16733696]
  end
end

describe :string_unpack_32bit_be_unsigned, shared: true do
  it "decodes an int with most significant bit set as a positive number" do
    "\xff\x00\xaa\x00".unpack(unpack_format()).should == [4278233600]
  end
end

describe :string_unpack_64bit_le, shared: true do
  it "decodes one long for a single format character" do
    "abcdefgh".unpack(unpack_format).should == [7523094288207667809]
  end

  it "decodes two longs for two format characters" do
    array = "abghefcdghefabcd".unpack(unpack_format(nil, 2))
    array.should == [7233738012216484449, 7233733596956420199]
  end

  it "decodes the number of longs requested by the count modifier" do
    array = "abcedfghefcdghef".unpack(unpack_format(2))
    array.should == [7523094283929477729, 7378418357791581797]
  end

  it "decodes the remaining longs when passed the '*' modifier" do
    array = "acbdegfhdegfhacb".unpack(unpack_format('*'))
    array.should == [7522813912742519649, 7089617339433837924]
  end

  it "decodes the remaining longs when passed the '*' modifier after another directive" do
    array = "bcahfgedhfgedbca".unpack(unpack_format()+unpack_format('*'))
    array.should == [7234302065976107874, 7017560827710891624]
  end

  it "does not decode a long when fewer bytes than a long remain and the '*' modifier is passed" do
    "abc".unpack(unpack_format('*')).should == []
  end

  it "ignores NULL bytes between directives" do
    array = "abcdefghabghefcd".unpack(unpack_format("\000", 2))
    array.should == [7523094288207667809, 7233738012216484449]
  end

  it "ignores spaces between directives" do
    array = "abcdefghabghefcd".unpack(unpack_format(' ', 2))
    array.should == [7523094288207667809, 7233738012216484449]
  end
end

describe :string_unpack_64bit_le_extra, shared: true do
  it "adds nil for each element requested beyond the end of the String" do
    [ ["",                  [nil, nil, nil]],
      ["abcdefgh",          [7523094288207667809, nil, nil]],
      ["abcdefghcdefab",    [7523094288207667809, nil, nil]],
      ["abcdefghcdefabde",  [7523094288207667809, 7306072665971057763, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end
end

describe :string_unpack_64bit_le_signed, shared: true do
  it "decodes a long with most significant bit set as a negative number" do
    "\x00\xcc\x00\xbb\x00\xaa\x00\xff".unpack(unpack_format()).should == [-71870673923814400]
  end
end

describe :string_unpack_64bit_le_unsigned, shared: true do
  it "decodes a long with most significant bit set as a positive number" do
    "\x00\xcc\x00\xbb\x00\xaa\x00\xff".unpack(unpack_format()).should == [18374873399785737216]
  end
end

describe :string_unpack_64bit_be, shared: true do
  it "decodes one long for a single format character" do
    "hgfedcba".unpack(unpack_format).should == [7523094288207667809]
  end

  it "decodes two longs for two format characters" do
    array = "dcfehgbadcbafehg".unpack(unpack_format(nil, 2))
    array.should == [7233738012216484449, 7233733596956420199]
  end

  it "decodes the number of longs requested by the count modifier" do
    array = "hgfdecbafehgdcfe".unpack(unpack_format(2))
    array.should == [7523094283929477729, 7378418357791581797]
  end

  it "decodes the remaining longs when passed the '*' modifier" do
    array = "hfgedbcabcahfged".unpack(unpack_format('*'))
    array.should == [7522813912742519649, 7089617339433837924]
  end

  it "decodes the remaining longs when passed the '*' modifier after another directive" do
    array = "degfhacbacbdegfh".unpack(unpack_format()+unpack_format('*'))
    array.should == [7234302065976107874, 7017560827710891624]
  end

  it "does not decode a long when fewer bytes than a long remain and the '*' modifier is passed" do
    "abc".unpack(unpack_format('*')).should == []
  end

  it "ignores NULL bytes between directives" do
    array = "hgfedcbadcfehgba".unpack(unpack_format("\000", 2))
    array.should == [7523094288207667809, 7233738012216484449]
  end

  it "ignores spaces between directives" do
    array = "hgfedcbadcfehgba".unpack(unpack_format(' ', 2))
    array.should == [7523094288207667809, 7233738012216484449]
  end
end

describe :string_unpack_64bit_be_extra, shared: true do
  it "adds nil for each element requested beyond the end of the String" do
    [ ["",                  [nil, nil, nil]],
      ["hgfedcba",          [7523094288207667809, nil, nil]],
      ["hgfedcbacdefab",    [7523094288207667809, nil, nil]],
      ["hgfedcbaedbafedc",  [7523094288207667809, 7306072665971057763, nil]]
    ].should be_computed_by(:unpack, unpack_format(3))
  end
end

describe :string_unpack_64bit_be_signed, shared: true do
  it "decodes a long with most significant bit set as a negative number" do
    "\xff\x00\xaa\x00\xbb\x00\xcc\x00".unpack(unpack_format()).should == [-71870673923814400]
  end
end

describe :string_unpack_64bit_be_unsigned, shared: true do
  it "decodes a long with most significant bit set as a positive number" do
    "\xff\x00\xaa\x00\xbb\x00\xcc\x00".unpack(unpack_format()).should == [18374873399785737216]
  end
end