aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/geojson/bbox_test.go
blob: 695320d7cede00b6653fac7d7e7be1763d0c125f (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package geojson

import (
	"reflect"
	"testing"

	"github.com/paulmach/orb"
)

func TestBBox(t *testing.T) {
	ls := orb.LineString{{1, 3}, {0, 4}}
	b := ls.Bound()

	bbox := NewBBox(b)
	expected := BBox{0, 3, 1, 4}
	if !reflect.DeepEqual(bbox, expected) {
		t.Errorf("incorrect result: %v != %v", bbox, expected)
	}
}

func TestBBoxValid(t *testing.T) {
	cases := []struct {
		name   string
		bbox   BBox
		result bool
	}{
		{
			name:   "true for 4 length array",
			bbox:   []float64{1, 2, 3, 4},
			result: true,
		},
		{
			name:   "true for 3d box",
			bbox:   []float64{1, 2, 3, 4, 5, 6},
			result: true,
		},
		{
			name:   "false for nil box",
			bbox:   nil,
			result: false,
		},
		{
			name:   "false for short array",
			bbox:   []float64{1, 2, 3},
			result: false,
		},
		{
			name:   "false for incorrect length array",
			bbox:   []float64{1, 2, 3, 4, 5},
			result: false,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if v := tc.bbox.Valid(); v != tc.result {
				t.Errorf("incorrect result: %v != %v", v, tc.result)
			}
		})
	}
}

func TestBBoxBound(t *testing.T) {
	cases := []struct {
		name   string
		bbox   BBox
		result orb.Bound
	}{
		{
			name:   "empty for invalid bbox",
			bbox:   []float64{1, 2, 3},
			result: orb.Bound{},
		},
		{
			name:   "correct order for 2d box",
			bbox:   []float64{1, 2, 3, 4},
			result: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{3, 4}},
		},
		{
			name:   "correct order for 3d box",
			bbox:   []float64{1, 2, 3, 4, 5, 6},
			result: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{4, 5}},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if v := tc.bbox.Bound(); !v.Equal(tc.result) {
				t.Errorf("incorrect result: %v != %v", v, tc.result)
			}
		})
	}

}