unit UTestCacheManager; {$mode objfpc}{$H+} interface uses Classes, SysUtils, fpcunit, testregistry, UCacheManager; type TIntegerCache = specialize TCacheManager; TTestCacheManager = class(TTestCase) published procedure RejectsSizeBelowTwo; procedure StoreAfterMissKeepsInvariant; procedure StoreTwiceKeepsInvariant; procedure HitReordersWithoutBreakingStore; procedure RemoveIDKeepsInvariant; procedure ClearRemovesAllObjects; procedure DiscardIDKeepsEntryUsable; procedure DestroyFreesAllEntries; end; implementation procedure TTestCacheManager.RejectsSizeBelowTwo; var raised: Boolean; begin raised := False; try TIntegerCache.Create(1).Free; except on E: Exception do raised := True; end; AssertTrue('size 1 must be rejected', raised); raised := False; try TIntegerCache.Create(0).Free; except on E: Exception do raised := True; end; AssertTrue('size 0 must be rejected', raised); end; procedure TTestCacheManager.StoreAfterMissKeepsInvariant; var cache: TIntegerCache; objects: array[0..15] of TObject; resultObj: TObject; i: Integer; begin for i := 0 to High(objects) do objects[i] := TObject.Create; cache := TIntegerCache.Create(8); try for i := 0 to High(objects) do begin AssertFalse('query must miss', cache.QueryID(i, resultObj)); cache.StoreID(i, objects[i]); end; //entries 0..7 were evicted (and freed by the cache), 8..15 must hit AssertTrue('most recent entry must hit', cache.QueryID(15, resultObj)); AssertSame('hit must return the stored object', objects[15], resultObj); AssertTrue('oldest surviving entry must hit', cache.QueryID(8, resultObj)); AssertFalse('evicted entry must miss', cache.QueryID(7, resultObj)); finally cache.Free; //frees the remaining owned objects end; end; procedure TTestCacheManager.StoreTwiceKeepsInvariant; var cache: TIntegerCache; objA, objB, resultObj: TObject; begin objA := TObject.Create; objB := TObject.Create; cache := TIntegerCache.Create(4); try AssertFalse(cache.QueryID(1, resultObj)); cache.StoreID(1, objA); cache.StoreID(2, objB); //used to nil-deref via a stale tail pointer AssertTrue(cache.QueryID(2, resultObj)); AssertSame(objB, resultObj); AssertTrue(cache.QueryID(1, resultObj)); AssertSame(objA, resultObj); finally cache.Free; //frees objA and objB end; end; procedure TTestCacheManager.HitReordersWithoutBreakingStore; var cache: TIntegerCache; objects: array[0..6] of TObject; resultObj: TObject; i: Integer; begin for i := 0 to High(objects) do objects[i] := TObject.Create; cache := TIntegerCache.Create(8); try for i := 0 to High(objects) do begin AssertFalse(cache.QueryID(i, resultObj)); cache.StoreID(i, objects[i]); end; //hit an entry that is not at the front, then store again AssertTrue(cache.QueryID(2, resultObj)); AssertSame(objects[2], resultObj); AssertFalse(cache.QueryID(7, resultObj)); cache.StoreID(7, TObject.Create); //entry 7 becomes front AssertTrue(cache.QueryID(2, resultObj)); AssertSame(objects[2], resultObj); AssertTrue(cache.QueryID(5, resultObj)); AssertSame(objects[5], resultObj); finally cache.Free; end; end; procedure TTestCacheManager.RemoveIDKeepsInvariant; var cache: TIntegerCache; objects: array[0..3] of TObject; obj5, resultObj: TObject; i: Integer; begin for i := 0 to High(objects) do objects[i] := TObject.Create; obj5 := TObject.Create; cache := TIntegerCache.Create(4); try for i := 0 to High(objects) do begin AssertFalse(cache.QueryID(i, resultObj)); cache.StoreID(i, objects[i]); end; cache.RemoveID(3); //frees objects[3] cache.RemoveID(0); //frees objects[0] AssertFalse('removed entry must miss', cache.QueryID(3, resultObj)); AssertFalse('removed entry must miss', cache.QueryID(0, resultObj)); cache.StoreID(5, obj5); //store right after removals AssertTrue(cache.QueryID(5, resultObj)); AssertSame(obj5, resultObj); AssertTrue(cache.QueryID(2, resultObj)); AssertTrue(cache.QueryID(1, resultObj)); finally cache.Free; //frees objects[1], objects[2], obj5 end; end; procedure TTestCacheManager.ClearRemovesAllObjects; var cache: TIntegerCache; objects: array[0..2] of TObject; replacement, resultObj: TObject; i: Integer; begin for i := 0 to High(objects) do objects[i] := TObject.Create; cache := TIntegerCache.Create(4); try for i := 0 to High(objects) do begin AssertFalse(cache.QueryID(i, resultObj)); cache.StoreID(i, objects[i]); end; cache.Clear; //frees the objects for i := 0 to High(objects) do AssertFalse('cleared entry must miss', cache.QueryID(i, resultObj)); replacement := TObject.Create; cache.StoreID(9, replacement); //entries must be reusable afterwards AssertTrue(cache.QueryID(9, resultObj)); AssertSame(replacement, resultObj); finally cache.Free; end; end; procedure TTestCacheManager.DiscardIDKeepsEntryUsable; var cache: TIntegerCache; objA, objB, resultObj: TObject; begin objA := TObject.Create; objB := TObject.Create; cache := TIntegerCache.Create(4); try AssertFalse(cache.QueryID(1, resultObj)); cache.StoreID(1, objA); cache.DiscardID(1); //entry detached, ownership back to the caller AssertFalse('discarded entry must miss', cache.QueryID(1, resultObj)); cache.StoreID(2, objB); AssertTrue(cache.QueryID(2, resultObj)); AssertSame(objB, resultObj); finally cache.Free; //frees objB only objA.Free; //discarded object is ours again end; end; procedure TTestCacheManager.DestroyFreesAllEntries; var cache: TIntegerCache; objects: array[0..3] of TObject; resultObj: TObject; i: Integer; begin for i := 0 to High(objects) do objects[i] := TObject.Create; cache := TIntegerCache.Create(4); for i := 0 to High(objects) do begin AssertFalse(cache.QueryID(i, resultObj)); cache.StoreID(i, objects[i]); end; AssertTrue(cache.QueryID(3, resultObj)); //move-to-front before destroy cache.Free; //must not crash, must free all entries end; initialization RegisterTests([TTestCacheManager]); end.