github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/setalgebra/interval_test.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package setalgebra
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	"github.com/dolthub/dolt/go/store/types"
    23  )
    24  
    25  func testInterv(start, end *IntervalEndpoint) Interval {
    26  	return Interval{types.Format_Default, start, end}
    27  }
    28  
    29  func TestCompareIntervals(t *testing.T) {
    30  	tests := []struct {
    31  		name        string
    32  		in1         Interval
    33  		in2         Interval
    34  		expected    intervalComparison
    35  		revExpected intervalComparison
    36  		expectErr   bool
    37  	}{
    38  		{
    39  			"universal compare",
    40  			testInterv(nil, nil),
    41  			testInterv(nil, nil),
    42  			intervalComparison{0, -1, 1, 0},
    43  			intervalComparison{0, -1, 1, 0},
    44  			false,
    45  		},
    46  
    47  		{
    48  			"negative and positive infinite intervals meeting at open endpoint",
    49  			testInterv(nil, &IntervalEndpoint{types.Int(0), false}),
    50  			testInterv(&IntervalEndpoint{types.Int(0), false}, nil),
    51  			intervalComparison{-1, -1, -1, -1},
    52  			intervalComparison{1, 1, 1, 1},
    53  			false,
    54  		},
    55  
    56  		{
    57  			"negative and positive infinite intervals meeting at closed endpoint",
    58  			testInterv(nil, &IntervalEndpoint{types.Int(0), true}),
    59  			testInterv(&IntervalEndpoint{types.Int(0), true}, nil),
    60  			intervalComparison{-1, -1, 0, -1},
    61  			intervalComparison{1, 0, 1, 1},
    62  			false,
    63  		},
    64  
    65  		{
    66  			"overlapping",
    67  			testInterv(&IntervalEndpoint{types.Int(0), true}, &IntervalEndpoint{types.Int(6), true}),
    68  			testInterv(&IntervalEndpoint{types.Int(0), false}, &IntervalEndpoint{types.Int(3), true}),
    69  			intervalComparison{-1, -1, 1, 1},
    70  			intervalComparison{1, -1, 1, -1},
    71  			false,
    72  		},
    73  
    74  		{
    75  			"negative and positive infinite intervals meeting at open endpoint",
    76  			testInterv(&IntervalEndpoint{types.Int(0), true}, &IntervalEndpoint{types.Int(1), true}),
    77  			testInterv(&IntervalEndpoint{types.Int(2), true}, &IntervalEndpoint{types.Int(3), true}),
    78  			intervalComparison{-1, -1, -1, -1},
    79  			intervalComparison{1, 1, 1, 1},
    80  			false,
    81  		},
    82  
    83  		{
    84  			"equal open starting point",
    85  			testInterv(&IntervalEndpoint{types.Int(0), false}, &IntervalEndpoint{types.Int(1), true}),
    86  			testInterv(&IntervalEndpoint{types.Int(0), false}, &IntervalEndpoint{types.Int(3), true}),
    87  			intervalComparison{0, -1, 1, -1},
    88  			intervalComparison{0, -1, 1, 1},
    89  			false,
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		t.Run(test.name, func(t *testing.T) {
    95  			res, err := compareIntervals(test.in1, test.in2)
    96  			assertErr(t, test.expectErr, err)
    97  
    98  			assert.Equal(t, res, test.expected)
    99  
   100  			res, err = compareIntervals(test.in2, test.in1)
   101  			assertErr(t, test.expectErr, err)
   102  
   103  			assert.Equal(t, res, test.revExpected)
   104  		})
   105  	}
   106  }