PageRenderTime 59ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/test/externals/ruby1.8/zlib/test_zlib.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 804 lines | 676 code | 122 blank | 6 comment | 3 complexity | df55d9ef6db69b0c7b908efc51d20d04 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  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 XXX_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. s = 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. s = 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. COMPRESS_MSG = '0000000100100011010001010110011110001001101010111100110111101111'
  154. def test_deflate_no_flush
  155. d = Zlib::Deflate.new
  156. d.deflate(COMPRESS_MSG, Zlib::SYNC_FLUSH) # for header output
  157. assert(d.deflate(COMPRESS_MSG, Zlib::NO_FLUSH).empty?)
  158. assert(!d.finish.empty?)
  159. d.close
  160. end
  161. def test_deflate_sync_flush
  162. d = Zlib::Deflate.new
  163. assert_nothing_raised do
  164. d.deflate(COMPRESS_MSG, Zlib::SYNC_FLUSH)
  165. end
  166. assert(!d.finish.empty?)
  167. d.close
  168. end
  169. def test_deflate_full_flush
  170. d = Zlib::Deflate.new
  171. assert_nothing_raised do
  172. d.deflate(COMPRESS_MSG, Zlib::FULL_FLUSH)
  173. end
  174. assert(!d.finish.empty?)
  175. d.close
  176. end
  177. def test_deflate_flush_finish
  178. d = Zlib::Deflate.new
  179. d.deflate("init", Zlib::SYNC_FLUSH) # for flushing header
  180. assert(!d.deflate(COMPRESS_MSG, Zlib::FINISH).empty?)
  181. d.close
  182. end
  183. def test_deflate_raise_after_finish
  184. d = Zlib::Deflate.new
  185. d.deflate("init")
  186. d.finish
  187. assert_raise(Zlib::StreamError) do
  188. d.deflate('foo')
  189. end
  190. #
  191. d = Zlib::Deflate.new
  192. d.deflate("init", Zlib::FINISH)
  193. assert_raise(Zlib::StreamError) do
  194. d.deflate('foo')
  195. end
  196. end
  197. end
  198. class TestZlibInflate < Test::Unit::TestCase
  199. def test_initialize
  200. assert_raise(Zlib::StreamError) { Zlib::Inflate.new(-1) }
  201. s = Zlib::Deflate.deflate("foo")
  202. z = Zlib::Inflate.new
  203. z << s << nil
  204. assert_equal("foo", z.finish)
  205. end
  206. def test_inflate
  207. s = Zlib::Deflate.deflate("foo")
  208. z = Zlib::Inflate.new
  209. s = z.inflate(s)
  210. s << z.inflate(nil)
  211. assert_equal("foo", s)
  212. z.inflate("foo") # ???
  213. z << "foo" # ???
  214. end
  215. def XXX_test_sync
  216. z = Zlib::Deflate.new
  217. s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
  218. z.avail_out = 0
  219. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  220. s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
  221. z.avail_out = 0
  222. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  223. s << z.deflate("baz" * 1000, Zlib::FINISH)
  224. z = Zlib::Inflate.new
  225. assert_raise(Zlib::DataError) { z << "\0" * 100 }
  226. assert_equal(false, z.sync(""))
  227. assert_equal(false, z.sync_point?)
  228. z = Zlib::Inflate.new
  229. assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
  230. assert_equal(true, z.sync(""))
  231. #assert_equal(true, z.sync_point?)
  232. z = Zlib::Inflate.new
  233. assert_equal(false, z.sync("\0" * 100))
  234. assert_equal(false, z.sync_point?)
  235. z = Zlib::Inflate.new
  236. assert_equal(true, z.sync("\0" * 100 + s))
  237. #assert_equal(true, z.sync_point?)
  238. end
  239. def test_set_dictionary
  240. z = Zlib::Inflate.new
  241. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  242. z.close
  243. end
  244. end
  245. class TestZlibGzipFile < Test::Unit::TestCase
  246. def test_to_io
  247. t = Tempfile.new("test_zlib_gzip_file")
  248. t.close
  249. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  250. f = Zlib::GzipReader.open(t.path)
  251. assert_kind_of(IO, f.to_io)
  252. end
  253. def test_crc
  254. t = Tempfile.new("test_zlib_gzip_file")
  255. t.close
  256. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  257. f = Zlib::GzipReader.open(t.path)
  258. f.read
  259. assert_equal(0x8c736521, f.crc)
  260. end
  261. def test_mtime
  262. tim = Time.now
  263. t = Tempfile.new("test_zlib_gzip_file")
  264. t.close
  265. Zlib::GzipWriter.open(t.path) do |gz|
  266. gz.mtime = -1
  267. gz.mtime = tim
  268. gz.print("foo")
  269. gz.flush
  270. assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
  271. end
  272. f = Zlib::GzipReader.open(t.path)
  273. assert_equal(tim.to_i, f.mtime.to_i)
  274. end
  275. def test_level
  276. t = Tempfile.new("test_zlib_gzip_file")
  277. t.close
  278. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  279. f = Zlib::GzipReader.open(t.path)
  280. assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
  281. end
  282. def test_os_code
  283. t = Tempfile.new("test_zlib_gzip_file")
  284. t.close
  285. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  286. f = Zlib::GzipReader.open(t.path)
  287. assert_equal(Zlib::OS_CODE, f.os_code)
  288. end
  289. def test_orig_name
  290. t = Tempfile.new("test_zlib_gzip_file")
  291. t.close
  292. Zlib::GzipWriter.open(t.path) do |gz|
  293. gz.orig_name = "foobarbazqux\0quux"
  294. gz.print("foo")
  295. gz.flush
  296. assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
  297. end
  298. f = Zlib::GzipReader.open(t.path)
  299. assert_equal("foobarbazqux", f.orig_name)
  300. end
  301. def test_comment
  302. t = Tempfile.new("test_zlib_gzip_file")
  303. t.close
  304. Zlib::GzipWriter.open(t.path) do |gz|
  305. gz.comment = "foobarbazqux\0quux"
  306. gz.print("foo")
  307. gz.flush
  308. assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
  309. end
  310. f = Zlib::GzipReader.open(t.path)
  311. assert_equal("foobarbazqux", f.comment)
  312. end
  313. def test_lineno
  314. t = Tempfile.new("test_zlib_gzip_file")
  315. t.close
  316. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
  317. f = Zlib::GzipReader.open(t.path)
  318. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  319. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  320. f.lineno = 1000
  321. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  322. assert_equal([1001, "qux\n"], [f.lineno, f.gets])
  323. end
  324. def test_closed_p
  325. t = Tempfile.new("test_zlib_gzip_file")
  326. t.close
  327. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  328. f = Zlib::GzipReader.open(t.path)
  329. assert_equal(false, f.closed?)
  330. f.read
  331. assert_equal(false, f.closed?)
  332. f.close
  333. assert_equal(true, f.closed?)
  334. end
  335. def XXX_test_sync
  336. t = Tempfile.new("test_zlib_gzip_file")
  337. t.close
  338. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  339. f = Zlib::GzipReader.open(t.path)
  340. f.sync = true
  341. assert_equal(true, f.sync)
  342. f.read
  343. f.sync = false
  344. assert_equal(false, f.sync)
  345. f.close
  346. end
  347. def test_pos
  348. t = Tempfile.new("test_zlib_gzip_file")
  349. t.close
  350. Zlib::GzipWriter.open(t.path) do |gz|
  351. gz.print("foo")
  352. gz.flush
  353. assert_equal(3, gz.tell)
  354. end
  355. end
  356. def test_path
  357. t = Tempfile.new("test_zlib_gzip_file")
  358. t.close
  359. gz = Zlib::GzipWriter.open(t.path)
  360. unless gz.respond_to?(:path)
  361. gz.close
  362. return
  363. end
  364. gz.print("foo")
  365. assert_equal(t.path, gz.path)
  366. gz.close
  367. assert_equal(t.path, gz.path)
  368. f = Zlib::GzipReader.open(t.path)
  369. assert_equal(t.path, f.path)
  370. f.close
  371. assert_equal(t.path, f.path)
  372. s = ""
  373. sio = StringIO.new(s)
  374. gz = Zlib::GzipWriter.new(sio)
  375. gz.print("foo")
  376. assert_raise(NoMethodError) { gz.path }
  377. gz.close
  378. sio = StringIO.new(s)
  379. f = Zlib::GzipReader.new(sio)
  380. assert_raise(NoMethodError) { f.path }
  381. f.close
  382. end
  383. end
  384. class TestZlibGzipReader < Test::Unit::TestCase
  385. D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
  386. def test_read0
  387. assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
  388. end
  389. def test_ungetc
  390. s = ""
  391. w = Zlib::GzipWriter.new(StringIO.new(s))
  392. w << (1...1000).to_a.inspect
  393. w.close
  394. r = Zlib::GzipReader.new(StringIO.new(s))
  395. r.read(100)
  396. r.ungetc ?a
  397. assert_nothing_raised("[ruby-dev:24060]") {
  398. r.read(100)
  399. r.read
  400. r.close
  401. }
  402. end
  403. def test_ungetc_paragraph
  404. s = ""
  405. w = Zlib::GzipWriter.new(StringIO.new(s))
  406. w << "abc"
  407. w.close
  408. r = Zlib::GzipReader.new(StringIO.new(s))
  409. r.ungetc ?\n
  410. assert_equal("abc", r.gets(""))
  411. assert_nothing_raised("[ruby-dev:24065]") {
  412. r.read
  413. r.close
  414. }
  415. end
  416. def test_open
  417. t = Tempfile.new("test_zlib_gzip_reader")
  418. t.close
  419. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  420. assert_raise(ArgumentError) { Zlib::GzipReader.open }
  421. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  422. f = Zlib::GzipReader.open(t.path)
  423. assert_equal("foo", f.read)
  424. f.close
  425. end
  426. def test_rewind
  427. t = Tempfile.new("test_zlib_gzip_reader")
  428. t.close
  429. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  430. f = Zlib::GzipReader.open(t.path)
  431. assert_equal("foo", f.read)
  432. f.rewind
  433. assert_equal("foo", f.read)
  434. f.close
  435. end
  436. def test_unused
  437. t = Tempfile.new("test_zlib_gzip_reader")
  438. t.close
  439. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  440. f = Zlib::GzipReader.open(t.path)
  441. assert_equal("foo", f.read(3))
  442. f.unused
  443. assert_equal("bar", f.read)
  444. f.unused
  445. f.close
  446. end
  447. def test_read
  448. t = Tempfile.new("test_zlib_gzip_reader")
  449. t.close
  450. str = "\u3042\u3044\u3046"
  451. Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
  452. f = Zlib::GzipReader.open(t.path)
  453. assert_raise(ArgumentError) { f.read(-1) }
  454. assert_equal(str, f.read)
  455. end
  456. def test_readpartial
  457. t = Tempfile.new("test_zlib_gzip_reader")
  458. t.close
  459. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  460. f = Zlib::GzipReader.open(t.path)
  461. if f.respond_to?(:readpartial)
  462. assert("foo".start_with?(f.readpartial(3)))
  463. f = Zlib::GzipReader.open(t.path)
  464. s = ""
  465. f.readpartial(3, s)
  466. assert("foo".start_with?(s))
  467. assert_raise(ArgumentError) { f.readpartial(-1) }
  468. end
  469. end
  470. def test_getc
  471. t = Tempfile.new("test_zlib_gzip_reader")
  472. t.close
  473. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  474. f = Zlib::GzipReader.open(t.path)
  475. # .chr should not be needed in the future.
  476. # f.getc of 1.9 returns "f" instead of 102.
  477. "foobar".each_char {|c| assert_equal(c, f.getc.chr) }
  478. assert_nil(f.getc)
  479. end
  480. def test_getbyte
  481. t = Tempfile.new("test_zlib_gzip_reader")
  482. t.close
  483. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  484. f = Zlib::GzipReader.open(t.path)
  485. if f.respond_to?(:getbyte)
  486. "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
  487. assert_nil(f.getbyte)
  488. end
  489. end
  490. def test_readchar
  491. t = Tempfile.new("test_zlib_gzip_reader")
  492. t.close
  493. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  494. f = Zlib::GzipReader.open(t.path)
  495. "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
  496. assert_raise(EOFError) { f.readchar }
  497. end
  498. def test_each_byte
  499. t = Tempfile.new("test_zlib_gzip_reader")
  500. t.close
  501. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  502. f = Zlib::GzipReader.open(t.path)
  503. a = []
  504. f.each_byte {|c| a << c }
  505. assert_equal("foobar".each_byte.to_a, a)
  506. end
  507. def test_gets
  508. t = Tempfile.new("test_zlib_gzip_reader")
  509. t.close
  510. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  511. f = Zlib::GzipReader.open(t.path)
  512. assert_equal("foo\n", f.gets)
  513. assert_equal("bar\n", f.gets)
  514. assert_equal("baz\n", f.gets)
  515. assert_nil(f.gets)
  516. f.close
  517. f = Zlib::GzipReader.open(t.path)
  518. assert_equal("foo\nbar\nbaz\n", f.gets(nil))
  519. f.close
  520. end
  521. def test_gets
  522. t = Tempfile.new("test_zlib_gzip_reader")
  523. t.close
  524. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  525. f = Zlib::GzipReader.open(t.path)
  526. assert_equal("foo\n", f.readline)
  527. assert_equal("bar\n", f.readline)
  528. assert_equal("baz\n", f.readline)
  529. assert_raise(EOFError) { f.readline }
  530. f.close
  531. end
  532. def test_each
  533. t = Tempfile.new("test_zlib_gzip_reader")
  534. t.close
  535. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  536. f = Zlib::GzipReader.open(t.path)
  537. a = ["foo\n", "bar\n", "baz\n"]
  538. f.each {|l| assert_equal(a.shift, l) }
  539. f.close
  540. end
  541. def test_readlines
  542. t = Tempfile.new("test_zlib_gzip_reader")
  543. t.close
  544. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  545. f = Zlib::GzipReader.open(t.path)
  546. assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
  547. f.close
  548. end
  549. def test_reader_wrap
  550. t = Tempfile.new("test_zlib_gzip_reader")
  551. t.close
  552. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  553. f = open(t.path)
  554. assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
  555. assert_raise(IOError) { f.close }
  556. end
  557. def test_native_exception_from_zlib_on_broken_header
  558. corrupt = StringIO.new
  559. corrupt.write('borkborkbork')
  560. begin
  561. Zlib::GzipReader.new(corrupt)
  562. flunk()
  563. rescue Zlib::GzipReader::Error
  564. end
  565. end
  566. def test_wrap
  567. content = StringIO.new "", "r+"
  568. Zlib::GzipWriter.wrap(content) do |io|
  569. io.write "hello\nworld\n"
  570. end
  571. content = StringIO.new content.string, "rb"
  572. gin = Zlib::GzipReader.new(content)
  573. assert_equal("hello\n", gin.gets)
  574. assert_equal("world\n", gin.gets)
  575. assert_nil gin.gets
  576. assert gin.eof?
  577. gin.close
  578. end
  579. def test_each_line_no_block
  580. t = Tempfile.new("test_zlib_gzip_reader")
  581. t.close
  582. Zlib::GzipWriter.open(t.path) { |io| io.write "hello\nworld\n" }
  583. lines = []
  584. z = Zlib::GzipReader.open(t.path)
  585. z.each_line do |line|
  586. lines << line
  587. end
  588. z.close
  589. assert_equal(2, lines.size, lines.inspect)
  590. assert_equal("hello\n", lines.first)
  591. assert_equal("world\n", lines.last)
  592. end
  593. def test_each_line_block
  594. t = Tempfile.new("test_zlib_gzip_reader")
  595. t.close
  596. Zlib::GzipWriter.open(t.path) { |io| io.write "hello\nworld\n" }
  597. lines = []
  598. Zlib::GzipReader.open(t.path) do |z|
  599. z.each_line do |line|
  600. lines << line
  601. end
  602. end
  603. assert_equal(2, lines.size, lines.inspect)
  604. end
  605. end
  606. class TestZlibGzipWriter < Test::Unit::TestCase
  607. def test_invalid_new
  608. assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
  609. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
  610. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
  611. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
  612. end
  613. def test_open
  614. assert_raise(ArgumentError) { Zlib::GzipWriter.open }
  615. t = Tempfile.new("test_zlib_gzip_writer")
  616. t.close
  617. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  618. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  619. f = Zlib::GzipWriter.open(t.path)
  620. f.print("bar")
  621. f.close
  622. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  623. assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
  624. end
  625. def test_write
  626. t = Tempfile.new("test_zlib_gzip_writer")
  627. t.close
  628. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  629. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  630. o = Object.new
  631. def o.to_s; "bar"; end
  632. Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
  633. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  634. end
  635. def test_putc
  636. t = Tempfile.new("test_zlib_gzip_writer")
  637. t.close
  638. Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
  639. assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  640. # todo: multibyte char
  641. end
  642. def test_writer_wrap
  643. t = Tempfile.new("test_zlib_gzip_writer")
  644. Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
  645. t.close
  646. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  647. end
  648. def test_empty_line
  649. t = Tempfile.new("test_zlib_gzip_writer")
  650. t.close
  651. Zlib::GzipWriter.open(t.path) { |io| io.write "hello\nworld\n\ngoodbye\n" }
  652. lines = nil
  653. Zlib::GzipReader.open(t.path) do |z|
  654. lines = z.readlines
  655. end
  656. assert_equal(4, lines.size, lines.inspect)
  657. end
  658. end
  659. class TestZlib < Test::Unit::TestCase
  660. def test_version
  661. assert_instance_of(String, Zlib.zlib_version)
  662. assert(Zlib.zlib_version.tainted?)
  663. end
  664. def test_adler32
  665. assert_equal(0x00000001, Zlib.adler32)
  666. assert_equal(0x02820145, Zlib.adler32("foo"))
  667. assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
  668. assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
  669. end
  670. def test_crc32
  671. assert_equal(0x00000000, Zlib.crc32)
  672. assert_equal(0x8c736521, Zlib.crc32("foo"))
  673. assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
  674. assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
  675. end
  676. def test_crc_table
  677. t = Zlib.crc_table
  678. assert_instance_of(Array, t)
  679. t.each {|x| assert_kind_of(Integer, x) }
  680. end
  681. end
  682. end