focus/sparse_array/array2/array2_test.go
eson 368447c31d new file: .gitignore
new file:   LICENSE
new file:   README.md
new file:   compare/compare.go
new file:   for_test.go
new file:   heap/heap.go
new file:   heap/heap_test.go
new file:   interface.go
new file:   lastack/lastack.go
new file:   lastack/lastack_test.go
new file:   list/list.go
new file:   list/list_test.go
new file:   map/hashmap/hashmap.go
new file:   map/hashmap/hashmap_test.go
new file:   priority_list/iterator.go
new file:   priority_list/priority_list.go
new file:   priority_list/priority_list_test.go
new file:   priority_queue/iterator.go
new file:   priority_queue/priority_queue.go
new file:   priority_queue/priority_queue_test.go
new file:   priority_queue/vbt.go
new file:   priority_queue/vbt_test.go
new file:   priority_queuekey/iterator.go
new file:   priority_queuekey/priority_queuekey.go
new file:   priority_queuekey/priority_queuekey_test.go
new file:   priority_queuekey/vbt.go
new file:   priority_queuekey/vbt_test.go
new file:   set/hashset/hashset.go
new file:   set/hashset/hashset_test.go
new file:   set/treeset/treeset.go
new file:   set/treeset/treeset_test.go
new file:   sparse_array/array2/array2.go
new file:   sparse_array/array2/array2_test.go
new file:   sparse_array/array3/array3.go
new file:   sparse_array/array3/array3_test.go
new file:   sparse_array/arrayn/arrayn.go
new file:   sparse_array/arrayn/arrayn_test.go
new file:   stack/stack.go
new file:   stack/stack_test.go
new file:   tree/avl/avl.go
new file:   tree/avl/avl_test.go
new file:   tree/avl/iterator.go
new file:   tree/avldup/avldup.go
new file:   tree/avldup/avldup_test.go
new file:   tree/avldup/iterator.go
new file:   tree/avlkey/avlkey.go
new file:   tree/avlkey/avlkey_test.go
new file:   tree/avlkey/iterator.go
new file:   tree/avlkeydup/avlkeydup.go
new file:   tree/avlkeydup/avlkeydup_test.go
new file:   tree/avlkeydup/iterator.go
new file:   tree/vbt/iterator.go
new file:   tree/vbt/vbt.go
new file:   tree/vbt/vbt_test.go
new file:   tree/vbtkey/iterator.go
new file:   tree/vbtkey/vbtkey.go
new file:   tree/vbtkey/vbtkey_test.go
2019-05-08 10:42:51 +08:00

249 lines
4.3 KiB
Go

package array2
import (
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/Pallinder/go-randomdata"
)
func testSet1(t *testing.T) {
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.debugValues())
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 testSet2(t *testing.T) {
arr := NewWithCap(5, 3)
l := []int{0, 6, 5, 14}
for _, v := range l {
arr.Set(v, v)
}
var result string
result = spew.Sprint(arr.debugValues())
if result != "[0 {} {} {} {} 5 6 {} {} <nil> <nil> <nil> {} {} 14]" {
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) {
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)
}
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)
}
func TestArray2Del(t *testing.T) {
arr := NewWithCap(3, 6)
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.debugValues())
if result != "[{} {} {} {} {} 5 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
t.Error(arr.data)
t.Error(result)
}
arr.Del(5)
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> 6 {} {} {} {} {} {} {} {} 15 {} {}]" {
t.Error(arr.data)
t.Error(result)
}
arr.Del(6)
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> {} {} {} 15 {} {}]" {
t.Error(result)
}
arr.Del(15)
result = spew.Sprint(arr.debugValues())
if result != "[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]" {
t.Error(result)
}
defer func() {
if err := recover(); err == nil {
t.Error("err == nil, but array the del is out of range")
}
}()
arr.Del(18)
}
func TestArray2Grow(t *testing.T) {
arr := NewWithCap(4, 4)
l := []int{0, 6, 5, 15}
for _, v := range l {
arr.Set(v, v)
}
arr.Grow(1)
if v, ok := arr.Get(15); ok {
if v != 15 {
t.Error(v)
}
} else {
t.Error(v)
}
arr.Set(19, 19)
if v, ok := arr.Get(19); ok {
if v != 19 {
t.Error(v)
}
} else {
t.Error(v)
}
arr.Grow(-1)
var result string
result = spew.Sprint(arr.debugValues())
if result != "[0 {} {} {} {} 5 6 {} <nil> <nil> <nil> <nil> {} {} {} 15]" {
t.Error(result)
}
}
func BenchmarkArray2Set(b *testing.B) {
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)
}
}
}
func BenchmarkArray2Get(b *testing.B) {
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)
}
}
func BenchmarkArray2Del(b *testing.B) {
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)
}
}