PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/zlib/test_zlib.rb

https://github.com/diabolo/ruby
Ruby | 716 lines | 602 code | 111 blank | 3 comment | 1 complexity | 8a2f88eec7b06a53bfc224795daa528b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  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")
  204. t.close
  205. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  206. f = Zlib::GzipReader.open(t.path)
  207. assert_kind_of(IO, f.to_io)
  208. end
  209. def test_crc
  210. t = Tempfile.new("test_zlib_gzip_file")
  211. t.close
  212. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  213. f = Zlib::GzipReader.open(t.path)
  214. f.read
  215. assert_equal(0x8c736521, f.crc)
  216. end
  217. def test_mtime
  218. tim = Time.now
  219. t = Tempfile.new("test_zlib_gzip_file")
  220. t.close
  221. Zlib::GzipWriter.open(t.path) do |gz|
  222. gz.mtime = -1
  223. gz.mtime = tim
  224. gz.print("foo")
  225. gz.flush
  226. assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
  227. end
  228. f = Zlib::GzipReader.open(t.path)
  229. assert_equal(tim.to_i, f.mtime.to_i)
  230. end
  231. def test_level
  232. t = Tempfile.new("test_zlib_gzip_file")
  233. t.close
  234. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  235. f = Zlib::GzipReader.open(t.path)
  236. assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
  237. end
  238. def test_os_code
  239. t = Tempfile.new("test_zlib_gzip_file")
  240. t.close
  241. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  242. f = Zlib::GzipReader.open(t.path)
  243. assert_equal(Zlib::OS_CODE, f.os_code)
  244. end
  245. def test_orig_name
  246. t = Tempfile.new("test_zlib_gzip_file")
  247. t.close
  248. Zlib::GzipWriter.open(t.path) do |gz|
  249. gz.orig_name = "foobarbazqux\0quux"
  250. gz.print("foo")
  251. gz.flush
  252. assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
  253. end
  254. f = Zlib::GzipReader.open(t.path)
  255. assert_equal("foobarbazqux", f.orig_name)
  256. end
  257. def test_comment
  258. t = Tempfile.new("test_zlib_gzip_file")
  259. t.close
  260. Zlib::GzipWriter.open(t.path) do |gz|
  261. gz.comment = "foobarbazqux\0quux"
  262. gz.print("foo")
  263. gz.flush
  264. assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
  265. end
  266. f = Zlib::GzipReader.open(t.path)
  267. assert_equal("foobarbazqux", f.comment)
  268. end
  269. def test_lineno
  270. t = Tempfile.new("test_zlib_gzip_file")
  271. t.close
  272. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
  273. f = Zlib::GzipReader.open(t.path)
  274. assert_equal([0, "foo\n"], [f.lineno, f.gets])
  275. assert_equal([1, "bar\n"], [f.lineno, f.gets])
  276. f.lineno = 1000
  277. assert_equal([1000, "baz\n"], [f.lineno, f.gets])
  278. assert_equal([1001, "qux\n"], [f.lineno, f.gets])
  279. end
  280. def test_closed_p
  281. t = Tempfile.new("test_zlib_gzip_file")
  282. t.close
  283. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  284. f = Zlib::GzipReader.open(t.path)
  285. assert_equal(false, f.closed?)
  286. f.read
  287. assert_equal(false, f.closed?)
  288. f.close
  289. assert_equal(true, f.closed?)
  290. end
  291. def test_sync
  292. t = Tempfile.new("test_zlib_gzip_file")
  293. t.close
  294. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  295. f = Zlib::GzipReader.open(t.path)
  296. f.sync = true
  297. assert_equal(true, f.sync)
  298. f.read
  299. f.sync = false
  300. assert_equal(false, f.sync)
  301. f.close
  302. end
  303. def test_pos
  304. t = Tempfile.new("test_zlib_gzip_file")
  305. t.close
  306. Zlib::GzipWriter.open(t.path) do |gz|
  307. gz.print("foo")
  308. gz.flush
  309. assert_equal(3, gz.tell)
  310. end
  311. end
  312. def test_path
  313. t = Tempfile.new("test_zlib_gzip_file")
  314. t.close
  315. gz = Zlib::GzipWriter.open(t.path)
  316. gz.print("foo")
  317. assert_equal(t.path, gz.path)
  318. gz.close
  319. assert_equal(t.path, gz.path)
  320. f = Zlib::GzipReader.open(t.path)
  321. assert_equal(t.path, f.path)
  322. f.close
  323. assert_equal(t.path, f.path)
  324. s = ""
  325. sio = StringIO.new(s)
  326. gz = Zlib::GzipWriter.new(sio)
  327. gz.print("foo")
  328. assert_raise(NoMethodError) { gz.path }
  329. gz.close
  330. sio = StringIO.new(s)
  331. f = Zlib::GzipReader.new(sio)
  332. assert_raise(NoMethodError) { f.path }
  333. f.close
  334. end
  335. end
  336. class TestZlibGzipReader < Test::Unit::TestCase
  337. D0 = "\037\213\010\000S`\017A\000\003\003\000\000\000\000\000\000\000\000\000"
  338. def test_read0
  339. assert_equal("", Zlib::GzipReader.new(StringIO.new(D0)).read(0))
  340. end
  341. def test_ungetc
  342. s = ""
  343. w = Zlib::GzipWriter.new(StringIO.new(s))
  344. w << (1...1000).to_a.inspect
  345. w.close
  346. r = Zlib::GzipReader.new(StringIO.new(s))
  347. r.read(100)
  348. r.ungetc ?a
  349. assert_nothing_raised("[ruby-dev:24060]") {
  350. r.read(100)
  351. r.read
  352. r.close
  353. }
  354. end
  355. def test_ungetc_paragraph
  356. s = ""
  357. w = Zlib::GzipWriter.new(StringIO.new(s))
  358. w << "abc"
  359. w.close
  360. r = Zlib::GzipReader.new(StringIO.new(s))
  361. r.ungetc ?\n
  362. assert_equal("abc", r.gets(""))
  363. assert_nothing_raised("[ruby-dev:24065]") {
  364. r.read
  365. r.close
  366. }
  367. end
  368. def test_open
  369. t = Tempfile.new("test_zlib_gzip_reader")
  370. t.close
  371. e = assert_raise(Zlib::GzipFile::Error) {
  372. Zlib::GzipReader.open(t.path)
  373. }
  374. assert_equal("not in gzip format", e.message)
  375. assert_nil(e.input)
  376. open(t.path, "wb") {|f| f.write("foo")}
  377. e = assert_raise(Zlib::GzipFile::Error) {
  378. Zlib::GzipReader.open(t.path)
  379. }
  380. assert_equal("not in gzip format", e.message)
  381. assert_equal("foo", e.input)
  382. open(t.path, "wb") {|f| f.write("foobarzothoge")}
  383. e = assert_raise(Zlib::GzipFile::Error) {
  384. Zlib::GzipReader.open(t.path)
  385. }
  386. assert_equal("not in gzip format", e.message)
  387. assert_equal("foobarzothoge", e.input)
  388. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  389. assert_raise(ArgumentError) { Zlib::GzipReader.open }
  390. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  391. f = Zlib::GzipReader.open(t.path)
  392. assert_equal("foo", f.read)
  393. f.close
  394. end
  395. def test_rewind
  396. t = Tempfile.new("test_zlib_gzip_reader")
  397. t.close
  398. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  399. f = Zlib::GzipReader.open(t.path)
  400. assert_equal("foo", f.read)
  401. f.rewind
  402. assert_equal("foo", f.read)
  403. f.close
  404. end
  405. def test_unused
  406. t = Tempfile.new("test_zlib_gzip_reader")
  407. t.close
  408. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  409. f = Zlib::GzipReader.open(t.path)
  410. assert_equal("foo", f.read(3))
  411. f.unused
  412. assert_equal("bar", f.read)
  413. f.unused
  414. f.close
  415. end
  416. def test_read
  417. t = Tempfile.new("test_zlib_gzip_reader")
  418. t.close
  419. str = "\u3042\u3044\u3046"
  420. Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
  421. f = Zlib::GzipReader.open(t.path, encoding: "UTF-8")
  422. assert_raise(ArgumentError) { f.read(-1) }
  423. assert_equal(str, f.read)
  424. end
  425. def test_readpartial
  426. t = Tempfile.new("test_zlib_gzip_reader")
  427. t.close
  428. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  429. f = Zlib::GzipReader.open(t.path)
  430. assert("foo".start_with?(f.readpartial(3)))
  431. f = Zlib::GzipReader.open(t.path)
  432. s = ""
  433. f.readpartial(3, s)
  434. assert("foo".start_with?(s))
  435. assert_raise(ArgumentError) { f.readpartial(-1) }
  436. end
  437. def test_getc
  438. t = Tempfile.new("test_zlib_gzip_reader")
  439. t.close
  440. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  441. f = Zlib::GzipReader.open(t.path)
  442. "foobar".each_char {|c| assert_equal(c, f.getc) }
  443. assert_nil(f.getc)
  444. end
  445. def test_getbyte
  446. t = Tempfile.new("test_zlib_gzip_reader")
  447. t.close
  448. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  449. f = Zlib::GzipReader.open(t.path)
  450. "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
  451. assert_nil(f.getbyte)
  452. end
  453. def test_readchar
  454. t = Tempfile.new("test_zlib_gzip_reader")
  455. t.close
  456. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  457. f = Zlib::GzipReader.open(t.path)
  458. "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
  459. assert_raise(EOFError) { f.readchar }
  460. end
  461. def test_each_byte
  462. t = Tempfile.new("test_zlib_gzip_reader")
  463. t.close
  464. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
  465. f = Zlib::GzipReader.open(t.path)
  466. a = []
  467. f.each_byte {|c| a << c }
  468. assert_equal("foobar".each_byte.to_a, a)
  469. end
  470. def test_gets2
  471. t = Tempfile.new("test_zlib_gzip_reader")
  472. t.close
  473. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  474. f = Zlib::GzipReader.open(t.path)
  475. assert_equal("foo\n", f.gets)
  476. assert_equal("bar\n", f.gets)
  477. assert_equal("baz\n", f.gets)
  478. assert_nil(f.gets)
  479. f.close
  480. f = Zlib::GzipReader.open(t.path)
  481. assert_equal("foo\nbar\nbaz\n", f.gets(nil))
  482. f.close
  483. end
  484. def test_gets
  485. t = Tempfile.new("test_zlib_gzip_reader")
  486. t.close
  487. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  488. f = Zlib::GzipReader.open(t.path)
  489. assert_equal("foo\n", f.readline)
  490. assert_equal("bar\n", f.readline)
  491. assert_equal("baz\n", f.readline)
  492. assert_raise(EOFError) { f.readline }
  493. f.close
  494. end
  495. def test_each
  496. t = Tempfile.new("test_zlib_gzip_reader")
  497. t.close
  498. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  499. f = Zlib::GzipReader.open(t.path)
  500. a = ["foo\n", "bar\n", "baz\n"]
  501. f.each {|l| assert_equal(a.shift, l) }
  502. f.close
  503. end
  504. def test_readlines
  505. t = Tempfile.new("test_zlib_gzip_reader")
  506. t.close
  507. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
  508. f = Zlib::GzipReader.open(t.path)
  509. assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
  510. f.close
  511. end
  512. def test_reader_wrap
  513. t = Tempfile.new("test_zlib_gzip_reader")
  514. t.close
  515. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  516. f = open(t.path)
  517. assert_equal("foo", Zlib::GzipReader.wrap(f) {|gz| gz.read })
  518. assert_raise(IOError) { f.close }
  519. end
  520. end
  521. class TestZlibGzipWriter < Test::Unit::TestCase
  522. def test_invalid_new
  523. assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close }
  524. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }
  525. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(0).close }
  526. assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(:hoge).close }
  527. end
  528. def test_open
  529. assert_raise(ArgumentError) { Zlib::GzipWriter.open }
  530. t = Tempfile.new("test_zlib_gzip_writer")
  531. t.close
  532. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  533. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  534. f = Zlib::GzipWriter.open(t.path)
  535. f.print("bar")
  536. f.close
  537. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  538. assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
  539. end
  540. def test_write
  541. t = Tempfile.new("test_zlib_gzip_writer")
  542. t.close
  543. Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
  544. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  545. o = Object.new
  546. def o.to_s; "bar"; end
  547. Zlib::GzipWriter.open(t.path) {|gz| gz.print(o) }
  548. assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  549. end
  550. def test_putc
  551. t = Tempfile.new("test_zlib_gzip_writer")
  552. t.close
  553. Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
  554. assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  555. # todo: multibyte char
  556. end
  557. def test_writer_wrap
  558. t = Tempfile.new("test_zlib_gzip_writer")
  559. Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
  560. t.close
  561. assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
  562. end
  563. end
  564. class TestZlib < Test::Unit::TestCase
  565. def test_version
  566. assert_instance_of(String, Zlib.zlib_version)
  567. assert(Zlib.zlib_version.tainted?)
  568. end
  569. def test_adler32
  570. assert_equal(0x00000001, Zlib.adler32)
  571. assert_equal(0x02820145, Zlib.adler32("foo"))
  572. assert_equal(0x02820145, Zlib.adler32("o", Zlib.adler32("fo")))
  573. assert_equal(0x8a62c964, Zlib.adler32("abc\x01\x02\x03" * 10000))
  574. end
  575. def test_adler32_combine
  576. one = Zlib.adler32("fo")
  577. two = Zlib.adler32("o")
  578. begin
  579. assert_equal(0x02820145, Zlib.adler32_combine(one, two, 1))
  580. rescue NotImplementedError
  581. skip "adler32_combine is not implemented"
  582. end
  583. end
  584. def test_crc32
  585. assert_equal(0x00000000, Zlib.crc32)
  586. assert_equal(0x8c736521, Zlib.crc32("foo"))
  587. assert_equal(0x8c736521, Zlib.crc32("o", Zlib.crc32("fo")))
  588. assert_equal(0x07f0d68f, Zlib.crc32("abc\x01\x02\x03" * 10000))
  589. end
  590. def test_crc32_combine
  591. one = Zlib.crc32("fo")
  592. two = Zlib.crc32("o")
  593. begin
  594. assert_equal(0x8c736521, Zlib.crc32_combine(one, two, 1))
  595. rescue NotImplementedError
  596. skip "crc32_combine is not implemented"
  597. end
  598. end
  599. def test_crc_table
  600. t = Zlib.crc_table
  601. assert_instance_of(Array, t)
  602. t.each {|x| assert_kind_of(Integer, x) }
  603. end
  604. end
  605. end