summaryrefslogtreecommitdiff
path: root/test/zlib
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2020-10-29 12:28:09 -0700
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2020-12-04 19:12:32 +0900
commit0adc426ca5cf83e57f5e8433a2f7b55fb7a1fad6 (patch)
treef6655d3aacca1025c002fe9f4132b5572003a2ba /test/zlib
parentbc63ec57e7f965796479f68f6f687187c089bd40 (diff)
[ruby/zlib] Add Zlib::Inflate#inflate :buffer keyword argument
If a buffer keyword argument is given, it is used as the buffer, instead of creating new strings. This can result in significantly lower memory usage during inflation. Implements #19 https://github.com/ruby/zlib/commit/dac9a9b57d
Diffstat (limited to 'test/zlib')
-rw-r--r--test/zlib/test_zlib.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb
index c72fe76..addd427 100644
--- a/test/zlib/test_zlib.rb
+++ b/test/zlib/test_zlib.rb
@@ -363,6 +363,65 @@ if defined? Zlib
}
end
+ def test_inflate_buffer
+ s = Zlib::Deflate.deflate("foo")
+ z = Zlib::Inflate.new
+ buf = String.new
+ s = z.inflate(s, buffer: buf)
+ assert_same(buf, s)
+ buf = String.new
+ s << z.inflate(nil, buffer: buf)
+ assert_equal("foo", s)
+ z.inflate("foo", buffer: buf) # ???
+ z << "foo" # ???
+ end
+
+ def test_inflate_buffer_partial_input
+ deflated = Zlib::Deflate.deflate "\0"
+
+ z = Zlib::Inflate.new
+
+ inflated = "".dup
+
+ buf = String.new
+ deflated.each_char do |byte|
+ inflated << z.inflate(byte, buffer: buf)
+ end
+
+ inflated << z.finish
+
+ assert_equal "\0", inflated
+ end
+
+ def test_inflate_buffer_chunked
+ # s = Zlib::Deflate.deflate("0" * 100_000)
+ zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \
+ "\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" \
+ "\000\000\000\000\000\000\000\257\006\351\247BH"
+
+ chunks = []
+
+ z = Zlib::Inflate.new
+
+ buf = String.new
+ z.inflate(zeros, buffer: buf) do |chunk|
+ assert_same(buf, chunk)
+ chunks << chunk.dup
+ end
+
+ assert_equal [16384, 16384, 16384, 16384, 16384, 16384, 1696],
+ chunks.map { |chunk| chunk.size }
+
+ assert chunks.all? { |chunk|
+ chunk =~ /\A0+\z/
+ }
+ end
+
def test_inflate_chunked_break
# zeros = Zlib::Deflate.deflate("0" * 100_000)
zeros = "x\234\355\3011\001\000\000\000\302\240J\353\237\316\032\036@" \