2019-04-11 20:48:54 +00:00
|
|
|
package array2
|
2019-04-11 13:34:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
)
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
func testSet1(t *testing.T) {
|
2019-04-11 13:34:50 +00:00
|
|
|
arr := NewWithCap(4, 4)
|
|
|
|
l := []int{0, 6, 5, 15}
|
|
|
|
for _, v := range l {
|
|
|
|
arr.Set(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result string
|
|
|
|
result = spew.Sprint(arr.Values())
|
|
|
|
if result != "[0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("err == nil, but array the set is out of range")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
arr.Set(16, 16)
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
func testSet2(t *testing.T) {
|
|
|
|
arr := NewWithCap(3, 6)
|
|
|
|
l := []int{0, 6, 5, 15}
|
|
|
|
for _, v := range l {
|
|
|
|
arr.Set(v, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result string
|
|
|
|
result = spew.Sprint(arr.Values())
|
|
|
|
if result != "[0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("err == nil, but array the set is out of range")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
arr.Set(16, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArray2Set(t *testing.T) {
|
|
|
|
testSet1(t)
|
|
|
|
testSet2(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testArray2Get1(t *testing.T) {
|
2019-04-11 13:34:50 +00:00
|
|
|
arr := New()
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
arr.Set(i, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
if v, ok := arr.Get(i); ok {
|
|
|
|
if v != i {
|
|
|
|
t.Error("v is equal i, but", v, i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("not ok is error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("err == nil, but array the get is out of range")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
arr.Get(64)
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
func testArray2Get2(t *testing.T) {
|
|
|
|
arr := NewWithCap(9, 8)
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
arr.Set(i, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 64; i++ {
|
|
|
|
if v, ok := arr.Get(i); ok {
|
|
|
|
if v != i {
|
|
|
|
t.Error("v is equal i, but", v, i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("not ok is error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("err == nil, but array the get is out of range")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
arr.Get(72)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArray2Get(t *testing.T) {
|
|
|
|
testArray2Get1(t)
|
|
|
|
testArray2Get2(t)
|
|
|
|
}
|
|
|
|
|
2019-04-11 20:48:54 +00:00
|
|
|
func TestArray2Del(t *testing.T) {
|
2019-04-12 19:35:18 +00:00
|
|
|
arr := NewWithCap(3, 6)
|
2019-04-11 13:34:50 +00:00
|
|
|
l := []int{0, 6, 5, 15}
|
|
|
|
for _, v := range l {
|
|
|
|
arr.Set(v, v)
|
|
|
|
}
|
|
|
|
// default [0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]
|
|
|
|
var result string
|
|
|
|
|
|
|
|
arr.Del(0)
|
|
|
|
result = spew.Sprint(arr.Values())
|
2019-04-12 19:35:18 +00:00
|
|
|
if result != "[{} {} {} {} {} 5 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
|
|
|
|
t.Error(arr.data)
|
2019-04-11 13:34:50 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr.Del(5)
|
|
|
|
result = spew.Sprint(arr.Values())
|
2019-04-12 19:35:18 +00:00
|
|
|
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
|
|
|
|
t.Error(arr.data)
|
2019-04-11 13:34:50 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr.Del(6)
|
|
|
|
result = spew.Sprint(arr.Values())
|
2019-04-12 19:35:18 +00:00
|
|
|
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> {} {} {} 15 {} {}]" {
|
2019-04-11 13:34:50 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr.Del(15)
|
|
|
|
result = spew.Sprint(arr.Values())
|
2019-04-12 19:35:18 +00:00
|
|
|
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
|
2019-04-11 13:34:50 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == nil {
|
|
|
|
t.Error("err == nil, but array the del is out of range")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-12 19:35:18 +00:00
|
|
|
arr.Del(18)
|
2019-04-11 13:34:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 20:48:54 +00:00
|
|
|
func BenchmarkArray2Set(b *testing.B) {
|
2019-04-11 13:34:50 +00:00
|
|
|
|
|
|
|
arr := NewWithCap(1000, 100)
|
|
|
|
b.N = 500000000
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
var l []int
|
|
|
|
for i := 0; i < b.N/10; i++ {
|
|
|
|
l = append(l, randomdata.Number(0, 65535))
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
for c := 0; c < 10; c++ {
|
|
|
|
for i := 0; i < b.N/10; i++ {
|
|
|
|
arr.Set(l[i], i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-11 20:48:54 +00:00
|
|
|
func BenchmarkArray2Get(b *testing.B) {
|
2019-04-11 13:34:50 +00:00
|
|
|
|
|
|
|
arr := NewWithCap(1000, 100)
|
|
|
|
b.N = 500000000
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
for i := 0; i < 105535; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
arr.Set(v, v)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
arr.Get(i % 65535)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-11 20:48:54 +00:00
|
|
|
func BenchmarkArray2Del(b *testing.B) {
|
2019-04-11 13:34:50 +00:00
|
|
|
|
|
|
|
arr := NewWithCap(1000, 100)
|
|
|
|
b.N = 500000000
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
for i := 0; i < 105535; i++ {
|
|
|
|
v := randomdata.Number(0, 65535)
|
|
|
|
arr.Set(v, v)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
arr.Del(i % 65535)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|