summaryrefslogtreecommitdiff
path: root/test/-ext-/bignum/test_pack.rb
blob: f2a3df814e03154bc6c59722f533936c00ebae64 (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
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
396
397
398
# coding: ASCII-8BIT

require 'test/unit'
require "-test-/bignum"

class TestBignum < Test::Unit::TestCase
  class TestPack < Test::Unit::TestCase

    MSWORD_FIRST = Integer::INTEGER_PACK_MSWORD_FIRST
    LSWORD_FIRST = Integer::INTEGER_PACK_LSWORD_FIRST
    MSBYTE_FIRST = Integer::INTEGER_PACK_MSBYTE_FIRST
    LSBYTE_FIRST = Integer::INTEGER_PACK_LSBYTE_FIRST
    NATIVE_BYTE_ORDER = Integer::INTEGER_PACK_NATIVE_BYTE_ORDER
    TWOCOMP = Integer::INTEGER_PACK_2COMP
    LITTLE_ENDIAN = Integer::INTEGER_PACK_LITTLE_ENDIAN
    BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN
    NEGATIVE = Integer::INTEGER_PACK_NEGATIVE
    GENERIC = Integer::INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION

    def test_pack_zero
      assert_equal([0, ""], 0.test_pack(0, 1, 0, BIG_ENDIAN))
    end

    def test_pack_argument_check
      assert_raise(ArgumentError) { 0.test_pack_raw("", 2, 1, 0, MSBYTE_FIRST) }
      assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 1, 0, MSWORD_FIRST) }
      assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 0, 0, BIG_ENDIAN) }
      assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 1, 8, BIG_ENDIAN) }

      # assume sizeof(ssize_t) == sizeof(intptr_t)
      assert_raise(ArgumentError) { 0.test_pack_raw("", 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
    end

    def test_pack_wordsize
      assert_equal([1, "\x01"], 1.test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([1, "\x00\x01"], 1.test_pack(1, 2, 0, BIG_ENDIAN))
      assert_equal([1, "\x00\x00\x01"], 1.test_pack(1, 3, 0, BIG_ENDIAN))
      assert_equal([1, "\x01"], 1.test_pack(1, 1, 0, LITTLE_ENDIAN))
      assert_equal([1, "\x01\x00"], 1.test_pack(1, 2, 0, LITTLE_ENDIAN))
      assert_equal([1, "\x01\x00\x00"], 1.test_pack(1, 3, 0, LITTLE_ENDIAN))
    end

    def test_pack_fixed_buffer
      assert_equal([0, "\x00\x00"], 0.test_pack(2, 1, 0, BIG_ENDIAN))
      assert_equal([1, "\x00\x01"], 0x01.test_pack(2, 1, 0, BIG_ENDIAN))
      assert_equal([1, "\x02\x01"], 0x0201.test_pack(2, 1, 0, BIG_ENDIAN))
      assert_equal([2, "\x02\x01"], 0x030201.test_pack(2, 1, 0, BIG_ENDIAN))
      assert_equal([2, "\x02\x01"], 0x04030201.test_pack(2, 1, 0, BIG_ENDIAN))
      assert_equal([0, "\x00\x00"], 0.test_pack(2, 1, 0, LITTLE_ENDIAN))
      assert_equal([1, "\x01\x00"], 0x01.test_pack(2, 1, 0, LITTLE_ENDIAN))
      assert_equal([1, "\x01\x02"], 0x0201.test_pack(2, 1, 0, LITTLE_ENDIAN))
      assert_equal([2, "\x01\x02"], 0x030201.test_pack(2, 1, 0, LITTLE_ENDIAN))
      assert_equal([2, "\x01\x02"], 0x04030201.test_pack(2, 1, 0, LITTLE_ENDIAN))
    end

    def test_pack_wordorder_and_endian
      assert_equal([1, "\x12\x34\x56\x78"], 0x12345678.test_pack(2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
      assert_equal([1, "\x34\x12\x78\x56"], 0x12345678.test_pack(2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
      assert_equal([1, "\x56\x78\x12\x34"], 0x12345678.test_pack(2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
      assert_equal([1, "\x78\x56\x34\x12"], 0x12345678.test_pack(2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
    end

    def test_pack_native_endian
      assert_equal([1, [0x1234].pack("S!")], 0x1234.test_pack(1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
    end

    def test_pack_nail
      assert_equal([1, "\x01\x00\x00\x00\x01\x01"], 0b100011.test_pack(6, 1, 7, BIG_ENDIAN))
      assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08"], 0x12345678.test_pack(8, 1, 4, BIG_ENDIAN))
      assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78"], 0x12345678.test_pack(4, 2, 8, BIG_ENDIAN))
    end

    def test_pack_overflow
      assert_equal([-2, "\x1"], (-0x11).test_pack(1, 1, 4, BIG_ENDIAN))
      assert_equal([-2, "\x0"], (-0x10).test_pack(1, 1, 4, BIG_ENDIAN))
      assert_equal([-1, "\xF"], (-0x0F).test_pack(1, 1, 4, BIG_ENDIAN))
      assert_equal([+1, "\xF"], (+0x0F).test_pack(1, 1, 4, BIG_ENDIAN))
      assert_equal([+2, "\x0"], (+0x10).test_pack(1, 1, 4, BIG_ENDIAN))
      assert_equal([+2, "\x1"], (+0x11).test_pack(1, 1, 4, BIG_ENDIAN))

      assert_equal([-2, "\x01"], (-0x101).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([-2, "\x00"], (-0x100).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([-1, "\xFF"], (-0x0FF).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([+1, "\xFF"], (+0x0FF).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([+2, "\x00"], (+0x100).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([+2, "\x01"], (+0x101).test_pack(1, 1, 0, BIG_ENDIAN))

      assert_equal([-2, "\x00\x00\x00\x00\x00\x00\x00\x01"], (-0x10000000000000001).test_pack(2, 4, 0, BIG_ENDIAN))
      assert_equal([-2, "\x00\x00\x00\x00\x00\x00\x00\x00"], (-0x10000000000000000).test_pack(2, 4, 0, BIG_ENDIAN))
      assert_equal([-1, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"], (-0x0FFFFFFFFFFFFFFFF).test_pack(2, 4, 0, BIG_ENDIAN))
      assert_equal([+1, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"], (+0x0FFFFFFFFFFFFFFFF).test_pack(2, 4, 0, BIG_ENDIAN))
      assert_equal([+2, "\x00\x00\x00\x00\x00\x00\x00\x00"], (+0x10000000000000000).test_pack(2, 4, 0, BIG_ENDIAN))
      assert_equal([+2, "\x00\x00\x00\x00\x00\x00\x00\x01"], (+0x10000000000000001).test_pack(2, 4, 0, BIG_ENDIAN))

      1.upto(16) {|wordsize|
        1.upto(20) {|numwords|
          w = numwords*wordsize
          n = 256**w
          assert_equal([-2, "\x00"*(w-1)+"\x01"], (-n-1).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
          assert_equal([-2, "\x00"*w],            (-n  ).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
          assert_equal([-1, "\xFF"*w],            (-n+1).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
          assert_equal([+1, "\xFF"*w],            (+n-1).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
          assert_equal([+2, "\x00"*w],            (+n  ).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
          assert_equal([+2, "\x00"*(w-1)+"\x01"], (+n+1).test_pack(numwords, wordsize, 0, BIG_ENDIAN))
        }
      }

      1.upto(16) {|wordsize|
        1.upto(20) {|numwords|
          w = numwords*wordsize
          n = 256**w
          assert_equal([-2, "\x01"+"\x00"*(w-1)], (-n-1).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
          assert_equal([-2, "\x00"*w],            (-n  ).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
          assert_equal([-1, "\xFF"*w],            (-n+1).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
          assert_equal([+1, "\xFF"*w],            (+n-1).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
          assert_equal([+2, "\x00"*w],            (+n  ).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
          assert_equal([+2, "\x01"+"\x00"*(w-1)], (+n+1).test_pack(numwords, wordsize, 0, LITTLE_ENDIAN))
        }
      }
    end

    def test_pack_sign
      assert_equal([-1, "\x01"], (-1).test_pack(1, 1, 0, BIG_ENDIAN))
      assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10"], (-0x8070605040302010).test_pack(8, 1, 0, BIG_ENDIAN))
    end

    def test_pack_orders
      [MSWORD_FIRST, LSWORD_FIRST].each {|word_order|
        [MSBYTE_FIRST, LSBYTE_FIRST, NATIVE_BYTE_ORDER].each {|byte_order|
          1.upto(16) {|wordsize|
            1.upto(20) {|numwords|
              w = numwords*wordsize
              n = 0;
              0.upto(w) {|i|
                n |= ((i+1) % 256) << (i*8)
              }
              assert_equal(n.test_pack(numwords, wordsize, 0, word_order|byte_order|GENERIC),
                           n.test_pack(numwords, wordsize, 0, word_order|byte_order),
                          "#{'%#x' % n}.test_pack(#{numwords}, #{wordsize}, 0, #{'%#x' % (word_order|byte_order)})")
            }
          }
        }
      }
    end

    def test_pack2comp_zero
      assert_equal([0, ""], 0.test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
    end

    def test_pack2comp_emptybuf
      assert_equal([-2, ""], (-3).test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-2, ""], (-2).test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, ""], (-1).test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([ 0, ""], 0.test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, ""], 1.test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, ""], 2.test_pack(0, 1, 0, TWOCOMP|BIG_ENDIAN))
    end

    def test_pack2comp_nearly_zero
      assert_equal([-1, "\xFE"], (-2).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\xFF"], (-1).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([ 0, "\x00"], 0.test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+1, "\x01"], 1.test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+1, "\x02"], 2.test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
    end

    def test_pack2comp_overflow
      assert_equal([-2, "\xF"], (-0x11).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x0"], (-0x10).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x1"], (-0x0F).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))
      assert_equal([+1, "\xF"], (+0x0F).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x0"], (+0x10).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x1"], (+0x11).test_pack(1, 1, 4, TWOCOMP|BIG_ENDIAN))

      assert_equal([-2, "\xFF"], (-0x101).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x00"], (-0x100).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x01"], (-0x0FF).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+1, "\xFF"], (+0x0FF).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x00"], (+0x100).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x01"], (+0x101).test_pack(1, 1, 0, TWOCOMP|BIG_ENDIAN))

      assert_equal([-2, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"], (-0x10000000000000001).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x00\x00\x00\x00\x00\x00\x00\x00"], (-0x10000000000000000).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([-1, "\x00\x00\x00\x00\x00\x00\x00\x01"], (-0x0FFFFFFFFFFFFFFFF).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+1, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"], (+0x0FFFFFFFFFFFFFFFF).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x00\x00\x00\x00\x00\x00\x00\x00"], (+0x10000000000000000).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal([+2, "\x00\x00\x00\x00\x00\x00\x00\x01"], (+0x10000000000000001).test_pack(2, 4, 0, TWOCOMP|BIG_ENDIAN))

      1.upto(16) {|wordsize|
        1.upto(20) {|numwords|
          w = numwords*wordsize
          n = 256**w
          assert_equal([-2, "\xFF"*w           ], (-n-1).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
          assert_equal([-1, "\x00"*w],            (-n  ).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
          assert_equal([-1, "\x00"*(w-1)+"\x01"], (-n+1).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
          assert_equal([+1, "\xFF"*w],            (+n-1).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
          assert_equal([+2, "\x00"*w],            (+n  ).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
          assert_equal([+2, "\x00"*(w-1)+"\x01"], (+n+1).test_pack(numwords, wordsize, 0, TWOCOMP|BIG_ENDIAN))
        }
      }

      1.upto(16) {|wordsize|
        1.upto(20) {|numwords|
          w = numwords*wordsize
          n = 256**w
          assert_equal([-2, "\xFF"*w           ], (-n-1).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
          assert_equal([-1, "\x00"*w],            (-n  ).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
          assert_equal([-1, "\x01"+"\x00"*(w-1)], (-n+1).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
          assert_equal([+1, "\xFF"*w],            (+n-1).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
          assert_equal([+2, "\x00"*w],            (+n  ).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
          assert_equal([+2, "\x01"+"\x00"*(w-1)], (+n+1).test_pack(numwords, wordsize, 0, TWOCOMP|LITTLE_ENDIAN))
        }
      }

      2.upto(16) {|wordsize|
        w = wordsize
        b = 8*wordsize-1
        n = 2**b
        assert_equal([-2, "\x7F"+"\xFF"*(w-2)+"\xFF"], (-n-1).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
        assert_equal([-1, "\x00"+"\x00"*(w-2)+"\x00"], (-n  ).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
        assert_equal([-1, "\x00"+"\x00"*(w-2)+"\x01"], (-n+1).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
        assert_equal([+1, "\x7F"+"\xFF"*(w-2)+"\xFF"], (+n-1).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
        assert_equal([+2, "\x00"+"\x00"*(w-2)+"\x00"], (+n  ).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
        assert_equal([+2, "\x00"+"\x00"*(w-2)+"\x01"], (+n+1).test_pack(1, wordsize, 1, TWOCOMP|MSBYTE_FIRST))
      }

      2.upto(16) {|wordsize|
        w = wordsize
        b = 8*wordsize-1
        n = 2**b
        assert_equal([-2, "\xFF"+"\xFF"*(w-2)+"\x7F"], (-n-1).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
        assert_equal([-1, "\x00"+"\x00"*(w-2)+"\x00"], (-n  ).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
        assert_equal([-1, "\x01"+"\x00"*(w-2)+"\x00"], (-n+1).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
        assert_equal([+1, "\xFF"+"\xFF"*(w-2)+"\x7F"], (+n-1).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
        assert_equal([+2, "\x00"+"\x00"*(w-2)+"\x00"], (+n  ).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
        assert_equal([+2, "\x01"+"\x00"*(w-2)+"\x00"], (+n+1).test_pack(1, wordsize, 1, TWOCOMP|LSBYTE_FIRST))
      }

    end

    def test_unpack_zero
      assert_equal(0, Integer.test_unpack("", 0, 1, 0, BIG_ENDIAN))
    end

    def test_unpack_argument_check
      assert_raise(ArgumentError) { Integer.test_unpack("x", 2, 1, 0, MSBYTE_FIRST) }
      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 0, MSWORD_FIRST) }
      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 0, 0, BIG_ENDIAN) }
      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 8, BIG_ENDIAN) }

      # assume sizeof(ssize_t) == sizeof(intptr_t)
      assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
    end

    def test_unpack_wordsize
      assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN))
      assert_equal(1, Integer.test_unpack("\x00\x01", 1, 2, 0, BIG_ENDIAN))
      assert_equal(1, Integer.test_unpack("\x00\x00\x01", 1, 3, 0, BIG_ENDIAN))
      assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, LITTLE_ENDIAN))
      assert_equal(1, Integer.test_unpack("\x01\x00", 1, 2, 0, LITTLE_ENDIAN))
      assert_equal(1, Integer.test_unpack("\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN))
    end

    def test_unpack_wordorder_and_endian
      assert_equal(0x01020304, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
      assert_equal(0x02010403, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
      assert_equal(0x03040102, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
      assert_equal(0x04030201, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
    end

    def test_unpack_native_endian
      assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack("\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
    end

    def test_unpack_nail
      assert_equal(0b100011, Integer.test_unpack("\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN))
      assert_equal(0x12345678, Integer.test_unpack("\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN))
      assert_equal(0x12345678, Integer.test_unpack("\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN))
    end

    def test_unpack_sign
      assert_equal(-1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN|NEGATIVE))
      assert_equal(-0x8070605040302010, Integer.test_unpack("\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN|NEGATIVE))
    end

    def test_unpack_orders
      [MSWORD_FIRST, LSWORD_FIRST].each {|word_order|
        [MSBYTE_FIRST, LSBYTE_FIRST, NATIVE_BYTE_ORDER].each {|byte_order|
          1.upto(16) {|wordsize|
            1.upto(20) {|numwords|
              w = numwords*wordsize
              ary = []
              0.upto(w) {|i|
                ary << ((i+1) % 256);
              }
              str = ary.pack("C*")
              flags = word_order|byte_order
              assert_equal(Integer.test_unpack(str, numwords, wordsize, 0, flags|GENERIC),
                           Integer.test_unpack(str, numwords, wordsize, 0, flags),
                          "Integer.test_unpack(#{str.dump}, #{numwords}, #{wordsize}, 0, #{'%#x' % flags})")
            }
          }
        }
      }
    end

    def test_unpack2comp_single_byte
      assert_equal(-128, Integer.test_unpack("\x80", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(  -2, Integer.test_unpack("\xFE", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(  -1, Integer.test_unpack("\xFF", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(   0, Integer.test_unpack("\x00", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(   1, Integer.test_unpack("\x01", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(   2, Integer.test_unpack("\x02", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal( 127, Integer.test_unpack("\x7F", 1, 1, 0, TWOCOMP|BIG_ENDIAN))
    end

    def test_unpack2comp_sequence_of_ff
      assert_equal(-1, Integer.test_unpack("\xFF"*2, 2, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*3, 3, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*4, 4, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*5, 5, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*6, 6, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*7, 7, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*8, 8, 1, 0, TWOCOMP|BIG_ENDIAN))
      assert_equal(-1, Integer.test_unpack("\xFF"*9, 9, 1, 0, TWOCOMP|BIG_ENDIAN))
    end

    def test_unpack2comp_negative_single_byte
      assert_equal(-256, Integer.test_unpack("\x00", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(-255, Integer.test_unpack("\x01", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(-254, Integer.test_unpack("\x02", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(-129, Integer.test_unpack("\x7F", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(-128, Integer.test_unpack("\x80", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(  -2, Integer.test_unpack("\xFE", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
      assert_equal(  -1, Integer.test_unpack("\xFF", 1, 1, 0, TWOCOMP|BIG_ENDIAN|NEGATIVE))
    end

    def test_unpack2comp_negative_zero
      0.upto(100) {|n|
        str = "\x00"*n
        flags = TWOCOMP|BIG_ENDIAN|NEGATIVE
        assert_equal(-(256**n), Integer.test_unpack(str, n, 1, 0, flags))
        flags = TWOCOMP|LITTLE_ENDIAN|NEGATIVE
        assert_equal(-(256**n), Integer.test_unpack(str, n, 1, 0, flags),
                    "Integer.test_unpack(#{str.dump}, #{n}, 1, 0, #{'%#x' % flags})")
      }
    end
  end

  def test_numbits_2comp
    assert_equal(4, -9.test_numbits_2comp_without_sign)
    assert_equal(3, -8.test_numbits_2comp_without_sign)
    assert_equal(3, -7.test_numbits_2comp_without_sign)
    assert_equal(3, -6.test_numbits_2comp_without_sign)
    assert_equal(3, -5.test_numbits_2comp_without_sign)
    assert_equal(2, -4.test_numbits_2comp_without_sign)
    assert_equal(2, -3.test_numbits_2comp_without_sign)
    assert_equal(1, -2.test_numbits_2comp_without_sign)
    assert_equal(0, -1.test_numbits_2comp_without_sign)
    assert_equal(0, 0.test_numbits_2comp_without_sign)
    assert_equal(1, 1.test_numbits_2comp_without_sign)
    assert_equal(2, 2.test_numbits_2comp_without_sign)
    assert_equal(2, 3.test_numbits_2comp_without_sign)
    assert_equal(3, 4.test_numbits_2comp_without_sign)
    assert_equal(3, 5.test_numbits_2comp_without_sign)
    assert_equal(3, 6.test_numbits_2comp_without_sign)
    assert_equal(3, 7.test_numbits_2comp_without_sign)
    assert_equal(4, 8.test_numbits_2comp_without_sign)
    assert_equal(4, 9.test_numbits_2comp_without_sign)
  end

  def test_numbytes_2comp
    assert_equal(6, -0x8000000001.test_numbytes_2comp_with_sign)
    assert_equal(5, -0x8000000000.test_numbytes_2comp_with_sign)
    assert_equal(5, -0x80000001.test_numbytes_2comp_with_sign)
    assert_equal(4, -0x80000000.test_numbytes_2comp_with_sign)
    assert_equal(4, -0x800001.test_numbytes_2comp_with_sign)
    assert_equal(3, -0x800000.test_numbytes_2comp_with_sign)
    assert_equal(3, -0x8001.test_numbytes_2comp_with_sign)
    assert_equal(2, -0x8000.test_numbytes_2comp_with_sign)
    assert_equal(2, -0x81.test_numbytes_2comp_with_sign)
    assert_equal(1, -0x80.test_numbytes_2comp_with_sign)
    assert_equal(1, -1.test_numbytes_2comp_with_sign)
    assert_equal(1, 0.test_numbytes_2comp_with_sign)
    assert_equal(1, 1.test_numbytes_2comp_with_sign)
    assert_equal(1, 0x7f.test_numbytes_2comp_with_sign)
    assert_equal(2, 0x80.test_numbytes_2comp_with_sign)
    assert_equal(2, 0x7fff.test_numbytes_2comp_with_sign)
    assert_equal(3, 0x8000.test_numbytes_2comp_with_sign)
    assert_equal(3, 0x7fffff.test_numbytes_2comp_with_sign)
    assert_equal(4, 0x800000.test_numbytes_2comp_with_sign)
    assert_equal(4, 0x7fffffff.test_numbytes_2comp_with_sign)
    assert_equal(5, 0x80000000.test_numbytes_2comp_with_sign)
    assert_equal(5, 0x7fffffffff.test_numbytes_2comp_with_sign)
    assert_equal(6, 0x8000000000.test_numbytes_2comp_with_sign)
  end

end