-
Notifications
You must be signed in to change notification settings - Fork 23
/
size_test.go
75 lines (72 loc) · 1.32 KB
/
size_test.go
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
package size
import (
"testing"
"github.com/golang/groupcache/lru"
)
func TestOf(t *testing.T) {
tests := []struct {
name string
v interface{}
want int
}{
{
name: "Array",
v: [3]int32{1, 2, 3}, // 3 * 4 = 12
want: 12,
},
{
name: "Slice",
v: make([]int64, 2, 5), // 5 * 8 + 24 = 64
want: 64,
},
{
name: "String",
v: "ABCdef", // 6 + 16 = 22
want: 22,
},
{
name: "Map",
// (8 + 3 + 16) + (8 + 4 + 16) = 55
// 55 + 8 + 10.79 * 2 = 84
v: map[int64]string{0: "ABC", 1: "DEFG"},
want: 84,
},
{
name: "Struct",
v: struct {
slice []int64
array [2]bool
structure struct {
i int8
s string
}
}{
slice: []int64{12345, 67890}, // 2 * 8 + 24 = 40
array: [2]bool{true, false}, // 2 * 1 = 2
structure: struct {
i int8
s string
}{
i: 5, // 1
s: "abc", // 3 * 1 + 16 = 19
}, // 20 + 7 (padding) = 27
}, // 40 + 2 + 27 = 69 + 6 (padding) = 75
want: 75,
},
{
name: "Struct With Func",
v: lru.Cache{
MaxEntries: 0, // 8
OnEvicted: nil, // 0
}, // + 16 (two more pointers) = 24
want: 24,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Of(tt.v); got != tt.want {
t.Errorf("Of() = %v, want %v", got, tt.want)
}
})
}
}