Menu

[4a4584]: / Test / EmmaDataInputTests.pas  Maximize  Restore  History

Download this file

337 lines (289 with data), 7.8 kB

  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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
(***********************************************************************)
(* Delphi Code Coverage *)
(* *)
(* A quick hack of a Code Coverage Tool for Delphi *)
(* by Christer Fahlgren and Nick Ring *)
(* *)
(* This Source Code Form is subject to the terms of the Mozilla Public *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this *)
(* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
unit EmmaDataInputTests;
interface
uses
TestFramework, Classes, EmmaFileHelper;
type
TestTEmmaDataInput = class(TTestCase)
strict private
FDataInput: TEmmaDataInput;
procedure SetUpDataInput(const AStream: TStream);
public
procedure TearDown; override;
published
procedure TestReadInt64;
procedure TestReadInteger;
procedure TestReadByte;
procedure TestReadBoolean;
procedure TestReadWord;
procedure TestReadUTF;
procedure TestReadIntArray;
procedure TestReadBooleanArray;
end;
implementation
uses
Types,
SysUtils,
Winsock;
procedure TestTEmmaDataInput.SetUpDataInput(const AStream: TStream);
begin
AStream.Seek(0, soFromBeginning);
FDataInput := TEmmaDataInput.Create(AStream);
end;
procedure TestTEmmaDataInput.TearDown;
begin
FDataInput.Free;
FDataInput := nil;
end;
procedure TestTEmmaDataInput.TestReadInt64;
var
i: Byte;
bytes: TBytes;
procedure CheckReadInt64(const Expected: Int64; const ABytes: TBytes);
var
FStream: TStream;
begin
FStream := TBytesStream.Create(ABytes);
try
SetUpDataInput(FStream);
CheckEquals(Expected, FDataInput.ReadInt64);
finally
FreeAndNil(FDataInput);
FreeAndNil(FStream);
end;
end;
begin
SetLength(bytes, 8);
FillChar(bytes[0], 8, 0);
for i := 0 to 255 do
begin
bytes[7] := i;
CheckReadInt64(i, bytes);
end;
bytes[7] := 0;
for i := 1 to 255 do
begin
bytes[6] := i;
CheckReadInt64(256 * i, bytes);
end;
bytes[6] := 0;
for i := 1 to 255 do
begin
bytes[5] := i;
CheckReadInt64(65536 * i, bytes);
end;
bytes[5] := 0;
for i := 1 to 255 do
begin
bytes[0] := i;
CheckReadInt64(72057594037927936 * i, bytes);
end;
end;
procedure TestTEmmaDataInput.TestReadInteger;
var
i: Byte;
bytes: TBytes;
procedure CheckReadInteger(const Expected: Integer; const ABytes: TBytes);
var
FStream: TStream;
begin
FStream := TBytesStream.Create(ABytes);
try
SetUpDataInput(FStream);
CheckEquals(Expected, FDataInput.ReadInteger);
finally
FreeAndNil(FDataInput);
FreeAndNil(FStream);
end;
end;
begin
SetLength(bytes, 4);
FillChar(bytes[0], 4, 0);
for i := 0 to 255 do
begin
bytes[3] := i;
CheckReadInteger(i, bytes);
end;
bytes[3] := 0;
for i := 1 to 255 do
begin
bytes[2] := i;
CheckReadInteger(256 * i, bytes);
end;
bytes[2] := 0;
for i := 1 to 255 do
begin
bytes[1] := i;
CheckReadInteger(65536 * i, bytes);
end;
bytes[1] := 0;
for i := 1 to 255 do
begin
bytes[0] := i;
CheckReadInteger(16777216 * i, bytes);
end;
end;
procedure TestTEmmaDataInput.TestReadByte;
var
i: Integer;
bytes: TBytes;
FStream: TBytesStream;
begin
Randomize;
SetLength(bytes, 10 + Random(100));
for i := 0 to Length(Bytes) - 1 do
bytes[i] := Random(256);
FStream := TBytesStream.Create(bytes);
try
SetUpDataInput(FStream);
for i := 0 to Length(Bytes) - 1 do
CheckEquals(bytes[i], FDataInput.ReadByte, IntToStr(i) + ': should be ' + IntToStr(bytes[i]));
finally
FStream.Free;
end;
end;
procedure TestTEmmaDataInput.TestReadBoolean;
var
i: Integer;
bytes: TBytes;
FStream: TBytesStream;
begin
Randomize;
SetLength(bytes, 10 + Random(100));
for i := 0 to Length(Bytes) - 1 do
bytes[i] := Random(2);
FStream := TBytesStream.Create(bytes);
try
SetUpDataInput(FStream);
for i := 0 to Length(Bytes) - 1 do
CheckEquals(Boolean(bytes[i]), FDataInput.ReadBoolean, IntToStr(i) + ': should be ' + BoolToStr(Boolean(bytes[i])));
finally
FStream.Free;
end;
end;
procedure TestTEmmaDataInput.TestReadWord;
var
i: Byte;
bytes: TBytes;
procedure CheckReadWord(const Expected: Word; const ABytes: TBytes);
var
FStream: TStream;
begin
FStream := TBytesStream.Create(ABytes);
try
SetUpDataInput(FStream);
CheckEquals(Expected, FDataInput.ReadWord);
finally
FreeAndNil(FDataInput);
FreeAndNil(FStream);
end;
end;
begin
SetLength(bytes, 2);
FillChar(bytes[0], 2, 0);
for i := 0 to 255 do
begin
bytes[1] := i;
CheckReadWord(i, bytes);
end;
bytes[1] := 0;
for i := 1 to 255 do
begin
bytes[0] := i;
CheckReadWord(256 * i, bytes);
end;
end;
procedure TestTEmmaDataInput.TestReadUTF;
var
FStream: TMemoryStream;
Expected: string;
DataSize: Word;
bytes: TBytes;
begin
Expected := 'Hello Wörld';
FStream := TMemoryStream.Create;
try
CheckEquals(0, FStream.Size);
DataSize := ntohs(GetUtf8Length(Expected) - SizeOf(DataSize));
bytes := TEncoding.UTF8.GetBytes(Expected);
FStream.Write(DataSize, SizeOf(DataSize));
FStream.Write(bytes[0], Length(bytes));
SetUpDataInput(FStream);
CheckEqualsString(Expected, FDataInput.ReadUTF);
finally
FStream.Free;
end;
end;
procedure TestTEmmaDataInput.TestReadIntArray;
var
ExpectedIntArray: TIntegerDynArray;
i: Integer;
v: Integer;
FStream: TMemoryStream;
actualArray: TIntegerDynArray;
begin
Randomize;
SetLength(ExpectedIntArray, 10 + Random(100));
for i := 0 to Length(ExpectedIntArray) - 1 do
ExpectedIntArray[i] := Random(MaxInt);
FStream := TMemoryStream.Create;
try
v := ntohl(Length(ExpectedIntArray));
FStream.Write(v, SizeOf(v));
for i := 0 to Length(ExpectedIntArray) - 1 do
begin
v := ntohl(ExpectedIntArray[i]);
FStream.Write(v, SizeOf(v));
end;
SetUpDataInput(FStream);
SetLength(actualArray, 0);
FDataInput.ReadIntArray(actualArray);
CheckEquals(Length(ExpectedIntArray), Length(actualArray), 'Length');
for i := 0 to Length(actualArray) - 1 do
CheckEquals(ExpectedIntArray[i], actualArray[i], IntToStr(i));
finally
FStream.Free;
end;
end;
procedure TestTEmmaDataInput.TestReadBooleanArray;
var
ExpectedBoolArray: TBooleanDynArray;
i: Integer;
v: Byte;
FStream: TMemoryStream;
actualArray: TBooleanDynArray;
begin
Randomize;
SetLength(ExpectedBoolArray, 10 + Random(100));
for i := 0 to Length(ExpectedBoolArray) - 1 do
ExpectedBoolArray[i] := Boolean(Random(2));
FStream := TMemoryStream.Create;
try
i := ntohl(Length(ExpectedBoolArray));
FStream.Write(i, SizeOf(i));
for i := 0 to Length(ExpectedBoolArray) - 1 do
begin
v := Byte(ExpectedBoolArray[i]);
FStream.Write(v, 1);
end;
SetUpDataInput(FStream);
SetLength(actualArray, 0);
actualArray := FDataInput.ReadBooleanArray;
CheckEquals(Length(ExpectedBoolArray), Length(actualArray), 'Length');
for i := 0 to Length(actualArray) - 1 do
CheckEquals(ExpectedBoolArray[i], actualArray[i], IntToStr(i));
finally
FStream.Free;
end;
end;
initialization
RegisterTest(TestTEmmaDataInput.Suite);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.