Index: src/test_fileseek.py IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- src/test_fileseek.py (revision 3:70f964480bcf95982daf8f7d948201bdc1760a51) +++ src/test_fileseek.py (revision 3+:70f964480bcf+) @@ -10,19 +10,19 @@ # Read entire file f.read(size) # You can seek within the limit. Negative results will fail! - print '--Result of read--' - pos1 = f.tell() - print 'File position: ', pos1, '<-- read limit' + print '\n--Result of read--\n' + posread = f.tell() + print 'File position:\t\t\t%4d' % posread # Seek backwards seek_back = back - print 'Amount to seek back: ', -back + print 'Amount to seek back:\t%4d' % -back f.seek(-seek_back, 1) pos2 = f.tell() - print 'New position: ', pos2 - if pos2 == pos1 - seek_back: - print 'Correct position!' + print 'New position:\t\t\t%4d' % pos2 + if pos2 == posread - seek_back: + print 'Correct position!\n\n' else: - print 'Wrong position' + print 'Wrong position\n\n' f.close() @@ -32,18 +32,18 @@ # readline() produces incorrect file position! # Change to readlines() and it will pass! f.readline(size) - print '--- Result of readline() ---' - pos1 = f.tell() - print 'File position: ', pos1, '<-- includes NEWLINE' + print '--- Result of readline() ---\n' + posline = f.tell() + print 'File position:\t\t\t%4d' % posline seek_back = back - print 'Amount to seek back: ', -seek_back + print 'Amount to seek back:\t%4d' % -seek_back f.seek(-seek_back, 1) pos2 = f.tell() - print 'New position in file: ', pos2 - if pos2 == pos1 - seek_back: - print 'Correct position' + print 'New position in file:\t%4d' % pos2 + if pos2 == posline - seek_back: + print 'Correct position\n\n' else: - print 'Wrong position' + print 'Wrong position\n\n' f.close() @@ -53,22 +53,47 @@ f.read(size) posread = f.tell() f.readline() - print '--- Result of both() ---' - pos1 = f.tell() - print 'File position from read(): ', posread - print 'File position from readline(): ', pos1 + posline = f.tell() + print '--- Result of both() ---\n' + print 'File position from read():\t\t%4d' % posread + print 'File position from readline():\t%4d' % posline seek_back = back - print 'Amount to seek back: ', -seek_back + print 'Amount to seek back:\t\t\t%4d' % -seek_back f.seek(-seek_back, 1) pos2 = f.tell() - print 'New position in file: ', pos2 - if pos2 == pos1 - seek_back: - print 'Correct position' + print 'New position in file:\t\t\t%4d' % pos2 + if pos2 == posline - seek_back: + print 'Correct position\n\n' else: - print 'Wrong position' + print 'Wrong position\n\n' f.close() +def seekboth_reversed(file, size, back): + # Now for processing readline() + f = open(file, 'rb') + f.readline() + posline = f.tell() + f.read(size) + posread = f.tell() + print '--- Result of both_reversed() ---\n' + + print 'File position from readline():\t%4d' % posline + print 'File position from read():\t\t%4d' %posread + seek_back = back + print 'Amount to seek back:\t\t\t%4d' % -seek_back + f.seek(-seek_back, 1) + pos2 = f.tell() + print 'New position in file:\t\t\t%4d' % pos2 + if pos2 == posread - seek_back: + print 'Correct position\n\n' + else: + print 'Wrong position\n\n' + f.close() + + + + # generate file file = open("test.log", "wb") file.write("."*100 + "\n" + "."*4100 + "\n" + "."*100) @@ -77,7 +102,8 @@ # Run tests seekread('test.log', 1024, 200) # pass seekreadline('test.log', 1024, 100) # Fail -seekboth('test.log', 101, 100) # Pass +seekboth('test.log', 1024, 100) # Pass +seekboth_reversed('test.log', 1024, 100)