blob: 8a2b80c433c8afbdac438786a5f7cd0c476d2cfc [file] [log] [blame]
Jan Schär75ea9f42024-07-29 17:01:41 +02001package cache
2
3// Taken and modified from CoreDNS, under Apache 2.0.
4
5import (
6 "testing"
7)
8
9func TestCacheAddAndGet(t *testing.T) {
10 const N = shardSize * 4
11 c := New[int](N)
12 c.Put(1, 1)
13
14 if _, found := c.Get(1); !found {
15 t.Fatal("Failed to find inserted record")
16 }
17}
18
19func TestCacheSharding(t *testing.T) {
20 c := New[int](shardSize)
21 for i := 0; i < shardSize*2; i++ {
22 c.Put(uint64(i), 1)
23 }
24 for i := range c.shards {
25 if len(c.shards[i].items) == 0 {
26 t.Errorf("Failed to populate shard: %d", i)
27 }
28 }
29}
30
31func BenchmarkCache(b *testing.B) {
32 b.ReportAllocs()
33
34 c := New[int](4)
35 for n := 0; n < b.N; n++ {
36 c.Put(1, 1)
37 c.Get(1)
38 }
39}