github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geopb/bbox_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package geopb
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestBoundingBoxIntersects(t *testing.T) {
    20  	testCases := []struct {
    21  		desc     string
    22  		a        *BoundingBox
    23  		b        *BoundingBox
    24  		expected bool
    25  	}{
    26  		{
    27  			desc:     "same bounding box intersects",
    28  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    29  			b:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    30  			expected: true,
    31  		},
    32  		{
    33  			desc:     "overlapping bounding box intersects",
    34  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    35  			b:        &BoundingBox{MinX: 0.5, MaxX: 1.5, MinY: 0.5, MaxY: 1.5},
    36  			expected: true,
    37  		},
    38  		{
    39  			desc:     "touching bounding box intersects",
    40  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    41  			b:        &BoundingBox{MinX: 1, MaxX: 2, MinY: 1, MaxY: 2},
    42  			expected: true,
    43  		},
    44  		{
    45  			desc:     "bounding box that is left does not intersect",
    46  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    47  			b:        &BoundingBox{MinX: 1.5, MaxX: 2, MinY: 0, MaxY: 1},
    48  			expected: false,
    49  		},
    50  		{
    51  			desc:     "higher bounding box does not intersect",
    52  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    53  			b:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 1.5, MaxY: 2},
    54  			expected: false,
    55  		},
    56  		{
    57  			desc:     "completely disjoint bounding box does not intersect",
    58  			a:        &BoundingBox{MinX: 0, MaxX: 1, MinY: 0, MaxY: 1},
    59  			b:        &BoundingBox{MinX: -3, MaxX: -2, MinY: 1.5, MaxY: 2},
    60  			expected: false,
    61  		},
    62  	}
    63  
    64  	for _, tc := range testCases {
    65  		t.Run(tc.desc, func(t *testing.T) {
    66  			require.Equal(t, tc.expected, tc.a.Intersects(tc.b))
    67  		})
    68  	}
    69  }