~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

Viewing changes to Lib/test/test_gzip.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

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