cachetest.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. Test program for the CacheCls unit
  3. Copyright (C) 2000 by Sebastian Guenther ([email protected])
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. }
  10. program CacheTest;
  11. {$MODE objfpc}
  12. uses Strings, CacheCls;
  13. type
  14. TCacheTester = class
  15. private
  16. TestCache: TCache;
  17. function TestCacheIsDataEqual(ACache: TCache; AData1, AData2: Pointer): Boolean;
  18. procedure TestCacheFreeSlot(ACache: TCache; SlotIndex: Integer);
  19. protected
  20. procedure AddString(const s: PChar);
  21. procedure DumpCache;
  22. public
  23. constructor Create;
  24. destructor Destroy; override;
  25. procedure Run;
  26. end;
  27. function TCacheTester.TestCacheIsDataEqual(ACache: TCache;
  28. AData1, AData2: Pointer): Boolean;
  29. begin
  30. if (not Assigned(AData1)) or (not Assigned(AData2)) then
  31. Result := (not Assigned(AData1)) and (not Assigned(AData2))
  32. else
  33. Result := StrComp(PChar(AData1), PChar(AData2)) = 0;
  34. end;
  35. procedure TCacheTester.TestCacheFreeSlot(ACache: TCache; SlotIndex: Integer);
  36. var
  37. p: PChar;
  38. begin
  39. Write(' Cache slot #', SlotIndex, ' has been freed (content: ');
  40. p := PChar(ACache.Slots[SlotIndex]^.Data);
  41. if Assigned(p) then
  42. WriteLn('"', p, '")')
  43. else
  44. WriteLn('nil)');
  45. end;
  46. procedure TCacheTester.AddString(const s: PChar);
  47. var
  48. i: Integer;
  49. begin
  50. WriteLn('Adding string "', s, '"...');
  51. i := TestCache.Add(Pointer(s));
  52. WriteLn('string got cache index #', i);
  53. WriteLn('New cache state:');
  54. DumpCache;
  55. WriteLn;
  56. end;
  57. procedure TCacheTester.DumpCache;
  58. var
  59. Slot, PrevSlot: PCacheSlot;
  60. begin
  61. Slot := TestCache.MRUSlot;
  62. PrevSlot := nil;
  63. while Assigned(Slot) do
  64. begin
  65. Write(' Slot #', Slot^.Index, ' ');
  66. if Assigned(Slot^.Data) then
  67. Write('"', PChar(Slot^.Data), '"')
  68. else
  69. Write('nil');
  70. if Slot^.Prev <> PrevSlot then
  71. begin
  72. Write(' Slot^.Prev is invalid! (');
  73. if Assigned(Slot^.Prev) then
  74. Write('points to #', Slot^.Prev^.Index)
  75. else
  76. Write('nil');
  77. Write(')');
  78. end;
  79. WriteLn;
  80. PrevSlot := Slot;
  81. Slot := Slot^.Next;
  82. end;
  83. end;
  84. constructor TCacheTester.Create;
  85. begin
  86. inherited Create;
  87. TestCache := TCache.Create(4);
  88. TestCache.OnIsDataEqual := @TestCacheIsDataEqual;
  89. TestCache.OnFreeSlot := @TestCacheFreeSlot;
  90. WriteLn('Initial cache state:');
  91. DumpCache;
  92. WriteLn;
  93. end;
  94. destructor TCacheTester.Destroy;
  95. begin
  96. TestCache.Free;
  97. inherited Destroy;
  98. end;
  99. procedure TCacheTester.Run;
  100. begin
  101. AddString('1st');
  102. AddString('2nd');
  103. AddString('3rd');
  104. AddString('4th');
  105. AddString('5th');
  106. AddString('3rd');
  107. AddString('2nd');
  108. WriteLn('Setting slot count to 2...');
  109. TestCache.SlotCount := 2;
  110. WriteLn('Cache state after resize:');
  111. DumpCache;
  112. WriteLn;
  113. AddString('4th');
  114. WriteLn('Setting slot count to 6...');
  115. TestCache.SlotCount := 6;
  116. WriteLn('Cache state after resize:');
  117. DumpCache;
  118. WriteLn;
  119. AddString('5th');
  120. AddString('6th');
  121. AddString('7th');
  122. AddString('8th');
  123. end;
  124. var
  125. CacheTester: TCacheTester;
  126. begin
  127. CacheTester := TCacheTester.Create;
  128. CacheTester.Run;
  129. CacheTester.Free;
  130. end.