add test for DeltaaCoordsCache; fixed missing inserted nodes
parent
828f96e1d7
commit
a7f58cbfe1
|
@ -135,6 +135,8 @@ func (self *DeltaCoordsCache) FillWay(way *element.Way) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// PutCoords puts nodes into cache.
|
||||
// nodes need to be sorted by Id.
|
||||
func (self *DeltaCoordsCache) PutCoords(nodes []element.Node) {
|
||||
var start, currentBunchId int64
|
||||
currentBunchId = getBunchId(nodes[0].Id)
|
||||
|
@ -143,7 +145,8 @@ func (self *DeltaCoordsCache) PutCoords(nodes []element.Node) {
|
|||
bunchId := getBunchId(node.Id)
|
||||
if bunchId != currentBunchId {
|
||||
bunch := self.getBunch(currentBunchId)
|
||||
bunch.coords = append(bunch.coords, nodes[start:i-1]...)
|
||||
bunch.coords = append(bunch.coords, nodes[start:i]...)
|
||||
// make sure our coords are sorted
|
||||
sort.Sort(Nodes(bunch.coords))
|
||||
currentBunchId = bunchId
|
||||
start = int64(i)
|
||||
|
@ -153,7 +156,9 @@ func (self *DeltaCoordsCache) PutCoords(nodes []element.Node) {
|
|||
}
|
||||
bunch := self.getBunch(currentBunchId)
|
||||
bunch.coords = append(bunch.coords, nodes[start:]...)
|
||||
// make sure our coords are sorted
|
||||
sort.Sort(Nodes(bunch.coords))
|
||||
|
||||
bunch.needsWrite = true
|
||||
bunch.Unlock()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"goposm/element"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mknode(id int64) element.Node {
|
||||
return element.Node{
|
||||
OSMElem: element.OSMElem{
|
||||
Id: id,
|
||||
},
|
||||
Long: 8,
|
||||
Lat: 10,
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadWriteDeltaCoords(t *testing.T) {
|
||||
cache_dir, _ := ioutil.TempDir("", "goposm_test")
|
||||
defer os.RemoveAll(cache_dir)
|
||||
|
||||
cache, err := NewDeltaCoordsCache(cache_dir)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
// create list with nodes from Id 0->999 in random order
|
||||
nodeIds := rand.Perm(1000)
|
||||
nodes := make([]element.Node, 1000)
|
||||
for i := 0; i < len(nodes); i++ {
|
||||
nodes[i] = mknode(int64(nodeIds[i]))
|
||||
}
|
||||
|
||||
// add nodes in batches of ten
|
||||
for i := 0; i <= len(nodes)-10; i = i + 10 {
|
||||
// sort each batch as required by PutCoords
|
||||
sort.Sort(Nodes(nodes[i : i+10]))
|
||||
cache.PutCoords(nodes[i : i+10])
|
||||
}
|
||||
|
||||
cache.Close()
|
||||
|
||||
cache, err = NewDeltaCoordsCache(cache_dir)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
defer cache.Close()
|
||||
|
||||
for i := 0; i < len(nodes); i++ {
|
||||
data, ok := cache.GetCoord(int64(i))
|
||||
if !ok {
|
||||
t.Fatal("missing coord:", i)
|
||||
}
|
||||
if data.Id != int64(i) {
|
||||
t.Errorf("unexpected result of GetNode: %v", data)
|
||||
}
|
||||
}
|
||||
|
||||
_, ok := cache.GetCoord(999999)
|
||||
if ok {
|
||||
t.Error("missing node not nil")
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue