Changeset 6650
- Timestamp:
- Mar 27, 2009, 4:21:20 PM (16 years ago)
- Location:
- anuga_core/source/anuga/utilities
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/system_tools.py
r6646 r6650 448 448 fd.close() 449 449 450 450 ## 451 # @brief Function to return the length of a file. 452 # @param in_file Path to file to get length of. 453 # @return Number of lines in file. 454 # @note Doesn't count '\n' characters. 455 # @note Zero byte file, returns 0. 456 # @note No \n in file at all, but >0 chars, returns 1. 457 def file_length(in_file): 458 '''Function to return the length of a file.''' 459 460 fid = open(in_file) 461 data = fid.readlines() 462 fid.close() 463 return len(data) 464 465 -
anuga_core/source/anuga/utilities/test_system_tools.py
r6625 r6650 252 252 self.failUnless(expected_digest == digest, msg) 253 253 254 254 def test_file_length_function(self): 255 '''Test that file_length() give 'correct' answer.''' 256 257 # prepare test directory and filenames 258 tmp_dir = tempfile.mkdtemp() 259 test_file1 = os.path.join(tmp_dir, 'test.file1') 260 test_file2 = os.path.join(tmp_dir, 'test.file2') 261 test_file3 = os.path.join(tmp_dir, 'test.file3') 262 test_file4 = os.path.join(tmp_dir, 'test.file4') 263 264 # create files of known length 265 fd = open(test_file1, 'w') # 0 lines 266 fd.close 267 fd = open(test_file2, 'w') # 5 lines, all '\n' 268 for i in range(5): 269 fd.write('\n') 270 fd.close() 271 fd = open(test_file3, 'w') # 25 chars, no \n, 1 lines 272 fd.write('no newline at end of line') 273 fd.close() 274 fd = open(test_file4, 'w') # 1000 lines 275 for i in range(1000): 276 fd.write('The quick brown fox jumps over the lazy dog.\n') 277 fd.close() 278 279 # use file_length() to get and check lengths 280 size1 = file_length(test_file1) 281 msg = 'Expected file_length() to return 0, but got %d' % size1 282 self.failUnless(size1 == 0, msg) 283 size2 = file_length(test_file2) 284 msg = 'Expected file_length() to return 5, but got %d' % size2 285 self.failUnless(size2 == 5, msg) 286 size3 = file_length(test_file3) 287 msg = 'Expected file_length() to return 1, but got %d' % size3 288 self.failUnless(size3 == 1, msg) 289 size4 = file_length(test_file4) 290 msg = 'Expected file_length() to return 1000, but got %d' % size4 291 self.failUnless(size4 == 1000, msg) 292 293 255 294 #------------------------------------------------------------- 256 295 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.