From a01281a52631c2670d198ca4ffaf8e0dfa1259e9 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Fri, 4 May 2018 11:42:35 -0700 Subject: [PATCH] Add tests for the new CIRCLE subcommand. --- pkg/geojson/circle_test.go | 42 ++++++++++++++++++++++++++++++++++++++ tests/keys_search.go | 35 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 pkg/geojson/circle_test.go diff --git a/pkg/geojson/circle_test.go b/pkg/geojson/circle_test.go new file mode 100644 index 00000000..8d693c25 --- /dev/null +++ b/pkg/geojson/circle_test.go @@ -0,0 +1,42 @@ +package geojson + +import ( + "testing" +) + +func TestSegmentIntersectsCirclePointsInside(t *testing.T) { + // either start or end is within the circle + start := Position{X: -122.4408378, Y: 37.7341129, Z: 0} + end := Position{X: -122.4408378, Y: 37.733, Z: 0} + center := Position{X: -122.4409, Y: 37.734, Z: 0} + meters := 30.0 + if !SegmentIntersectsCircle(start, end, center, meters) { + t.Fatal("!") + } + center = Position{X: -122.4409, Y: 37.733, Z: 0} + if !SegmentIntersectsCircle(start, end, center, meters) { + t.Fatal("!") + } +} + +func TestSegmentIntersectsCirclePointsOutside(t *testing.T) { + // neither start nor end are within the circle, but the segment intersects it + start := Position{X: -122.4408378, Y: 37.7341129, Z: 0} + end := Position{X: -122.4408378, Y: 37.733, Z: 0} + center := Position{X: -122.4412, Y: 37.7335, Z: 0} + meters := 70.0 + if !SegmentIntersectsCircle(start, end, center, meters) { + t.Fatal("!") + } +} + +func TestSegmentIntersectsCircleLineButNotSegment(t *testing.T) { + // the line of the segment intersects the circle, but the segment does not + start := Position{X: -122.4408378, Y: 37.7341129, Z: 0} + end := Position{X: -122.4408378, Y: 37.733, Z: 0} + center := Position{X: -122.4412, Y: 37.737, Z: 0} + meters := 70.0 + if SegmentIntersectsCircle(start, end, center, meters) { + t.Fatal("!") + } +} diff --git a/tests/keys_search.go b/tests/keys_search.go index c7c64a82..eaf84a0c 100644 --- a/tests/keys_search.go +++ b/tests/keys_search.go @@ -4,6 +4,8 @@ import "testing" func subTestSearch(t *testing.T, mc *mockServer) { runStep(t, mc, "KNN", keys_KNN_test) + runStep(t, mc, "WITHIN_CIRCLE", keys_WITHIN_CIRCLE_test) + runStep(t, mc, "INTERSECTS_CIRCLE", keys_INTERSECTS_CIRCLEtest) } func keys_KNN_test(mc *mockServer) error { @@ -23,3 +25,36 @@ func keys_KNN_test(mc *mockServer) error { "]]"}, }) } + + +func keys_WITHIN_CIRCLE_test(mc *mockServer) error { + return mc.DoBatch([][]interface{}{ + {"SET", "mykey", "1", "POINT", 37.7335, -122.4412}, {"OK"}, + {"SET", "mykey", "2", "POINT", 37.7335, -122.44121}, {"OK"}, + {"SET", "mykey", "3", "OBJECT", `{"type":"LineString","coordinates":[[-122.4408378,37.7341129],[-122.4408378,37.733]]}`}, {"OK"}, + {"SET", "mykey", "4", "OBJECT", `{"type":"Polygon","coordinates":[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]]]}`}, {"OK"}, + {"SET", "mykey", "5", "OBJECT", `{"type":"MultiPolygon","coordinates":[[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]]]]}`}, {"OK"}, + {"SET", "mykey", "6", "POINT", -5, 5}, {"OK"}, + {"SET", "mykey", "7", "POINT", 33, 21}, {"OK"}, + {"WITHIN", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 1000}, { + "[0 [1 2 3 4 5]]"}, + {"WITHIN", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 10}, { + "[0 [1 2]]"}, + }) +} + +func keys_INTERSECTS_CIRCLEtest(mc *mockServer) error { + return mc.DoBatch([][]interface{}{ + {"SET", "mykey", "1", "POINT", 37.7335, -122.4412}, {"OK"}, + {"SET", "mykey", "2", "POINT", 37.7335, -122.44121}, {"OK"}, + {"SET", "mykey", "3", "OBJECT", `{"type":"LineString","coordinates":[[-122.4408378,37.7341129],[-122.4408378,37.733]]}`}, {"OK"}, + {"SET", "mykey", "4", "OBJECT", `{"type":"Polygon","coordinates":[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]]]}`}, {"OK"}, + {"SET", "mykey", "5", "OBJECT", `{"type":"MultiPolygon","coordinates":[[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]]]]}`}, {"OK"}, + {"SET", "mykey", "6", "POINT", -5, 5}, {"OK"}, + {"SET", "mykey", "7", "POINT", 33, 21}, {"OK"}, + {"INTERSECTS", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 70}, { + "[0 [1 2 3 4 5]]"}, + {"INTERSECTS", "mykey", "IDS", "CIRCLE", 37.7335, -122.4412, 10}, { + "[0 [1 2]]"}, + }) +}