2020-05-12 13:05:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-05-12 13:05:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRefCounter_Add(t *testing.T) {
|
|
|
|
r := newRefCounter()
|
|
|
|
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 0, int(*r))
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
r.Add(stackitem.Null{})
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 1, int(*r))
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
r.Add(stackitem.Null{})
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 2, int(*r)) // count scalar items twice
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
arr := stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{1}), stackitem.NewBool(false)})
|
2020-05-12 13:05:10 +00:00
|
|
|
r.Add(arr)
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 5, int(*r)) // array + 2 elements
|
2020-05-12 13:05:10 +00:00
|
|
|
|
|
|
|
r.Add(arr)
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 6, int(*r)) // count only array
|
2020-05-12 13:05:10 +00:00
|
|
|
|
|
|
|
r.Remove(arr)
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 5, int(*r))
|
2020-05-12 13:05:10 +00:00
|
|
|
|
|
|
|
r.Remove(arr)
|
2021-08-11 12:16:29 +00:00
|
|
|
require.Equal(t, 2, int(*r))
|
2022-05-16 13:07:25 +00:00
|
|
|
|
|
|
|
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))
|
2021-08-11 12:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRefCounter_Add(b *testing.B) {
|
|
|
|
a := stackitem.NewArray(nil)
|
|
|
|
rc := newRefCounter()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2024-08-30 18:41:02 +00:00
|
|
|
for range b.N {
|
2021-08-11 12:16:29 +00:00
|
|
|
rc.Add(a)
|
|
|
|
}
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
2022-08-23 06:34:00 +00:00
|
|
|
|
|
|
|
func BenchmarkRefCounter_AddRemove(b *testing.B) {
|
|
|
|
a := stackitem.NewArray([]stackitem.Item{})
|
|
|
|
rc := newRefCounter()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2024-08-30 18:41:02 +00:00
|
|
|
for range b.N {
|
2022-08-23 06:34:00 +00:00
|
|
|
rc.Add(a)
|
|
|
|
rc.Remove(a)
|
|
|
|
}
|
|
|
|
}
|