Back to index

python3.2  3.2.2
Public Member Functions
test.test_fileinput.BufferSizesTests Class Reference

List of all members.

Public Member Functions

def test_buffer_sizes
def buffer_size_test

Detailed Description

Definition at line 33 of file test_fileinput.py.


Member Function Documentation

def test.test_fileinput.BufferSizesTests.buffer_size_test (   self,
  t1,
  t2,
  t3,
  t4,
  bs = 0,
  round = 0 
)

Definition at line 47 of file test_fileinput.py.

00047 
00048     def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
00049         pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
00050 
00051         start = 1 + round*6
00052         if verbose:
00053             print('%s. Simple iteration (bs=%s)' % (start+0, bs))
00054         fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
00055         lines = list(fi)
00056         fi.close()
00057         self.assertEqual(len(lines), 31)
00058         self.assertEqual(lines[4], 'Line 5 of file 1\n')
00059         self.assertEqual(lines[30], 'Line 1 of file 4\n')
00060         self.assertEqual(fi.lineno(), 31)
00061         self.assertEqual(fi.filename(), t4)
00062 
00063         if verbose:
00064             print('%s. Status variables (bs=%s)' % (start+1, bs))
00065         fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
00066         s = "x"
00067         while s and s != 'Line 6 of file 2\n':
00068             s = fi.readline()
00069         self.assertEqual(fi.filename(), t2)
00070         self.assertEqual(fi.lineno(), 21)
00071         self.assertEqual(fi.filelineno(), 6)
00072         self.assertFalse(fi.isfirstline())
00073         self.assertFalse(fi.isstdin())
00074 
00075         if verbose:
00076             print('%s. Nextfile (bs=%s)' % (start+2, bs))
00077         fi.nextfile()
00078         self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
00079         self.assertEqual(fi.lineno(), 22)
00080         fi.close()
00081 
00082         if verbose:
00083             print('%s. Stdin (bs=%s)' % (start+3, bs))
00084         fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
00085         savestdin = sys.stdin
00086         try:
00087             sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
00088             lines = list(fi)
00089             self.assertEqual(len(lines), 33)
00090             self.assertEqual(lines[32], 'Line 2 of stdin\n')
00091             self.assertEqual(fi.filename(), '<stdin>')
00092             fi.nextfile()
00093         finally:
00094             sys.stdin = savestdin
00095 
00096         if verbose:
00097             print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
00098         fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
00099         self.assertEqual(fi.lineno(), 0)
00100         self.assertEqual(fi.filename(), None)
00101         fi.nextfile()
00102         self.assertEqual(fi.lineno(), 0)
00103         self.assertEqual(fi.filename(), None)
00104 
00105         if verbose:
00106             print('%s. Inplace (bs=%s)' % (start+5, bs))
00107         savestdout = sys.stdout
00108         try:
00109             fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
00110             for line in fi:
00111                 line = line[:-1].upper()
00112                 print(line)
00113             fi.close()
00114         finally:
00115             sys.stdout = savestdout
00116 
00117         fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
00118         for line in fi:
00119             self.assertEqual(line[-1], '\n')
00120             m = pat.match(line[:-1])
00121             self.assertNotEqual(m, None)
00122             self.assertEqual(int(m.group(1)), fi.filelineno())
00123         fi.close()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 34 of file test_fileinput.py.

00034 
00035     def test_buffer_sizes(self):
00036         # First, run the tests with default and teeny buffer size.
00037         for round, bs in (0, 0), (1, 30):
00038             t1 = t2 = t3 = t4 = None
00039             try:
00040                 t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
00041                 t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
00042                 t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
00043                 t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
00044                 self.buffer_size_test(t1, t2, t3, t4, bs, round)
00045             finally:
00046                 remove_tempfiles(t1, t2, t3, t4)

Here is the call graph for this function:


The documentation for this class was generated from the following file: