import { ObjectCache } from "./ObjectCache";

describe("Object cache", () => {
  it("should keep size at or below limit", () => {
    const cache = new ObjectCache<number>(2);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);
    cache.set("d", 4);
    expect(cache.size).toBe(2);
  });

  it("should remove first added element to keep size at limit", () => {
    const cache = new ObjectCache<number>(2);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.set("c", 3);
    expect(cache.has("a")).toBe(false);
    expect(cache.has("b")).toBe(true);
    expect(cache.has("c")).toBe(true);
  });

  it("should remove first accessed element to keep size at limit", () => {
    const cache = new ObjectCache<number>(2);
    cache.set("a", 1);
    cache.set("b", 2);
    cache.get("a");
    cache.set("c", 3);
    expect(cache.has("a")).toBe(true);
    expect(cache.has("b")).toBe(false);
    expect(cache.has("c")).toBe(true);
  });
});
