added coverage
This commit is contained in:
parent
2bd1b29128
commit
c0836dd1ac
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
tile38-*
|
tile38-*
|
||||||
!cmd/tile38-*
|
!cmd/tile38-*
|
||||||
data*/
|
data*/
|
||||||
|
coverage.out
|
2
Makefile
2
Makefile
@ -5,6 +5,8 @@ clean:
|
|||||||
rm -f tile38-cli
|
rm -f tile38-cli
|
||||||
test:
|
test:
|
||||||
@./build.sh test
|
@./build.sh test
|
||||||
|
cover:
|
||||||
|
@./build.sh cover
|
||||||
install: all
|
install: all
|
||||||
cp tile38-server /usr/local/bin
|
cp tile38-server /usr/local/bin
|
||||||
cp tile38-cli /usr/local/bin
|
cp tile38-cli /usr/local/bin
|
||||||
|
11
build.sh
11
build.sh
@ -108,3 +108,14 @@ if [ "$1" == "test" ]; then
|
|||||||
go test $(go list ./... | grep -v /vendor/)
|
go test $(go list ./... | grep -v /vendor/)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# cover if requested
|
||||||
|
if [ "$1" == "cover" ]; then
|
||||||
|
$OD/tile38-server -p 9876 -d "$TMP" -q &
|
||||||
|
PID=$!
|
||||||
|
function testend {
|
||||||
|
kill $PID &
|
||||||
|
}
|
||||||
|
trap testend EXIT
|
||||||
|
go test -cover $(go list ./... | grep -v /vendor/)
|
||||||
|
fi
|
||||||
|
|
||||||
|
@ -201,8 +201,6 @@ func (ix *Index) Search(cursor uint64, swLat, swLon, neLat, neLon float64, itera
|
|||||||
idm[iitm] = true
|
idm[iitm] = true
|
||||||
active = iterator(iitm)
|
active = iterator(iitm)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
active = iterator(iitm)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
idx++
|
idx++
|
||||||
@ -217,12 +215,14 @@ func (ix *Index) Search(cursor uint64, swLat, swLon, neLat, neLon float64, itera
|
|||||||
if idx >= cursor {
|
if idx >= cursor {
|
||||||
iitm := ix.getRTreeItem(item)
|
iitm := ix.getRTreeItem(item)
|
||||||
if iitm != nil {
|
if iitm != nil {
|
||||||
if !idm[iitm] {
|
if ix.mulm[iitm] {
|
||||||
idm[iitm] = true
|
if !idm[iitm] {
|
||||||
|
idm[iitm] = true
|
||||||
|
active = iterator(iitm)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
active = iterator(iitm)
|
active = iterator(iitm)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
active = iterator(iitm)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
idx++
|
idx++
|
||||||
|
@ -13,11 +13,13 @@ func randf(min, max float64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func randPoint() (lat float64, lon float64) {
|
func randPoint() (lat float64, lon float64) {
|
||||||
return randf(-90, 90), randf(-180, 180)
|
// intentionally go out of range.
|
||||||
|
return randf(-100, 100), randf(-190, 190)
|
||||||
}
|
}
|
||||||
|
|
||||||
func randRect() (swLat, swLon, neLat, neLon float64) {
|
func randRect() (swLat, swLon, neLat, neLon float64) {
|
||||||
swLat, swLon = randPoint()
|
swLat, swLon = randPoint()
|
||||||
|
// intentionally go out of range even more.
|
||||||
neLat = randf(swLat-10, swLat+10)
|
neLat = randf(swLat-10, swLat+10)
|
||||||
neLon = randf(swLon-10, swLon+10)
|
neLon = randf(swLon-10, swLon+10)
|
||||||
return
|
return
|
||||||
@ -33,8 +35,8 @@ func wp(swLat, swLon, neLat, neLon float64) *FlexItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRandomInserts(t *testing.T) {
|
func TestRandomInserts(t *testing.T) {
|
||||||
rand.Seed(0) //time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
l := 1000000
|
l := 200000
|
||||||
tr := New()
|
tr := New()
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
i := 0
|
i := 0
|
||||||
@ -50,30 +52,86 @@ func TestRandomInserts(t *testing.T) {
|
|||||||
tr.Insert(wp(swLat, swLon, neLat, neLon))
|
tr.Insert(wp(swLat, swLon, neLat, neLon))
|
||||||
}
|
}
|
||||||
insrdur := time.Now().Sub(start)
|
insrdur := time.Now().Sub(start)
|
||||||
|
count := 0
|
||||||
|
|
||||||
count := tr.Count()
|
count = tr.Count()
|
||||||
if count != l {
|
if count != l {
|
||||||
t.Fatalf("count == %d, expect %d", count, l)
|
t.Fatalf("count == %d, expect %d", count, l)
|
||||||
}
|
}
|
||||||
count = 0
|
count = 0
|
||||||
|
items := make([]Item, 0, l)
|
||||||
tr.Search(0, -90, -180, 90, 180, func(item Item) bool {
|
tr.Search(0, -90, -180, 90, 180, func(item Item) bool {
|
||||||
count++
|
count++
|
||||||
|
items = append(items, item)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if count != l {
|
if count != l {
|
||||||
t.Fatalf("count == %d, expect %d", count, l)
|
t.Fatalf("count == %d, expect %d", count, l)
|
||||||
}
|
}
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
count = 0
|
count1 := 0
|
||||||
tr.Search(0, 33, -115, 34, -114, func(item Item) bool {
|
tr.Search(0, 33, -115, 34, -114, func(item Item) bool {
|
||||||
count++
|
count1++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
searchdur := time.Now().Sub(start)
|
searchdur1 := time.Now().Sub(start)
|
||||||
|
|
||||||
|
start = time.Now()
|
||||||
|
count2 := 0
|
||||||
|
|
||||||
|
tr.Search(0, 33-180, -115-360, 34-180, -114-360, func(item Item) bool {
|
||||||
|
count2++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
searchdur2 := time.Now().Sub(start)
|
||||||
|
|
||||||
|
start = time.Now()
|
||||||
|
count3 := 0
|
||||||
|
tr.Search(0, -10, 170, 20, 200, func(item Item) bool {
|
||||||
|
count3++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
searchdur3 := time.Now().Sub(start)
|
||||||
|
|
||||||
fmt.Printf("Randomly inserted %d points in %s.\n", l/2, inspdur.String())
|
fmt.Printf("Randomly inserted %d points in %s.\n", l/2, inspdur.String())
|
||||||
fmt.Printf("Randomly inserted %d rects in %s.\n", l/2, insrdur.String())
|
fmt.Printf("Randomly inserted %d rects in %s.\n", l/2, insrdur.String())
|
||||||
fmt.Printf("Searched %d items in %s.\n", count, searchdur.String())
|
fmt.Printf("Searched %d items in %s.\n", count1, searchdur1.String())
|
||||||
|
fmt.Printf("Searched %d items in %s.\n", count2, searchdur2.String())
|
||||||
|
fmt.Printf("Searched %d items in %s.\n", count3, searchdur3.String())
|
||||||
|
|
||||||
|
tr.Search(0, -10, 170, 20, 200, func(item Item) bool {
|
||||||
|
lat1, lon1, lat2, lon2 := item.Rect()
|
||||||
|
if lat1 == lat2 && lon1 == lon2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
tr.Search(0, -10, 170, 20, 200, func(item Item) bool {
|
||||||
|
lat1, lon1, lat2, lon2 := item.Rect()
|
||||||
|
if lat1 != lat2 || lon1 != lon2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
// Remove all of the elements
|
||||||
|
for _, item := range items {
|
||||||
|
tr.Remove(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
count = tr.Count()
|
||||||
|
if count != 0 {
|
||||||
|
t.Fatalf("count == %d, expect %d", count, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr.RemoveAll()
|
||||||
|
if tr.getQTreeItem(nil) != nil {
|
||||||
|
t.Fatal("getQTreeItem(nil) should return nil")
|
||||||
|
}
|
||||||
|
if tr.getRTreeItem(nil) != nil {
|
||||||
|
t.Fatal("getRTreeItem(nil) should return nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMemory(t *testing.T) {
|
func TestMemory(t *testing.T) {
|
||||||
|
@ -189,6 +189,9 @@ func insertRectRec(rect rectT, item Item, node *nodeT, newNode **nodeT, level in
|
|||||||
var branch branchT
|
var branch branchT
|
||||||
var otherNode *nodeT
|
var otherNode *nodeT
|
||||||
// Still above level for insertion, go down tree recursively
|
// Still above level for insertion, go down tree recursively
|
||||||
|
if node == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if node.level > level {
|
if node.level > level {
|
||||||
index = pickBranch(rect, node)
|
index = pickBranch(rect, node)
|
||||||
if !insertRectRec(rect, item, node.branch[index].child, &otherNode, level) {
|
if !insertRectRec(rect, item, node.branch[index].child, &otherNode, level) {
|
||||||
@ -198,6 +201,9 @@ func insertRectRec(rect rectT, item Item, node *nodeT, newNode **nodeT, level in
|
|||||||
} // Child was split
|
} // Child was split
|
||||||
node.branch[index].rect = nodeCover(node.branch[index].child)
|
node.branch[index].rect = nodeCover(node.branch[index].child)
|
||||||
branch.child = otherNode
|
branch.child = otherNode
|
||||||
|
if branch.child == nil {
|
||||||
|
println(">> child assigned is nil")
|
||||||
|
}
|
||||||
branch.rect = nodeCover(otherNode)
|
branch.rect = nodeCover(otherNode)
|
||||||
return addBranch(&branch, node, newNode)
|
return addBranch(&branch, node, newNode)
|
||||||
} else if node.level == level { // Have reached level for insertion. Add rect, split if necessary
|
} else if node.level == level { // Have reached level for insertion. Add rect, split if necessary
|
||||||
@ -221,14 +227,23 @@ func insertRect(rect rectT, item Item, root **nodeT, level int) bool {
|
|||||||
var newRoot *nodeT
|
var newRoot *nodeT
|
||||||
var newNode *nodeT
|
var newNode *nodeT
|
||||||
var branch branchT
|
var branch branchT
|
||||||
|
if *root == nil {
|
||||||
|
println(">> root is nil")
|
||||||
|
}
|
||||||
if insertRectRec(rect, item, *root, &newNode, level) { // Root split
|
if insertRectRec(rect, item, *root, &newNode, level) { // Root split
|
||||||
newRoot = &nodeT{} // Grow tree taller and new root
|
newRoot = &nodeT{} // Grow tree taller and new root
|
||||||
newRoot.level = (*root).level + 1
|
newRoot.level = (*root).level + 1
|
||||||
branch.rect = nodeCover(*root)
|
branch.rect = nodeCover(*root)
|
||||||
branch.child = *root
|
branch.child = *root
|
||||||
|
if branch.child == nil {
|
||||||
|
println(">> child assigned is nil 2")
|
||||||
|
}
|
||||||
addBranch(&branch, newRoot, nil)
|
addBranch(&branch, newRoot, nil)
|
||||||
branch.rect = nodeCover(newNode)
|
branch.rect = nodeCover(newNode)
|
||||||
branch.child = newNode
|
branch.child = newNode
|
||||||
|
if branch.child == nil {
|
||||||
|
println(">> child assigned is nil 3")
|
||||||
|
}
|
||||||
addBranch(&branch, newRoot, nil)
|
addBranch(&branch, newRoot, nil)
|
||||||
*root = newRoot
|
*root = newRoot
|
||||||
return true
|
return true
|
||||||
@ -549,6 +564,9 @@ func removeRect(rect rectT, item Item, root **nodeT) bool {
|
|||||||
// merges branches on the way back up.
|
// merges branches on the way back up.
|
||||||
// Returns 1 if record not found, 0 if success.
|
// Returns 1 if record not found, 0 if success.
|
||||||
func removeRectRec(rect rectT, item Item, node *nodeT, listNode **listNodeT) bool {
|
func removeRectRec(rect rectT, item Item, node *nodeT, listNode **listNodeT) bool {
|
||||||
|
if node == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if node.isInternalNode() { // not a leaf node
|
if node.isInternalNode() { // not a leaf node
|
||||||
for index := 0; index < node.count; index++ {
|
for index := 0; index < node.count; index++ {
|
||||||
if overlap(rect, node.branch[index].rect) {
|
if overlap(rect, node.branch[index].rect) {
|
||||||
@ -599,6 +617,9 @@ func reInsert(node *nodeT, listNode **listNodeT) {
|
|||||||
|
|
||||||
// Search in an index tree or subtree for all data retangles that overlap the argument rectangle.
|
// Search in an index tree or subtree for all data retangles that overlap the argument rectangle.
|
||||||
func search(node *nodeT, rect rectT, iterator func(item Item) bool) bool {
|
func search(node *nodeT, rect rectT, iterator func(item Item) bool) bool {
|
||||||
|
if node == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if node.isInternalNode() { // This is an internal node in the tree
|
if node.isInternalNode() { // This is an internal node in the tree
|
||||||
for index := 0; index < node.count; index++ {
|
for index := 0; index < node.count; index++ {
|
||||||
nrect := node.branch[index].rect
|
nrect := node.branch[index].rect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user