tio.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. { Program to test OS-specific features of the system unit }
  2. { routines to test: }
  3. { do_open() }
  4. { do_read() }
  5. { do_write() }
  6. { do_close() }
  7. { do_filesize() }
  8. { do_seek() }
  9. { do_truncate() }
  10. { This routine overwrites/creates a filename called test.tmp }
  11. { fills it up with values, checks its file size, reads the }
  12. { data back in, }
  13. Program tio;
  14. {$I-}
  15. {$IFDEF TP}
  16. type
  17. shortstring = string;
  18. {$ENDIF}
  19. var
  20. F: File;
  21. procedure test(value, required: longint);
  22. begin
  23. if value <> required then
  24. begin
  25. writeln('Got ',value,' instead of ',required);
  26. halt(1);
  27. end;
  28. end;
  29. const
  30. FILE_NAME = 'test.tmp';
  31. FILE_NAME2 = 'test1.tmp';
  32. DATA_SIZE = 17;
  33. MODE_RESET = 0;
  34. MODE_REWRITE = 1;
  35. DATA: array[1..DATA_SIZE] of byte =
  36. ($01,$02,$03,$04,$05,$06,$07,$08,
  37. $09,$A,$B,$C,$D,$E,$F,$10,
  38. $11
  39. );
  40. procedure test_do_open(name : shortstring; mode: word);
  41. begin
  42. Write('opening file...');
  43. Assign(F,name);
  44. test(IOResult, 0);
  45. if mode = MODE_REWRITE then
  46. Rewrite(F,1)
  47. else
  48. Reset(F,1);
  49. test(IOResult, 0);
  50. WriteLn('Passed!');
  51. end;
  52. procedure test_do_write(var buf; BytesToWrite : longint);
  53. var
  54. BytesWritten : word;
  55. begin
  56. Write('writing to file...');
  57. BlockWrite(F,buf,BytesToWrite,BytesWritten);
  58. test(IOResult, 0);
  59. if BytesWritten<>DATA_SIZE then
  60. RunError(255);
  61. Writeln('Passed!');
  62. end;
  63. procedure test_do_filesize(size : longint);
  64. begin
  65. Write('getting filesize...');
  66. { verifying if correct filesize }
  67. test(FileSize(F),size);
  68. { verify if IOError }
  69. test(IOResult, 0);
  70. WriteLn('Passed!');
  71. end;
  72. procedure test_do_seek(_pos : longint);
  73. begin
  74. { Seek to beginning of file }
  75. Write('seek to beginning of file...');
  76. Seek(F, _pos);
  77. test(IOResult, 0);
  78. WriteLn('Passed!');
  79. end;
  80. procedure test_do_read(var buf; BytesToRead : word);
  81. var
  82. BytesRead : word;
  83. begin
  84. Write('reading from file...');
  85. BlockRead(F,buf,BytesToRead,BytesRead);
  86. test(BytesToRead, BytesRead);
  87. test(IOResult, 0);
  88. WriteLn('Passed!');
  89. end;
  90. procedure test_filepos(_pos : longint);
  91. var
  92. BytesRead : word;
  93. begin
  94. write('verifying file position...');
  95. test(FilePos(F),_pos);
  96. test(IOResult, 0);
  97. WriteLn('Passed!');
  98. end;
  99. procedure test_do_close;
  100. begin
  101. Write('closing file...');
  102. Close(F);
  103. test(IOResult, 0);
  104. WriteLn('Passed!');
  105. end;
  106. procedure test_rename(oldname, newname : shortstring);
  107. begin
  108. Assign(F,oldname);
  109. Write('renaming file...');
  110. ReName(F,newname);
  111. test(IOResult, 0);
  112. WriteLn('Passed!');
  113. end;
  114. procedure test_erase(name : shortstring);
  115. begin
  116. Assign(F,name);
  117. Write('erasing file...');
  118. Erase(F);
  119. test(IOResult, 0);
  120. WriteLn('Passed!');
  121. end;
  122. var
  123. I: Integer;
  124. readData : array[1..DATA_SIZE] of byte;
  125. Begin
  126. {------------------------ create and play with a new file --------------------------}
  127. FillChar(readData,DATA_SIZE,0);
  128. test_do_open(FILE_NAME, MODE_REWRITE);
  129. test_do_write(DATA, DATA_SIZE);
  130. test_do_filesize(DATA_SIZE);
  131. test_do_seek(0);
  132. test_do_read(readData, DATA_SIZE);
  133. for i:=1 to DATA_SIZE do
  134. Begin
  135. test(readData[i], data[i]);
  136. end;
  137. test_do_seek(5);
  138. test_filepos(5);
  139. (*
  140. test_do_truncate()
  141. WriteLn('truncating file...');
  142. Truncate(F);
  143. WriteLn(FileSize(F));
  144. if FileSize(F) <> 5 then
  145. RunError(255);
  146. *)
  147. test_do_close;
  148. {------------------------ create and play with an old file --------------------------}
  149. FillChar(readData,DATA_SIZE,0);
  150. test_do_open(FILE_NAME2, MODE_REWRITE);
  151. test_do_write(DATA, DATA_SIZE);
  152. test_do_close;
  153. FillChar(readData,DATA_SIZE,0);
  154. test_do_open(FILE_NAME2, MODE_RESET);
  155. test_do_write(DATA, DATA_SIZE);
  156. test_do_filesize(DATA_SIZE);
  157. test_do_seek(0);
  158. test_do_read(readData, DATA_SIZE);
  159. for i:=1 to DATA_SIZE do
  160. Begin
  161. test(readData[i], data[i]);
  162. end;
  163. test_do_close;
  164. test_rename(FILE_NAME2, 'test3.tmp');
  165. test_erase(FILE_NAME);
  166. end.