PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_pack.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 654 lines | 582 code | 70 blank | 2 comment | 3 complexity | 399da8a7c035217db76cc95cda6240e2 MD5 | raw file
  1. require 'test/unit'
  2. class TestPack < Test::Unit::TestCase
  3. def test_pack
  4. $format = "c2x5CCxsdils_l_a6";
  5. # Need the expression in here to force ary[5] to be numeric. This avoids
  6. # test2 failing because ary2 goes str->numeric->str and ary does not.
  7. ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,-32767,-123456,"abcdef"]
  8. $x = ary.pack($format)
  9. ary2 = $x.unpack($format)
  10. assert_equal(ary.length, ary2.length)
  11. assert_equal(ary.join(':'), ary2.join(':'))
  12. assert_match(/def/, $x)
  13. $x = [-1073741825]
  14. assert_equal($x, $x.pack("q").unpack("q"))
  15. $x = [-1]
  16. assert_equal($x, $x.pack("l").unpack("l"))
  17. end
  18. def test_pack_n
  19. assert_equal "\000\000", [0].pack('n')
  20. assert_equal "\000\001", [1].pack('n')
  21. assert_equal "\000\002", [2].pack('n')
  22. assert_equal "\000\003", [3].pack('n')
  23. assert_equal "\377\376", [65534].pack('n')
  24. assert_equal "\377\377", [65535].pack('n')
  25. assert_equal "\200\000", [2**15].pack('n')
  26. assert_equal "\177\377", [-2**15-1].pack('n')
  27. assert_equal "\377\377", [-1].pack('n')
  28. assert_equal "\000\001\000\001", [1,1].pack('n*')
  29. assert_equal "\000\001\000\001\000\001", [1,1,1].pack('n*')
  30. end
  31. def test_unpack_n
  32. assert_equal 1, "\000\001".unpack('n')[0]
  33. assert_equal 2, "\000\002".unpack('n')[0]
  34. assert_equal 3, "\000\003".unpack('n')[0]
  35. assert_equal 65535, "\377\377".unpack('n')[0]
  36. assert_equal [1,1], "\000\001\000\001".unpack('n*')
  37. assert_equal [1,1,1], "\000\001\000\001\000\001".unpack('n*')
  38. end
  39. def test_pack_N
  40. assert_equal "\000\000\000\000", [0].pack('N')
  41. assert_equal "\000\000\000\001", [1].pack('N')
  42. assert_equal "\000\000\000\002", [2].pack('N')
  43. assert_equal "\000\000\000\003", [3].pack('N')
  44. assert_equal "\377\377\377\376", [4294967294].pack('N')
  45. assert_equal "\377\377\377\377", [4294967295].pack('N')
  46. assert_equal "\200\000\000\000", [2**31].pack('N')
  47. assert_equal "\177\377\377\377", [-2**31-1].pack('N')
  48. assert_equal "\377\377\377\377", [-1].pack('N')
  49. assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*')
  50. assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*')
  51. end
  52. def test_unpack_N
  53. assert_equal 1, "\000\000\000\001".unpack('N')[0]
  54. assert_equal 2, "\000\000\000\002".unpack('N')[0]
  55. assert_equal 3, "\000\000\000\003".unpack('N')[0]
  56. assert_equal 4294967295, "\377\377\377\377".unpack('N')[0]
  57. assert_equal [1,1], "\000\000\000\001\000\000\000\001".unpack('N*')
  58. assert_equal [1,1,1], "\000\000\000\001\000\000\000\001\000\000\000\001".unpack('N*')
  59. end
  60. def _integer_big_endian(mod='')
  61. assert_equal("\x01\x02", [0x0102].pack("s"+mod))
  62. assert_equal("\x01\x02", [0x0102].pack("S"+mod))
  63. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("l"+mod))
  64. assert_equal("\x01\x02\x03\x04", [0x01020304].pack("L"+mod))
  65. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("q"+mod))
  66. assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", [0x0102030405060708].pack("Q"+mod))
  67. assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("s!"+mod))
  68. assert_match(/\A\x00*\x01\x02\z/, [0x0102].pack("S!"+mod))
  69. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i"+mod))
  70. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I"+mod))
  71. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("i!"+mod))
  72. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("I!"+mod))
  73. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("l!"+mod))
  74. assert_match(/\A\x00*\x01\x02\x03\x04\z/, [0x01020304].pack("L!"+mod))
  75. %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt|
  76. fmt += mod
  77. nuls = [0].pack(fmt)
  78. v = 0
  79. s = "".force_encoding("ascii-8bit")
  80. nuls.bytesize.times {|i|
  81. j = i + 40
  82. v = v * 256 + j
  83. s << [j].pack("C")
  84. }
  85. assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
  86. assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
  87. s2 = s+s
  88. fmt2 = fmt+"*"
  89. assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
  90. }
  91. end
  92. def _integer_little_endian(mod='')
  93. assert_equal("\x02\x01", [0x0102].pack("s"+mod))
  94. assert_equal("\x02\x01", [0x0102].pack("S"+mod))
  95. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("l"+mod))
  96. assert_equal("\x04\x03\x02\x01", [0x01020304].pack("L"+mod))
  97. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("q"+mod))
  98. assert_equal("\x08\x07\x06\x05\x04\x03\x02\x01", [0x0102030405060708].pack("Q"+mod))
  99. assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("s!"+mod))
  100. assert_match(/\A\x02\x01\x00*\z/, [0x0102].pack("S!"+mod))
  101. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i"+mod))
  102. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I"+mod))
  103. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("i!"+mod))
  104. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("I!"+mod))
  105. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("l!"+mod))
  106. assert_match(/\A\x04\x03\x02\x01\x00*\z/, [0x01020304].pack("L!"+mod))
  107. %w[s S l L q Q s! S! i I i! I! l! L!].each {|fmt|
  108. fmt += mod
  109. nuls = [0].pack(fmt)
  110. v = 0
  111. s = "".force_encoding("ascii-8bit")
  112. nuls.bytesize.times {|i|
  113. j = i+40
  114. v = v * 256 + j
  115. s << [j].pack("C")
  116. }
  117. s.reverse!
  118. assert_equal(s, [v].pack(fmt), "[#{v}].pack(#{fmt.dump})")
  119. assert_equal([v], s.unpack(fmt), "#{s.dump}.unpack(#{fmt.dump})")
  120. s2 = s+s
  121. fmt2 = fmt+"*"
  122. assert_equal([v,v], s2.unpack(fmt2), "#{s2.dump}.unpack(#{fmt2.dump})")
  123. }
  124. end
  125. def test_integer_endian
  126. s = [1].pack("s")
  127. assert_include(["\0\1", "\1\0"], s)
  128. if s == "\0\1"
  129. _integer_big_endian()
  130. else
  131. _integer_little_endian()
  132. end
  133. assert_equal("\x01\x02\x02\x01", [0x0102,0x0102].pack("s>s<"))
  134. assert_equal([0x0102,0x0102], "\x01\x02\x02\x01".unpack("s>s<"))
  135. end
  136. def test_integer_endian_explicit
  137. _integer_big_endian('>')
  138. _integer_little_endian('<')
  139. end
  140. def test_pack_U
  141. assert_raise(RangeError) { [-0x40000001].pack("U") }
  142. assert_raise(RangeError) { [-0x40000000].pack("U") }
  143. assert_raise(RangeError) { [-1].pack("U") }
  144. assert_equal "\000", [0].pack("U")
  145. assert_equal "\374\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x3fffffff].pack("U")
  146. assert_equal "\375\200\200\200\200\200".force_encoding(Encoding::UTF_8), [0x40000000].pack("U")
  147. assert_equal "\375\277\277\277\277\277".force_encoding(Encoding::UTF_8), [0x7fffffff].pack("U")
  148. assert_raise(RangeError) { [0x80000000].pack("U") }
  149. assert_raise(RangeError) { [0x100000000].pack("U") }
  150. end
  151. def test_pack_P
  152. a = ["abc"]
  153. assert_equal a, a.pack("P").unpack("P*")
  154. assert_equal "a", a.pack("P").unpack("P")[0]
  155. assert_equal a, a.pack("P").freeze.unpack("P*")
  156. assert_raise(ArgumentError) { (a.pack("P") + "").unpack("P*") }
  157. end
  158. def test_pack_p
  159. a = ["abc"]
  160. assert_equal a, a.pack("p").unpack("p*")
  161. assert_equal a[0], a.pack("p").unpack("p")[0]
  162. assert_equal a, a.pack("p").freeze.unpack("p*")
  163. assert_raise(ArgumentError) { (a.pack("p") + "").unpack("p*") }
  164. end
  165. def test_format_string_modified
  166. fmt = "CC"
  167. o = Object.new
  168. class << o; self; end.class_eval do
  169. define_method(:to_int) { fmt.clear; 0 }
  170. end
  171. assert_raise(RuntimeError) do
  172. [o, o].pack(fmt)
  173. end
  174. end
  175. def test_comment
  176. assert_equal("\0\1", [0,1].pack(" C #foo \n C "))
  177. assert_equal([0,1], "\0\1".unpack(" C #foo \n C "))
  178. end
  179. def test_illegal_bang
  180. assert_raise(ArgumentError) { [].pack("a!") }
  181. assert_raise(ArgumentError) { "".unpack("a!") }
  182. end
  183. def test_pack_unpack_aA
  184. assert_equal("f", ["foo"].pack("A"))
  185. assert_equal("f", ["foo"].pack("a"))
  186. assert_equal("foo", ["foo"].pack("A*"))
  187. assert_equal("foo", ["foo"].pack("a*"))
  188. assert_equal("fo", ["foo"].pack("A2"))
  189. assert_equal("fo", ["foo"].pack("a2"))
  190. assert_equal("foo ", ["foo"].pack("A4"))
  191. assert_equal("foo\0", ["foo"].pack("a4"))
  192. assert_equal(" ", [nil].pack("A"))
  193. assert_equal("\0", [nil].pack("a"))
  194. assert_equal("", [nil].pack("A*"))
  195. assert_equal("", [nil].pack("a*"))
  196. assert_equal(" ", [nil].pack("A2"))
  197. assert_equal("\0\0", [nil].pack("a2"))
  198. assert_equal("foo" + "\0" * 27, ["foo"].pack("a30"))
  199. assert_equal(["f"], "foo\0".unpack("A"))
  200. assert_equal(["f"], "foo\0".unpack("a"))
  201. assert_equal(["foo"], "foo\0".unpack("A4"))
  202. assert_equal(["foo\0"], "foo\0".unpack("a4"))
  203. assert_equal(["foo"], "foo ".unpack("A4"))
  204. assert_equal(["foo "], "foo ".unpack("a4"))
  205. assert_equal(["foo"], "foo".unpack("A4"))
  206. assert_equal(["foo"], "foo".unpack("a4"))
  207. end
  208. def test_pack_unpack_Z
  209. assert_equal("f", ["foo"].pack("Z"))
  210. assert_equal("foo\0", ["foo"].pack("Z*"))
  211. assert_equal("fo", ["foo"].pack("Z2"))
  212. assert_equal("foo\0\0", ["foo"].pack("Z5"))
  213. assert_equal("\0", [nil].pack("Z"))
  214. assert_equal("\0", [nil].pack("Z*"))
  215. assert_equal("\0\0", [nil].pack("Z2"))
  216. assert_equal(["f"], "foo\0".unpack("Z"))
  217. assert_equal(["foo"], "foo".unpack("Z*"))
  218. assert_equal(["foo"], "foo\0".unpack("Z*"))
  219. assert_equal(["foo"], "foo".unpack("Z5"))
  220. end
  221. def test_pack_unpack_bB
  222. assert_equal("\xff\x00", ["1111111100000000"].pack("b*"))
  223. assert_equal("\x01\x02", ["1000000001000000"].pack("b*"))
  224. assert_equal("", ["1"].pack("b0"))
  225. assert_equal("\x01", ["1"].pack("b1"))
  226. assert_equal("\x01\x00", ["1"].pack("b2"))
  227. assert_equal("\x01\x00", ["1"].pack("b3"))
  228. assert_equal("\x01\x00\x00", ["1"].pack("b4"))
  229. assert_equal("\x01\x00\x00", ["1"].pack("b5"))
  230. assert_equal("\x01\x00\x00\x00", ["1"].pack("b6"))
  231. assert_equal("\xff\x00", ["1111111100000000"].pack("B*"))
  232. assert_equal("\x01\x02", ["0000000100000010"].pack("B*"))
  233. assert_equal("", ["1"].pack("B0"))
  234. assert_equal("\x80", ["1"].pack("B1"))
  235. assert_equal("\x80\x00", ["1"].pack("B2"))
  236. assert_equal("\x80\x00", ["1"].pack("B3"))
  237. assert_equal("\x80\x00\x00", ["1"].pack("B4"))
  238. assert_equal("\x80\x00\x00", ["1"].pack("B5"))
  239. assert_equal("\x80\x00\x00\x00", ["1"].pack("B6"))
  240. assert_equal(["1111111100000000"], "\xff\x00".unpack("b*"))
  241. assert_equal(["1000000001000000"], "\x01\x02".unpack("b*"))
  242. assert_equal([""], "".unpack("b0"))
  243. assert_equal(["1"], "\x01".unpack("b1"))
  244. assert_equal(["10"], "\x01".unpack("b2"))
  245. assert_equal(["100"], "\x01".unpack("b3"))
  246. assert_equal(["1111111100000000"], "\xff\x00".unpack("B*"))
  247. assert_equal(["0000000100000010"], "\x01\x02".unpack("B*"))
  248. assert_equal([""], "".unpack("B0"))
  249. assert_equal(["1"], "\x80".unpack("B1"))
  250. assert_equal(["10"], "\x80".unpack("B2"))
  251. assert_equal(["100"], "\x80".unpack("B3"))
  252. end
  253. def test_pack_unpack_hH
  254. assert_equal("\x01\xfe", ["10ef"].pack("h*"))
  255. assert_equal("", ["10ef"].pack("h0"))
  256. assert_equal("\x01\x0e", ["10ef"].pack("h3"))
  257. assert_equal("\x01\xfe\x0", ["10ef"].pack("h5"))
  258. assert_equal("\xff\x0f", ["fff"].pack("h3"))
  259. assert_equal("\xff\x0f", ["fff"].pack("h4"))
  260. assert_equal("\xff\x0f\0", ["fff"].pack("h5"))
  261. assert_equal("\xff\x0f\0", ["fff"].pack("h6"))
  262. assert_equal("\xff\x0f\0\0", ["fff"].pack("h7"))
  263. assert_equal("\xff\x0f\0\0", ["fff"].pack("h8"))
  264. assert_equal("\x10\xef", ["10ef"].pack("H*"))
  265. assert_equal("", ["10ef"].pack("H0"))
  266. assert_equal("\x10\xe0", ["10ef"].pack("H3"))
  267. assert_equal("\x10\xef\x0", ["10ef"].pack("H5"))
  268. assert_equal("\xff\xf0", ["fff"].pack("H3"))
  269. assert_equal("\xff\xf0", ["fff"].pack("H4"))
  270. assert_equal("\xff\xf0\0", ["fff"].pack("H5"))
  271. assert_equal("\xff\xf0\0", ["fff"].pack("H6"))
  272. assert_equal("\xff\xf0\0\0", ["fff"].pack("H7"))
  273. assert_equal("\xff\xf0\0\0", ["fff"].pack("H8"))
  274. assert_equal(["10ef"], "\x01\xfe".unpack("h*"))
  275. assert_equal([""], "\x01\xfe".unpack("h0"))
  276. assert_equal(["1"], "\x01\xfe".unpack("h1"))
  277. assert_equal(["10"], "\x01\xfe".unpack("h2"))
  278. assert_equal(["10e"], "\x01\xfe".unpack("h3"))
  279. assert_equal(["10ef"], "\x01\xfe".unpack("h4"))
  280. assert_equal(["10ef"], "\x01\xfe".unpack("h5"))
  281. assert_equal(["10ef"], "\x10\xef".unpack("H*"))
  282. assert_equal([""], "\x10\xef".unpack("H0"))
  283. assert_equal(["1"], "\x10\xef".unpack("H1"))
  284. assert_equal(["10"], "\x10\xef".unpack("H2"))
  285. assert_equal(["10e"], "\x10\xef".unpack("H3"))
  286. assert_equal(["10ef"], "\x10\xef".unpack("H4"))
  287. assert_equal(["10ef"], "\x10\xef".unpack("H5"))
  288. end
  289. def test_pack_unpack_cC
  290. assert_equal("\0\1\xff", [0, 1, 255].pack("c*"))
  291. assert_equal("\0\1\xff", [0, 1, -1].pack("c*"))
  292. assert_equal("\0\1\xff", [0, 1, 255].pack("C*"))
  293. assert_equal("\0\1\xff", [0, 1, -1].pack("C*"))
  294. assert_equal([0, 1, -1], "\0\1\xff".unpack("c*"))
  295. assert_equal([0, 1, 255], "\0\1\xff".unpack("C*"))
  296. end
  297. def test_pack_unpack_sS
  298. s1 = [513, -514].pack("s*")
  299. s2 = [513, 65022].pack("S*")
  300. assert_equal(s1, s2)
  301. assert_equal([513, -514], s2.unpack("s*"))
  302. assert_equal([513, 65022], s1.unpack("S*"))
  303. s1 = [513, -514].pack("s!*")
  304. s2 = [513, 65022].pack("S!*")
  305. assert_equal(s1, s2)
  306. assert_equal([513, -514], s2.unpack("s!*"))
  307. assert_equal([513, 65022], s1.unpack("S!*"))
  308. assert_equal(2, [1].pack("s").bytesize)
  309. assert_equal(2, [1].pack("S").bytesize)
  310. assert_operator(2, :<=, [1].pack("s!").bytesize)
  311. assert_operator(2, :<=, [1].pack("S!").bytesize)
  312. end
  313. def test_pack_unpack_iI
  314. s1 = [67305985, -50462977].pack("i*")
  315. s2 = [67305985, 4244504319].pack("I*")
  316. assert_equal(s1, s2)
  317. assert_equal([67305985, -50462977], s2.unpack("i*"))
  318. assert_equal([67305985, 4244504319], s1.unpack("I*"))
  319. s1 = [67305985, -50462977].pack("i!*")
  320. s2 = [67305985, 4244504319].pack("I!*")
  321. assert_equal([67305985, -50462977], s1.unpack("i!*"))
  322. assert_equal([67305985, 4244504319], s2.unpack("I!*"))
  323. assert_operator(4, :<=, [1].pack("i").bytesize)
  324. assert_operator(4, :<=, [1].pack("I").bytesize)
  325. assert_operator(4, :<=, [1].pack("i!").bytesize)
  326. assert_operator(4, :<=, [1].pack("I!").bytesize)
  327. end
  328. def test_pack_unpack_lL
  329. s1 = [67305985, -50462977].pack("l*")
  330. s2 = [67305985, 4244504319].pack("L*")
  331. assert_equal(s1, s2)
  332. assert_equal([67305985, -50462977], s2.unpack("l*"))
  333. assert_equal([67305985, 4244504319], s1.unpack("L*"))
  334. s1 = [67305985, -50462977].pack("l!*")
  335. s2 = [67305985, 4244504319].pack("L!*")
  336. assert_equal([67305985, -50462977], s1.unpack("l!*"))
  337. assert_equal([67305985, 4244504319], s2.unpack("L!*"))
  338. assert_equal(4, [1].pack("l").bytesize)
  339. assert_equal(4, [1].pack("L").bytesize)
  340. assert_operator(4, :<=, [1].pack("l!").bytesize)
  341. assert_operator(4, :<=, [1].pack("L!").bytesize)
  342. end
  343. def test_pack_unpack_qQ
  344. s1 = [578437695752307201, -506097522914230529].pack("q*")
  345. s2 = [578437695752307201, 17940646550795321087].pack("Q*")
  346. assert_equal(s1, s2)
  347. assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*"))
  348. assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*"))
  349. assert_equal(8, [1].pack("q").bytesize)
  350. assert_equal(8, [1].pack("Q").bytesize)
  351. end
  352. def test_pack_unpack_nN
  353. assert_equal("\000\000\000\001\377\377\177\377\200\000\377\377", [0,1,-1,32767,-32768,65535].pack("n*"))
  354. assert_equal("\000\000\000\000\000\000\000\001\377\377\377\377", [0,1,-1].pack("N*"))
  355. assert_equal([0,1,65535,32767,32768,65535], "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*"))
  356. assert_equal([0,1,4294967295], "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*"))
  357. assert_equal(2, [1].pack("n").bytesize)
  358. assert_equal(4, [1].pack("N").bytesize)
  359. end
  360. def test_pack_unpack_vV
  361. assert_equal("\000\000\001\000\377\377\377\177\000\200\377\377", [0,1,-1,32767,-32768,65535].pack("v*"))
  362. assert_equal("\000\000\000\000\001\000\000\000\377\377\377\377", [0,1,-1].pack("V*"))
  363. assert_equal([0,1,65535,32767,32768,65535], "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*"))
  364. assert_equal([0,1,4294967295], "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*"))
  365. assert_equal(2, [1].pack("v").bytesize)
  366. assert_equal(4, [1].pack("V").bytesize)
  367. end
  368. def test_pack_unpack_fdeEgG
  369. inf = 1.0/0.0
  370. nan = inf/inf
  371. [0.0, 1.0, 3.0, inf, -inf, nan].each do |x|
  372. %w(f d e E g G).each do |f|
  373. v = [x].pack(f).unpack(f)
  374. if x.nan?
  375. assert(v.first.nan?)
  376. else
  377. assert_equal([x], v)
  378. end
  379. end
  380. end
  381. end
  382. def test_pack_unpack_x
  383. assert_equal("", [].pack("x0"))
  384. assert_equal("\0", [].pack("x"))
  385. assert_equal("\0" * 30, [].pack("x30"))
  386. assert_equal([0, 2], "\x00\x00\x02".unpack("CxC"))
  387. assert_raise(ArgumentError) { "".unpack("x") }
  388. end
  389. def test_pack_unpack_X
  390. assert_equal("\x00\x02", [0, 1, 2].pack("CCXC"))
  391. assert_equal("\x02", [0, 1, 2].pack("CCX2C"))
  392. assert_raise(ArgumentError) { [].pack("X") }
  393. assert_equal([0, 2, 2], "\x00\x02".unpack("CCXC"))
  394. assert_raise(ArgumentError) { "".unpack("X") }
  395. end
  396. def test_pack_unpack_atmark
  397. assert_equal("\x01\x00\x00\x02", [1, 2].pack("C@3C"))
  398. assert_equal("\x02", [1, 2].pack("C@0C"))
  399. assert_equal("\x01\x02", [1, 2].pack("C@1C"))
  400. assert_equal([1, 2], "\x01\x00\x00\x02".unpack("C@3C"))
  401. assert_equal([nil], "\x00".unpack("@1C")) # is it OK?
  402. assert_raise(ArgumentError) { "\x00".unpack("@2C") }
  403. end
  404. def test_pack_unpack_percent
  405. assert_raise(ArgumentError) { [].pack("%") }
  406. assert_raise(ArgumentError) { "".unpack("%") }
  407. end
  408. def test_pack_unpack_U
  409. assert_equal([0], [0].pack("U").unpack("U"))
  410. assert_equal([0x80], [0x80].pack("U").unpack("U"))
  411. assert_equal([0x800], [0x800].pack("U").unpack("U"))
  412. assert_equal([0x10000], [0x10000].pack("U").unpack("U"))
  413. assert_equal([0x400000], [0x400000].pack("U").unpack("U"))
  414. assert_raise(ArgumentError) { "\x80".unpack("U") }
  415. assert_raise(ArgumentError) { "\xff".unpack("U") }
  416. assert_raise(ArgumentError) { "\xfc\x00".unpack("U") }
  417. assert_raise(ArgumentError) { "\xc0\xc0".unpack("U") }
  418. assert_raise(ArgumentError) { "\xe0\x80\x80".unpack("U") }
  419. end
  420. def test_pack_unpack_u
  421. assert_equal("", [""].pack("u"))
  422. assert_equal("!80``\n", ["a"].pack("u"))
  423. assert_equal("#86)C\n", ["abc"].pack("u"))
  424. assert_equal("$86)C9```\n", ["abcd"].pack("u"))
  425. assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n", ["a"*45].pack("u"))
  426. 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"))
  427. assert_equal("&86)C9&5F\n#9VAI\n", ["abcdefghi"].pack("u6"))
  428. 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"))
  429. 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"))
  430. 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"))
  431. assert_equal([""], "".unpack("u"))
  432. assert_equal(["a"], "!80``\n".unpack("u"))
  433. assert_equal(["abc"], "#86)C\n".unpack("u"))
  434. assert_equal(["abcd"], "$86)C9```\n".unpack("u"))
  435. assert_equal(["a"*45], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n".unpack("u"))
  436. 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"))
  437. assert_equal(["abcdefghi"], "&86)C9&5F\n#9VAI\n".unpack("u"))
  438. assert_equal(["\x00"], "\"\n".unpack("u"))
  439. assert_equal(["\x00"], "! \r \n".unpack("u"))
  440. end
  441. def test_pack_unpack_m
  442. assert_equal("", [""].pack("m"))
  443. assert_equal("AA==\n", ["\0"].pack("m"))
  444. assert_equal("AAA=\n", ["\0\0"].pack("m"))
  445. assert_equal("AAAA\n", ["\0\0\0"].pack("m"))
  446. assert_equal("/w==\n", ["\377"].pack("m"))
  447. assert_equal("//8=\n", ["\377\377"].pack("m"))
  448. assert_equal("////\n", ["\377\377\377"].pack("m"))
  449. assert_equal([""], "".unpack("m"))
  450. assert_equal(["\0"], "AA==\n".unpack("m"))
  451. assert_equal(["\0\0"], "AAA=\n".unpack("m"))
  452. assert_equal(["\0\0\0"], "AAAA\n".unpack("m"))
  453. assert_equal(["\377"], "/w==\n".unpack("m"))
  454. assert_equal(["\377\377"], "//8=\n".unpack("m"))
  455. assert_equal(["\377\377\377"], "////\n".unpack("m"))
  456. end
  457. def test_pack_unpack_m0
  458. assert_equal("", [""].pack("m0"))
  459. assert_equal("AA==", ["\0"].pack("m0"))
  460. assert_equal("AAA=", ["\0\0"].pack("m0"))
  461. assert_equal("AAAA", ["\0\0\0"].pack("m0"))
  462. assert_equal("/w==", ["\377"].pack("m0"))
  463. assert_equal("//8=", ["\377\377"].pack("m0"))
  464. assert_equal("////", ["\377\377\377"].pack("m0"))
  465. assert_equal([""], "".unpack("m0"))
  466. assert_equal(["\0"], "AA==".unpack("m0"))
  467. assert_equal(["\0\0"], "AAA=".unpack("m0"))
  468. assert_equal(["\0\0\0"], "AAAA".unpack("m0"))
  469. assert_equal(["\377"], "/w==".unpack("m0"))
  470. assert_equal(["\377\377"], "//8=".unpack("m0"))
  471. assert_equal(["\377\377\377"], "////".unpack("m0"))
  472. assert_raise(ArgumentError) { "^".unpack("m0") }
  473. assert_raise(ArgumentError) { "A".unpack("m0") }
  474. assert_raise(ArgumentError) { "A^".unpack("m0") }
  475. assert_raise(ArgumentError) { "AA".unpack("m0") }
  476. assert_raise(ArgumentError) { "AA=".unpack("m0") }
  477. assert_raise(ArgumentError) { "AA===".unpack("m0") }
  478. assert_raise(ArgumentError) { "AA=x".unpack("m0") }
  479. assert_raise(ArgumentError) { "AAA".unpack("m0") }
  480. assert_raise(ArgumentError) { "AAA^".unpack("m0") }
  481. assert_raise(ArgumentError) { "AB==".unpack("m0") }
  482. assert_raise(ArgumentError) { "AAB=".unpack("m0") }
  483. end
  484. def test_pack_unpack_M
  485. assert_equal("a b c\td =\n\ne=\n", ["a b c\td \ne"].pack("M"))
  486. assert_equal(["a b c\td \ne"], "a b c\td =\n\ne=\n".unpack("M"))
  487. assert_equal("=00=\n", ["\0"].pack("M"))
  488. assert_equal("a"*73+"=\na=\n", ["a"*74].pack("M"))
  489. assert_equal(("a"*73+"=\n")*14+"a=\n", ["a"*1023].pack("M"))
  490. assert_equal(["\0"], "=00=\n".unpack("M"))
  491. assert_equal(["a"*74], ("a"*73+"=\na=\n").unpack("M"))
  492. assert_equal(["a"*1023], (("a"*73+"=\n")*14+"a=\n").unpack("M"))
  493. assert_equal(["\x0a"], "=0a=\n".unpack("M"))
  494. assert_equal(["\x0a"], "=0A=\n".unpack("M"))
  495. assert_equal([""], "=0Z=\n".unpack("M"))
  496. assert_equal([""], "=\r\n".unpack("M"))
  497. assert_equal([""], "=\r\n".unpack("M"))
  498. assert_equal(["\xC6\xF7"], "=C6=F7".unpack('M*'))
  499. end
  500. def test_pack_unpack_P2
  501. assert_raise(ArgumentError) { ["abc"].pack("P4") }
  502. assert_raise(ArgumentError) { [""].pack("P") }
  503. assert_equal([], ".".unpack("P"))
  504. assert_equal([], ".".unpack("p"))
  505. assert_equal([nil], ("\0" * 1024).unpack("P"))
  506. end
  507. def test_pack_p2
  508. assert_match(/\A\0*\Z/, [nil].pack("p"))
  509. end
  510. def test_pack_unpack_w
  511. assert_equal("\000", [0].pack("w"))
  512. assert_equal("\001", [1].pack("w"))
  513. assert_equal("\177", [127].pack("w"))
  514. assert_equal("\201\000", [128].pack("w"))
  515. assert_equal("\377\177", [0x3fff].pack("w"))
  516. assert_equal("\201\200\000", [0x4000].pack("w"))
  517. assert_equal("\203\377\377\377\177", [0x3fffffff].pack("w"))
  518. assert_equal("\204\200\200\200\000", [0x40000000].pack("w"))
  519. assert_equal("\217\377\377\377\177", [0xffffffff].pack("w"))
  520. assert_equal("\220\200\200\200\000", [0x100000000].pack("w"))
  521. assert_raise(ArgumentError) { [-1].pack("w") }
  522. assert_equal([0], "\000".unpack("w"))
  523. assert_equal([1], "\001".unpack("w"), [1])
  524. assert_equal([127], "\177".unpack("w"), [127])
  525. assert_equal([128], "\201\000".unpack("w"), [128])
  526. assert_equal([0x3fff], "\377\177".unpack("w"), [0x3fff])
  527. assert_equal([0x4000], "\201\200\000".unpack("w"), [0x4000])
  528. assert_equal([0x3fffffff], "\203\377\377\377\177".unpack("w"), [0x3fffffff])
  529. assert_equal([0x40000000], "\204\200\200\200\000".unpack("w"), [0x40000000])
  530. assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff])
  531. assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000])
  532. end
  533. def test_pack_unpack_M
  534. assert_equal(["pre123after"], "pre=31=32=33after".unpack("M"))
  535. assert_equal(["preafter"], "pre=\nafter".unpack("M"))
  536. assert_equal(["preafter"], "pre=\r\nafter".unpack("M"))
  537. assert_equal(["pre="], "pre=".unpack("M"))
  538. assert_equal(["pre=\r"], "pre=\r".unpack("M"))
  539. assert_equal(["pre=hoge"], "pre=hoge".unpack("M"))
  540. assert_equal(["pre==31after"], "pre==31after".unpack("M"))
  541. assert_equal(["pre===31after"], "pre===31after".unpack("M"))
  542. end
  543. def test_modify_under_safe4
  544. s = "foo"
  545. assert_raise(SecurityError) do
  546. Thread.new do
  547. $SAFE = 4
  548. s.clear
  549. end.join
  550. end
  551. end
  552. def test_length_too_big
  553. assert_raise(RangeError) { [].pack("C100000000000000000000") }
  554. end
  555. def test_short_string
  556. %w[n N v V s S i I l L q Q s! S! i! I! l! l!].each {|fmt|
  557. str = [1].pack(fmt)
  558. assert_equal([1,nil], str.unpack("#{fmt}2"))
  559. }
  560. end
  561. def test_short_with_block
  562. bug4059 = '[ruby-core:33193]'
  563. result = :ok
  564. assert_nil("".unpack("i") {|x| result = x}, bug4059)
  565. assert_equal(:ok, result)
  566. end
  567. end