AdithyaSK's picture
AdithyaSK HF Staff
Add files using upload-large-folder tool
197607f verified
Raw History Blame Contribute Delete
10.2 kB
diff --git a/lib/lru/synthesis_lru_contract_test.go b/lib/lru/synthesis_lru_contract_test.go
new file mode 100644
index 00000000..27fc3fc9
--- /dev/null
+++ b/lib/lru/synthesis_lru_contract_test.go
@@ -0,0 +1,298 @@
+package lru_test
+
+import (
+ "reflect"
+ "testing"
+
+ "awesome-golang-algorithm/lib/lru"
+)
+
+type synthesisStringValue string
+
+func (v synthesisStringValue) Len() int { return len(v) }
+
+func synthesisBytes(t *testing.T, c *lru.Cache) int64 {
+ t.Helper()
+ v, ok := any(c).(interface{ Bytes() int64 })
+ if !ok {
+ t.Fatal("Cache must expose Bytes() int64")
+ }
+ return v.Bytes()
+}
+
+func synthesisRemove(t *testing.T, c *lru.Cache, key string) (lru.Value, bool) {
+ t.Helper()
+ v, ok := any(c).(interface {
+ Remove(string) (lru.Value, bool)
+ })
+ if !ok {
+ t.Fatal("Cache must expose Remove(string) (Value, bool)")
+ }
+ return v.Remove(key)
+}
+
+func synthesisClear(t *testing.T, c *lru.Cache) {
+ t.Helper()
+ v, ok := any(c).(interface{ Clear() })
+ if !ok {
+ t.Fatal("Cache must expose Clear()")
+ }
+ v.Clear()
+}
+
+func synthesisPeek(t *testing.T, c *lru.Cache, key string) (lru.Value, bool) {
+ t.Helper()
+ v, ok := any(c).(interface {
+ Peek(string) (lru.Value, bool)
+ })
+ if !ok {
+ t.Fatal("Cache must expose Peek(string) (Value, bool)")
+ }
+ return v.Peek(key)
+}
+
+func synthesisKeys(t *testing.T, c *lru.Cache) []string {
+ t.Helper()
+ v, ok := any(c).(interface{ Keys() []string })
+ if !ok {
+ t.Fatal("Cache must expose Keys() []string")
+ }
+ return v.Keys()
+}
+
+func synthesisResize(t *testing.T, c *lru.Cache, maxBytes int64) {
+ t.Helper()
+ v, ok := any(c).(interface{ Resize(int64) })
+ if !ok {
+ t.Fatal("Cache must expose Resize(int64)")
+ }
+ v.Resize(maxBytes)
+}
+
+func synthesisStats(t *testing.T, c *lru.Cache) (uint64, uint64) {
+ t.Helper()
+ method := reflect.ValueOf(c).MethodByName("Stats")
+ if !method.IsValid() || method.Type().NumIn() != 0 || method.Type().NumOut() != 1 {
+ t.Fatal("Cache must expose Stats() Stats")
+ }
+ result := method.Call(nil)[0]
+ if result.Kind() != reflect.Struct {
+ t.Fatalf("Stats result has kind %s, want struct", result.Kind())
+ }
+ hits, misses := result.FieldByName("Hits"), result.FieldByName("Misses")
+ if !hits.IsValid() || !misses.IsValid() || hits.Kind() != reflect.Uint64 || misses.Kind() != reflect.Uint64 {
+ t.Fatal("Stats must expose uint64 fields Hits and Misses")
+ }
+ return hits.Uint(), misses.Uint()
+}
+
+func synthesisResetStats(t *testing.T, c *lru.Cache) {
+ t.Helper()
+ v, ok := any(c).(interface{ ResetStats() })
+ if !ok {
+ t.Fatal("Cache must expose ResetStats()")
+ }
+ v.ResetStats()
+}
+
+func TestSynthesisReplacementAccountingAndEviction(t *testing.T) {
+ var evicted []string
+ c := lru.New(10, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("a", synthesisStringValue("1234"))
+ c.Add("b", synthesisStringValue("1234"))
+ if got := synthesisBytes(t, c); got != 10 {
+ t.Fatalf("initial byte count = %d, want 10", got)
+ }
+
+ c.Add("a", synthesisStringValue("123456"))
+ if got := synthesisBytes(t, c); got != 7 {
+ t.Fatalf("byte count after replacement = %d, want 7", got)
+ }
+ if _, ok := c.Get("a"); !ok {
+ t.Fatal("updated key a should remain present")
+ }
+ if _, ok := c.Get("b"); ok {
+ t.Fatal("least-recently-used key b should be evicted")
+ }
+ if !reflect.DeepEqual(evicted, []string{"b"}) {
+ t.Fatalf("evictions = %v, want [b]", evicted)
+ }
+
+ var replacementCallbacks []string
+ unlimited := lru.New(0, func(key string, _ lru.Value) {
+ replacementCallbacks = append(replacementCallbacks, key)
+ })
+ unlimited.Add("key", synthesisStringValue("12345"))
+ unlimited.Add("key", synthesisStringValue("1"))
+ if got := synthesisBytes(t, unlimited); got != 4 {
+ t.Fatalf("byte count after smaller replacement = %d, want 4", got)
+ }
+ if len(replacementCallbacks) != 0 {
+ t.Fatalf("replacement invoked eviction callback for %v", replacementCallbacks)
+ }
+}
+
+func TestSynthesisOversizedEntryIsImmediatelyEvicted(t *testing.T) {
+ var evicted []string
+ c := lru.New(5, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("long", synthesisStringValue("12"))
+ if c.Len() != 0 || synthesisBytes(t, c) != 0 {
+ t.Fatalf("oversized entry left cache state len=%d bytes=%d", c.Len(), synthesisBytes(t, c))
+ }
+ if _, ok := c.Get("long"); ok {
+ t.Fatal("oversized entry should not be retrievable")
+ }
+ if !reflect.DeepEqual(evicted, []string{"long"}) {
+ t.Fatalf("evictions = %v, want [long]", evicted)
+ }
+}
+
+func TestSynthesisRemoveCallbackAndAccounting(t *testing.T) {
+ var evicted []string
+ c := lru.New(0, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("a", synthesisStringValue("1"))
+ c.Add("b", synthesisStringValue("2"))
+ c.Add("c", synthesisStringValue("3"))
+ if value, ok := synthesisRemove(t, c, "c"); !ok || value != synthesisStringValue("3") {
+ t.Fatalf("Remove(c) = (%v, %v), want (3, true)", value, ok)
+ }
+ if got := synthesisBytes(t, c); got != 4 {
+ t.Fatalf("after Remove(c), bytes = %d, want 4", got)
+ }
+ if value, ok := synthesisRemove(t, c, "missing"); ok || value != nil {
+ t.Fatalf("Remove(missing) = (%v, %v), want (nil, false)", value, ok)
+ }
+ if !reflect.DeepEqual(evicted, []string{"c"}) {
+ t.Fatalf("callbacks = %v, want [c]", evicted)
+ }
+}
+
+func TestSynthesisClearCallbacksOldestFirst(t *testing.T) {
+ var evicted []string
+ c := lru.New(0, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("a", synthesisStringValue("1"))
+ c.Add("b", synthesisStringValue("2"))
+ c.Add("c", synthesisStringValue("3"))
+ if _, ok := c.Get("a"); !ok {
+ t.Fatal("expected cache hit for a")
+ }
+ synthesisClear(t, c)
+ if c.Len() != 0 || synthesisBytes(t, c) != 0 {
+ t.Fatalf("Clear left cache state len=%d bytes=%d", c.Len(), synthesisBytes(t, c))
+ }
+ if !reflect.DeepEqual(evicted, []string{"b", "c", "a"}) {
+ t.Fatalf("callbacks = %v, want [b c a]", evicted)
+ }
+}
+
+func TestSynthesisPeekDoesNotAffectOrderOrStats(t *testing.T) {
+ c := lru.New(0, nil)
+ c.Add("x", synthesisStringValue("one"))
+ c.Add("y", synthesisStringValue("two"))
+ if value, ok := synthesisPeek(t, c, "x"); !ok || value != synthesisStringValue("one") {
+ t.Fatalf("Peek(x) = (%v, %v), want (one, true)", value, ok)
+ }
+ if _, ok := synthesisPeek(t, c, "missing"); ok {
+ t.Fatal("Peek(missing) should report false")
+ }
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"y", "x"}) {
+ t.Fatalf("Peek changed order: Keys = %v", got)
+ }
+ hits, misses := synthesisStats(t, c)
+ if hits != 0 || misses != 0 {
+ t.Fatalf("Peek changed Stats to (%d, %d), want (0, 0)", hits, misses)
+ }
+}
+
+func TestSynthesisKeysReflectGetRecency(t *testing.T) {
+ c := lru.New(0, nil)
+ c.Add("x", synthesisStringValue("one"))
+ c.Add("y", synthesisStringValue("two"))
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"y", "x"}) {
+ t.Fatalf("initial Keys = %v, want [y x]", got)
+ }
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"y", "x"}) {
+ t.Fatalf("repeated Keys changed order: got %v, want [y x]", got)
+ }
+ if _, ok := c.Get("x"); !ok {
+ t.Fatal("Get(x) should hit")
+ }
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"x", "y"}) {
+ t.Fatalf("Get did not promote x: Keys = %v", got)
+ }
+}
+
+func TestSynthesisStatsAndReset(t *testing.T) {
+ c := lru.New(0, nil)
+ c.Add("x", synthesisStringValue("one"))
+ if _, ok := c.Get("x"); !ok {
+ t.Fatal("Get(x) should hit")
+ }
+ if _, ok := c.Get("missing"); ok {
+ t.Fatal("Get(missing) should miss")
+ }
+ hits, misses := synthesisStats(t, c)
+ if hits != 1 || misses != 1 {
+ t.Fatalf("Stats = (%d, %d), want one hit and one miss", hits, misses)
+ }
+ synthesisResetStats(t, c)
+ if hits, misses = synthesisStats(t, c); hits != 0 || misses != 0 {
+ t.Fatalf("ResetStats left counters = (%d, %d)", hits, misses)
+ }
+ if c.Len() != 1 {
+ t.Fatalf("ResetStats changed cache length to %d, want 1", c.Len())
+ }
+}
+
+func TestSynthesisResizeEvictsOldestAndZeroIsUnlimited(t *testing.T) {
+ var evicted []string
+ c := lru.New(0, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("a", synthesisStringValue("11"))
+ c.Add("b", synthesisStringValue("22"))
+ c.Add("c", synthesisStringValue("33"))
+ if _, ok := c.Get("a"); !ok {
+ t.Fatal("expected cache hit for a")
+ }
+ synthesisResize(t, c, 6)
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"a", "c"}) {
+ t.Fatalf("after Resize(6), Keys = %v, want [a c]", got)
+ }
+ if got := synthesisBytes(t, c); got != 6 {
+ t.Fatalf("after Resize(6), bytes = %d, want 6", got)
+ }
+ if !reflect.DeepEqual(evicted, []string{"b"}) {
+ t.Fatalf("Resize callback keys = %v, want [b]", evicted)
+ }
+ synthesisResize(t, c, 3)
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"a"}) {
+ t.Fatalf("after Resize(3), Keys = %v, want [a]", got)
+ }
+ if !reflect.DeepEqual(evicted, []string{"b", "c"}) {
+ t.Fatalf("second Resize callback keys = %v, want [b c]", evicted)
+ }
+ synthesisResize(t, c, 0)
+ c.Add("d", synthesisStringValue("44"))
+ if got := synthesisKeys(t, c); !reflect.DeepEqual(got, []string{"d", "a"}) {
+ t.Fatalf("zero-capacity cache Keys = %v, want [d a]", got)
+ }
+}
+
+func TestSynthesisExistingRemoveOldestCompatibility(t *testing.T) {
+ var evicted []string
+ c := lru.New(0, func(key string, _ lru.Value) { evicted = append(evicted, key) })
+ c.Add("a", synthesisStringValue("1"))
+ c.Add("b", synthesisStringValue("2"))
+ if _, ok := c.Get("a"); !ok {
+ t.Fatal("expected cache hit for a")
+ }
+ c.RemoveOldest()
+ if _, ok := c.Get("b"); ok {
+ t.Fatal("RemoveOldest should remove least-recently-used key b")
+ }
+ if _, ok := c.Get("a"); !ok || c.Len() != 1 {
+ t.Fatalf("RemoveOldest should retain a as the sole entry, len=%d", c.Len())
+ }
+ if !reflect.DeepEqual(evicted, []string{"b"}) {
+ t.Fatalf("RemoveOldest callback keys = %v, want [b]", evicted)
+ }
+}
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+set -euo pipefail
+
+
+go test ./lib/lru