test_gzip.py 4.32 KB
Newer Older
1 2 3 4 5 6
#! /usr/bin/env python
"""Test script for the gzip module.
"""

import unittest
from test import test_support
7
import sys, os
8
import gzip
9 10


11
data1 = b"""  int length=DEFAULTALLOC, err = Z_OK;
12 13 14 15 16 17
  PyObject *RetVal;
  int flushmode = Z_FINISH;
  unsigned long start_total_out;

"""

18
data2 = b"""/* zlibmodule.c -- gzip-compatible data compression */
19
/* See http://www.gzip.org/zlib/
20 21 22
/* See http://www.winimage.com/zLibDll for Windows */
"""

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

class TestGzip(unittest.TestCase):
    filename = test_support.TESTFN

    def setUp (self):
        pass

    def tearDown (self):
        try:
            os.unlink(self.filename)
        except os.error:
            pass


    def test_write (self):
        f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)

        # Try flush and fileno.
        f.flush()
        f.fileno()
        if hasattr(os, 'fsync'):
            os.fsync(f.fileno())
        f.close()

    def test_read(self):
        self.test_write()
        # Try reading.
        f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
        self.assertEqual(d, data1*50)

    def test_append(self):
        self.test_write()
        # Append to the previous file
        f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()

        f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
        self.assertEqual(d, (data1*50) + (data2*15))

61 62 63 64 65
    def test_many_append(self):
        # Bug #1074261 was triggered when reading a file that contained
        # many, many members.  Create such a file and verify that reading it
        # works.
        f = gzip.open(self.filename, 'wb', 9)
66
        f.write(b'a')
67
        f.close()
68
        for i in range(0, 200):
69
            f = gzip.open(self.filename, "ab", 9) # append
70
            f.write(b'a')
71 72 73 74
            f.close()

        # Try reading the file
        zgfile = gzip.open(self.filename, "rb")
75
        contents = b""
76 77 78 79 80
        while 1:
            ztxt = zgfile.read(8192)
            contents += ztxt
            if not ztxt: break
        zgfile.close()
81
        self.assertEquals(contents, b'a'*201)
82 83


84 85 86 87 88 89 90 91
    def test_readline(self):
        self.test_write()
        # Try .readline() with varying line lengths

        f = gzip.GzipFile(self.filename, 'rb')
        line_length = 0
        while 1:
            L = f.readline(line_length)
92
            if not L and line_length != 0: break
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
            self.assert_(len(L) <= line_length)
            line_length = (line_length + 1) % 50
        f.close()

    def test_readlines(self):
        self.test_write()
        # Try .readlines()

        f = gzip.GzipFile(self.filename, 'rb')
        L = f.readlines()
        f.close()

        f = gzip.GzipFile(self.filename, 'rb')
        while 1:
            L = f.readlines(150)
            if L == []: break
        f.close()

    def test_seek_read(self):
        self.test_write()
        # Try seek, read test

        f = gzip.GzipFile(self.filename)
        while 1:
            oldpos = f.tell()
            line1 = f.readline()
            if not line1: break
            newpos = f.tell()
            f.seek(oldpos)  # negative seek
            if len(line1)>10:
                amount = 10
            else:
                amount = len(line1)
            line2 = f.read(amount)
            self.assertEqual(line1[:amount], line2)
            f.seek(newpos)  # positive seek
        f.close()

131 132 133 134 135 136 137 138 139 140
    def test_seek_whence(self):
        self.test_write()
        # Try seek(whence=1), read test

        f = gzip.GzipFile(self.filename)
        f.read(10)
        f.seek(10, whence=1)
        y = f.read(10)
        f.close()
        self.assertEquals(y, data1[20:30])
141

142 143 144 145 146
    def test_seek_write(self):
        # Try seek, write test
        f = gzip.GzipFile(self.filename, 'w')
        for pos in range(0, 256, 16):
            f.seek(pos)
147
            f.write(b'GZ\n')
148 149 150 151 152 153 154 155
        f.close()

    def test_mode(self):
        self.test_write()
        f = gzip.GzipFile(self.filename, 'r')
        self.assertEqual(f.myfileobj.mode, 'rb')
        f.close()

156 157 158 159 160 161 162
    def test_1647484(self):
        for mode in ('wb', 'rb'):
            f = gzip.GzipFile(self.filename, mode)
            self.assert_(hasattr(f, "name"))
            self.assertEqual(f.name, self.filename)
            f.close()

163 164 165 166 167
def test_main(verbose=None):
    test_support.run_unittest(TestGzip)

if __name__ == "__main__":
    test_main(verbose=True)