~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/ruby/test_pack.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    assert_equal($x, $x.pack("l").unpack("l"))
21
21
  end
22
22
 
 
23
  def test_pack_n
 
24
    assert_equal "\000\000", [0].pack('n')
 
25
    assert_equal "\000\001", [1].pack('n')
 
26
    assert_equal "\000\002", [2].pack('n')
 
27
    assert_equal "\000\003", [3].pack('n')
 
28
    assert_equal "\377\376", [65534].pack('n')
 
29
    assert_equal "\377\377", [65535].pack('n')
 
30
 
 
31
    assert_equal "\200\000", [2**15].pack('n')
 
32
    assert_equal "\177\377", [-2**15-1].pack('n')
 
33
    assert_equal "\377\377", [-1].pack('n')
 
34
 
 
35
    assert_equal "\000\001\000\001", [1,1].pack('n*')
 
36
    assert_equal "\000\001\000\001\000\001", [1,1,1].pack('n*')
 
37
  end
 
38
 
 
39
  def test_unpack_n
 
40
    assert_equal 1, "\000\001".unpack('n')[0]
 
41
    assert_equal 2, "\000\002".unpack('n')[0]
 
42
    assert_equal 3, "\000\003".unpack('n')[0]
 
43
    assert_equal 65535, "\377\377".unpack('n')[0]
 
44
    assert_equal [1,1], "\000\001\000\001".unpack('n*')
 
45
    assert_equal [1,1,1], "\000\001\000\001\000\001".unpack('n*')
 
46
  end
 
47
 
23
48
  def test_pack_N
24
49
    assert_equal "\000\000\000\000", [0].pack('N')
25
50
    assert_equal "\000\000\000\001", [1].pack('N')
40
65
    assert_equal 1, "\000\000\000\001".unpack('N')[0]
41
66
    assert_equal 2, "\000\000\000\002".unpack('N')[0]
42
67
    assert_equal 3, "\000\000\000\003".unpack('N')[0]
43
 
    assert_equal 3, "\000\000\000\003".unpack('N')[0]
44
68
    assert_equal 4294967295, "\377\377\377\377".unpack('N')[0]
45
69
    assert_equal [1,1], "\000\000\000\001\000\000\000\001".unpack('N*')
46
70
    assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*')
47
71
  end
48
72
 
 
73
  def test_integer_endian
 
74
    s = [1].pack("s")
 
75
    assert_includes(["\0\1", "\1\0"], s)
 
76
    if s == "\0\1"
 
77
      # big endian
 
78
      assert_equal("\x01\x02", [0x0102].pack("s"))
 
79
      assert_equal("\x01\x02", [0x0102].pack("S"))
 
80
      assert_equal("\x01\x02\x03\x04", [0x01020304].pack("l"))
 
81
      assert_equal("\x01\x02\x03\x04", [0x01020304].pack("L"))
 
82
      assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("q"))
 
83
      assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("Q"))
 
84
      assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("s!"))
 
85
      assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("S!"))
 
86
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i"))
 
87
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I"))
 
88
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i!"))
 
89
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I!"))
 
90
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("l!"))
 
91
      assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("L!"))
 
92
      %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt|
 
93
        nuls = [0].pack(fmt)
 
94
        v = 0
 
95
        s = "".force_encoding("ascii-8bit")
 
96
        nuls.bytesize.times {|i|
 
97
          j = i + 40
 
98
          v = v * 256 + j
 
99
          s << [j].pack("C")
 
100
        }
 
101
        assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
 
102
        assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
 
103
        s2 = s+s
 
104
        fmt2 = fmt+"*"
 
105
        assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
 
106
      }
 
107
    else
 
108
      # little endian
 
109
      assert_equal("\x02\x01", [0x0102].pack("s"))
 
110
      assert_equal("\x02\x01", [0x0102].pack("S"))
 
111
      assert_equal("\x04\x03\x02\x01", [0x01020304].pack("l"))
 
112
      assert_equal("\x04\x03\x02\x01", [0x01020304].pack("L"))
 
113
      assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("q"))
 
114
      assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("Q"))
 
115
      assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("s!"))
 
116
      assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("S!"))
 
117
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i"))
 
118
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I"))
 
119
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i!"))
 
120
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I!"))
 
121
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("l!"))
 
122
      assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("L!"))
 
123
      %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt|
 
124
        nuls = [0].pack(fmt)
 
125
        v = 0
 
126
        s = "".force_encoding("ascii-8bit")
 
127
        nuls.bytesize.times {|i|
 
128
          j = i+40
 
129
          v = v * 256 + j
 
130
          s << [j].pack("C")
 
131
        }
 
132
        s.reverse!
 
133
        assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
 
134
        assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
 
135
        s2 = s+s
 
136
        fmt2 = fmt+"*"
 
137
        assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
 
138
      }
 
139
    end
 
140
  end
 
141
 
49
142
  def test_pack_U
50
143
    assert_raise(RangeError) { [-0x40000001].pack("U") }
51
144
    assert_raise(RangeError) { [-0x40000000].pack("U") }
238
331
    assert_equal(s1, s2)
239
332
    assert_equal([513, -514], s2.unpack("s!*"))
240
333
    assert_equal([513, 65022], s1.unpack("S!*"))
 
334
 
 
335
    assert_equal(2, [1].pack("s").bytesize)
 
336
    assert_equal(2, [1].pack("S").bytesize)
 
337
    assert_operator(2, :<=, [1].pack("s!").bytesize)
 
338
    assert_operator(2, :<=, [1].pack("S!").bytesize)
241
339
  end
242
340
 
243
341
  def test_pack_unpack_iI
251
349
    s2 = [67305985, 4244504319].pack("I!*")
252
350
    assert_equal([67305985, -50462977], s1.unpack("i!*"))
253
351
    assert_equal([67305985, 4244504319], s2.unpack("I!*"))
 
352
 
 
353
    assert_operator(4, :<=, [1].pack("i").bytesize)
 
354
    assert_operator(4, :<=, [1].pack("I").bytesize)
 
355
    assert_operator(4, :<=, [1].pack("i!").bytesize)
 
356
    assert_operator(4, :<=, [1].pack("I!").bytesize)
254
357
  end
255
358
 
256
359
  def test_pack_unpack_lL
264
367
    s2 = [67305985, 4244504319].pack("L!*")
265
368
    assert_equal([67305985, -50462977], s1.unpack("l!*"))
266
369
    assert_equal([67305985, 4244504319], s2.unpack("L!*"))
 
370
 
 
371
    assert_equal(4, [1].pack("l").bytesize)
 
372
    assert_equal(4, [1].pack("L").bytesize)
 
373
    assert_operator(4, :<=, [1].pack("l!").bytesize)
 
374
    assert_operator(4, :<=, [1].pack("L!").bytesize)
267
375
  end
268
376
 
269
377
  def test_pack_unpack_qQ
272
380
    assert_equal(s1, s2)
273
381
    assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*"))
274
382
    assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*"))
 
383
 
 
384
    assert_equal(8, [1].pack("q").bytesize)
 
385
    assert_equal(8, [1].pack("Q").bytesize)
275
386
  end
276
387
 
277
388
  def test_pack_unpack_nN
280
391
 
281
392
    assert_equal([0,1,65535,32767,32768,65535], "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*"))
282
393
    assert_equal([0,1,4294967295], "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*"))
 
394
 
 
395
    assert_equal(2, [1].pack("n").bytesize)
 
396
    assert_equal(4, [1].pack("N").bytesize)
283
397
  end
284
398
 
285
399
  def test_pack_unpack_vV
288
402
 
289
403
    assert_equal([0,1,65535,32767,32768,65535], "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*"))
290
404
    assert_equal([0,1,4294967295], "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*"))
 
405
 
 
406
    assert_equal(2, [1].pack("v").bytesize)
 
407
    assert_equal(4, [1].pack("V").bytesize)
291
408
  end
292
409
 
293
410
  def test_pack_unpack_fdeEgG
450
567
  end
451
568
 
452
569
  def test_pack_p2
453
 
    assert([nil].pack("p") =~ /\A\0*\Z/)
 
570
    assert_match(/\A\0*\Z/, [nil].pack("p"))
454
571
  end
455
572
 
456
573
  def test_pack_unpack_w
491
608
  def test_length_too_big
492
609
    assert_raise(RangeError) { [].pack("C100000000000000000000") }
493
610
  end
 
611
 
 
612
  def test_short_string
 
613
    %w[n N v V s S i I l L q Q s! S! i! I! l! l!].each {|fmt|
 
614
      str = [1].pack(fmt)
 
615
      assert_equal([1,nil], str.unpack("#{fmt}2"))
 
616
    }
 
617
  end
494
618
end