PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.9/zlib/test_zlib.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 838 lines | 704 code | 130 blank | 4 comment | 1 complexity | 87346706bb57d6f7837387ae1c6d514b MD5 | raw file
  1. require 'test/unit'
  2. require 'stringio'
  3. require 'tempfile'
  4. begin
  5. require 'zlib'
  6. rescue LoadError
  7. end
  8. if defined? Zlib
  9. class TestZlibDeflate < Test::Unit::TestCase
  10. def test_initialize
  11. z = Zlib::Deflate.new
  12. s = z.deflate("foo", Zlib::FINISH)
  13. assert_equal("foo", Zlib::Inflate.inflate(s))
  14. z = Zlib::Deflate.new
  15. s = z.deflate("foo")
  16. s << z.deflate(nil, Zlib::FINISH)
  17. assert_equal("foo", Zlib::Inflate.inflate(s))
  18. assert_raise(Zlib::StreamError) { Zlib::Deflate.new(10000) }
  19. end
  20. def test_dup
  21. z1 = Zlib::Deflate.new
  22. s = z1.deflate("foo")
  23. z2 = z1.dup
  24. s1 = s + z1.deflate("bar", Zlib::FINISH)
  25. s2 = s + z2.deflate("baz", Zlib::FINISH)
  26. assert_equal("foobar", Zlib::Inflate.inflate(s1))
  27. assert_equal("foobaz", Zlib::Inflate.inflate(s2))
  28. end
  29. def test_deflate
  30. s = Zlib::Deflate.deflate("foo")
  31. assert_equal("foo", Zlib::Inflate.inflate(s))
  32. assert_raise(Zlib::StreamError) { Zlib::Deflate.deflate("foo", 10000) }
  33. end
  34. def test_addstr
  35. z = Zlib::Deflate.new
  36. z << "foo"
  37. s = z.deflate(nil, Zlib::FINISH)
  38. assert_equal("foo", Zlib::Inflate.inflate(s))
  39. end
  40. def test_flush
  41. z = Zlib::Deflate.new
  42. z << "foo"
  43. s = z.flush
  44. z << "bar"
  45. s << z.flush_next_in
  46. z << "baz"
  47. s << z.flush_next_out
  48. s << z.deflate("qux", Zlib::FINISH)
  49. assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
  50. end
  51. def test_avail
  52. z = Zlib::Deflate.new
  53. assert_equal(0, z.avail_in)
  54. assert_equal(0, z.avail_out)
  55. z << "foo"
  56. z.avail_out += 100
  57. z << "bar"
  58. s = z.finish
  59. assert_equal("foobar", Zlib::Inflate.inflate(s))
  60. end
  61. def test_total
  62. z = Zlib::Deflate.new
  63. 1000.times { z << "foo" }
  64. s = z.finish
  65. assert_equal(3000, z.total_in)
  66. assert_operator(3000, :>, z.total_out)
  67. assert_equal("foo" * 1000, Zlib::Inflate.inflate(s))
  68. end
  69. def test_data_type
  70. z = Zlib::Deflate.new
  71. assert([Zlib::ASCII, Zlib::BINARY, Zlib::UNKNOWN].include?(z.data_type))
  72. end
  73. def test_adler
  74. z = Zlib::Deflate.new
  75. z << "foo"
  76. z.finish
  77. assert_equal(0x02820145, z.adler)
  78. end
  79. def test_finished_p
  80. z = Zlib::Deflate.new
  81. assert_equal(false, z.finished?)
  82. z << "foo"
  83. assert_equal(false, z.finished?)
  84. z.finish
  85. assert_equal(true, z.finished?)
  86. z.close
  87. assert_raise(Zlib::Error) { z.finished? }
  88. end
  89. def test_closed_p
  90. z = Zlib::Deflate.new
  91. assert_equal(false, z.closed?)
  92. z << "foo"
  93. assert_equal(false, z.closed?)
  94. s = z.finish
  95. assert_equal(false, z.closed?)
  96. z.close
  97. assert_equal(true, z.closed?)
  98. end
  99. def test_params
  100. z = Zlib::Deflate.new
  101. z << "foo"
  102. z.params(Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  103. z << "bar"
  104. s = z.finish
  105. assert_equal("foobar", Zlib::Inflate.inflate(s))
  106. data = ('a'..'z').to_a.join
  107. z = Zlib::Deflate.new(Zlib::NO_COMPRESSION, Zlib::MAX_WBITS,
  108. Zlib::DEF_MEM_LEVEL, Zlib::DEFAULT_STRATEGY)
  109. z << data[0, 10]
  110. z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  111. z << data[10 .. -1]
  112. assert_equal(data, Zlib::Inflate.inflate(z.finish))
  113. z = Zlib::Deflate.new
  114. s = z.deflate("foo", Zlib::FULL_FLUSH)
  115. z.avail_out = 0
  116. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  117. s << z.deflate("bar", Zlib::FULL_FLUSH)
  118. z.avail_out = 0
  119. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  120. s << z.deflate("baz", Zlib::FINISH)
  121. assert_equal("foobarbaz", Zlib::Inflate.inflate(s))
  122. z = Zlib::Deflate.new
  123. assert_raise(Zlib::StreamError) { z.params(10000, 10000) }
  124. z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
  125. end
  126. def test_set_dictionary
  127. z = Zlib::Deflate.new
  128. z.set_dictionary("foo")
  129. s = z.deflate("foo" * 100, Zlib::FINISH)
  130. z = Zlib::Inflate.new
  131. assert_raise(Zlib::NeedDict) { z.inflate(s) }
  132. z.set_dictionary("foo")
  133. assert_equal("foo" * 100, z.inflate(s)) # ???
  134. z = Zlib::Deflate.new
  135. z << "foo"
  136. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  137. z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
  138. end
  139. def test_reset
  140. z = Zlib::Deflate.new
  141. z << "foo"
  142. z.reset
  143. z << "bar"
  144. s = z.finish
  145. assert_equal("bar", Zlib::Inflate.inflate(s))
  146. end
  147. def test_close
  148. z = Zlib::Deflate.new
  149. z.close
  150. assert_raise(Zlib::Error) { z << "foo" }
  151. assert_raise(Zlib::Error) { z.reset }
  152. end
  153. end
  154. class TestZlibInflate < Test::Unit::TestCase
  155. def test_initialize
  156. assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
  157. s = Zlib::Deflate.deflate("foo")
  158. z = Zlib::Inflate.new
  159. z << s << nil
  160. assert_equal("foo", z.finish)
  161. end
  162. def test_inflate
  163. s = Zlib::Deflate.deflate("foo")
  164. z = Zlib::Inflate.new
  165. s = z.inflate(s)
  166. s << z.inflate(nil)
  167. assert_equal("foo", s)
  168. z.inflate("foo") # ???
  169. z << "foo" # ???
  170. end
  171. def test_inflate_dictionary
  172. dictionary = "foo"
  173. deflate = Zlib::Deflate.new
  174. deflate.set_dictionary dictionary
  175. compressed = deflate.deflate "foofoofoo", Zlib::FINISH
  176. deflate.close
  177. out = nil
  178. inflate = Zlib::Inflate.new
  179. begin
  180. out = inflate.inflate compressed
  181. flunk "Zlib::NeedDict was not raised"
  182. rescue Zlib::NeedDict
  183. inflate.set_dictionary dictionary
  184. out = inflate.inflate ""
  185. end
  186. assert_equal "foofoofoo", out
  187. end
  188. def test_sync
  189. z = Zlib::Deflate.new
  190. s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
  191. z.avail_out = 0
  192. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  193. s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
  194. z.avail_out = 0
  195. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  196. s << z.deflate("baz" * 1000, Zlib::FINISH)
  197. z = Zlib::Inflate.new
  198. assert_raise(Zlib::DataError) { z << "\0" * 100 }
  199. assert_equal(false, z.sync(""))
  200. assert_equal(false, z.sync_point?)
  201. z = Zlib::Inflate.new
  202. assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
  203. assert_equal(true, z.sync(""))
  204. #assert_equal(true, z.sync_point?)
  205. z = Zlib::Inflate.new
  206. assert_equal(false, z.sync("\0" * 100))
  207. assert_equal(false, z.sync_point?)
  208. z = Zlib::Inflate.new
  209. assert_equal(true, z.sync("\0" * 100 + s))
  210. #assert_equal(true, z.sync_point?)
  211. end
  212. def test_set_dictionary
  213. z = Zlib::Inflate.new
  214. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  215. z.close
  216. end
  217. end
  218. class TestZlibGzipFile < Test::Unit::TestCase
  219. def test_to_io
  220. t = Tempfile.new("test_zlib_gzip_file_to_io")
  221. t.close
  222. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  223. Zlib::GzipReader.open(t.path) do |f|
  224. assert_kind_of(IO, f.to_io)
  225. end
  226. end
  227. def test_crc
  228. t = Tempfile.new("test_zlib_gzip_file_crc")
  229. t.close
  230. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  231. Zlib::GzipReader.open(t.path) do |f|
  232. f.read
  233. assert_equal(0x8c736521, f.crc)
  234. end
  235. end
  236. def test_mtime
  237. tim = Time.now
  238. t = Tempfile.new("test_zlib_gzip_file_mtime")
  239. t.close
  240. Zlib::GzipWriter.open(t.path) do |gz|
  241. gz.mtime = -1
  242. gz.mtime = tim
  243. gz.print("foo")
  244. gz.flush
  245. assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
  246. end
  247. Zlib::GzipReader.open(t.path) do |f|
  248. assert_equal(tim.to_i, f.mtime.to_i)
  249. end
  250. end
  251. def test_level
  252. t = Tempfile.new("test_zlib_gzip_file_level")
  253. t.close
  254. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  255. Zlib::GzipReader.open(t.path) do |f|
  256. assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
  257. end
  258. end
  259. def test_os_code
  260. t = Tempfile.new("test_zlib_gzip_file_os_code")
  261. t.close
  262. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  263. Zlib::GzipReader.open(t.path) do |f|
  264. assert_equal(Zlib::OS_CODE, f.os_code)
  265. end
  266. end
  267. def test_orig_name
  268. t = Tempfile.new("test_zlib_gzip_file_orig_name")
  269. t.close
  270. Zlib::GzipWriter.open(t.path) do |gz|
  271. gz.orig_name = "foobarbazqux\0quux"
  272. gz.print("foo")
  273. gz.flush
  274. assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
  275. end
  276. Zlib::GzipReader.open(t.path) do |f|
  277. assert_equal("foobarbazqux", f.orig_name)
  278. end
  279. end
  280. def test_comment
  281. t = Tempfile.new("test_zlib_gzip_file_comment")
  282. t.close
  283. Zlib::GzipWriter.open(t.path) do |gz|
  284. gz.comment = "foobarbazqux\0quux"
  285. gz.print("foo")
  286. gz.flush
  287. assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
  288. end
  289. Zlib::GzipReader.open(t.path) do |f|
  290. assert_equal("foobarbazqux", f.comment)
  291. end
  292. end
  293. def test_lineno
  294. t = Tempfile.new("test_zlib_gzip_file_lineno")
  295. t.close
  296. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
  297. Zlib::GzipReader.open(t.path) do |f|
  298. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  299. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  300. f.lineno = 1000
  301. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  302. assert_equal([1001, "qux\n"], [f.lineno, f.gets])
  303. end
  304. end
  305. def test_closed_p
  306. t = Tempfile.new("test_zlib_gzip_file_closed_p")
  307. t.close
  308. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  309. Zlib::GzipReader.open(t.path) do |f|
  310. assert_equal(false, f.closed?)
  311. f.read
  312. assert_equal(false, f.closed?)
  313. f.close
  314. assert_equal(true, f.closed?)
  315. end
  316. end
  317. def test_sync
  318. t = Tempfile.new("test_zlib_gzip_file_sync")
  319. t.close
  320. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  321. Zlib::GzipReader.open(t.path) do |f|
  322. f.sync = true
  323. assert_equal(true, f.sync)
  324. f.read
  325. f.sync = false
  326. assert_equal(false, f.sync)
  327. end
  328. end
  329. def test_pos
  330. t = Tempfile.new("test_zlib_gzip_file_pos")
  331. t.close
  332. Zlib::GzipWriter.open(t.path) do |gz|
  333. gz.print("foo")
  334. gz.flush
  335. assert_equal(3, gz.tell)
  336. end
  337. end
  338. def test_path
  339. t = Tempfile.new("test_zlib_gzip_file_path")
  340. t.close
  341. gz = Zlib::GzipWriter.open(t.path)
  342. gz.print("foo")
  343. assert_equal(t.path, gz.path)
  344. gz.close
  345. assert_equal(t.path, gz.path)
  346. Zlib::GzipReader.open(t.path) do |f|
  347. assert_equal(t.path, f.path)
  348. f.close
  349. assert_equal(t.path, f.path)
  350. end
  351. s = ""
  352. sio = StringIO.new(s)
  353. gz = Zlib::GzipWriter.new(sio)
  354. gz.print("foo")
  355. assert_raise(NoMethodError) { gz.path }
  356. gz.close
  357. sio = StringIO.new(s)
  358. Zlib::GzipReader.new(sio) do |f|
  359. assert_raise(NoMethodError) { f.path }
  360. end
  361. end
  362. end
  363. class TestZlibGzipReader < Test::Unit::TestCase
  364. D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
  365. def test_read0
  366. assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
  367. end
  368. def test_ungetc
  369. s = ""
  370. w = Zlib::GzipWriter.new(StringIO.new(s))
  371. w << (1...1000).to_a.inspect
  372. w.close
  373. r = Zlib::GzipReader.new(StringIO.new(s))
  374. r.read(100)
  375. r.ungetc ?a
  376. assert_nothing_raised("[ruby-dev:24060]") {
  377. r.read(100)
  378. r.read
  379. r.close
  380. }
  381. end
  382. def test_ungetc_paragraph
  383. s = ""
  384. w = Zlib::GzipWriter.new(StringIO.new(s))
  385. w << "abc"
  386. w.close
  387. r = Zlib::GzipReader.new(StringIO.new(s))
  388. r.ungetc ?\n
  389. assert_equal("abc", r.gets(""))
  390. assert_nothing_raised("[ruby-dev:24065]") {
  391. r.read
  392. r.close
  393. }
  394. end
  395. def test_open
  396. t = Tempfile.new("test_zlib_gzip_reader_open")
  397. t.close
  398. e = assert_raise(Zlib::GzipFile::Error) {
  399. Zlib::GzipReader.open(t.path)
  400. }
  401. assert_equal("not in gzip format", e.message)
  402. assert_nil(e.input)
  403. open(t.path, "wb") {|f| f.write("foo")}
  404. e = assert_raise(Zlib::GzipFile::Error) {
  405. Zlib::GzipReader.open(t.path)
  406. }
  407. assert_equal("not in gzip format", e.message)
  408. assert_equal("foo", e.input)
  409. open(t.path, "wb") {|f| f.write("foobarzothoge")}
  410. e = assert_raise(Zlib::GzipFile::Error) {
  411. Zlib::GzipReader.open(t.path)
  412. }
  413. assert_equal("not in gzip format", e.message)
  414. assert_equal("foobarzothoge", e.input)
  415. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  416. assert_raise(ArgumentError) { Zlib::GzipReader.open }
  417. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  418. f = Zlib::GzipReader.open(t.path)
  419. begin
  420. assert_equal("foo", f.read)
  421. ensure
  422. f.close
  423. end
  424. end
  425. def test_rewind
  426. t = Tempfile.new("test_zlib_gzip_reader_rewind")
  427. t.close
  428. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  429. Zlib::GzipReader.open(t.path) do |f|
  430. assert_equal("foo", f.read)
  431. f.rewind
  432. assert_equal("foo", f.read)
  433. end
  434. end
  435. def test_unused
  436. t = Tempfile.new("test_zlib_gzip_reader_unused")
  437. t.close
  438. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  439. Zlib::GzipReader.open(t.path) do |f|
  440. assert_equal(nil, f.unused)
  441. assert_equal("foo", f.read(3))
  442. assert_equal(nil, f.unused)
  443. assert_equal("bar", f.read)
  444. assert_equal(nil, f.unused)
  445. end
  446. end
  447. def test_unused2
  448. zio = StringIO.new
  449. io = Zlib::GzipWriter.new zio
  450. io.write 'aaaa'
  451. io.finish
  452. io = Zlib::GzipWriter.new zio
  453. io.write 'bbbb'
  454. io.finish
  455. zio.rewind
  456. io = Zlib::GzipReader.new zio
  457. assert_equal('aaaa', io.read)
  458. unused = io.unused
  459. assert_equal(24, unused.bytesize)
  460. io.finish
  461. zio.pos -= unused.length
  462. io = Zlib::GzipReader.new zio
  463. assert_equal('bbbb', io.read)
  464. assert_equal(nil, io.unused)
  465. io.finish
  466. end
  467. def test_read
  468. t = Tempfile.new("test_zlib_gzip_reader_read")
  469. t.close
  470. str = "\u3042\u3044\u3046"
  471. Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
  472. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  473. assert_raise(ArgumentError) { f.read(-1) }
  474. assert_equal(str, f.read)
  475. end
  476. end
  477. def test_readpartial
  478. t = Tempfile.new("test_zlib_gzip_reader_readpartial")
  479. t.close
  480. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  481. Zlib::GzipReader.open(t.path) do |f|
  482. assert("foo".start_with?(f.readpartial(3)))
  483. end
  484. Zlib::GzipReader.open(t.path) do |f|
  485. s = ""
  486. f.readpartial(3, s)
  487. assert("foo".start_with?(s))
  488. assert_raise(ArgumentError) { f.readpartial(-1) }
  489. end
  490. end
  491. def test_getc
  492. t = Tempfile.new("test_zlib_gzip_reader_getc")
  493. t.close
  494. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  495. Zlib::GzipReader.open(t.path) do |f|
  496. "foobar".each_char {|c| assert_equal(c, f.getc) }
  497. assert_nil(f.getc)
  498. end
  499. end
  500. def test_getbyte
  501. t = Tempfile.new("test_zlib_gzip_reader_getbyte")
  502. t.close
  503. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  504. Zlib::GzipReader.open(t.path) do |f|
  505. "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
  506. assert_nil(f.getbyte)
  507. end
  508. end
  509. def test_readchar
  510. t = Tempfile.new("test_zlib_gzip_reader_readchar")
  511. t.close
  512. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  513. Zlib::GzipReader.open(t.path) do |f|
  514. "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
  515. assert_raise(EOFError) { f.readchar }
  516. end
  517. end
  518. def test_each_byte
  519. t = Tempfile.new("test_zlib_gzip_reader_each_byte")
  520. t.close
  521. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  522. Zlib::GzipReader.open(t.path) do |f|
  523. a = []
  524. f.each_byte {|c| a << c }
  525. assert_equal("foobar".each_byte.to_a, a)
  526. end
  527. end
  528. def test_gets
  529. t = Tempfile.new("test_zlib_gzip_reader_gets")
  530. t.close
  531. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  532. Zlib::GzipReader.open(t.path) do |f|
  533. assert_equal("foo\n", f.gets)
  534. assert_equal("bar\n", f.gets)
  535. assert_equal("baz\n", f.gets)
  536. assert_nil(f.gets)
  537. end
  538. Zlib::GzipReader.open(t.path) do |f|
  539. assert_equal("foo\nbar\nbaz\n", f.gets(nil))
  540. end
  541. Zlib::GzipReader.open(t.path) do |f|
  542. assert_equal("foo\n", f.gets(10))
  543. assert_equal("ba", f.gets(2))
  544. assert_equal("r\nb", f.gets(nil, 3))
  545. assert_equal("az\n", f.gets(nil, 10))
  546. assert_nil(f.gets)
  547. end
  548. end
  549. def test_gets2
  550. t = Tempfile.new("test_zlib_gzip_reader_gets2")
  551. t.close
  552. ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
  553. Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
  554. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  555. assert_equal(ustrs[0], f.gets)
  556. assert_equal(ustrs[1], f.gets)
  557. assert_equal(ustrs[2], f.gets)
  558. assert_nil(f.gets)
  559. end
  560. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  561. assert_equal(ustrs.join(''), f.gets(nil))
  562. end
  563. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  564. assert_equal(ustrs[0], f.gets(20))
  565. assert_equal(ustrs[1][0,2], f.gets(5))
  566. assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
  567. assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
  568. assert_nil(f.gets)
  569. end
  570. end
  571. def test_readline
  572. t = Tempfile.new("test_zlib_gzip_reader_readline")
  573. t.close
  574. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  575. Zlib::GzipReader.open(t.path) do |f|
  576. assert_equal("foo\n", f.readline)
  577. assert_equal("bar\n", f.readline)
  578. assert_equal("baz\n", f.readline)
  579. assert_raise(EOFError) { f.readline }
  580. end
  581. end
  582. def test_each
  583. t = Tempfile.new("test_zlib_gzip_reader_each")
  584. t.close
  585. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  586. Zlib::GzipReader.open(t.path) do |f|
  587. a = ["foo\n", "bar\n", "baz\n"]
  588. f.each {|l| assert_equal(a.shift, l) }
  589. end
  590. end
  591. def test_readlines
  592. t = Tempfile.new("test_zlib_gzip_reader_readlines")
  593. t.close
  594. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  595. Zlib::GzipReader.open(t.path) do |f|
  596. assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
  597. end
  598. end
  599. def test_reader_wrap
  600. t = Tempfile.new("test_zlib_gzip_reader_wrap")
  601. t.close
  602. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  603. f = open(t.path)
  604. assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
  605. assert_raise(IOError) { f.close }
  606. end
  607. def test_corrupted_header
  608. gz = Zlib::GzipWriter.new(StringIO.new(s = ""))
  609. gz.orig_name = "X"
  610. gz.comment = "Y"
  611. gz.print("foo")
  612. gz.finish
  613. # 14: magic(2) + method(1) + flag(1) + mtime(4) + exflag(1) + os(1) + orig_name(2) + comment(2)
  614. 1.upto(14) do |idx|
  615. assert_raise(Zlib::GzipFile::Error, idx) do
  616. Zlib::GzipReader.new(StringIO.new(s[0, idx])).read
  617. end
  618. end
  619. end
  620. end
  621. class TestZlibGzipWriter < Test::Unit::TestCase
  622. def test_invalid_new
  623. assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
  624. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
  625. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
  626. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
  627. end
  628. def test_open
  629. assert_raise(ArgumentError) { Zlib::GzipWriter.open }
  630. t = Tempfile.new("test_zlib_gzip_writer_open")
  631. t.close
  632. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  633. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  634. f = Zlib::GzipWriter.open(t.path)
  635. begin
  636. f.print("bar")
  637. ensure
  638. f.close
  639. end
  640. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  641. assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
  642. end
  643. def test_write
  644. t = Tempfile.new("test_zlib_gzip_writer_write")
  645. t.close
  646. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  647. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  648. o = Object.new
  649. def o.to_s; "bar"; end
  650. Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
  651. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  652. end
  653. def test_putc
  654. t = Tempfile.new("test_zlib_gzip_writer_putc")
  655. t.close
  656. Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
  657. assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  658. # todo: multibyte char
  659. end
  660. def test_writer_wrap
  661. t = Tempfile.new("test_zlib_gzip_writer_wrap")
  662. Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
  663. t.close
  664. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  665. end
  666. end
  667. class TestZlib < Test::Unit::TestCase
  668. def test_version
  669. assert_instance_of(String, Zlib.zlib_version)
  670. assert(Zlib.zlib_version.tainted?)
  671. end
  672. def test_adler32
  673. assert_equal(0x00000001, Zlib.adler32)
  674. assert_equal(0x02820145, Zlib.adler32("foo"))
  675. assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
  676. assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
  677. end
  678. def test_adler32_combine
  679. one = Zlib.adler32("fo")
  680. two = Zlib.adler32("o")
  681. begin
  682. assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
  683. rescue NotImplementedError
  684. skip "adler32_combine is not implemented"
  685. end
  686. end
  687. def test_crc32
  688. assert_equal(0x00000000, Zlib.crc32)
  689. assert_equal(0x8c736521, Zlib.crc32("foo"))
  690. assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
  691. assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
  692. end
  693. def test_crc32_combine
  694. one = Zlib.crc32("fo")
  695. two = Zlib.crc32("o")
  696. begin
  697. assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
  698. rescue NotImplementedError
  699. skip "crc32_combine is not implemented"
  700. end
  701. end
  702. def test_crc_table
  703. t = Zlib.crc_table
  704. assert_instance_of(Array, t)
  705. t.each {|x| assert_kind_of(Integer, x) }
  706. end
  707. end
  708. end