require File.expand_path('simple', File.dirname(__FILE__))

require 'zlib'
require 'stringio'

data_dir     = File.dirname(__FILE__) + '/test_data'
plain_text   = File.read(data_dir + '/zlib_test_file')
mri_deflated = File.read(data_dir + '/zlib_test_file.dflated')

# ####################################################################
#    Zlib::Deflate tests
# ####################################################################
deflated_text = Zlib::Deflate.deflate(plain_text)

# The expected_deflated was generated by MRI
expected_deflated = "x\234\355VMk\3430\020='\277b\310^R\232\230\366\352M\nei\241\247@\266\354\245\244AM\224\304\254+\aYNI\351\217\337\031\311\222\307\256\e\366\260e\331\305>\311\322\323\2337\037\222f\251\213\247\343\265\211A\250\331f\223K\003\251T[\263\303\211oY\241\f\354\vZ\374!\322B\366\001\0060G<\\k-\216\000\017~\323\250D/\246%\022\2410\227\373T\254$\230\235\204<Mp\224m \227\351f\004\271\021\332$j\v\302T\206\205Z\023\242\264\353d\214\210\350%1;\313\262\312\224\221\312\344\026f\rE\000w~\fI\016*I!\323\026,\237\367\346\b\202\224\216`-Si\310\001O\217\377\317\226j\243\263g\320r%\223\203\324\255\302\"\353\215Ls\264\300m\tU\306a\204\373\235\253\2015\354\215\206~x\356,\217/\317\210\016= \221\301p\3601P\004\037\221\177\253\263\027R\205\256\345;\235\250\237\370C,a\273@\327\245\\\3135|\265jCPQ\347\253\324\031<\211\\\256#\227\026Sh\225{\362h\200\e\336\\\202\276\223\367nx\203\311ps\311+\312\\\035\264\e8GI\241\037\333y%_\346\036\223\357)\024o\244\203\261\306S\233{XR\301\335gw\212\327\\\024\260D\320\nui\v@\322G\270\312\3009c X\320\354\351r\267\202K\303\n9\201\2133L\353\275.d\f\017\360\350\r-\245\326\231\216\341\21367Z_\353\355\2540\263\315\\\250\255\204\205\345\0372\333u\026\212r\323u\2563(\263\3718ADr|\210N\nB\236\205s\215'\250\021G[\254q\310;\242\021\317\023\207x\256\345*\b\345\222\030`\201\323\267\002\317\005\315\a\247\312\360\360\352@b\256\353]\"\234\271i\233\275~/\344.\256\301\317\271\2053\244\353\r\271\311\351\324E\322\353\353\367\240\3075\254\262\375\361\026\217~\f\227`\262\270&7Q4\343\354\226\367\001\335\220u\353\356\030{o\361{\344>V\t\251\251\272\252j\264Y/\215D\360\337\232\2530n\3249\317\202\253'\216\236|l\361\244\3151?$\343Z\254\355fVrC\276\257Q\300\r\v\027\376\354\360\371\017\352\254J;\a/\252\243GW\300\244\265fH\340`\246\322\243M\263{\200\n}H\016t\207\032Ao\204\273~3\235l\023%R\377L$vZ\333\353=SrP\306\311j\251*\2462Oe`\313g\310d\374N\365\360\024Y\222\250\017m\261i\365\357Dl\336\027\334\273\213\351\317\237\002\247\273^\377\315o\331\365\031]\237\321\365\031]\237\321\365\031]\237\321\365\031]\237\361I}F\327pt\r\307g5\034]\257\361\017\364\032\177\273\323\370\277\237\341\356\025\356^\341\226W\370\027\235\247\344\027"

test(deflated_text, expected_deflated, "Zlib::Deflate.deflate")

# Test a round trip:
rt_plain_text = "hello there good sir"
rt_deflated_text = Zlib::Deflate.deflate(rt_plain_text)
rt_inflated_text = Zlib::Inflate.inflate(rt_deflated_text)
test(rt_plain_text, rt_inflated_text, "Zlib::Deflate/Zlib::Inflate round trip")

# ####################################################################
#    Zlib::Inflate tests
# ####################################################################
test(Zlib::Inflate.inflate(mri_deflated), plain_text, 'Zlib::Inflate.inflate')

# ####################################################################
#    Zlib::Gzip* tests
# ####################################################################
# Write a gzip file of data
def zip(data, file)
  f = File.open(file, 'w+')
  gz_writer = Zlib::GzipWriter.new(f)
  gz_writer.orig_name=('test.i')
  gz_writer.write(data)
  gz_writer.close
  # MRI GzipWriter closes f TOOD: test f is closed...
end

# Read a gzip file and return the contents
def unzip(file)
  new_data = ''
  f = File.open(file, 'r')
  gz_reader = Zlib::GzipReader.new(f)
  new_data = gz_reader.read
  gz_reader.close
  new_data
  # MRI GzipReader closes f TOOD: test f is closed...
end

# A simple round-trip test through the file system
original = "Some test data."
filename = 'test.gz'

zip(original, filename)
copy = unzip(filename)

test(copy, original, 'Round trip')

# Test that MagLev can read a gzip file generated by MRI
r = unzip(data_dir + '/test-mri.gz')
test(r, original, "Unzip of MRI file")

File.delete(filename)

# ####################################################################
#    Other Tests
# ####################################################################

# Test that zlib will accept "IO" objects that are not derived from IO.
# RubyGems passes in a wrapper object.  This is important since
# CZstream>>_readAndDecompress used to call IO>>read:into:, but a non IO
# derived object won't have that.  This test ensures we go through the
# @ruby:read path.  Ditto the write path and IO>>write:from:.
class IODelegate
  def initialize(io)
    @real_io = io
  end
  def read(*args)
    @real_io.read(*args)
  end
  def write(*args)
    @real_io.write(*args)
  end
  def flush
    @real_io.flush
  end
  def close
    @real_io.close
  end
end

# Compress the data
sio = StringIO.new('', "r+")
dio = IODelegate.new(sio)
gz_writer = Zlib::GzipWriter.new(dio)

gz_writer.write(original)
gz_writer.close

# Uncompress the data
sio2 = StringIO.new(sio.string, "r")
dio2 = IODelegate.new(sio2)
gz_reader = Zlib::GzipReader.new(dio2)

new_data = gz_reader.read
gz_reader.close
test(new_data, original, 'Delegate')


# Bug test.  Writing data of specific lengths was broken.  The length
# varied depending on how much data was in the buffer already and what the
# half size of the buffer was.  So, we loop over a good number of sizes to
# trigger the buggy behavior (seems that when i is 65, it triggers).


# This test passes if no exception is raised.
sio = StringIO.new('', "r+")
dio = IODelegate.new(sio)
gz_writer = Zlib::GzipWriter.new(dio)

1.upto(2049) do |i|
  gz_writer.write("x"*i)
end
gz_writer.close

######## coverage for Trac781
fname = data_dir + '/Trac781.gz'
fio = File.new(fname, "w")
gz_wr = Zlib::GzipWriter.new(fio)
gz_wr.write('abcdef')
gz_wr.flush
gz_wr.write("xxx0123456789\n")
gz_wr.flush
gz_wr.close
fio = File.new(fname, "r")
gz_rd = Zlib::GzipReader.new(fio)
str = gz_rd.read
test(str, "abcdefxxx0123456789\n" , "Trac 781")

#####################
report
true
