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