File size: 2,510 Bytes
80b7320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package flatbuf

import (
	"testing"

	xs "github.com/ringsaturn/xiangshan/generated/xiangshan/v1"
	"github.com/ringsaturn/xiangshan/internal/types"
)

func TestEncodeDivisions(t *testing.T) {
	divs := []types.Division{{
		ID:         "area-1",
		Name:       "Test",
		Subtype:    types.SubtypeCountry,
		AdminLevel: -1,
		Country:    "XX",
		BBox:       types.BBox{Xmin: 0, Xmax: 1, Ymin: 0, Ymax: 1},
		Polygons: []types.Polygon{{
			Exterior: types.Ring{Coords: []float32{0, 0, 1, 0, 1, 1, 0, 1}},
		}},
	}}
	buf, err := EncodeDivisions(divs, map[[2]int16][]uint32{{0, 0}: {0}}, nil, "v", "s")
	if err != nil {
		t.Fatal(err)
	}
	if !xs.SizePrefixedDivisionsBufferHasIdentifier(buf) {
		t.Fatal("missing size-prefixed identifier")
	}
	root := xs.GetSizePrefixedRootAsDivisions(buf, 0)
	if root.ItemsLength() != 1 {
		t.Fatalf("ItemsLength = %d, want 1", root.ItemsLength())
	}
	if root.GridCoarse(nil).CellsLength() != 1 {
		t.Fatalf("GridCoarse cells = %d, want 1", root.GridCoarse(nil).CellsLength())
	}
	if root.CountryPreindex(nil).CellsLength() != 1 {
		t.Fatalf("CountryPreindex cells = %d, want 1", root.CountryPreindex(nil).CellsLength())
	}
}

func TestEncodeCompressedDivisionsFromXSFB(t *testing.T) {
	divs := []types.Division{{
		ID:         "area-1",
		Name:       "Test",
		Subtype:    types.SubtypeCountry,
		AdminLevel: -1,
		Country:    "XX",
		BBox:       types.BBox{Xmin: 0, Xmax: 1, Ymin: 0, Ymax: 1},
		Polygons: []types.Polygon{{
			Exterior: types.Ring{Coords: []float32{0, 0, 1, 0, 1, 1, 0, 1}},
		}},
	}}
	buf, err := EncodeDivisions(divs, map[[2]int16][]uint32{{0, 0}: {0}}, nil, "v", "s")
	if err != nil {
		t.Fatal(err)
	}
	root := xs.GetSizePrefixedRootAsDivisions(buf, 0)
	cbuf, stats, err := EncodeCompressedDivisionsFromXSFB(root)
	if err != nil {
		t.Fatal(err)
	}
	if !xs.SizePrefixedCompressedDivisionsBufferHasIdentifier(cbuf) {
		t.Fatal("missing compressed size-prefixed identifier")
	}
	if stats.Divisions != 1 || stats.Rings != 1 || stats.Points != 4 {
		t.Fatalf("stats = %+v, want one division, one ring, four points", stats)
	}
	croot := xs.GetSizePrefixedRootAsCompressedDivisions(cbuf, 0)
	if croot.ItemsLength() != 1 {
		t.Fatalf("ItemsLength = %d, want 1", croot.ItemsLength())
	}
	if croot.GridCoarse(nil).CellsLength() != 1 {
		t.Fatalf("GridCoarse cells = %d, want 1", croot.GridCoarse(nil).CellsLength())
	}
	if croot.CountryPreindex(nil).CellsLength() != 1 {
		t.Fatalf("CountryPreindex cells = %d, want 1", croot.CountryPreindex(nil).CellsLength())
	}
}