github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/common/intervals_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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 common
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestMerge1(t *testing.T) {
    25  	defer testutils.AfterTest(t)()
    26  	i1 := &ClosedIntervals{
    27  		Intervals: []*ClosedInterval{
    28  			{Start: 1, End: 3},
    29  			{Start: 5, End: 5},
    30  			{Start: 11, End: 12},
    31  		},
    32  	}
    33  	i2 := &ClosedIntervals{
    34  		Intervals: []*ClosedInterval{
    35  			{Start: 2, End: 2},
    36  			{Start: 4, End: 4},
    37  			{Start: 6, End: 6},
    38  			{Start: 8, End: 13},
    39  		},
    40  	}
    41  	i2bk := &ClosedIntervals{
    42  		Intervals: []*ClosedInterval{
    43  			{Start: 2, End: 2},
    44  			{Start: 4, End: 4},
    45  			{Start: 6, End: 6},
    46  			{Start: 8, End: 13},
    47  		},
    48  	}
    49  	i1.TryMerge(*i2)
    50  	res := &ClosedIntervals{
    51  		Intervals: []*ClosedInterval{
    52  			{Start: 1, End: 6},
    53  			{Start: 8, End: 13},
    54  		},
    55  	}
    56  	assert.True(t, i1.Equal(res))
    57  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
    58  }
    59  
    60  func TestMerge2(t *testing.T) {
    61  	defer testutils.AfterTest(t)()
    62  	i1 := &ClosedIntervals{
    63  		Intervals: []*ClosedInterval{},
    64  	}
    65  	i2 := &ClosedIntervals{
    66  		Intervals: []*ClosedInterval{
    67  			{Start: 2, End: 2},
    68  			{Start: 4, End: 4},
    69  			{Start: 6, End: 6},
    70  			{Start: 8, End: 13},
    71  		},
    72  	}
    73  	i2bk := &ClosedIntervals{
    74  		Intervals: []*ClosedInterval{
    75  			{Start: 2, End: 2},
    76  			{Start: 4, End: 4},
    77  			{Start: 6, End: 6},
    78  			{Start: 8, End: 13},
    79  		},
    80  	}
    81  	i1.TryMerge(*i2)
    82  	res := &ClosedIntervals{
    83  		Intervals: []*ClosedInterval{
    84  			{Start: 2, End: 2},
    85  			{Start: 4, End: 4},
    86  			{Start: 6, End: 6},
    87  			{Start: 8, End: 13},
    88  		},
    89  	}
    90  	assert.True(t, i1.Equal(res))
    91  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
    92  }
    93  
    94  func TestMerge3(t *testing.T) {
    95  	defer testutils.AfterTest(t)()
    96  	i1 := &ClosedIntervals{
    97  		Intervals: []*ClosedInterval{
    98  			{Start: 2, End: 2},
    99  			{Start: 4, End: 4},
   100  			{Start: 6, End: 6},
   101  			{Start: 8, End: 13},
   102  		},
   103  	}
   104  	i2 := &ClosedIntervals{
   105  		Intervals: []*ClosedInterval{},
   106  	}
   107  	i2bk := &ClosedIntervals{
   108  		Intervals: []*ClosedInterval{},
   109  	}
   110  	i1.TryMerge(*i2)
   111  	res := &ClosedIntervals{
   112  		Intervals: []*ClosedInterval{
   113  			{Start: 2, End: 2},
   114  			{Start: 4, End: 4},
   115  			{Start: 6, End: 6},
   116  			{Start: 8, End: 13},
   117  		},
   118  	}
   119  	assert.True(t, i1.Equal(res))
   120  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
   121  }
   122  func TestContains1(t *testing.T) {
   123  	defer testutils.AfterTest(t)()
   124  	i1 := &ClosedIntervals{
   125  		Intervals: []*ClosedInterval{
   126  			{Start: 2, End: 2},
   127  			{Start: 4, End: 4},
   128  			{Start: 6, End: 6},
   129  			{Start: 8, End: 13},
   130  		},
   131  	}
   132  	i2 := &ClosedIntervals{
   133  		Intervals: []*ClosedInterval{},
   134  	}
   135  	i2bk := &ClosedIntervals{
   136  		Intervals: []*ClosedInterval{},
   137  	}
   138  	assert.True(t, i1.Contains(*i2))
   139  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
   140  }
   141  func TestContains2(t *testing.T) {
   142  	defer testutils.AfterTest(t)()
   143  	i1 := &ClosedIntervals{
   144  		Intervals: []*ClosedInterval{
   145  			{Start: 2, End: 2},
   146  			{Start: 4, End: 4},
   147  			{Start: 6, End: 6},
   148  			{Start: 8, End: 13},
   149  		},
   150  	}
   151  	i2 := &ClosedIntervals{
   152  		Intervals: []*ClosedInterval{
   153  			{Start: 2, End: 2},
   154  			{Start: 10, End: 13},
   155  		},
   156  	}
   157  	i2bk := &ClosedIntervals{
   158  		Intervals: []*ClosedInterval{
   159  			{Start: 2, End: 2},
   160  			{Start: 10, End: 13},
   161  		},
   162  	}
   163  	assert.True(t, i1.Contains(*i2))
   164  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
   165  }
   166  func TestContains3(t *testing.T) {
   167  	defer testutils.AfterTest(t)()
   168  	i1 := &ClosedIntervals{
   169  		Intervals: []*ClosedInterval{},
   170  	}
   171  	i2 := &ClosedIntervals{
   172  		Intervals: []*ClosedInterval{
   173  			{Start: 6, End: 6},
   174  			{Start: 8, End: 13},
   175  		},
   176  	}
   177  	i2bk := &ClosedIntervals{
   178  		Intervals: []*ClosedInterval{
   179  			{Start: 6, End: 6},
   180  			{Start: 8, End: 13},
   181  		},
   182  	}
   183  	assert.False(t, i1.Contains(*i2))
   184  	assert.True(t, i2.Equal(i2bk)) //i2 should remain the same
   185  }
   186  
   187  func TestNewBySlice(t *testing.T) {
   188  	defer testutils.AfterTest(t)()
   189  	i := NewClosedIntervalsBySlice([]uint64{})
   190  	t.Log(i)
   191  	assert.Equal(t, 0, len(i.Intervals))
   192  	i = NewClosedIntervalsBySlice([]uint64{1})
   193  	t.Log(i)
   194  	assert.Equal(t, 1, len(i.Intervals))
   195  	i = NewClosedIntervalsBySlice([]uint64{1, 2})
   196  	t.Log(i)
   197  	assert.Equal(t, 1, len(i.Intervals))
   198  	i = NewClosedIntervalsBySlice([]uint64{2, 1})
   199  	t.Log(i)
   200  	assert.Equal(t, 1, len(i.Intervals))
   201  	i = NewClosedIntervalsBySlice([]uint64{1, 2, 3})
   202  	t.Log(i)
   203  	assert.Equal(t, 1, len(i.Intervals))
   204  	i = NewClosedIntervalsBySlice([]uint64{1, 3, 2})
   205  	t.Log(i)
   206  	assert.Equal(t, 1, len(i.Intervals))
   207  	i = NewClosedIntervalsBySlice([]uint64{1, 3, 4})
   208  	t.Log(i)
   209  	assert.Equal(t, 2, len(i.Intervals))
   210  	i = NewClosedIntervalsBySlice([]uint64{1, 4, 3})
   211  	t.Log(i)
   212  	assert.Equal(t, 2, len(i.Intervals))
   213  	i = NewClosedIntervalsBySlice([]uint64{1, 4, 3, 3, 2, 5, 3, 4, 6, 10, 7, 4567, 654, 67, 78, 67, 68})
   214  	t.Log(i)
   215  	assert.Equal(t, 6, len(i.Intervals))
   216  }