PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/zlib/test_zlib.rb

https://github.com/bhupendramishra/ruby
Ruby | 773 lines | 654 code | 116 blank | 3 comment | 1 complexity | 9abc6fa5dd8e56d5f8902d1a61a7d8b5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  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. 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. 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_sync
  172. z = Zlib::Deflate.new
  173. s = z.deflate("foo" * 1000, Zlib::FULL_FLUSH)
  174. z.avail_out = 0
  175. z.params(Zlib::NO_COMPRESSION, Zlib::FILTERED)
  176. s << z.deflate("bar" * 1000, Zlib::FULL_FLUSH)
  177. z.avail_out = 0
  178. z.params(Zlib::BEST_COMPRESSION, Zlib::HUFFMAN_ONLY)
  179. s << z.deflate("baz" * 1000, Zlib::FINISH)
  180. z = Zlib::Inflate.new
  181. assert_raise(Zlib::DataError) { z << "\0" * 100 }
  182. assert_equal(false, z.sync(""))
  183. assert_equal(false, z.sync_point?)
  184. z = Zlib::Inflate.new
  185. assert_raise(Zlib::DataError) { z << "\0" * 100 + s }
  186. assert_equal(true, z.sync(""))
  187. #assert_equal(true, z.sync_point?)
  188. z = Zlib::Inflate.new
  189. assert_equal(false, z.sync("\0" * 100))
  190. assert_equal(false, z.sync_point?)
  191. z = Zlib::Inflate.new
  192. assert_equal(true, z.sync("\0" * 100 + s))
  193. #assert_equal(true, z.sync_point?)
  194. end
  195. def test_set_dictionary
  196. z = Zlib::Inflate.new
  197. assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
  198. z.close
  199. end
  200. end
  201. class TestZlibGzipFile < Test::Unit::TestCase
  202. def test_to_io
  203. t = Tempfile.new("test_zlib_gzip_file_to_io")
  204. t.close
  205. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  206. Zlib::GzipReader.open(t.path) do |f|
  207. assert_kind_of(IO, f.to_io)
  208. end
  209. end
  210. def test_crc
  211. t = Tempfile.new("test_zlib_gzip_file_crc")
  212. t.close
  213. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  214. Zlib::GzipReader.open(t.path) do |f|
  215. f.read
  216. assert_equal(0x8c736521, f.crc)
  217. end
  218. end
  219. def test_mtime
  220. tim = Time.now
  221. t = Tempfile.new("test_zlib_gzip_file_mtime")
  222. t.close
  223. Zlib::GzipWriter.open(t.path) do |gz|
  224. gz.mtime = -1
  225. gz.mtime = tim
  226. gz.print("foo")
  227. gz.flush
  228. assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
  229. end
  230. Zlib::GzipReader.open(t.path) do |f|
  231. assert_equal(tim.to_i, f.mtime.to_i)
  232. end
  233. end
  234. def test_level
  235. t = Tempfile.new("test_zlib_gzip_file_level")
  236. t.close
  237. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  238. Zlib::GzipReader.open(t.path) do |f|
  239. assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
  240. end
  241. end
  242. def test_os_code
  243. t = Tempfile.new("test_zlib_gzip_file_os_code")
  244. t.close
  245. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  246. Zlib::GzipReader.open(t.path) do |f|
  247. assert_equal(Zlib::OS_CODE, f.os_code)
  248. end
  249. end
  250. def test_orig_name
  251. t = Tempfile.new("test_zlib_gzip_file_orig_name")
  252. t.close
  253. Zlib::GzipWriter.open(t.path) do |gz|
  254. gz.orig_name = "foobarbazqux\0quux"
  255. gz.print("foo")
  256. gz.flush
  257. assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
  258. end
  259. Zlib::GzipReader.open(t.path) do |f|
  260. assert_equal("foobarbazqux", f.orig_name)
  261. end
  262. end
  263. def test_comment
  264. t = Tempfile.new("test_zlib_gzip_file_comment")
  265. t.close
  266. Zlib::GzipWriter.open(t.path) do |gz|
  267. gz.comment = "foobarbazqux\0quux"
  268. gz.print("foo")
  269. gz.flush
  270. assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
  271. end
  272. Zlib::GzipReader.open(t.path) do |f|
  273. assert_equal("foobarbazqux", f.comment)
  274. end
  275. end
  276. def test_lineno
  277. t = Tempfile.new("test_zlib_gzip_file_lineno")
  278. t.close
  279. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
  280. Zlib::GzipReader.open(t.path) do |f|
  281. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  282. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  283. f.lineno = 1000
  284. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  285. assert_equal([1001, "qux\n"], [f.lineno, f.gets])
  286. end
  287. end
  288. def test_closed_p
  289. t = Tempfile.new("test_zlib_gzip_file_closed_p")
  290. t.close
  291. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  292. Zlib::GzipReader.open(t.path) do |f|
  293. assert_equal(false, f.closed?)
  294. f.read
  295. assert_equal(false, f.closed?)
  296. f.close
  297. assert_equal(true, f.closed?)
  298. end
  299. end
  300. def test_sync
  301. t = Tempfile.new("test_zlib_gzip_file_sync")
  302. t.close
  303. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  304. Zlib::GzipReader.open(t.path) do |f|
  305. f.sync = true
  306. assert_equal(true, f.sync)
  307. f.read
  308. f.sync = false
  309. assert_equal(false, f.sync)
  310. end
  311. end
  312. def test_pos
  313. t = Tempfile.new("test_zlib_gzip_file_pos")
  314. t.close
  315. Zlib::GzipWriter.open(t.path) do |gz|
  316. gz.print("foo")
  317. gz.flush
  318. assert_equal(3, gz.tell)
  319. end
  320. end
  321. def test_path
  322. t = Tempfile.new("test_zlib_gzip_file_path")
  323. t.close
  324. gz = Zlib::GzipWriter.open(t.path)
  325. gz.print("foo")
  326. assert_equal(t.path, gz.path)
  327. gz.close
  328. assert_equal(t.path, gz.path)
  329. Zlib::GzipReader.open(t.path) do |f|
  330. assert_equal(t.path, f.path)
  331. f.close
  332. assert_equal(t.path, f.path)
  333. end
  334. s = ""
  335. sio = StringIO.new(s)
  336. gz = Zlib::GzipWriter.new(sio)
  337. gz.print("foo")
  338. assert_raise(NoMethodError) { gz.path }
  339. gz.close
  340. sio = StringIO.new(s)
  341. Zlib::GzipReader.new(sio) do |f|
  342. assert_raise(NoMethodError) { f.path }
  343. end
  344. end
  345. end
  346. class TestZlibGzipReader < Test::Unit::TestCase
  347. D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
  348. def test_read0
  349. assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
  350. end
  351. def test_ungetc
  352. s = ""
  353. w = Zlib::GzipWriter.new(StringIO.new(s))
  354. w << (1...1000).to_a.inspect
  355. w.close
  356. r = Zlib::GzipReader.new(StringIO.new(s))
  357. r.read(100)
  358. r.ungetc ?a
  359. assert_nothing_raised("[ruby-dev:24060]") {
  360. r.read(100)
  361. r.read
  362. r.close
  363. }
  364. end
  365. def test_ungetc_paragraph
  366. s = ""
  367. w = Zlib::GzipWriter.new(StringIO.new(s))
  368. w << "abc"
  369. w.close
  370. r = Zlib::GzipReader.new(StringIO.new(s))
  371. r.ungetc ?\n
  372. assert_equal("abc", r.gets(""))
  373. assert_nothing_raised("[ruby-dev:24065]") {
  374. r.read
  375. r.close
  376. }
  377. end
  378. def test_open
  379. t = Tempfile.new("test_zlib_gzip_reader_open")
  380. t.close
  381. e = assert_raise(Zlib::GzipFile::Error) {
  382. Zlib::GzipReader.open(t.path)
  383. }
  384. assert_equal("not in gzip format", e.message)
  385. assert_nil(e.input)
  386. open(t.path, "wb") {|f| f.write("foo")}
  387. e = assert_raise(Zlib::GzipFile::Error) {
  388. Zlib::GzipReader.open(t.path)
  389. }
  390. assert_equal("not in gzip format", e.message)
  391. assert_equal("foo", e.input)
  392. open(t.path, "wb") {|f| f.write("foobarzothoge")}
  393. e = assert_raise(Zlib::GzipFile::Error) {
  394. Zlib::GzipReader.open(t.path)
  395. }
  396. assert_equal("not in gzip format", e.message)
  397. assert_equal("foobarzothoge", e.input)
  398. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  399. assert_raise(ArgumentError) { Zlib::GzipReader.open }
  400. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  401. f = Zlib::GzipReader.open(t.path)
  402. begin
  403. assert_equal("foo", f.read)
  404. ensure
  405. f.close
  406. end
  407. end
  408. def test_rewind
  409. t = Tempfile.new("test_zlib_gzip_reader_rewind")
  410. t.close
  411. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  412. Zlib::GzipReader.open(t.path) do |f|
  413. assert_equal("foo", f.read)
  414. f.rewind
  415. assert_equal("foo", f.read)
  416. end
  417. end
  418. def test_unused
  419. t = Tempfile.new("test_zlib_gzip_reader_unused")
  420. t.close
  421. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  422. Zlib::GzipReader.open(t.path) do |f|
  423. assert_equal("foo", f.read(3))
  424. f.unused
  425. assert_equal("bar", f.read)
  426. f.unused
  427. end
  428. end
  429. def test_read
  430. t = Tempfile.new("test_zlib_gzip_reader_read")
  431. t.close
  432. str = "\u3042\u3044\u3046"
  433. Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
  434. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  435. assert_raise(ArgumentError) { f.read(-1) }
  436. assert_equal(str, f.read)
  437. end
  438. end
  439. def test_readpartial
  440. t = Tempfile.new("test_zlib_gzip_reader_readpartial")
  441. t.close
  442. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  443. Zlib::GzipReader.open(t.path) do |f|
  444. assert("foo".start_with?(f.readpartial(3)))
  445. end
  446. Zlib::GzipReader.open(t.path) do |f|
  447. s = ""
  448. f.readpartial(3, s)
  449. assert("foo".start_with?(s))
  450. assert_raise(ArgumentError) { f.readpartial(-1) }
  451. end
  452. end
  453. def test_getc
  454. t = Tempfile.new("test_zlib_gzip_reader_getc")
  455. t.close
  456. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  457. Zlib::GzipReader.open(t.path) do |f|
  458. "foobar".each_char {|c| assert_equal(c, f.getc) }
  459. assert_nil(f.getc)
  460. end
  461. end
  462. def test_getbyte
  463. t = Tempfile.new("test_zlib_gzip_reader_getbyte")
  464. t.close
  465. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  466. Zlib::GzipReader.open(t.path) do |f|
  467. "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
  468. assert_nil(f.getbyte)
  469. end
  470. end
  471. def test_readchar
  472. t = Tempfile.new("test_zlib_gzip_reader_readchar")
  473. t.close
  474. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  475. Zlib::GzipReader.open(t.path) do |f|
  476. "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
  477. assert_raise(EOFError) { f.readchar }
  478. end
  479. end
  480. def test_each_byte
  481. t = Tempfile.new("test_zlib_gzip_reader_each_byte")
  482. t.close
  483. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  484. Zlib::GzipReader.open(t.path) do |f|
  485. a = []
  486. f.each_byte {|c| a << c }
  487. assert_equal("foobar".each_byte.to_a, a)
  488. end
  489. end
  490. def test_gets
  491. t = Tempfile.new("test_zlib_gzip_reader_gets")
  492. t.close
  493. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  494. Zlib::GzipReader.open(t.path) do |f|
  495. assert_equal("foo\n", f.gets)
  496. assert_equal("bar\n", f.gets)
  497. assert_equal("baz\n", f.gets)
  498. assert_nil(f.gets)
  499. end
  500. Zlib::GzipReader.open(t.path) do |f|
  501. assert_equal("foo\nbar\nbaz\n", f.gets(nil))
  502. end
  503. Zlib::GzipReader.open(t.path) do |f|
  504. assert_equal("foo\n", f.gets(10))
  505. assert_equal("ba", f.gets(2))
  506. assert_equal("r\nb", f.gets(nil, 3))
  507. assert_equal("az\n", f.gets(nil, 10))
  508. assert_nil(f.gets)
  509. end
  510. end
  511. def test_gets2
  512. t = Tempfile.new("test_zlib_gzip_reader_gets2")
  513. t.close
  514. ustrs = %W"\u{3042 3044 3046}\n \u{304b 304d 304f}\n \u{3055 3057 3059}\n"
  515. Zlib::GzipWriter.open(t.path) {|gz| gz.print(*ustrs) }
  516. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  517. assert_equal(ustrs[0], f.gets)
  518. assert_equal(ustrs[1], f.gets)
  519. assert_equal(ustrs[2], f.gets)
  520. assert_nil(f.gets)
  521. end
  522. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  523. assert_equal(ustrs.join(''), f.gets(nil))
  524. end
  525. Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
  526. assert_equal(ustrs[0], f.gets(20))
  527. assert_equal(ustrs[1][0,2], f.gets(5))
  528. assert_equal(ustrs[1][2..-1]+ustrs[2][0,1], f.gets(nil, 5))
  529. assert_equal(ustrs[2][1..-1], f.gets(nil, 20))
  530. assert_nil(f.gets)
  531. end
  532. end
  533. def test_readline
  534. t = Tempfile.new("test_zlib_gzip_reader_readline")
  535. t.close
  536. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  537. Zlib::GzipReader.open(t.path) do |f|
  538. assert_equal("foo\n", f.readline)
  539. assert_equal("bar\n", f.readline)
  540. assert_equal("baz\n", f.readline)
  541. assert_raise(EOFError) { f.readline }
  542. end
  543. end
  544. def test_each
  545. t = Tempfile.new("test_zlib_gzip_reader_each")
  546. t.close
  547. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  548. Zlib::GzipReader.open(t.path) do |f|
  549. a = ["foo\n", "bar\n", "baz\n"]
  550. f.each {|l| assert_equal(a.shift, l) }
  551. end
  552. end
  553. def test_readlines
  554. t = Tempfile.new("test_zlib_gzip_reader_readlines")
  555. t.close
  556. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  557. Zlib::GzipReader.open(t.path) do |f|
  558. assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
  559. end
  560. end
  561. def test_reader_wrap
  562. t = Tempfile.new("test_zlib_gzip_reader_wrap")
  563. t.close
  564. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  565. f = open(t.path)
  566. assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
  567. assert_raise(IOError) { f.close }
  568. end
  569. end
  570. class TestZlibGzipWriter < Test::Unit::TestCase
  571. def test_invalid_new
  572. assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
  573. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
  574. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
  575. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
  576. end
  577. def test_open
  578. assert_raise(ArgumentError) { Zlib::GzipWriter.open }
  579. t = Tempfile.new("test_zlib_gzip_writer_open")
  580. t.close
  581. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  582. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  583. f = Zlib::GzipWriter.open(t.path)
  584. begin
  585. f.print("bar")
  586. ensure
  587. f.close
  588. end
  589. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  590. assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
  591. end
  592. def test_write
  593. t = Tempfile.new("test_zlib_gzip_writer_write")
  594. t.close
  595. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  596. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  597. o = Object.new
  598. def o.to_s; "bar"; end
  599. Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
  600. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  601. end
  602. def test_putc
  603. t = Tempfile.new("test_zlib_gzip_writer_putc")
  604. t.close
  605. Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
  606. assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  607. # todo: multibyte char
  608. end
  609. def test_writer_wrap
  610. t = Tempfile.new("test_zlib_gzip_writer_wrap")
  611. Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
  612. t.close
  613. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  614. end
  615. end
  616. class TestZlib < Test::Unit::TestCase
  617. def test_version
  618. assert_instance_of(String, Zlib.zlib_version)
  619. assert(Zlib.zlib_version.tainted?)
  620. end
  621. def test_adler32
  622. assert_equal(0x00000001, Zlib.adler32)
  623. assert_equal(0x02820145, Zlib.adler32("foo"))
  624. assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
  625. assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
  626. end
  627. def test_adler32_combine
  628. one = Zlib.adler32("fo")
  629. two = Zlib.adler32("o")
  630. begin
  631. assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
  632. rescue NotImplementedError
  633. skip "adler32_combine is not implemented"
  634. end
  635. end
  636. def test_crc32
  637. assert_equal(0x00000000, Zlib.crc32)
  638. assert_equal(0x8c736521, Zlib.crc32("foo"))
  639. assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
  640. assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
  641. end
  642. def test_crc32_combine
  643. one = Zlib.crc32("fo")
  644. two = Zlib.crc32("o")
  645. begin
  646. assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
  647. rescue NotImplementedError
  648. skip "crc32_combine is not implemented"
  649. end
  650. end
  651. def test_crc_table
  652. t = Zlib.crc_table
  653. assert_instance_of(Array, t)
  654. t.each {|x| assert_kind_of(Integer, x) }
  655. end
  656. end
  657. end