Improved tests of base tile hash. They fail. TODO: Fix bugs in base tile hash.

This commit is contained in:
Sascha L. Teichmann 2016-05-11 11:39:50 +02:00
parent bae7e7c3e6
commit b257a60b2d

View File

@ -4,24 +4,116 @@
package common
import "testing"
import (
"math/rand"
"testing"
)
func TestBaseTileHash(t *testing.T) {
func randomBaseTileHash(updates int) *BaseTileHash {
bth := NewBaseTileHash(256)
h1 := []byte{1}
h2 := []byte{2}
for i := 0; i < 600; i++ {
x := i % 200
for j := 0; j < 600; j++ {
y := j % 200
for i := 0; i < updates; i++ {
x, y := rand.Intn(100), rand.Intn(100)
var h []byte
if j%2 == 0 {
if i%2 == 0 {
h = h1
} else {
h = h2
}
_ = bth.Update(x, y, h)
bth.Update(x, y, h)
}
return bth
}
func TestBaseTileHash(t *testing.T) {
for _, updates := range []int{10, 100, 1000, 10000} {
bth := randomBaseTileHash(updates)
entries := map[*btHashEntry]bool{}
for cur := bth.root.next; cur != &bth.root; cur = cur.next {
if entries[cur] {
t.Errorf("hash element found more than once: %d", updates)
}
entries[cur] = true
}
if len(entries) != len(bth.hashes) {
t.Errorf("List has differnt length than hashes: %d : %d",
len(entries), len(bth.hashes))
}
for k, v := range bth.hashes {
if !entries[v] {
t.Errorf("Hash contains pointer to element not being in list: %d",
updates)
}
if k != v.btKey {
t.Error("Key in entry does not match hash key: %d", updates)
}
delete(entries, v)
}
if len(entries) > 0 {
t.Error("There a are more entries than indexed by hash")
}
}
t.Logf("size: %d\n", len(bth.hashes))
}
func TestBaseTileHashOverwrite(t *testing.T) {
bth := NewBaseTileHash(256)
h1 := []byte{1}
h2 := []byte{2}
if updated := bth.Update(0, 0, h1); !updated {
t.Error("First insert does not trigger update")
}
if updated := bth.Update(0, 0, h2); !updated {
t.Error("Second insert does not trigger update")
}
if updated := bth.Update(0, 0, h2); updated {
t.Error("Third insert does trigger update")
}
}
func TestBaseTileHashSeparate(t *testing.T) {
bth := NewBaseTileHash(256)
h1 := []byte{1}
if updated := bth.Update(0, 0, h1); !updated {
t.Error("First insert does not trigger update")
}
if updated := bth.Update(0, 1, h1); !updated {
t.Error("Second insert does not trigger update")
}
if updated := bth.Update(1, 0, h1); !updated {
t.Error("Third insert does trigger update")
}
if len(bth.hashes) != 3 {
t.Errorf("Expected size to be 3. Current size: %d", len(bth.hashes))
}
}
func TestBaseTileHashLRU(t *testing.T) {
bth := NewBaseTileHash(2)
h1 := []byte{1}
if updated := bth.Update(0, 0, h1); !updated {
t.Error("First insert does not trigger update")
}
if updated := bth.Update(0, 1, h1); !updated {
t.Error("Second insert does not trigger update")
}
if updated := bth.Update(1, 0, h1); !updated {
t.Error("Third insert does trigger update")
}
if len(bth.hashes) != 2 {
t.Errorf("Expected size to be 2. Current size: %d", len(bth.hashes))
}
}