github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geomfn/de9im_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 geomfn
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/geo"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestRelate(t *testing.T) {
    22  	testCases := []struct {
    23  		a        *geo.Geometry
    24  		b        *geo.Geometry
    25  		expected string
    26  	}{
    27  		{leftRect, rightRect, "FF2F11212"},
    28  	}
    29  
    30  	for i, tc := range testCases {
    31  		t.Run(fmt.Sprintf("tc:%d", i), func(t *testing.T) {
    32  			ret, err := Relate(tc.a, tc.b)
    33  			require.NoError(t, err)
    34  			require.Equal(t, tc.expected, ret)
    35  		})
    36  	}
    37  }
    38  
    39  func TestMatchesDE9IM(t *testing.T) {
    40  	testCases := []struct {
    41  		str           string
    42  		pattern       string
    43  		expected      bool
    44  		expectedError string
    45  	}{
    46  		{"", "T**FF*FF*", false, `relation "" should be of length 9`},
    47  		{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`},
    48  		{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`},
    49  		{"000FFF000", "cTTFfFTTT", false, `unrecognized pattern character: c`},
    50  		{"120FFF021", "TTTFfFTTT", true, ""},
    51  		{"02FFFF000", "T**FfFTTT", true, ""},
    52  		{"020F1F010", "TTTFFFTtT", false, ""},
    53  		{"020FFF0f0", "TTTFFFTtT", false, ""},
    54  	}
    55  
    56  	for _, tc := range testCases {
    57  		t.Run(fmt.Sprintf("%s has pattern %s", tc.str, tc.pattern), func(t *testing.T) {
    58  			ret, err := MatchesDE9IM(tc.str, tc.pattern)
    59  			if tc.expectedError == "" {
    60  				require.NoError(t, err)
    61  				require.Equal(t, tc.expected, ret)
    62  			} else {
    63  				require.EqualError(t, err, tc.expectedError)
    64  			}
    65  		})
    66  	}
    67  
    68  	t.Run("errors if SRIDs mismatch", func(t *testing.T) {
    69  		_, err := Relate(mismatchingSRIDGeometryA, mismatchingSRIDGeometryB)
    70  		requireMismatchingSRIDError(t, err)
    71  	})
    72  }