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
|
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package lazy
import (
"errors"
"testing"
)
func TestGMap(t *testing.T) {
var gm GMap[string, int]
n := int(testing.AllocsPerRun(1000, func() {
got := gm.Get("42", fortyTwo)
if got != 42 {
t.Fatalf("got %v; want 42", got)
}
}))
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
}
}
func TestGMapErr(t *testing.T) {
var gm GMap[string, int]
n := int(testing.AllocsPerRun(1000, func() {
got, err := gm.GetErr("42", func() (int, error) {
return 42, nil
})
if got != 42 || err != nil {
t.Fatalf("got %v, %v; want 42, nil", got, err)
}
}))
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
}
var gmErr GMap[string, int]
wantErr := errors.New("test error")
n = int(testing.AllocsPerRun(1000, func() {
got, err := gmErr.GetErr("42", func() (int, error) {
return 0, wantErr
})
if got != 0 || err != wantErr {
t.Fatalf("got %v, %v; want 0, %v", got, err, wantErr)
}
}))
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
}
}
func TestGMapSet(t *testing.T) {
var gm GMap[string, int]
if !gm.Set("42", 42) {
t.Fatalf("Set failed")
}
if gm.Set("42", 43) {
t.Fatalf("Set succeeded after first Set")
}
n := int(testing.AllocsPerRun(1000, func() {
got := gm.Get("42", fortyTwo)
if got != 42 {
t.Fatalf("got %v; want 42", got)
}
}))
if n != 0 {
t.Errorf("allocs = %v; want 0", n)
}
}
func TestGMapMustSet(t *testing.T) {
var gm GMap[string, int]
gm.MustSet("42", 42)
defer func() {
if e := recover(); e == nil {
t.Errorf("unexpected success; want panic")
}
}()
gm.MustSet("42", 43)
}
func TestGMapRecursivePanic(t *testing.T) {
defer func() {
if e := recover(); e != nil {
t.Logf("got panic, as expected")
} else {
t.Errorf("unexpected success; want panic")
}
}()
gm := GMap[string, int]{}
gm.Get("42", func() int {
return gm.Get("42", func() int { return 42 })
})
}
|