cache - cache_test.go

package cache

import (
	"testing"
)

func TestLRUCache(t *testing.T) {
	c := NewLRU[string, string](5)

	var _ Cache[string, string] = c

	if v, ok := c.Get("a"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	}

	if !c.Set("a", "1") {
		t.Error("unexpectedly got set = false")
	} else if v, ok := c.Get("a"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "1" {
		t.Errorf("expecting %q, got %q", "1", v)
	} else if len(c.data) != 1 {
		t.Errorf("expecting cache length to be 1, got %d", len(c.data))
	}

	if c.Set("a", "1") {
		t.Error("unexpectedly got set = true")
	} else if v, ok := c.Get("a"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "1" {
		t.Errorf("expecting %q, got %q", "1", v)
	} else if len(c.data) != 1 {
		t.Errorf("expecting cache length to be 1, got %d", len(c.data))
	}

	c.Set("b", "2")

	if v, ok := c.Get("b"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "2" {
		t.Errorf("expecting %q, got %q", "2", v)
	} else if len(c.data) != 2 {
		t.Errorf("expecting cache length to be 2, got %d", len(c.data))
	}

	c.Set("c", "3")
	c.Set("d", "4")
	c.Set("e", "5")

	if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	c.Set("f", "6")

	if v, ok := c.Get("a"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	if v, ok := c.Get("b"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "2" {
		t.Errorf("expecting %q, got %q", "2", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	c.Set("g", "7")

	if v, ok := c.Get("c"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	if c.Remove("a") {
		t.Error("unexpectedly got removed = true")
	}

	if !c.Remove("d") {
		t.Error("unexpectedly got removed = false")
	} else if _, ok := c.Get("d"); ok {
		t.Error("unexpectedly got ok = true")
	} else if len(c.data) != 4 {
		t.Errorf("expecting cache length to be 4, got %d", len(c.data))
	}
}

func TestMRUCache(t *testing.T) {
	c := NewMRU[string, string](5)

	var _ Cache[string, string] = c

	if v, ok := c.Get("a"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	}

	if !c.Set("a", "1") {
		t.Error("unexpectedly got set = false")
	} else if v, ok := c.Get("a"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "1" {
		t.Errorf("expecting %q, got %q", "1", v)
	} else if len(c.data) != 1 {
		t.Errorf("expecting cache length to be 1, got %d", len(c.data))
	}

	if c.Set("a", "1") {
		t.Error("unexpectedly got set = true")
	} else if v, ok := c.Get("a"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "1" {
		t.Errorf("expecting %q, got %q", "1", v)
	} else if len(c.data) != 1 {
		t.Errorf("expecting cache length to be 1, got %d", len(c.data))
	}

	c.Set("b", "2")

	if v, ok := c.Get("b"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "2" {
		t.Errorf("expecting %q, got %q", "2", v)
	} else if len(c.data) != 2 {
		t.Errorf("expecting cache length to be 2, got %d", len(c.data))
	}

	c.Set("c", "3")
	c.Set("d", "4")
	c.Set("e", "5")

	if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	c.Set("f", "6")

	if v, ok := c.Get("e"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	if v, ok := c.Get("a"); !ok {
		t.Error("unexpectedly got ok = false")
	} else if v != "1" {
		t.Errorf("expecting %q, got %q", "2", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	c.Set("g", "7")

	if v, ok := c.Get("a"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting empty string, got %q", v)
	} else if len(c.data) != 5 {
		t.Errorf("expecting cache length to be 5, got %d", len(c.data))
	}

	if c.Remove("a") {
		t.Error("unexpectedly got removed = true")
	}

	if !c.Remove("d") {
		t.Error("unexpectedly got removed = false")
	} else if _, ok := c.Get("d"); ok {
		t.Error("unexpectedly got ok = true")
	} else if len(c.data) != 4 {
		t.Errorf("expecting cache length to be 4, got %d", len(c.data))
	}
}

func TestCounts(t *testing.T) {
	c := NewLRU[string, string](5)

	if count := c.Count(); count != 0 {
		t.Errorf("expecting count %d, got %d", 0, count)
	}

	c.Set("", "")

	if count := c.Count(); count != 1 {
		t.Errorf("expecting count %d, got %d", 1, count)
	}

	c.Set("", "")

	if count := c.Count(); count != 1 {
		t.Errorf("expecting count %d, got %d", 1, count)
	}

	c.Set("1", "1")
	c.Set("2", "2")
	c.Set("3", "3")
	c.Set("4", "4")
	c.Set("5", "5")

	if count := c.Count(); count != 5 {
		t.Errorf("expecting count %d, got %d", 5, count)
	}
}

func TestClear(t *testing.T) {
	c := NewLRU[string, string](5)

	c.Set("a", "1")
	c.Set("b", "2")
	c.Set("c", "3")
	c.Set("d", "4")
	c.Set("e", "5")

	if count := c.Count(); count != 5 {
		t.Errorf("expecting count %d, got %d", 5, count)
	}

	c.Clear()

	if count := c.Count(); count != 0 {
		t.Errorf("expecting count %d, got %d", 0, count)
	}

	if v, ok := c.Get("b"); ok {
		t.Error("unexpectedly got ok = true")
	} else if v != "" {
		t.Errorf("expecting %q, got %q", "", v)
	}
}