/Lib/test/test_gzip.py

http://unladen-swallow.googlecode.com/ · Python · 167 lines · 119 code · 32 blank · 16 comment · 16 complexity · b3df8636548b0cf04bb55963e873207d MD5 · raw file

  1. #! /usr/bin/env python
  2. """Test script for the gzip module.
  3. """
  4. import unittest
  5. from test import test_support
  6. import os
  7. import gzip
  8. data1 = """ int length=DEFAULTALLOC, err = Z_OK;
  9. PyObject *RetVal;
  10. int flushmode = Z_FINISH;
  11. unsigned long start_total_out;
  12. """
  13. data2 = """/* zlibmodule.c -- gzip-compatible data compression */
  14. /* See http://www.gzip.org/zlib/
  15. /* See http://www.winimage.com/zLibDll for Windows */
  16. """
  17. class TestGzip(unittest.TestCase):
  18. filename = test_support.TESTFN
  19. def setUp(self):
  20. test_support.unlink(self.filename)
  21. def tearDown(self):
  22. test_support.unlink(self.filename)
  23. def test_write(self):
  24. f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
  25. # Try flush and fileno.
  26. f.flush()
  27. f.fileno()
  28. if hasattr(os, 'fsync'):
  29. os.fsync(f.fileno())
  30. f.close()
  31. # Test multiple close() calls.
  32. f.close()
  33. def test_read(self):
  34. self.test_write()
  35. # Try reading.
  36. f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
  37. self.assertEqual(d, data1*50)
  38. def test_append(self):
  39. self.test_write()
  40. # Append to the previous file
  41. f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
  42. f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
  43. self.assertEqual(d, (data1*50) + (data2*15))
  44. def test_many_append(self):
  45. # Bug #1074261 was triggered when reading a file that contained
  46. # many, many members. Create such a file and verify that reading it
  47. # works.
  48. f = gzip.open(self.filename, 'wb', 9)
  49. f.write('a')
  50. f.close()
  51. for i in range(0,200):
  52. f = gzip.open(self.filename, "ab", 9) # append
  53. f.write('a')
  54. f.close()
  55. # Try reading the file
  56. zgfile = gzip.open(self.filename, "rb")
  57. contents = ""
  58. while 1:
  59. ztxt = zgfile.read(8192)
  60. contents += ztxt
  61. if not ztxt: break
  62. zgfile.close()
  63. self.assertEquals(contents, 'a'*201)
  64. def test_readline(self):
  65. self.test_write()
  66. # Try .readline() with varying line lengths
  67. f = gzip.GzipFile(self.filename, 'rb')
  68. line_length = 0
  69. while 1:
  70. L = f.readline(line_length)
  71. if L == "" and line_length != 0: break
  72. self.assert_(len(L) <= line_length)
  73. line_length = (line_length + 1) % 50
  74. f.close()
  75. def test_readlines(self):
  76. self.test_write()
  77. # Try .readlines()
  78. f = gzip.GzipFile(self.filename, 'rb')
  79. L = f.readlines()
  80. f.close()
  81. f = gzip.GzipFile(self.filename, 'rb')
  82. while 1:
  83. L = f.readlines(150)
  84. if L == []: break
  85. f.close()
  86. def test_seek_read(self):
  87. self.test_write()
  88. # Try seek, read test
  89. f = gzip.GzipFile(self.filename)
  90. while 1:
  91. oldpos = f.tell()
  92. line1 = f.readline()
  93. if not line1: break
  94. newpos = f.tell()
  95. f.seek(oldpos) # negative seek
  96. if len(line1)>10:
  97. amount = 10
  98. else:
  99. amount = len(line1)
  100. line2 = f.read(amount)
  101. self.assertEqual(line1[:amount], line2)
  102. f.seek(newpos) # positive seek
  103. f.close()
  104. def test_seek_whence(self):
  105. self.test_write()
  106. # Try seek(whence=1), read test
  107. f = gzip.GzipFile(self.filename)
  108. f.read(10)
  109. f.seek(10, whence=1)
  110. y = f.read(10)
  111. f.close()
  112. self.assertEquals(y, data1[20:30])
  113. def test_seek_write(self):
  114. # Try seek, write test
  115. f = gzip.GzipFile(self.filename, 'w')
  116. for pos in range(0, 256, 16):
  117. f.seek(pos)
  118. f.write('GZ\n')
  119. f.close()
  120. def test_mode(self):
  121. self.test_write()
  122. f = gzip.GzipFile(self.filename, 'r')
  123. self.assertEqual(f.myfileobj.mode, 'rb')
  124. f.close()
  125. def test_1647484(self):
  126. for mode in ('wb', 'rb'):
  127. f = gzip.GzipFile(self.filename, mode)
  128. self.assert_(hasattr(f, "name"))
  129. self.assertEqual(f.name, self.filename)
  130. f.close()
  131. def test_main(verbose=None):
  132. test_support.run_unittest(TestGzip)
  133. if __name__ == "__main__":
  134. test_main(verbose=True)