Menu

[r945]: / branches / new-backend / Tests / Src / DUnit / TestNsDatabaseUDataPool.pas  Maximize  Restore  History

Download this file

276 lines (242 with data), 7.0 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
{
Delphi DUnit Test Case for the NsDatabase.UDataPool Unit
--------------------------------------------------------
$Rev$
$Date$
}
unit TestNsDatabaseUDataPool;
interface
uses
SysUtils, TestFramework, Generics.Collections,
NsDatabase.UCookies, NsDatabase.UDataItem, NsDatabase.UDataPool;
type
TTestObject = class(TDBDataItem)
strict private
fID: Integer;
strict protected
procedure Finalize; override;
public
constructor Create(ID: Integer; Cookie: TDBCookie);
class var InstanceCount: Integer;
property ID: Integer read fID;
function ToString: string; override;
end;
TestTDBDataPool = class(TTestCase)
strict private
O1, O2, O3, O4, ONotInPool: TTestObject;
Pool: TDBDataPool<TTestObject>;
public
procedure SetUp; override;
procedure TearDown; override;
procedure ErrorAdd;
procedure ErrorAddNulCookie;
procedure ErrorRemove;
procedure ErrorItemsProp;
function CheckOffFromList<T>(const List: TList<T>; const Item: T): Boolean;
published
procedure TestAddCountAndFree;
procedure TestIsInPool;
procedure TestRemove;
procedure TestClear;
procedure TestItemsProp;
procedure TestEnumerator;
procedure TestCookiesEnumerator;
end;
implementation
{ TTestObject }
constructor TTestObject.Create(ID: Integer; Cookie: TDBCookie);
begin
inherited Create(Cookie);
fID := ID;
Inc(InstanceCount);
end;
procedure TTestObject.Finalize;
begin
inherited;
Dec(InstanceCount);
end;
function TTestObject.ToString: string;
begin
Result := Format('%s %d', [ClassName, ID]);
end;
{ TestTDBDataPool }
function TestTDBDataPool.CheckOffFromList<T>(const List: TList<T>;
const Item: T): Boolean;
begin
Result := List.Contains(Item);
if Result then
List.Remove(Item);
end;
procedure TestTDBDataPool.ErrorAdd;
begin
Pool.Add(O3); // O3 is already in pool
end;
procedure TestTDBDataPool.ErrorAddNulCookie;
var
ONul: TTestObject;
begin
ONul := TTestObject.Create(999, TDBCookie.CreateNul);
try
Pool.Add(ONul); // can't add date item with nul cookie
finally
ONul.Free;
end;
end;
procedure TestTDBDataPool.ErrorItemsProp;
begin
Pool.Items[ONotInPool.Cookie];
end;
procedure TestTDBDataPool.ErrorRemove;
begin
Pool.Remove(ONotInPool.Cookie);
end;
procedure TestTDBDataPool.SetUp;
begin
inherited;
Pool := TDBDataPool<TTestObject>.Create(8);
O1 := TTestObject.Create(100, TDBCookie.Create);
Pool.Add(O1);
O2 := TTestObject.Create(200, TDBCookie.Create);
Pool.Add(O2);
O3 := TTestObject.Create(300, TDBCookie.Create);
Pool.Add(O3);
O4 := TTestObject.Create(400, TDBCookie.Create);
Pool.Add(O4);
ONotInPool := TTestObject.Create(500, TDBCookie.Create);
end;
procedure TestTDBDataPool.TearDown;
begin
inherited;
ONotInPool.Free;
Pool.Free;
end;
procedure TestTDBDataPool.TestAddCountAndFree;
var
APool: TDBDataPool<TTestObject>;
O1, O2: TTestObject;
SavedInstCount: Integer;
begin
SavedInstCount := TTestObject.InstanceCount;
APool := TDBDataPool<TTestObject>.Create;
try
Check(APool.Count = 0, 'Expected TDBDataPool.Count = 0');
O1 := TTestObject.Create(10, TDBCookie.Create);
APool.Add(O1);
Check(APool.Count = 1, 'Expected TDBDataPool.Count = 1');
O2 := TTestObject.Create(20, TDBCookie.Create);
APool.Add(O2);
Check(APool.Count = 2, 'Expected TDBDataPool.Count = 2');
Check(TTestObject.InstanceCount = SavedInstCount + 2,
Format('Expected %d TTestObject instances, got %d',
[SavedInstCount + 2, TTestObject.InstanceCount]));
finally
APool.Free;
end;
Check(TTestObject.InstanceCount = SavedInstCount,
Format('Expected %d TTestObject instances, got %d',
[SavedInstCount, TTestObject.InstanceCount]));
CheckException(ErrorAdd, EDBDataPoolError,
'Added duplicate object');
CheckException(ErrorAddNulCookie, EDBDataPoolError,
'Added object with nul cookie');
end;
procedure TestTDBDataPool.TestClear;
begin
Check(Pool.Count = 4, 'Expected 4 items in initialised pool');
Pool.Clear;
Check(Pool.Count = 0, 'Expected 0 items in pool after .Clear');
// Re-initialise
TearDown;
Setup;
end;
procedure TestTDBDataPool.TestCookiesEnumerator;
var
Cookie: TDBCookie;
List: TList<TDBCookie>;
begin
List := TList<TDBCookie>.Create;
try
List.AddRange([O1.Cookie, O2.Cookie, O3.Cookie, O4.Cookie]);
for Cookie in Pool.Cookies do
begin
if not CheckOffFromList<TDBCookie>(List, Cookie) then
Fail('Cookie unexpected');
end;
Check(List.Count = 0, 'Enumeration was incomplete');
finally
List.Free;
end;
end;
procedure TestTDBDataPool.TestEnumerator;
var
DataItem: TTestObject;
List: TList<TTestObject>;
begin
List := TList<TTestObject>.Create;
try
List.AddRange([O1, O2, O3, O4]);
for DataItem in Pool do
begin
if not CheckOffFromList<TTestObject>(List, DataItem) then
Fail(Format('DataItem %s unexpected', [DataItem.ToString]));
end;
Check(List.Count = 0, 'Enumeration was incomplete');
finally
List.Free;
end;
end;
procedure TestTDBDataPool.TestIsInPool;
begin
Check(Pool.IsInPool(O1.Cookie), 'Expected O1 to be in pool');
Check(Pool.IsInPool(O2.Cookie), 'Expected O1 to be in pool');
Check(Pool.IsInPool(O3.Cookie), 'Expected O1 to be in pool');
Check(Pool.IsInPool(O4.Cookie), 'Expected O1 to be in pool');
Check(not Pool.IsInPool(ONotInPool.Cookie),
'Expected ONotInPool not to be in pool');
end;
procedure TestTDBDataPool.TestItemsProp;
var
ObjFound: TTestObject;
begin
ObjFound := Pool.Items[O1.Cookie];
Check(ObjFound = O1, 'ObjFound <> O1');
ObjFound := Pool.Items[O2.Cookie];
Check(ObjFound = O2, 'ObjFound <> O2');
ObjFound := Pool.Items[O3.Cookie];
Check(ObjFound = O3, 'ObjFound <> O3');
ObjFound := Pool[O4.Cookie];
Check(ObjFound = O4, 'ObjFound <> O4');
CheckException(ErrorItemsProp, EDBDataPoolError);
end;
procedure TestTDBDataPool.TestRemove;
var
APool: TDBDataPool<TTestObject>;
Cookie1, Cookie2: TDBCookie;
SavedInstCount: Integer;
begin
SavedInstCount := TTestObject.InstanceCount;
APool := TDBDataPool<TTestObject>.Create;
try
Cookie1 := TDBCookie.Create;
Cookie2 := TDBCookie.Create;
APool.Add(TTestObject.Create(10, Cookie1));
APool.Add(TTestObject.Create(20, Cookie2));
Check(APool.Count = 2, 'Expected TDBDataPool.Count = 2');
Check(TTestObject.InstanceCount = SavedInstCount + 2,
Format('Expected %d TTestObject instances, got %d',
[SavedInstCount + 2, TTestObject.InstanceCount]));
APool.Remove(Cookie2);
APool.Remove(Cookie1);
Check(APool.Count = 0, 'Expected TDBDataPool.Count = 2');
Check(TTestObject.InstanceCount = SavedInstCount,
Format('Expected %d TTestObject instances, got %d',
[SavedInstCount, TTestObject.InstanceCount]));
CheckException(ErrorRemove, EDBDataPoolError);
finally
APool.Free;
end;
end;
initialization
// Register test case with the test runner
RegisterTest(TestTDBDataPool.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.