cachetest.pp 3.3 KB

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