summaryrefslogtreecommitdiff
path: root/tests/test/units/system/tio.pp
blob: e8d3a23640d47b65a432f69cc1acf6ce052bc97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{ Program to test OS-specific features of the system unit }
{ routines to test:                                       }
{   do_open()                                             }
{   do_read()                                             }
{   do_write()                                            }
{   do_close()                                            }
{   do_filesize()                                         }
{   do_seek()                                             }
{   do_truncate()                                         }

{ This routine overwrites/creates a filename called test.tmp }
{ fills it up with values, checks its file size, reads the   }
{ data back in,                                              }

Program tio;
{$I-}

{$IFDEF TP}
type
  shortstring = string;
{$ENDIF}


var
 F: File;


procedure test(value, required: longint);
begin
  if value <> required then
    begin
      writeln('Got ',value,' instead of ',required);
      halt(1);
    end;
end;


const
  FILE_NAME = 'test.tmp';
  FILE_NAME2 = 'test1.tmp';
  DATA_SIZE = 17;

  MODE_RESET = 0;
  MODE_REWRITE = 1;

  DATA: array[1..DATA_SIZE] of byte =
  ($01,$02,$03,$04,$05,$06,$07,$08,
   $09,$A,$B,$C,$D,$E,$F,$10,
   $11
  );


procedure test_do_open(name : shortstring; mode: word);
begin
  Write('opening file...');
  Assign(F,name);
  test(IOResult, 0);
  if mode = MODE_REWRITE then
    Rewrite(F,1)
  else
    Reset(F,1);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_do_write(var buf; BytesToWrite : longint);
var
  BytesWritten : word;
begin
  Write('writing to file...');
  BlockWrite(F,buf,BytesToWrite,BytesWritten);
  test(IOResult, 0);
  if BytesWritten<>DATA_SIZE then
    RunError(255);
  Writeln('Passed!');
end;

procedure test_do_filesize(size : longint);
begin
  Write('getting filesize...');
  { verifying if correct filesize }
  test(FileSize(F),size);
  { verify if IOError }
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_do_seek(_pos : longint);
begin
  { Seek to beginning of file }
  Write('seek to beginning of file...');
  Seek(F, _pos);
  test(IOResult, 0);
  WriteLn('Passed!');
end;


procedure test_do_read(var buf; BytesToRead : word);
var
 BytesRead : word;
begin
  Write('reading from file...');
  BlockRead(F,buf,BytesToRead,BytesRead);
  test(BytesToRead, BytesRead);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_filepos(_pos : longint);
var
 BytesRead : word;
begin
  write('verifying file position...');
  test(FilePos(F),_pos);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_do_close;
begin
  Write('closing file...');
  Close(F);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_already_closed_close;
begin
  Write('closing already closed file...(IOResult=103 expected) ');
  Close(F);
  test(IOResult, 103);
  WriteLn('Passed!');
end;

procedure test_not_yet_open_close(name : string);
begin
  Write('closing assigned only file...(IOResult=103 expected) ');
  Assign(F,name);
  test(IOResult,0);
  Close(F);
  test(IOResult, 103);
  WriteLn('Passed!');
end;


procedure test_rename(oldname, newname : shortstring);
begin
  Assign(F,oldname);
  Write('renaming file...');
  ReName(F,newname);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

procedure test_erase(name : shortstring);
begin
  Assign(F,name);
  Write('erasing file...');
  Erase(F);
  test(IOResult, 0);
  WriteLn('Passed!');
end;

var
 I: Integer;
 readData : array[1..DATA_SIZE] of byte;
Begin
  {------------------------ create and play with a new file --------------------------}
  FillChar(readData,DATA_SIZE,0);

  test_not_yet_open_close(FILE_NAME);
  test_do_open(FILE_NAME, MODE_REWRITE);
  test_do_write(DATA, DATA_SIZE);
  test_do_filesize(DATA_SIZE);
  test_do_seek(0);
  test_do_read(readData, DATA_SIZE);


  for i:=1 to DATA_SIZE do
   Begin
       test(readData[i], data[i]);
   end;

  test_do_seek(5);

  test_filepos(5);
(*
  test_do_truncate()
  WriteLn('truncating file...');
  Truncate(F);
  WriteLn(FileSize(F));
  if FileSize(F) <> 5 then
   RunError(255);
*)
  test_do_close;
  test_already_closed_close;
  {------------------------ create and play with an old file --------------------------}
  FillChar(readData,DATA_SIZE,0);
  test_do_open(FILE_NAME2, MODE_REWRITE);
  test_do_write(DATA, DATA_SIZE);
  test_do_close;

  FillChar(readData,DATA_SIZE,0);
  test_do_open(FILE_NAME2, MODE_RESET);
  test_do_write(DATA, DATA_SIZE);

  test_do_filesize(DATA_SIZE);
  test_do_seek(0);
  test_do_read(readData, DATA_SIZE);


  for i:=1 to DATA_SIZE do
   Begin
       test(readData[i], data[i]);
   end;

  test_do_close;

  test_rename(FILE_NAME2, 'test3.tmp');
  test_erase(FILE_NAME);
  test_erase('test3.tmp');
end.