-
Notifications
You must be signed in to change notification settings - Fork 80
/
ref_counter_test.go
68 lines (50 loc) · 1.29 KB
/
ref_counter_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
package vm
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestRefCounter_Add(t *testing.T) {
r := newRefCounter()
require.Equal(t, 0, int(*r))
r.Add(stackitem.Null{})
require.Equal(t, 1, int(*r))
r.Add(stackitem.Null{})
require.Equal(t, 2, int(*r)) // count scalar items twice
arr := stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{1}), stackitem.NewBool(false)})
r.Add(arr)
require.Equal(t, 5, int(*r)) // array + 2 elements
r.Add(arr)
require.Equal(t, 6, int(*r)) // count only array
r.Remove(arr)
require.Equal(t, 5, int(*r))
r.Remove(arr)
require.Equal(t, 2, int(*r))
m := stackitem.NewMap()
m.Add(stackitem.NewByteArray([]byte("some")), stackitem.NewBool(false))
r.Add(m)
require.Equal(t, 5, int(*r)) // map + key + value
r.Add(m)
require.Equal(t, 6, int(*r)) // map only
r.Remove(m)
require.Equal(t, 5, int(*r))
r.Remove(m)
require.Equal(t, 2, int(*r))
}
func BenchmarkRefCounter_Add(b *testing.B) {
a := stackitem.NewArray(nil)
rc := newRefCounter()
b.ResetTimer()
for range b.N {
rc.Add(a)
}
}
func BenchmarkRefCounter_AddRemove(b *testing.B) {
a := stackitem.NewArray([]stackitem.Item{})
rc := newRefCounter()
b.ResetTimer()
for range b.N {
rc.Add(a)
rc.Remove(a)
}
}