Debian Codegraph @

policy/simple/example_test.go

raw symbols SCIP
1 package simple_test
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/Code-Hex/go-generics-cache/policy/simple"
8 )
9
10 func ExampleNewCache() {
11 c := simple.NewCache[string, int]()
12 c.Set("a", 1)
13 c.Set("b", 2)
14 av, aok := c.Get("a")
15 bv, bok := c.Get("b")
16 cv, cok := c.Get("c")
17 fmt.Println(av, aok)
18 fmt.Println(bv, bok)
19 fmt.Println(cv, cok)
20 c.Delete("a")
21 _, aok2 := c.Get("a")
22 if !aok2 {
23 fmt.Println("key 'a' has been deleted")
24 }
25 // update
26 c.Set("b", 3)
27 newbv, _ := c.Get("b")
28 fmt.Println(newbv)
29 // Output:
30 // 1 true
31 // 2 true
32 // 0 false
33 // key 'a' has been deleted
34 // 3
35 }
36
37 func ExampleCache_Keys() {
38 c := simple.NewCache[string, int]()
39 c.Set("foo", 1)
40 c.Set("bar", 2)
41 c.Set("baz", 3)
42 keys := c.Keys()
43 for _, key := range keys {
44 fmt.Println(key)
45 }
46 // Output:
47 // foo
48 // bar
49 // baz
50 }
51
52 func ExampleCache_Len() {
53 c := simple.NewCache[string, int]()
54 c.Set("foo", 1)
55 c.Set("bar", 2)
56 c.Set("baz", 3)
57 len := c.Len()
58 fmt.Println(len)
59 // Output:
60 // 3
61 }
62
63 func BenchmarkLenWithKeys(b *testing.B) {
64 c := simple.NewCache[string, int]()
65 c.Set("foo", 1)
66 c.Set("bar", 2)
67 c.Set("baz", 3)
68 for i := 0; i < b.N; i++ {
69 var _ = len(c.Keys())
70 }
71 }
72
73 func BenchmarkJustLen(b *testing.B) {
74 c := simple.NewCache[string, int]()
75 c.Set("foo", 1)
76 c.Set("bar", 2)
77 c.Set("baz", 3)
78 for i := 0; i < b.N; i++ {
79 var _ = c.Len()
80 }
81 }