PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_pack.rb

http://github.com/ruby/ruby
Ruby | 872 lines | 767 code | 96 blank | 9 comment | 18 complexity | 28e6f4e34273f0f994a6cfe8748382bc MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # coding: US-ASCII
  2. # frozen_string_literal: false
  3. require 'test/unit'
  4. class TestPack < Test::Unit::TestCase
  5. def test_pack
  6. $format = "c2x5CCxsdils_l_a6";
  7. # Need the expression in here to force ary[5] to be numeric. This avoids
  8. # test2 failing because ary2 goes str->numeric->str and ary does not.
  9. ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
  10. $x = ary.pack($format)
  11. ary2 = $x.unpack($format)
  12. assert_equal(ary.length, ary2.length)
  13. assert_equal(ary.join(':'), ary2.join(':'))
  14. assert_match(/def/, $x)
  15. $x = [-1073741825]
  16. assert_equal($x, $x.pack("q").unpack("q"))
  17. $x = [-1]
  18. assert_equal($x, $x.pack("l").unpack("l"))
  19. end
  20. def test_pack_n
  21. assert_equal "\000\000", [0].pack('n')
  22. assert_equal "\000\001", [1].pack('n')
  23. assert_equal "\000\002", [2].pack('n')
  24. assert_equal "\000\003", [3].pack('n')
  25. assert_equal "\377\376", [65534].pack('n')
  26. assert_equal "\377\377", [65535].pack('n')
  27. assert_equal "\200\000", [2**15].pack('n')
  28. assert_equal "\177\377", [-2**15-1].pack('n')
  29. assert_equal "\377\377", [-1].pack('n')
  30. assert_equal "\000\001\000\001", [1,1].pack('n*')
  31. assert_equal "\000\001\000\001\000\001", [1,1,1].pack('n*')
  32. end
  33. def test_unpack_n
  34. assert_equal 1, "\000\001".unpack('n')[0]
  35. assert_equal 2, "\000\002".unpack('n')[0]
  36. assert_equal 3, "\000\003".unpack('n')[0]
  37. assert_equal 65535, "\377\377".unpack('n')[0]
  38. assert_equal [1,1], "\000\001\000\001".unpack('n*')
  39. assert_equal [1,1,1], "\000\001\000\001\000\001".unpack('n*')
  40. end
  41. def test_pack_N
  42. assert_equal "\000\000\000\000", [0].pack('N')
  43. assert_equal "\000\000\000\001", [1].pack('N')
  44. assert_equal "\000\000\000\002", [2].pack('N')
  45. assert_equal "\000\000\000\003", [3].pack('N')
  46. assert_equal "\377\377\377\376", [4294967294].pack('N')
  47. assert_equal "\377\377\377\377", [4294967295].pack('N')
  48. assert_equal "\200\000\000\000", [2**31].pack('N')
  49. assert_equal "\177\377\377\377", [-2**31-1].pack('N')
  50. assert_equal "\377\377\377\377", [-1].pack('N')
  51. assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*')
  52. assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*')
  53. end
  54. def test_unpack_N
  55. assert_equal 1, "\000\000\000\001".unpack('N')[0]
  56. assert_equal 2, "\000\000\000\002".unpack('N')[0]
  57. assert_equal 3, "\000\000\000\003".unpack('N')[0]
  58. assert_equal 4294967295, "\377\377\377\377".unpack('N')[0]
  59. assert_equal [1,1], "\000\000\000\001\000\000\000\001".unpack('N*')
  60. assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*')
  61. end
  62. def _integer_big_endian(mod='')
  63. assert_equal("\x01\x02", [0x0102].pack("s"+mod))
  64. assert_equal("\x01\x02", [0x0102].pack("S"+mod))
  65. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("l"+mod))
  66. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("L"+mod))
  67. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("q"+mod))
  68. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("Q"+mod))
  69. psize = [nil].pack('p').bytesize
  70. if psize == 4
  71. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("j"+mod))
  72. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("J"+mod))
  73. elsif psize == 8
  74. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("j"+mod))
  75. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("J"+mod))
  76. end
  77. assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("s!"+mod))
  78. assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("S!"+mod))
  79. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i"+mod))
  80. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I"+mod))
  81. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i!"+mod))
  82. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I!"+mod))
  83. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("l!"+mod))
  84. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("L!"+mod))
  85. if psize == 4
  86. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("j!"+mod))
  87. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("J!"+mod))
  88. elsif psize == 8
  89. assert_match(/\A\x00*\x01\x02\x03\x04\x05\x06\x07\x08\z/, [0x0102030405060708].pack("j!"+mod))
  90. assert_match(/\A\x00*\x01\x02\x03\x04\x05\x06\x07\x08\z/, [0x0102030405060708].pack("J!"+mod))
  91. end
  92. %w[s S l L q Q j J s! S! i I i! I! l! L! j! J!].each {|fmt|
  93. fmt += mod
  94. nuls = [0].pack(fmt)
  95. v = 0
  96. s = "".force_encoding("ascii-8bit")
  97. nuls.bytesize.times {|i|
  98. j = i + 40
  99. v = v * 256 + j
  100. s << [j].pack("C")
  101. }
  102. assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
  103. assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
  104. s2 = s+s
  105. fmt2 = fmt+"*"
  106. assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
  107. }
  108. end
  109. def _integer_little_endian(mod='')
  110. assert_equal("\x02\x01", [0x0102].pack("s"+mod))
  111. assert_equal("\x02\x01", [0x0102].pack("S"+mod))
  112. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("l"+mod))
  113. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("L"+mod))
  114. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("q"+mod))
  115. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("Q"+mod))
  116. psize = [nil].pack('p').bytesize
  117. if psize == 4
  118. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("j"+mod))
  119. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("J"+mod))
  120. elsif psize == 8
  121. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("j"+mod))
  122. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("J"+mod))
  123. end
  124. assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("s!"+mod))
  125. assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("S!"+mod))
  126. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i"+mod))
  127. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I"+mod))
  128. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i!"+mod))
  129. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I!"+mod))
  130. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("l!"+mod))
  131. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("L!"+mod))
  132. if psize == 4
  133. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("j!"+mod))
  134. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("J!"+mod))
  135. elsif psize == 8
  136. assert_match(/\A\x08\x07\x06\x05\x04\x03\x02\x01\x00*\z/, [0x0102030405060708].pack("j!"+mod))
  137. assert_match(/\A\x08\x07\x06\x05\x04\x03\x02\x01\x00*\z/, [0x0102030405060708].pack("J!"+mod))
  138. end
  139. %w[s S l L q Q j J s! S! i I i! I! l! L! j! J!].each {|fmt|
  140. fmt += mod
  141. nuls = [0].pack(fmt)
  142. v = 0
  143. s = "".force_encoding("ascii-8bit")
  144. nuls.bytesize.times {|i|
  145. j = i+40
  146. v = v * 256 + j
  147. s << [j].pack("C")
  148. }
  149. s.reverse!
  150. assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
  151. assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
  152. s2 = s+s
  153. fmt2 = fmt+"*"
  154. assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
  155. }
  156. end
  157. def test_integer_endian
  158. s = [1].pack("s")
  159. assert_include(["\0\1", "\1\0"], s)
  160. if s == "\0\1"
  161. _integer_big_endian()
  162. else
  163. _integer_little_endian()
  164. end
  165. assert_equal("\x01\x02\x02\x01", [0x0102,0x0102].pack("s>s<"))
  166. assert_equal([0x0102,0x0102], "\x01\x02\x02\x01".unpack("s>s<"))
  167. end
  168. def test_integer_endian_explicit
  169. _integer_big_endian('>')
  170. _integer_little_endian('<')
  171. end
  172. def test_pack_U
  173. assert_raise(RangeError) { [-0x40000001].pack("U") }
  174. assert_raise(RangeError) { [-0x40000000].pack("U") }
  175. assert_raise(RangeError) { [-1].pack("U") }
  176. assert_equal "\000", [0].pack("U")
  177. assert_equal "\374\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x3fffffff].pack("U")
  178. assert_equal "\375\200\200\200\200\200".force_encoding(Encoding::UTF_8), [0x40000000].pack("U")
  179. assert_equal "\375\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x7fffffff].pack("U")
  180. assert_raise(RangeError) { [0x80000000].pack("U") }
  181. assert_raise(RangeError) { [0x100000000].pack("U") }
  182. end
  183. def test_pack_P
  184. a = ["abc"]
  185. assert_equal a, a.pack("P").unpack("P*")
  186. assert_equal "a", a.pack("P").unpack("P")[0]
  187. assert_equal a, a.pack("P").freeze.unpack("P*")
  188. assert_raise(ArgumentError) { (a.pack("P") + "").unpack("P*") }
  189. end
  190. def test_pack_p
  191. a = ["abc"]
  192. assert_equal a, a.pack("p").unpack("p*")
  193. assert_equal a[0], a.pack("p").unpack("p")[0]
  194. assert_equal a, a.pack("p").freeze.unpack("p*")
  195. assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
  196. assert_equal a, (a.pack("p") << "d").unpack("p*")
  197. end
  198. def test_format_string_modified
  199. fmt = "CC"
  200. o = Object.new
  201. class << o; self; end.class_eval do
  202. define_method(:to_int) { fmt.clear; 0 }
  203. end
  204. assert_raise(RuntimeError) do
  205. [o, o].pack(fmt)
  206. end
  207. end
  208. def test_comment
  209. assert_equal("\0\1", [0,1].pack(" C #foo \n C "))
  210. assert_equal([0,1], "\0\1".unpack(" C #foo \n C "))
  211. end
  212. def test_illegal_bang
  213. assert_raise(ArgumentError) { [].pack("a!") }
  214. assert_raise(ArgumentError) { "".unpack("a!") }
  215. end
  216. def test_pack_unpack_aA
  217. assert_equal("f", ["foo"].pack("A"))
  218. assert_equal("f", ["foo"].pack("a"))
  219. assert_equal("foo", ["foo"].pack("A*"))
  220. assert_equal("foo", ["foo"].pack("a*"))
  221. assert_equal("fo", ["foo"].pack("A2"))
  222. assert_equal("fo", ["foo"].pack("a2"))
  223. assert_equal("foo ", ["foo"].pack("A4"))
  224. assert_equal("foo\0", ["foo"].pack("a4"))
  225. assert_equal(" ", [nil].pack("A"))
  226. assert_equal("\0", [nil].pack("a"))
  227. assert_equal("", [nil].pack("A*"))
  228. assert_equal("", [nil].pack("a*"))
  229. assert_equal(" ", [nil].pack("A2"))
  230. assert_equal("\0\0", [nil].pack("a2"))
  231. assert_equal("foo" + "\0" * 27, ["foo"].pack("a30"))
  232. assert_equal(["f"], "foo\0".unpack("A"))
  233. assert_equal(["f"], "foo\0".unpack("a"))
  234. assert_equal(["foo"], "foo\0".unpack("A4"))
  235. assert_equal(["foo\0"], "foo\0".unpack("a4"))
  236. assert_equal(["foo"], "foo ".unpack("A4"))
  237. assert_equal(["foo "], "foo ".unpack("a4"))
  238. assert_equal(["foo"], "foo".unpack("A4"))
  239. assert_equal(["foo"], "foo".unpack("a4"))
  240. end
  241. def test_pack_unpack_Z
  242. assert_equal("f", ["foo"].pack("Z"))
  243. assert_equal("foo\0", ["foo"].pack("Z*"))
  244. assert_equal("fo", ["foo"].pack("Z2"))
  245. assert_equal("foo\0\0", ["foo"].pack("Z5"))
  246. assert_equal("\0", [nil].pack("Z"))
  247. assert_equal("\0", [nil].pack("Z*"))
  248. assert_equal("\0\0", [nil].pack("Z2"))
  249. assert_equal(["f"], "foo\0".unpack("Z"))
  250. assert_equal(["foo"], "foo".unpack("Z*"))
  251. assert_equal(["foo"], "foo\0".unpack("Z*"))
  252. assert_equal(["foo"], "foo".unpack("Z5"))
  253. end
  254. def test_pack_unpack_bB
  255. assert_equal("\xff\x00", ["1111111100000000"].pack("b*"))
  256. assert_equal("\x01\x02", ["1000000001000000"].pack("b*"))
  257. assert_equal("", ["1"].pack("b0"))
  258. assert_equal("\x01", ["1"].pack("b1"))
  259. assert_equal("\x01\x00", ["1"].pack("b2"))
  260. assert_equal("\x01\x00", ["1"].pack("b3"))
  261. assert_equal("\x01\x00\x00", ["1"].pack("b4"))
  262. assert_equal("\x01\x00\x00", ["1"].pack("b5"))
  263. assert_equal("\x01\x00\x00\x00", ["1"].pack("b6"))
  264. assert_equal("\xff\x00", ["1111111100000000"].pack("B*"))
  265. assert_equal("\x01\x02", ["0000000100000010"].pack("B*"))
  266. assert_equal("", ["1"].pack("B0"))
  267. assert_equal("\x80", ["1"].pack("B1"))
  268. assert_equal("\x80\x00", ["1"].pack("B2"))
  269. assert_equal("\x80\x00", ["1"].pack("B3"))
  270. assert_equal("\x80\x00\x00", ["1"].pack("B4"))
  271. assert_equal("\x80\x00\x00", ["1"].pack("B5"))
  272. assert_equal("\x80\x00\x00\x00", ["1"].pack("B6"))
  273. assert_equal(["1111111100000000"], "\xff\x00".unpack("b*"))
  274. assert_equal(["1000000001000000"], "\x01\x02".unpack("b*"))
  275. assert_equal([""], "".unpack("b0"))
  276. assert_equal(["1"], "\x01".unpack("b1"))
  277. assert_equal(["10"], "\x01".unpack("b2"))
  278. assert_equal(["100"], "\x01".unpack("b3"))
  279. assert_equal(["1111111100000000"], "\xff\x00".unpack("B*"))
  280. assert_equal(["0000000100000010"], "\x01\x02".unpack("B*"))
  281. assert_equal([""], "".unpack("B0"))
  282. assert_equal(["1"], "\x80".unpack("B1"))
  283. assert_equal(["10"], "\x80".unpack("B2"))
  284. assert_equal(["100"], "\x80".unpack("B3"))
  285. assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("b*")[0].encoding)
  286. assert_equal(Encoding::US_ASCII, "\xff\x00".unpack("B*")[0].encoding)
  287. end
  288. def test_pack_unpack_hH
  289. assert_equal("\x01\xfe", ["10ef"].pack("h*"))
  290. assert_equal("", ["10ef"].pack("h0"))
  291. assert_equal("\x01\x0e", ["10ef"].pack("h3"))
  292. assert_equal("\x01\xfe\x0", ["10ef"].pack("h5"))
  293. assert_equal("\xff\x0f", ["fff"].pack("h3"))
  294. assert_equal("\xff\x0f", ["fff"].pack("h4"))
  295. assert_equal("\xff\x0f\0", ["fff"].pack("h5"))
  296. assert_equal("\xff\x0f\0", ["fff"].pack("h6"))
  297. assert_equal("\xff\x0f\0\0", ["fff"].pack("h7"))
  298. assert_equal("\xff\x0f\0\0", ["fff"].pack("h8"))
  299. assert_equal("\x10\xef", ["10ef"].pack("H*"))
  300. assert_equal("", ["10ef"].pack("H0"))
  301. assert_equal("\x10\xe0", ["10ef"].pack("H3"))
  302. assert_equal("\x10\xef\x0", ["10ef"].pack("H5"))
  303. assert_equal("\xff\xf0", ["fff"].pack("H3"))
  304. assert_equal("\xff\xf0", ["fff"].pack("H4"))
  305. assert_equal("\xff\xf0\0", ["fff"].pack("H5"))
  306. assert_equal("\xff\xf0\0", ["fff"].pack("H6"))
  307. assert_equal("\xff\xf0\0\0", ["fff"].pack("H7"))
  308. assert_equal("\xff\xf0\0\0", ["fff"].pack("H8"))
  309. assert_equal(["10ef"], "\x01\xfe".unpack("h*"))
  310. assert_equal([""], "\x01\xfe".unpack("h0"))
  311. assert_equal(["1"], "\x01\xfe".unpack("h1"))
  312. assert_equal(["10"], "\x01\xfe".unpack("h2"))
  313. assert_equal(["10e"], "\x01\xfe".unpack("h3"))
  314. assert_equal(["10ef"], "\x01\xfe".unpack("h4"))
  315. assert_equal(["10ef"], "\x01\xfe".unpack("h5"))
  316. assert_equal(["10ef"], "\x10\xef".unpack("H*"))
  317. assert_equal([""], "\x10\xef".unpack("H0"))
  318. assert_equal(["1"], "\x10\xef".unpack("H1"))
  319. assert_equal(["10"], "\x10\xef".unpack("H2"))
  320. assert_equal(["10e"], "\x10\xef".unpack("H3"))
  321. assert_equal(["10ef"], "\x10\xef".unpack("H4"))
  322. assert_equal(["10ef"], "\x10\xef".unpack("H5"))
  323. assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("h*")[0].encoding)
  324. assert_equal(Encoding::US_ASCII, "\x10\xef".unpack("H*")[0].encoding)
  325. end
  326. def test_pack_unpack_cC
  327. assert_equal("\0\1\xff", [0, 1, 255].pack("c*"))
  328. assert_equal("\0\1\xff", [0, 1, -1].pack("c*"))
  329. assert_equal("\0\1\xff", [0, 1, 255].pack("C*"))
  330. assert_equal("\0\1\xff", [0, 1, -1].pack("C*"))
  331. assert_equal([0, 1, -1], "\0\1\xff".unpack("c*"))
  332. assert_equal([0, 1, 255], "\0\1\xff".unpack("C*"))
  333. end
  334. def test_pack_unpack_sS
  335. s1 = [513, -514].pack("s*")
  336. s2 = [513, 65022].pack("S*")
  337. assert_equal(s1, s2)
  338. assert_equal([513, -514], s2.unpack("s*"))
  339. assert_equal([513, 65022], s1.unpack("S*"))
  340. s1 = [513, -514].pack("s!*")
  341. s2 = [513, 65022].pack("S!*")
  342. assert_equal(s1, s2)
  343. assert_equal([513, -514], s2.unpack("s!*"))
  344. assert_equal([513, 65022], s1.unpack("S!*"))
  345. assert_equal(2, [1].pack("s").bytesize)
  346. assert_equal(2, [1].pack("S").bytesize)
  347. assert_operator(2, :<=, [1].pack("s!").bytesize)
  348. assert_operator(2, :<=, [1].pack("S!").bytesize)
  349. end
  350. def test_pack_unpack_iI
  351. s1 = [67305985, -50462977].pack("i*")
  352. s2 = [67305985, 4244504319].pack("I*")
  353. assert_equal(s1, s2)
  354. assert_equal([67305985, -50462977], s2.unpack("i*"))
  355. assert_equal([67305985, 4244504319], s1.unpack("I*"))
  356. s1 = [67305985, -50462977].pack("i!*")
  357. s2 = [67305985, 4244504319].pack("I!*")
  358. assert_equal([67305985, -50462977], s1.unpack("i!*"))
  359. assert_equal([67305985, 4244504319], s2.unpack("I!*"))
  360. assert_operator(4, :<=, [1].pack("i").bytesize)
  361. assert_operator(4, :<=, [1].pack("I").bytesize)
  362. assert_operator(4, :<=, [1].pack("i!").bytesize)
  363. assert_operator(4, :<=, [1].pack("I!").bytesize)
  364. end
  365. def test_pack_unpack_lL
  366. s1 = [67305985, -50462977].pack("l*")
  367. s2 = [67305985, 4244504319].pack("L*")
  368. assert_equal(s1, s2)
  369. assert_equal([67305985, -50462977], s2.unpack("l*"))
  370. assert_equal([67305985, 4244504319], s1.unpack("L*"))
  371. s1 = [67305985, -50462977].pack("l!*")
  372. s2 = [67305985, 4244504319].pack("L!*")
  373. assert_equal([67305985, -50462977], s1.unpack("l!*"))
  374. assert_equal([67305985, 4244504319], s2.unpack("L!*"))
  375. assert_equal(4, [1].pack("l").bytesize)
  376. assert_equal(4, [1].pack("L").bytesize)
  377. assert_operator(4, :<=, [1].pack("l!").bytesize)
  378. assert_operator(4, :<=, [1].pack("L!").bytesize)
  379. end
  380. require 'rbconfig'
  381. def test_pack_unpack_qQ
  382. s1 = [578437695752307201, -506097522914230529].pack("q*")
  383. s2 = [578437695752307201, 17940646550795321087].pack("Q*")
  384. assert_equal(s1, s2)
  385. assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*"))
  386. assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*"))
  387. # Note: q! and Q! should not work on platform which has no long long type.
  388. # Is there a such platform now?
  389. # @shyouhei: Yes. gcc -ansi is one of such platform.
  390. s1 = [578437695752307201, -506097522914230529].pack("q!*")
  391. s2 = [578437695752307201, 17940646550795321087].pack("Q!*")
  392. assert_equal([578437695752307201, -506097522914230529], s2.unpack("q!*"))
  393. assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q!*"))
  394. assert_equal(8, [1].pack("q").bytesize)
  395. assert_equal(8, [1].pack("Q").bytesize)
  396. assert_operator(8, :<=, [1].pack("q!").bytesize)
  397. assert_operator(8, :<=, [1].pack("Q!").bytesize)
  398. end if RbConfig::CONFIG['HAVE_LONG_LONG']
  399. def test_pack_unpack_jJ
  400. # Note: we assume that the size of intptr_t and uintptr_t equals to the size
  401. # of real pointer.
  402. psize = [nil].pack("p").bytesize
  403. if psize == 4
  404. s1 = [67305985, -50462977].pack("j*")
  405. s2 = [67305985, 4244504319].pack("J*")
  406. assert_equal(s1, s2)
  407. assert_equal([67305985, -50462977], s2.unpack("j*"))
  408. assert_equal([67305985, 4244504319], s1.unpack("J*"))
  409. s1 = [67305985, -50462977].pack("j!*")
  410. s2 = [67305985, 4244504319].pack("J!*")
  411. assert_equal([67305985, -50462977], s1.unpack("j!*"))
  412. assert_equal([67305985, 4244504319], s2.unpack("J!*"))
  413. assert_equal(4, [1].pack("j").bytesize)
  414. assert_equal(4, [1].pack("J").bytesize)
  415. elsif psize == 8
  416. s1 = [578437695752307201, -506097522914230529].pack("j*")
  417. s2 = [578437695752307201, 17940646550795321087].pack("J*")
  418. assert_equal(s1, s2)
  419. assert_equal([578437695752307201, -506097522914230529], s2.unpack("j*"))
  420. assert_equal([578437695752307201, 17940646550795321087], s1.unpack("J*"))
  421. s1 = [578437695752307201, -506097522914230529].pack("j!*")
  422. s2 = [578437695752307201, 17940646550795321087].pack("J!*")
  423. assert_equal([578437695752307201, -506097522914230529], s2.unpack("j!*"))
  424. assert_equal([578437695752307201, 17940646550795321087], s1.unpack("J!*"))
  425. assert_equal(8, [1].pack("j").bytesize)
  426. assert_equal(8, [1].pack("J").bytesize)
  427. else
  428. assert false, "we don't know such platform now."
  429. end
  430. end
  431. def test_pack_unpack_nN
  432. assert_equal("\000\000\000\001\377\377\177\377\200\000\377\377", [0,1,-1,32767,-32768,65535].pack("n*"))
  433. assert_equal("\000\000\000\000\000\000\000\001\377\377\377\377", [0,1,-1].pack("N*"))
  434. assert_equal([0,1,65535,32767,32768,65535], "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*"))
  435. assert_equal([0,1,4294967295], "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*"))
  436. assert_equal(2, [1].pack("n").bytesize)
  437. assert_equal(4, [1].pack("N").bytesize)
  438. end
  439. def test_pack_unpack_vV
  440. assert_equal("\000\000\001\000\377\377\377\177\000\200\377\377", [0,1,-1,32767,-32768,65535].pack("v*"))
  441. assert_equal("\000\000\000\000\001\000\000\000\377\377\377\377", [0,1,-1].pack("V*"))
  442. assert_equal([0,1,65535,32767,32768,65535], "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*"))
  443. assert_equal([0,1,4294967295], "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*"))
  444. assert_equal(2, [1].pack("v").bytesize)
  445. assert_equal(4, [1].pack("V").bytesize)
  446. end
  447. def test_pack_unpack_fdeEgG
  448. inf = 1.0/0.0
  449. nan = inf/inf
  450. [0.0, 1.0, 3.0, inf, -inf, nan].each do |x|
  451. %w(f d e E g G).each do |f|
  452. v = [x].pack(f).unpack(f)
  453. if x.nan?
  454. assert_predicate(v.first, :nan?)
  455. else
  456. assert_equal([x], v)
  457. end
  458. end
  459. end
  460. end
  461. def test_pack_unpack_x
  462. assert_equal("", [].pack("x0"))
  463. assert_equal("\0", [].pack("x"))
  464. assert_equal("\0" * 30, [].pack("x30"))
  465. assert_equal([0, 2], "\x00\x00\x02".unpack("CxC"))
  466. assert_raise(ArgumentError) { "".unpack("x") }
  467. end
  468. def test_pack_unpack_X
  469. assert_equal("\x00\x02", [0, 1, 2].pack("CCXC"))
  470. assert_equal("\x02", [0, 1, 2].pack("CCX2C"))
  471. assert_raise(ArgumentError) { [].pack("X") }
  472. assert_equal([0, 2, 2], "\x00\x02".unpack("CCXC"))
  473. assert_raise(ArgumentError) { "".unpack("X") }
  474. end
  475. def test_pack_unpack_atmark
  476. assert_equal("\x01\x00\x00\x02", [1, 2].pack("C@3C"))
  477. assert_equal("\x02", [1, 2].pack("C@0C"))
  478. assert_equal("\x01\x02", [1, 2].pack("C@1C"))
  479. assert_equal([1, 2], "\x01\x00\x00\x02".unpack("C@3C"))
  480. assert_equal([nil], "\x00".unpack("@1C")) # is it OK?
  481. assert_raise(ArgumentError) { "\x00".unpack("@2C") }
  482. pos = RbConfig::LIMITS["UINTPTR_MAX"] - 99 # -100
  483. assert_raise(RangeError) {"0123456789".unpack("@#{pos}C10")}
  484. end
  485. def test_pack_unpack_percent
  486. assert_raise(ArgumentError) { [].pack("%") }
  487. assert_raise(ArgumentError) { "".unpack("%") }
  488. end
  489. def test_pack_unpack_U
  490. assert_equal([0], [0].pack("U").unpack("U"))
  491. assert_equal([0x80], [0x80].pack("U").unpack("U"))
  492. assert_equal([0x800], [0x800].pack("U").unpack("U"))
  493. assert_equal([0x10000], [0x10000].pack("U").unpack("U"))
  494. assert_equal([0x400000], [0x400000].pack("U").unpack("U"))
  495. assert_raise(ArgumentError) { "\x80".unpack("U") }
  496. assert_raise(ArgumentError) { "\xff".unpack("U") }
  497. assert_raise(ArgumentError) { "\xfc\x00".unpack("U") }
  498. assert_raise(ArgumentError) { "\xc0\xc0".unpack("U") }
  499. assert_raise(ArgumentError) { "\xe0\x80\x80".unpack("U") }
  500. end
  501. def test_pack_unpack_u
  502. assert_equal("", [""].pack("u"))
  503. assert_equal("!80``\n", ["a"].pack("u"))
  504. assert_equal("#86)C\n", ["abc"].pack("u"))
  505. assert_equal("$86)C9```\n", ["abcd"].pack("u"))
  506. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n", ["a"*45].pack("u"))
  507. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u"))
  508. assert_equal("&86)C9&5F\n#9VAI\n", ["abcdefghi"].pack("u6"))
  509. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u0"))
  510. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u1"))
  511. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u2"))
  512. assert_equal(<<EXPECTED, ["a"*80].pack("u68"))
  513. _86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A
  514. 186%A86%A86%A86%A86%A86$`
  515. EXPECTED
  516. assert_equal([""], "".unpack("u"))
  517. assert_equal(["a"], "!80``\n".unpack("u"))
  518. assert_equal(["abc"], "#86)C\n".unpack("u"))
  519. assert_equal(["abcd"], "$86)C9```\n".unpack("u"))
  520. assert_equal(["a"*45], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n".unpack("u"))
  521. assert_equal(["a"*46], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n".unpack("u"))
  522. assert_equal(["abcdefghi"], "&86)C9&5F\n#9VAI\n".unpack("u"))
  523. assert_equal(["abcdef"], "#86)C\n#9&5F\n".unpack("u"))
  524. assert_equal(["abcdef"], "#86)CX\n#9&5FX\n".unpack("u")) # X is a (dummy) checksum.
  525. assert_equal(["abcdef"], "#86)C\r\n#9&5F\r\n".unpack("u"))
  526. assert_equal(["abcdef"], "#86)CX\r\n#9&5FX\r\n".unpack("u")) # X is a (dummy) checksum.
  527. assert_equal(["\x00"], "\"\n".unpack("u"))
  528. assert_equal(["\x00"], "! \r \n".unpack("u"))
  529. end
  530. def test_pack_unpack_m
  531. assert_equal("", [""].pack("m"))
  532. assert_equal("AA==\n", ["\0"].pack("m"))
  533. assert_equal("AAA=\n", ["\0\0"].pack("m"))
  534. assert_equal("AAAA\n", ["\0\0\0"].pack("m"))
  535. assert_equal("/w==\n", ["\377"].pack("m"))
  536. assert_equal("//8=\n", ["\377\377"].pack("m"))
  537. assert_equal("////\n", ["\377\377\377"].pack("m"))
  538. assert_equal([""], "".unpack("m"))
  539. assert_equal(["\0"], "AA==\n".unpack("m"))
  540. assert_equal(["\0\0"], "AAA=\n".unpack("m"))
  541. assert_equal(["\0\0\0"], "AAAA\n".unpack("m"))
  542. assert_equal(["\377"], "/w==\n".unpack("m"))
  543. assert_equal(["\377\377"], "//8=\n".unpack("m"))
  544. assert_equal(["\377\377\377"], "////\n".unpack("m"))
  545. assert_equal([""], "A\n".unpack("m"))
  546. assert_equal(["\0"], "AA\n".unpack("m"))
  547. assert_equal(["\0"], "AA=\n".unpack("m"))
  548. assert_equal(["\0\0"], "AAA\n".unpack("m"))
  549. bug10019 = '[ruby-core:63604] [Bug #10019]'
  550. size = ((4096-4)/4*3+1)
  551. assert_separately(%W[- #{size} #{bug10019}], <<-'end;')
  552. size = ARGV.shift.to_i
  553. bug = ARGV.shift
  554. assert_equal(size, ["a"*size].pack("m#{size+2}").unpack("m")[0].size, bug)
  555. end;
  556. end
  557. def test_pack_unpack_m0
  558. assert_equal("", [""].pack("m0"))
  559. assert_equal("AA==", ["\0"].pack("m0"))
  560. assert_equal("AAA=", ["\0\0"].pack("m0"))
  561. assert_equal("AAAA", ["\0\0\0"].pack("m0"))
  562. assert_equal("/w==", ["\377"].pack("m0"))
  563. assert_equal("//8=", ["\377\377"].pack("m0"))
  564. assert_equal("////", ["\377\377\377"].pack("m0"))
  565. assert_equal([""], "".unpack("m0"))
  566. assert_equal(["\0"], "AA==".unpack("m0"))
  567. assert_equal(["\0\0"], "AAA=".unpack("m0"))
  568. assert_equal(["\0\0\0"], "AAAA".unpack("m0"))
  569. assert_equal(["\377"], "/w==".unpack("m0"))
  570. assert_equal(["\377\377"], "//8=".unpack("m0"))
  571. assert_equal(["\377\377\377"], "////".unpack("m0"))
  572. assert_raise(ArgumentError) { "^".unpack("m0") }
  573. assert_raise(ArgumentError) { "A".unpack("m0") }
  574. assert_raise(ArgumentError) { "A^".unpack("m0") }
  575. assert_raise(ArgumentError) { "AA".unpack("m0") }
  576. assert_raise(ArgumentError) { "AA=".unpack("m0") }
  577. assert_raise(ArgumentError) { "AA===".unpack("m0") }
  578. assert_raise(ArgumentError) { "AA=x".unpack("m0") }
  579. assert_raise(ArgumentError) { "AAA".unpack("m0") }
  580. assert_raise(ArgumentError) { "AAA^".unpack("m0") }
  581. assert_raise(ArgumentError) { "AB==".unpack("m0") }
  582. assert_raise(ArgumentError) { "AAB=".unpack("m0") }
  583. end
  584. def test_pack_unpack_M
  585. assert_equal("a b c\td =\n\ne=\n", ["a b c\td \ne"].pack("M"))
  586. assert_equal(["a b c\td \ne"], "a b c\td =\n\ne=\n".unpack("M"))
  587. assert_equal("=00=\n", ["\0"].pack("M"))
  588. assert_equal("a"*73+"=\na=\n", ["a"*74].pack("M"))
  589. assert_equal(("a"*73+"=\n")*14+"a=\n", ["a"*1023].pack("M"))
  590. assert_equal(["\0"], "=00=\n".unpack("M"))
  591. assert_equal(["a"*74], ("a"*73+"=\na=\n").unpack("M"))
  592. assert_equal(["a"*1023], (("a"*73+"=\n")*14+"a=\n").unpack("M"))
  593. assert_equal(["\x0a"], "=0a=\n".unpack("M"))
  594. assert_equal(["\x0a"], "=0A=\n".unpack("M"))
  595. assert_equal(["=0Z=\n"], "=0Z=\n".unpack("M"))
  596. assert_equal([""], "=\r\n".unpack("M"))
  597. assert_equal(["\xC6\xF7"], "=C6=F7".unpack('M*'))
  598. assert_equal(["pre123after"], "pre=31=32=33after".unpack("M"))
  599. assert_equal(["preafter"], "pre=\nafter".unpack("M"))
  600. assert_equal(["preafter"], "pre=\r\nafter".unpack("M"))
  601. assert_equal(["pre="], "pre=".unpack("M"))
  602. assert_equal(["pre=\r"], "pre=\r".unpack("M"))
  603. assert_equal(["pre=hoge"], "pre=hoge".unpack("M"))
  604. assert_equal(["pre==31after"], "pre==31after".unpack("M"))
  605. assert_equal(["pre===31after"], "pre===31after".unpack("M"))
  606. bug = '[ruby-core:83055] [Bug #13949]'
  607. s = "abcdef".unpack1("M")
  608. assert_equal(Encoding::ASCII_8BIT, s.encoding)
  609. assert_predicate(s, :ascii_only?, bug)
  610. end
  611. def test_pack_unpack_P2
  612. assert_raise(ArgumentError) { ["abc"].pack("P4") }
  613. assert_raise(ArgumentError) { [""].pack("P") }
  614. assert_equal([], ".".unpack("P"))
  615. assert_equal([], ".".unpack("p"))
  616. assert_equal([nil], ("\0" * 1024).unpack("P"))
  617. end
  618. def test_pack_p2
  619. assert_match(/\A\0*\Z/, [nil].pack("p"))
  620. end
  621. def test_pack_unpack_w
  622. assert_equal("\000", [0].pack("w"))
  623. assert_equal("\001", [1].pack("w"))
  624. assert_equal("\177", [127].pack("w"))
  625. assert_equal("\201\000", [128].pack("w"))
  626. assert_equal("\377\177", [0x3fff].pack("w"))
  627. assert_equal("\201\200\000", [0x4000].pack("w"))
  628. assert_equal("\203\377\377\377\177", [0x3fffffff].pack("w"))
  629. assert_equal("\204\200\200\200\000", [0x40000000].pack("w"))
  630. assert_equal("\217\377\377\377\177", [0xffffffff].pack("w"))
  631. assert_equal("\220\200\200\200\000", [0x100000000].pack("w"))
  632. assert_raise(ArgumentError) { [-1].pack("w") }
  633. assert_equal([0], "\000".unpack("w"))
  634. assert_equal([1], "\001".unpack("w"), [1])
  635. assert_equal([127], "\177".unpack("w"), [127])
  636. assert_equal([128], "\201\000".unpack("w"), [128])
  637. assert_equal([0x3fff], "\377\177".unpack("w"), [0x3fff])
  638. assert_equal([0x4000], "\201\200\000".unpack("w"), [0x4000])
  639. assert_equal([0x3fffffff], "\203\377\377\377\177".unpack("w"), [0x3fffffff])
  640. assert_equal([0x40000000], "\204\200\200\200\000".unpack("w"), [0x40000000])
  641. assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff])
  642. assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000])
  643. end
  644. def test_length_too_big
  645. assert_raise(RangeError) { [].pack("C100000000000000000000") }
  646. end
  647. def test_short_string
  648. %w[n N v V s S i I l L q Q s! S! i! I! l! l!].each {|fmt|
  649. str = [1].pack(fmt)
  650. assert_equal([1,nil], str.unpack("#{fmt}2"))
  651. }
  652. end
  653. def test_short_with_block
  654. bug4059 = '[ruby-core:33193]'
  655. result = :ok
  656. assert_nil("".unpack("i") {|x| result = x}, bug4059)
  657. assert_equal(:ok, result)
  658. end
  659. def test_pack_garbage
  660. verbose = $VERBOSE
  661. $VERBOSE = false
  662. assert_silent do
  663. assert_equal "\000", [0].pack("*U")
  664. end
  665. $VERBOSE = true
  666. _, err = capture_io do
  667. assert_equal "\000", [0].pack("*U")
  668. end
  669. assert_match %r%unknown pack directive '\*' in '\*U'$%, err
  670. ensure
  671. $VERBOSE = verbose
  672. end
  673. def test_unpack_garbage
  674. verbose = $VERBOSE
  675. $VERBOSE = false
  676. assert_silent do
  677. assert_equal [0], "\000".unpack("*U")
  678. end
  679. $VERBOSE = true
  680. _, err = capture_io do
  681. assert_equal [0], "\000".unpack("*U")
  682. end
  683. assert_match %r%unknown unpack directive '\*' in '\*U'$%, err
  684. ensure
  685. $VERBOSE = verbose
  686. end
  687. def test_invalid_warning
  688. assert_warning(/unknown pack directive ',' in ','/) {
  689. [].pack(",")
  690. }
  691. assert_warning(/\A[ -~]+\Z/) {
  692. [].pack("\x7f")
  693. }
  694. assert_warning(/\A(.* in '\u{3042}'\n)+\z/) {
  695. [].pack("\u{3042}")
  696. }
  697. assert_warning(/\A.* in '.*U'\Z/) {
  698. assert_equal "\000", [0].pack("\0U")
  699. }
  700. assert_warning(/\A.* in '.*U'\Z/) {
  701. "\000".unpack("\0U")
  702. }
  703. end
  704. def test_pack_resize
  705. assert_separately([], <<-'end;')
  706. ary = []
  707. obj = Class.new {
  708. define_method(:to_str) {
  709. ary.clear()
  710. ary = nil
  711. GC.start
  712. "TALOS"
  713. }
  714. }.new
  715. ary.push(obj)
  716. ary.push(".")
  717. assert_raise_with_message(ArgumentError, /too few/) {ary.pack("AA")}
  718. end;
  719. end
  720. def test_pack_with_buffer
  721. buf = String.new(capacity: 100)
  722. assert_raise_with_message(FrozenError, /frozen/) {
  723. [0xDEAD_BEEF].pack('N', buffer: 'foo'.freeze)
  724. }
  725. assert_raise_with_message(TypeError, /must be String/) {
  726. [0xDEAD_BEEF].pack('N', buffer: Object.new)
  727. }
  728. addr = [buf].pack('p')
  729. [0xDEAD_BEEF].pack('N', buffer: buf)
  730. assert_equal "\xDE\xAD\xBE\xEF", buf
  731. [0xBABE_F00D].pack('@4N', buffer: buf)
  732. assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D", buf
  733. assert_equal addr, [buf].pack('p')
  734. [0xBAAD_FACE].pack('@10N', buffer: buf)
  735. assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D\0\0\xBA\xAD\xFA\xCE", buf
  736. assert_equal addr, [buf].pack('p')
  737. end
  738. def test_unpack_with_block
  739. ret = []; "ABCD".unpack("CCCC") {|v| ret << v }
  740. assert_equal [65, 66, 67, 68], ret
  741. ret = []; "A".unpack("B*") {|v| ret << v.dup }
  742. assert_equal ["01000001"], ret
  743. end
  744. def test_unpack1
  745. assert_equal 65, "A".unpack1("C")
  746. assert_equal 68, "ABCD".unpack1("x3C")
  747. assert_equal 0x3042, "\u{3042 3044 3046}".unpack1("U*")
  748. assert_equal "hogefuga", "aG9nZWZ1Z2E=".unpack1("m")
  749. assert_equal "01000001", "A".unpack1("B*")
  750. end
  751. end