| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestGenericsCollections" -*- |
| 2 |
{ |
| 3 |
Copyright 2017-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test Generics.Collections unit. These tests are independent from CGE. } |
| 18 |
unit TestGenericsCollections; |
| 19 |
|
| 20 |
{ Needed to define GENERICS_CONSTREF on some platforms/compilers. } |
| 21 |
{$I ../../../src/common_includes/castleconf.inc} |
| 22 |
|
| 23 |
interface |
| 24 |
|
| 25 |
uses |
| 26 |
Classes, SysUtils, Generics.Collections, |
| 27 |
CastleTester; |
| 28 |
|
| 29 |
type |
| 30 |
TTestGenericsCollections = class(TCastleTestCase) |
| 31 |
procedure Test1; |
| 32 |
procedure TestFreeingManually; |
| 33 |
procedure TestAddingLists; |
| 34 |
procedure TestRecordSort; |
| 35 |
//procedure TestRecordStableSort; |
| 36 |
procedure TestSort; |
| 37 |
procedure TestPack; |
| 38 |
procedure TestMapTryGetValue; |
| 39 |
procedure TestRecordsList; |
| 40 |
procedure TestVectorsList; |
| 41 |
procedure TestMethodsList; |
| 42 |
procedure TestTryGetValueNil; |
| 43 |
end; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses Generics.Defaults, |
| 48 |
CastleUtils; |
| 49 |
|
| 50 |
type |
| 51 |
TApple = class |
| 52 |
Name: string; |
| 53 |
procedure Eat; |
| 54 |
end; |
| 55 |
|
| 56 |
procedure TApple.Eat; |
| 57 |
begin |
| 58 |
// Writeln('TApple.Eat'); |
| 59 |
end; |
| 60 |
|
| 61 |
type |
| 62 |
TAppleList = class({$ifdef FPC}specialize{$endif} TObjectList<TApple>) |
| 63 |
procedure Pack; |
| 64 |
end; |
| 65 |
|
| 66 |
procedure TAppleList.Pack; |
| 67 |
begin |
| 68 |
while Remove(nil) <> -1 do ; |
| 69 |
end; |
| 70 |
|
| 71 |
procedure TTestGenericsCollections.Test1; |
| 72 |
var |
| 73 |
A: TApple; |
| 74 |
Apples: TAppleList; |
| 75 |
begin |
| 76 |
Apples := TAppleList.Create(true); |
| 77 |
try |
| 78 |
A := TApple.Create; |
| 79 |
Apples.Add(A); |
| 80 |
Apples.Add(TApple.Create); |
| 81 |
A := TApple.Create; |
| 82 |
Apples.Add(A); |
| 83 |
|
| 84 |
AssertEquals(3, Apples.Count); |
| 85 |
AssertEquals(2, Apples.IndexOf(A)); |
| 86 |
|
| 87 |
Apples.Delete(0); |
| 88 |
|
| 89 |
AssertEquals(2, Apples.Count); |
| 90 |
AssertEquals(1, Apples.IndexOf(A)); |
| 91 |
|
| 92 |
Apples.Remove(A); |
| 93 |
|
| 94 |
AssertEquals(1, Apples.Count); |
| 95 |
|
| 96 |
Apples.Delete(0); |
| 97 |
|
| 98 |
AssertEquals(0, Apples.Count); |
| 99 |
finally FreeAndNil(Apples) end; |
| 100 |
end; |
| 101 |
|
| 102 |
procedure TTestGenericsCollections.TestFreeingManually; |
| 103 |
var |
| 104 |
A: TApple; |
| 105 |
Apples: TAppleList; |
| 106 |
begin |
| 107 |
Apples := TAppleList.Create(false); |
| 108 |
try |
| 109 |
A := TApple.Create; |
| 110 |
Apples.Add(A); |
| 111 |
Apples.Add(A); |
| 112 |
Apples.Add(TApple.Create); |
| 113 |
|
| 114 |
{ This freeing would be invalid on a list that owns children, |
| 115 |
as we free something twice, and we leave some invalid references |
| 116 |
(to already freed items) in the list at various stages. |
| 117 |
But it should be OK with list that has OwnsChildren = false. } |
| 118 |
|
| 119 |
Apples[0].Free; |
| 120 |
Apples[0] := nil; |
| 121 |
Apples[1] := nil; |
| 122 |
Apples[2].Free; |
| 123 |
finally FreeAndNil(Apples) end; |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TTestGenericsCollections.TestAddingLists; |
| 127 |
var |
| 128 |
A: TApple; |
| 129 |
Apples, Apples2: TAppleList; |
| 130 |
begin |
| 131 |
Apples := TAppleList.Create(true); |
| 132 |
try |
| 133 |
A := TApple.Create; |
| 134 |
A.Name := 'One'; |
| 135 |
Apples.Add(A); |
| 136 |
|
| 137 |
A := TApple.Create; |
| 138 |
A.Name := 'Two'; |
| 139 |
Apples.Add(A); |
| 140 |
|
| 141 |
Apples2 := TAppleList.Create(false); |
| 142 |
try |
| 143 |
Apples2.AddRange(Apples); |
| 144 |
Apples2.AddRange(Apples); |
| 145 |
Apples2.AddRange(Apples); |
| 146 |
AssertEquals(6, Apples2.Count); |
| 147 |
AssertEquals('One', Apples2[0].Name); |
| 148 |
AssertEquals('Two', Apples2[1].Name); |
| 149 |
AssertEquals('One', Apples2[2].Name); |
| 150 |
AssertEquals('Two', Apples2[3].Name); |
| 151 |
AssertEquals('One', Apples2[4].Name); |
| 152 |
AssertEquals('Two', Apples2[5].Name); |
| 153 |
finally FreeAndNil(Apples2) end; |
| 154 |
finally FreeAndNil(Apples) end; |
| 155 |
end; |
| 156 |
|
| 157 |
function CompareApples({$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} Left, Right: TApple): Integer; |
| 158 |
begin |
| 159 |
Result := AnsiCompareStr(Left.Name, Right.Name); |
| 160 |
end; |
| 161 |
|
| 162 |
procedure TTestGenericsCollections.TestSort; |
| 163 |
type |
| 164 |
TAppleComparer = {$ifdef FPC}specialize{$endif} TComparer<TApple>; |
| 165 |
var |
| 166 |
A: TApple; |
| 167 |
L: TAppleList; |
| 168 |
begin |
| 169 |
L := TAppleList.Create(true); |
| 170 |
try |
| 171 |
A := TApple.Create; |
| 172 |
A.Name := '11'; |
| 173 |
L.Add(A); |
| 174 |
|
| 175 |
A := TApple.Create; |
| 176 |
A.Name := '33'; |
| 177 |
L.Add(A); |
| 178 |
|
| 179 |
A := TApple.Create; |
| 180 |
A.Name := '22'; |
| 181 |
L.Add(A); |
| 182 |
|
| 183 |
L.Sort(TAppleComparer.Construct({$ifdef FPC}@{$endif}CompareApples)); |
| 184 |
|
| 185 |
AssertEquals(3, L.Count); |
| 186 |
AssertEquals('11', L[0].Name); |
| 187 |
AssertEquals('22', L[1].Name); |
| 188 |
AssertEquals('33', L[2].Name); |
| 189 |
finally FreeAndNil(L) end; |
| 190 |
end; |
| 191 |
|
| 192 |
type |
| 193 |
TRec = record Id: Integer; SortKey: Integer; end; |
| 194 |
PRec = ^TRec; |
| 195 |
|
| 196 |
TRecList = class({$ifdef FPC}specialize{$endif} TStructList<TRec>) |
| 197 |
end; |
| 198 |
|
| 199 |
TRecComparer = {$ifdef FPC}specialize{$endif} TComparer<TRec>; |
| 200 |
|
| 201 |
function CompareRec({$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} A, B: TRec): Integer; |
| 202 |
begin |
| 203 |
Result := A.SortKey - B.SortKey; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TTestGenericsCollections.TestRecordSort; |
| 207 |
var |
| 208 |
Recs: TRecList; |
| 209 |
begin |
| 210 |
Recs := TRecList.Create; |
| 211 |
|
| 212 |
Recs.Count := 6; |
| 213 |
Recs.L[0].Id := 0; |
| 214 |
Recs.L[0].SortKey := -1; |
| 215 |
Recs.L[1].Id := 1; |
| 216 |
Recs.L[1].SortKey := -1; |
| 217 |
Recs.L[2].Id := 2; |
| 218 |
Recs.L[2].SortKey := -1; |
| 219 |
Recs.L[3].Id := 3; |
| 220 |
Recs.L[3].SortKey := -1; |
| 221 |
Recs.L[4].Id := 4; |
| 222 |
Recs.L[4].SortKey := -10; |
| 223 |
Recs.L[5].Id := 5; |
| 224 |
Recs.L[5].SortKey := 10; |
| 225 |
|
| 226 |
Recs.Sort(TRecComparer.Construct({$ifdef FPC}@{$endif}CompareRec)); |
| 227 |
|
| 228 |
AssertTrue(Recs.L[0].SortKey = -10); |
| 229 |
AssertTrue(Recs.L[1].SortKey = -1); |
| 230 |
AssertTrue(Recs.L[2].SortKey = -1); |
| 231 |
AssertTrue(Recs.L[3].SortKey = -1); |
| 232 |
AssertTrue(Recs.L[4].SortKey = -1); |
| 233 |
AssertTrue(Recs.L[5].SortKey = 10); |
| 234 |
|
| 235 |
FreeAndNil(Recs); |
| 236 |
end; |
| 237 |
|
| 238 |
(* |
| 239 |
// Not tested, because it is not a stable sort -- and that's OK, |
| 240 |
/ nothing guaranteed it, we also don't need it in practice. |
| 241 |
procedure TTestGenericsCollections.TestRecordStableSort; |
| 242 |
var |
| 243 |
Recs: TRecList; |
| 244 |
begin |
| 245 |
Recs := TRecList.Create; |
| 246 |
|
| 247 |
Recs.Count := 6; |
| 248 |
Recs.L[0].Id := 0; |
| 249 |
Recs.L[0].SortKey := -1; |
| 250 |
Recs.L[1].Id := 1; |
| 251 |
Recs.L[1].SortKey := -1; |
| 252 |
Recs.L[2].Id := 2; |
| 253 |
Recs.L[2].SortKey := -1; |
| 254 |
Recs.L[3].Id := 3; |
| 255 |
Recs.L[3].SortKey := -1; |
| 256 |
Recs.L[4].Id := 4; |
| 257 |
Recs.L[4].SortKey := -10; |
| 258 |
Recs.L[5].Id := 5; |
| 259 |
Recs.L[5].SortKey := 10; |
| 260 |
|
| 261 |
Recs.Sort(TRecComparer.Construct({$ifdef FPC}@{$endif}CompareRec)); |
| 262 |
|
| 263 |
AssertTrue(Recs.L[0].Id = 4); |
| 264 |
AssertTrue(Recs.L[0].SortKey = -10); |
| 265 |
AssertTrue(Recs.L[1].Id = 0); |
| 266 |
AssertTrue(Recs.L[1].SortKey = -1); |
| 267 |
AssertTrue(Recs.L[2].Id = 1); |
| 268 |
AssertTrue(Recs.L[2].SortKey = -1); |
| 269 |
AssertTrue(Recs.L[3].Id = 2); |
| 270 |
AssertTrue(Recs.L[3].SortKey = -1); |
| 271 |
AssertTrue(Recs.L[4].Id = 3); |
| 272 |
AssertTrue(Recs.L[4].SortKey = -1); |
| 273 |
AssertTrue(Recs.L[5].Id = 5); |
| 274 |
AssertTrue(Recs.L[5].SortKey = 10); |
| 275 |
|
| 276 |
FreeAndNil(Recs); |
| 277 |
end; |
| 278 |
*) |
| 279 |
|
| 280 |
procedure TTestGenericsCollections.TestPack; |
| 281 |
var |
| 282 |
A: TApple; |
| 283 |
L: TAppleList; |
| 284 |
begin |
| 285 |
L := TAppleList.Create(true); |
| 286 |
try |
| 287 |
L.Add(nil); |
| 288 |
|
| 289 |
A := TApple.Create; |
| 290 |
A.Name := '11'; |
| 291 |
L.Add(A); |
| 292 |
|
| 293 |
L.Add(nil); |
| 294 |
|
| 295 |
A := TApple.Create; |
| 296 |
A.Name := '33'; |
| 297 |
L.Add(A); |
| 298 |
|
| 299 |
A := TApple.Create; |
| 300 |
A.Name := '22'; |
| 301 |
L.Add(A); |
| 302 |
|
| 303 |
L.Add(nil); |
| 304 |
L.Add(nil); |
| 305 |
|
| 306 |
L.Pack; |
| 307 |
|
| 308 |
AssertEquals(3, L.Count); |
| 309 |
AssertEquals('11', L[0].Name); |
| 310 |
AssertEquals('33', L[1].Name); |
| 311 |
AssertEquals('22', L[2].Name); |
| 312 |
finally FreeAndNil(L) end; |
| 313 |
end; |
| 314 |
|
| 315 |
type |
| 316 |
TAppleDictionary = {$ifdef FPC}specialize{$endif} TDictionary<string, TApple>; |
| 317 |
|
| 318 |
procedure TTestGenericsCollections.TestMapTryGetValue; |
| 319 |
var |
| 320 |
Apples: TAppleDictionary; |
| 321 |
A, FoundA: TApple; |
| 322 |
begin |
| 323 |
Apples := TAppleDictionary.Create; |
| 324 |
try |
| 325 |
Apples.TryGetValue('blah', FoundA); |
| 326 |
AssertTrue(nil = FoundA); |
| 327 |
|
| 328 |
A := TApple.Create; |
| 329 |
Apples.AddOrSetValue('nope', A); |
| 330 |
|
| 331 |
Apples.TryGetValue('blah', FoundA); |
| 332 |
AssertTrue(nil = FoundA); |
| 333 |
|
| 334 |
A := TApple.Create; |
| 335 |
Apples.AddOrSetValue('blah', A); |
| 336 |
|
| 337 |
Apples.TryGetValue('blah', FoundA); |
| 338 |
AssertTrue(A = FoundA); |
| 339 |
|
| 340 |
Apples.Remove('blah'); |
| 341 |
|
| 342 |
A.Free; |
| 343 |
|
| 344 |
Apples.TryGetValue('blah', FoundA); |
| 345 |
AssertTrue(nil = FoundA); |
| 346 |
|
| 347 |
Apples['nope'].Free; |
| 348 |
finally FreeAndNil(Apples) end; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure TTestGenericsCollections.TestRecordsList; |
| 352 |
type |
| 353 |
TMyRecord = packed record |
| 354 |
A, B: Integer; |
| 355 |
end; |
| 356 |
TMyRecordList = {$ifdef FPC}specialize{$endif} TList<TMyRecord>; |
| 357 |
var |
| 358 |
List: TMyRecordList; |
| 359 |
R1, R2, R: TMyRecord; |
| 360 |
begin |
| 361 |
List := TMyRecordList.Create; |
| 362 |
try |
| 363 |
R1.A := 11; |
| 364 |
R1.B := 22; |
| 365 |
List.Add(R1); |
| 366 |
|
| 367 |
R2.A := 33; |
| 368 |
R2.B := 44; |
| 369 |
List.Add(R2); |
| 370 |
|
| 371 |
R2.A := 33; |
| 372 |
R2.B := 44; |
| 373 |
List.Add(R2); |
| 374 |
|
| 375 |
AssertEquals(3, List.Count); |
| 376 |
AssertEquals(11, List[0].A); |
| 377 |
AssertEquals(22, List[0].B); |
| 378 |
AssertEquals(33, List[1].A); |
| 379 |
AssertEquals(44, List[1].B); |
| 380 |
AssertEquals(33, List[2].A); |
| 381 |
AssertEquals(44, List[2].B); |
| 382 |
|
| 383 |
List.Delete(2); |
| 384 |
|
| 385 |
AssertEquals(2, List.Count); |
| 386 |
AssertEquals(11, List[0].A); |
| 387 |
AssertEquals(22, List[0].B); |
| 388 |
AssertEquals(33, List[1].A); |
| 389 |
AssertEquals(44, List[1].B); |
| 390 |
|
| 391 |
AssertEquals(0, List.IndexOf(R1)); |
| 392 |
AssertEquals(1, List.IndexOf(R2)); |
| 393 |
|
| 394 |
// change R1 and R2, to make sure it doesn't matter for tests |
| 395 |
R1.A := 111111; |
| 396 |
R1.B := 222222; |
| 397 |
R2.A := 333333; |
| 398 |
R2.B := 444444; |
| 399 |
AssertEquals(-1, List.IndexOf(R1)); |
| 400 |
AssertEquals(-1, List.IndexOf(R2)); |
| 401 |
|
| 402 |
R.A := 11; |
| 403 |
R.B := 22; |
| 404 |
AssertEquals(0, List.IndexOf(R)); |
| 405 |
|
| 406 |
R.A := 33; |
| 407 |
R.B := 44; |
| 408 |
AssertEquals(1, List.IndexOf(R)); |
| 409 |
|
| 410 |
R.A := 11; |
| 411 |
R.B := 22; |
| 412 |
List.Remove(R); |
| 413 |
AssertEquals(1, List.Count); |
| 414 |
AssertEquals(33, List[0].A); |
| 415 |
AssertEquals(44, List[0].B); |
| 416 |
|
| 417 |
R.A := 666; |
| 418 |
R.B := 22; |
| 419 |
List.Remove(R); // does nothing, no such record |
| 420 |
AssertEquals(1, List.Count); |
| 421 |
AssertEquals(33, List[0].A); |
| 422 |
AssertEquals(44, List[0].B); |
| 423 |
finally FreeAndNil(List) end; |
| 424 |
end; |
| 425 |
|
| 426 |
procedure TTestGenericsCollections.TestVectorsList; |
| 427 |
type |
| 428 |
TMyVector = packed array [0..1] of Single; |
| 429 |
TMyVectorList = {$ifdef FPC}specialize{$endif} TList<TMyVector>; |
| 430 |
var |
| 431 |
List: TMyVectorList; |
| 432 |
R1, R2, R: TMyVector; |
| 433 |
begin |
| 434 |
List := TMyVectorList.Create; |
| 435 |
try |
| 436 |
R1[0] := 11; |
| 437 |
R1[1] := 22; |
| 438 |
List.Add(R1); |
| 439 |
|
| 440 |
R2[0] := 33; |
| 441 |
R2[1] := 44; |
| 442 |
List.Add(R2); |
| 443 |
|
| 444 |
R2[0] := 33; |
| 445 |
R2[1] := 44; |
| 446 |
List.Add(R2); |
| 447 |
|
| 448 |
AssertEquals(3, List.Count); |
| 449 |
AssertEquals(11, List[0][0]); |
| 450 |
AssertEquals(22, List[0][1]); |
| 451 |
AssertEquals(33, List[1][0]); |
| 452 |
AssertEquals(44, List[1][1]); |
| 453 |
AssertEquals(33, List[2][0]); |
| 454 |
AssertEquals(44, List[2][1]); |
| 455 |
|
| 456 |
List.Delete(2); |
| 457 |
|
| 458 |
AssertEquals(2, List.Count); |
| 459 |
AssertEquals(11, List[0][0]); |
| 460 |
AssertEquals(22, List[0][1]); |
| 461 |
AssertEquals(33, List[1][0]); |
| 462 |
AssertEquals(44, List[1][1]); |
| 463 |
|
| 464 |
AssertEquals(0, List.IndexOf(R1)); |
| 465 |
AssertEquals(1, List.IndexOf(R2)); |
| 466 |
|
| 467 |
// change R1 and R2, to make sure it doesn't matter for tests |
| 468 |
R1[0] := 111111; |
| 469 |
R1[1] := 222222; |
| 470 |
R2[0] := 333333; |
| 471 |
R2[1] := 444444; |
| 472 |
AssertEquals(-1, List.IndexOf(R1)); |
| 473 |
AssertEquals(-1, List.IndexOf(R2)); |
| 474 |
|
| 475 |
R[0] := 11; |
| 476 |
R[1] := 22; |
| 477 |
AssertEquals(0, List.IndexOf(R)); |
| 478 |
|
| 479 |
R[0] := 33; |
| 480 |
R[1] := 44; |
| 481 |
AssertEquals(1, List.IndexOf(R)); |
| 482 |
|
| 483 |
R[0] := 11; |
| 484 |
R[1] := 22; |
| 485 |
List.Remove(R); |
| 486 |
AssertEquals(1, List.Count); |
| 487 |
AssertEquals(33, List[0][0]); |
| 488 |
AssertEquals(44, List[0][1]); |
| 489 |
|
| 490 |
R[0] := 666; |
| 491 |
R[1] := 22; |
| 492 |
List.Remove(R); // does nothing, no such item |
| 493 |
AssertEquals(1, List.Count); |
| 494 |
AssertEquals(33, List[0][0]); |
| 495 |
AssertEquals(44, List[0][1]); |
| 496 |
finally FreeAndNil(List) end; |
| 497 |
end; |
| 498 |
|
| 499 |
type |
| 500 |
TSomeClass = class |
| 501 |
procedure Foo(A: Integer); |
| 502 |
end; |
| 503 |
|
| 504 |
procedure TSomeClass.Foo(A: Integer); |
| 505 |
begin |
| 506 |
end; |
| 507 |
|
| 508 |
{ Delphi 10.2.3 on Win32 (but not Win64) crashes on List.IndexOf by default, workaround } |
| 509 |
{$if (not defined(FPC)) and (CompilerVersion < 35) and (defined(WIN32))} |
| 510 |
{$define LIST_INDEXOF_WORKAROUND} |
| 511 |
{$endif} |
| 512 |
|
| 513 |
type |
| 514 |
TMyMethod = procedure (A: Integer) of object; |
| 515 |
TMyMethodList = class({$ifdef FPC}specialize{$endif} TList<TMyMethod>) |
| 516 |
{$ifdef LIST_INDEXOF_WORKAROUND} |
| 517 |
function IndexOf(const M: TMyMethod): Integer; |
| 518 |
{$endif} |
| 519 |
end; |
| 520 |
|
| 521 |
{$ifdef LIST_INDEXOF_WORKAROUND} |
| 522 |
function TMyMethodList.IndexOf(const M: TMyMethod): Integer; |
| 523 |
var |
| 524 |
M2: TMyMethod; |
| 525 |
begin |
| 526 |
for Result := 0 to Count - 1 do |
| 527 |
begin |
| 528 |
M2 := Items[Result]; |
| 529 |
if (TMethod(M).Code = TMethod(M2).Code) and |
| 530 |
(TMethod(M).Data = TMethod(M2).Data) then |
| 531 |
Exit; |
| 532 |
end; |
| 533 |
Result := -1; |
| 534 |
end; |
| 535 |
{$endif} |
| 536 |
|
| 537 |
procedure TTestGenericsCollections.TestMethodsList; |
| 538 |
|
| 539 |
procedure AssertMethodsEqual(const M1, M2: TMyMethod); |
| 540 |
begin |
| 541 |
AssertTrue(TMethod(M1).Code = TMethod(M2).Code); |
| 542 |
AssertTrue(TMethod(M1).Data = TMethod(M2).Data); |
| 543 |
end; |
| 544 |
|
| 545 |
var |
| 546 |
List: TMyMethodList; |
| 547 |
C1, C2, C3: TSomeClass; |
| 548 |
M: TMyMethod; |
| 549 |
begin |
| 550 |
C1 := TSomeClass.Create; |
| 551 |
C2 := TSomeClass.Create; |
| 552 |
C3 := TSomeClass.Create; |
| 553 |
|
| 554 |
List := TMyMethodList.Create; |
| 555 |
try |
| 556 |
List.Add({$ifdef FPC}@{$endif}C1.Foo); |
| 557 |
List.Add({$ifdef FPC}@{$endif}C2.Foo); |
| 558 |
List.Add({$ifdef FPC}@{$endif}C2.Foo); |
| 559 |
|
| 560 |
AssertEquals(3, List.Count); |
| 561 |
M := {$ifdef FPC}@{$endif}C1.Foo; |
| 562 |
AssertMethodsEqual(List[0], M); |
| 563 |
M := {$ifdef FPC}@{$endif}C2.Foo; |
| 564 |
AssertMethodsEqual(List[1], M); |
| 565 |
AssertMethodsEqual(List[2], M); |
| 566 |
|
| 567 |
List.Delete(2); |
| 568 |
|
| 569 |
AssertEquals(2, List.Count); |
| 570 |
M := {$ifdef FPC}@{$endif}C1.Foo; |
| 571 |
AssertMethodsEqual(List[0], M); |
| 572 |
M := {$ifdef FPC}@{$endif}C2.Foo; |
| 573 |
AssertMethodsEqual(List[1], M); |
| 574 |
|
| 575 |
AssertEquals(0, List.IndexOf({$ifdef FPC}@{$endif}C1.Foo)); |
| 576 |
AssertEquals(1, List.IndexOf({$ifdef FPC}@{$endif}C2.Foo)); |
| 577 |
|
| 578 |
AssertEquals(-1, List.IndexOf({$ifdef FPC}@{$endif}C3.Foo)); |
| 579 |
|
| 580 |
List.Remove({$ifdef FPC}@{$endif}C1.Foo); |
| 581 |
AssertEquals(1, List.Count); |
| 582 |
M := {$ifdef FPC}@{$endif}C2.Foo; |
| 583 |
AssertMethodsEqual(List[0], M); |
| 584 |
|
| 585 |
List.Remove({$ifdef FPC}@{$endif}C3.Foo); // does nothing, no such item |
| 586 |
AssertEquals(1, List.Count); |
| 587 |
M := {$ifdef FPC}@{$endif}C2.Foo; |
| 588 |
AssertMethodsEqual(List[0], M); |
| 589 |
finally FreeAndNil(List) end; |
| 590 |
|
| 591 |
C1.Free; |
| 592 |
C2.Free; |
| 593 |
C3.Free; |
| 594 |
end; |
| 595 |
|
| 596 |
type |
| 597 |
TStringStringMap = class({$ifdef FPC}specialize{$endif} TDictionary<string, string>) |
| 598 |
end; |
| 599 |
|
| 600 |
procedure TTestGenericsCollections.TestTryGetValueNil; |
| 601 |
var |
| 602 |
Map: TStringStringMap; |
| 603 |
V: String; |
| 604 |
B: Boolean; |
| 605 |
begin |
| 606 |
Map := TStringStringMap.Create; |
| 607 |
try |
| 608 |
Map.AddOrSetValue('some key', 'some value'); |
| 609 |
|
| 610 |
B := Map.TryGetValue('some key', V); |
| 611 |
AssertTrue(B); |
| 612 |
AssertEquals('some value', V); |
| 613 |
|
| 614 |
B := Map.TryGetValue('some other key', V); |
| 615 |
AssertFalse(B); |
| 616 |
|
| 617 |
B := Map.TryGetValue('', V); |
| 618 |
AssertFalse(B); |
| 619 |
finally FreeAndNil(Map) end; |
| 620 |
end; |
| 621 |
|
| 622 |
initialization |
| 623 |
RegisterTest(TTestGenericsCollections); |
| 624 |
end. |