From b257a60b2d29eeadc0e262026fa473ee16d4f014 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Wed, 11 May 2016 11:39:50 +0200 Subject: [PATCH] Improved tests of base tile hash. They fail. TODO: Fix bugs in base tile hash. --- common/basetilehash_test.go | 118 ++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 13 deletions(-) diff --git a/common/basetilehash_test.go b/common/basetilehash_test.go index 7aa89c6..6e7fd7e 100644 --- a/common/basetilehash_test.go +++ b/common/basetilehash_test.go @@ -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 - var h []byte - if j%2 == 0 { - h = h1 - } else { - h = h2 + for i := 0; i < updates; i++ { + x, y := rand.Intn(100), rand.Intn(100) + var h []byte + if i%2 == 0 { + h = h1 + } else { + h = h2 + } + 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) } - _ = bth.Update(x, y, h) + 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)) + } }