File size: 10,227 Bytes
197607f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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