imposm3/cache/binary/serialize_test.go

128 lines
2.7 KiB
Go
Raw Normal View History

2013-02-12 22:45:49 +04:00
package binary
import (
2014-08-04 17:19:35 +04:00
"github.com/omniscale/imposm3/element"
2013-02-12 22:45:49 +04:00
"testing"
)
func compareRefs(a []int64, b []int64) bool {
2013-02-12 22:45:49 +04:00
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func TestMarshalNode(t *testing.T) {
node := &element.Node{}
node.Id = 12345
node.Tags = make(element.Tags)
node.Tags["name"] = "test"
node.Tags["place"] = "city"
data, _ := MarshalNode(node)
node, _ = UnmarshalNode(data)
if node.Tags["name"] != "test" {
t.Error("name tag does not match")
}
if node.Tags["place"] != "city" {
t.Error("place tag does not match")
}
if len(node.Tags) != 2 {
t.Error("tags len does not match")
}
}
func TestMarshalWay(t *testing.T) {
way := &element.Way{}
way.Id = 12345
way.Tags = make(element.Tags)
way.Tags["name"] = "test"
way.Tags["highway"] = "trunk"
2013-04-24 00:30:41 +04:00
way.Refs = append(way.Refs, 1, 2, 3, 4)
2013-02-12 22:45:49 +04:00
data, _ := MarshalWay(way)
way, _ = UnmarshalWay(data)
if way.Tags["name"] != "test" {
t.Error("name tag does not match")
}
if way.Tags["highway"] != "trunk" {
t.Error("highway tag does not match")
}
if len(way.Tags) != 2 {
t.Error("tags len does not match")
}
if !compareRefs(way.Refs, []int64{1, 2, 3, 4}) {
2013-02-12 22:45:49 +04:00
t.Error("nodes do not match")
}
}
2013-05-13 16:24:57 +04:00
2013-05-15 16:32:13 +04:00
func TestMarshalRelation(t *testing.T) {
rel := &element.Relation{}
rel.Id = 12345
rel.Tags = make(element.Tags)
rel.Tags["name"] = "test"
rel.Tags["landusage"] = "forest"
2013-05-17 17:33:36 +04:00
rel.Members = append(rel.Members, element.Member{123, element.WAY, "outer", nil})
rel.Members = append(rel.Members, element.Member{124, element.WAY, "inner", nil})
2013-05-15 16:32:13 +04:00
data, _ := MarshalRelation(rel)
rel, _ = UnmarshalRelation(data)
if rel.Tags["name"] != "test" {
t.Error("name tag does not match")
}
if rel.Tags["landusage"] != "forest" {
t.Error("landusage tag does not match")
}
if len(rel.Tags) != 2 {
t.Error("tags len does not match")
}
if len(rel.Members) != 2 {
t.Error("members len does not match")
}
if rel.Members[0].Id != 123 || rel.Members[0].Type != element.WAY || rel.Members[0].Role != "outer" {
t.Error("members do not match", rel.Members[0])
}
if rel.Members[1].Id != 124 || rel.Members[1].Type != element.WAY || rel.Members[1].Role != "inner" {
t.Error("members do not match", rel.Members[1])
}
}
2013-05-13 16:24:57 +04:00
func TestDeltaPack(t *testing.T) {
ids := []int64{1000, 999, 1001, -8, 1234}
deltaPack(ids)
for i, id := range []int64{1000, -1, 2, -1009, 1242} {
if ids[i] != id {
t.Fatal(ids[i], id, ids)
}
}
}
func TestDeltaUnpack(t *testing.T) {
ids := []int64{1000, -1, 2, -1009, 1242}
deltaUnpack(ids)
for i, id := range []int64{1000, 999, 1001, -8, 1234} {
if ids[i] != id {
t.Fatal(ids[i], id, ids)
}
}
}