2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class DeflaterTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
include CrcTest
|
|
|
|
|
|
|
|
def test_outputOperator
|
|
|
|
txt = load_file("test/data/file2.txt")
|
|
|
|
deflate(txt, "deflatertest.bin")
|
|
|
|
inflatedTxt = inflate("deflatertest.bin")
|
|
|
|
assert_equal(txt, inflatedTxt)
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:35:20 +08:00
|
|
|
def test_default_compression
|
|
|
|
txt = load_file("test/data/file2.txt")
|
|
|
|
|
|
|
|
Zip.default_compression = ::Zlib::BEST_COMPRESSION
|
|
|
|
deflate(txt, "compressiontest_best_compression.bin")
|
|
|
|
Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION
|
|
|
|
deflate(txt, "compressiontest_default_compression.bin")
|
|
|
|
Zip.default_compression = ::Zlib::NO_COMPRESSION
|
|
|
|
deflate(txt, "compressiontest_no_compression.bin")
|
|
|
|
|
|
|
|
best = File.size("compressiontest_best_compression.bin")
|
|
|
|
default = File.size("compressiontest_default_compression.bin")
|
|
|
|
no = File.size("compressiontest_no_compression.bin")
|
|
|
|
|
|
|
|
assert(best < default)
|
|
|
|
assert(best < no)
|
|
|
|
assert(default < no)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
private
|
|
|
|
def load_file(fileName)
|
|
|
|
txt = nil
|
|
|
|
File.open(fileName, "rb") { |f| txt = f.read }
|
|
|
|
end
|
|
|
|
|
|
|
|
def deflate(data, fileName)
|
|
|
|
File.open(fileName, "wb") {
|
|
|
|
|file|
|
|
|
|
deflater = ::Zip::Deflater.new(file)
|
|
|
|
deflater << data
|
|
|
|
deflater.finish
|
|
|
|
assert_equal(deflater.size, data.size)
|
|
|
|
file << "trailing data for zlib with -MAX_WBITS"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def inflate(fileName)
|
|
|
|
txt = nil
|
|
|
|
File.open(fileName, "rb") {
|
|
|
|
|file|
|
|
|
|
inflater = ::Zip::Inflater.new(file)
|
|
|
|
txt = inflater.sysread
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_crc
|
|
|
|
run_crc_test(::Zip::Deflater)
|
|
|
|
end
|
|
|
|
end
|