github.com/matrixorigin/matrixone@v1.2.0/pkg/compare/compare_test.go (about)

     1  // Copyright 2021 - 2022 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 compare
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  const (
    29  	Rows = 100
    30  )
    31  
    32  type testCase struct {
    33  	desc bool
    34  	proc *process.Process
    35  	vecs []*vector.Vector
    36  }
    37  
    38  var (
    39  	tcs []testCase
    40  )
    41  
    42  func init() {
    43  	mp := mpool.MustNewZero()
    44  	tcs = []testCase{
    45  		newTestCase(true, mp, types.New(types.T_bool, 0, 0)),
    46  		newTestCase(false, mp, types.New(types.T_bool, 0, 0)),
    47  
    48  		newTestCase(true, mp, types.New(types.T_bit, 0, 0)),
    49  		newTestCase(false, mp, types.New(types.T_bit, 0, 0)),
    50  
    51  		newTestCase(true, mp, types.New(types.T_int8, 0, 0)),
    52  		newTestCase(false, mp, types.New(types.T_int8, 0, 0)),
    53  		newTestCase(true, mp, types.New(types.T_int16, 0, 0)),
    54  		newTestCase(false, mp, types.New(types.T_int16, 0, 0)),
    55  		newTestCase(true, mp, types.New(types.T_int32, 0, 0)),
    56  		newTestCase(false, mp, types.New(types.T_int32, 0, 0)),
    57  		newTestCase(true, mp, types.New(types.T_int64, 0, 0)),
    58  		newTestCase(false, mp, types.New(types.T_int64, 0, 0)),
    59  
    60  		newTestCase(true, mp, types.New(types.T_uint8, 0, 0)),
    61  		newTestCase(false, mp, types.New(types.T_uint8, 0, 0)),
    62  		newTestCase(true, mp, types.New(types.T_uint16, 0, 0)),
    63  		newTestCase(false, mp, types.New(types.T_uint16, 0, 0)),
    64  		newTestCase(true, mp, types.New(types.T_uint32, 0, 0)),
    65  		newTestCase(false, mp, types.New(types.T_uint32, 0, 0)),
    66  		newTestCase(true, mp, types.New(types.T_uint64, 0, 0)),
    67  		newTestCase(false, mp, types.New(types.T_uint64, 0, 0)),
    68  
    69  		newTestCase(true, mp, types.New(types.T_float32, 0, 0)),
    70  		newTestCase(false, mp, types.New(types.T_float32, 0, 0)),
    71  
    72  		newTestCase(true, mp, types.New(types.T_float64, 0, 0)),
    73  		newTestCase(false, mp, types.New(types.T_float64, 0, 0)),
    74  
    75  		newTestCase(true, mp, types.New(types.T_date, 0, 0)),
    76  		newTestCase(false, mp, types.New(types.T_date, 0, 0)),
    77  
    78  		newTestCase(true, mp, types.New(types.T_time, 0, 0)),
    79  		newTestCase(false, mp, types.New(types.T_time, 0, 0)),
    80  
    81  		newTestCase(true, mp, types.New(types.T_datetime, 0, 0)),
    82  		newTestCase(false, mp, types.New(types.T_datetime, 0, 0)),
    83  
    84  		newTestCase(true, mp, types.New(types.T_timestamp, 0, 0)),
    85  		newTestCase(false, mp, types.New(types.T_timestamp, 0, 0)),
    86  
    87  		newTestCase(true, mp, types.New(types.T_decimal64, 0, 0)),
    88  		newTestCase(false, mp, types.New(types.T_decimal64, 0, 0)),
    89  
    90  		newTestCase(true, mp, types.New(types.T_decimal128, 0, 0)),
    91  		newTestCase(false, mp, types.New(types.T_decimal128, 0, 0)),
    92  
    93  		newTestCase(true, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0)),
    94  		newTestCase(false, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0)),
    95  
    96  		newTestCase(true, mp, types.New(types.T_blob, 0, 0)),
    97  		newTestCase(false, mp, types.New(types.T_blob, 0, 0)),
    98  
    99  		newTestCase(true, mp, types.New(types.T_text, 0, 0)),
   100  		newTestCase(false, mp, types.New(types.T_text, 0, 0)),
   101  
   102  		newTestCase(true, mp, types.New(types.T_array_float32, types.MaxArrayDimension, 0)),
   103  		newTestCase(false, mp, types.New(types.T_array_float32, types.MaxArrayDimension, 0)),
   104  
   105  		newTestCase(true, mp, types.New(types.T_array_float64, types.MaxArrayDimension, 0)),
   106  		newTestCase(false, mp, types.New(types.T_array_float64, types.MaxArrayDimension, 0)),
   107  	}
   108  }
   109  
   110  func TestCompare(t *testing.T) {
   111  	for _, tc := range tcs {
   112  		nb0 := tc.proc.Mp().CurrNB()
   113  		c := New(*tc.vecs[0].GetType(), tc.desc, false)
   114  		c.Set(0, tc.vecs[0])
   115  		c.Set(1, tc.vecs[1])
   116  		err := c.Copy(0, 1, 0, 0, tc.proc)
   117  		require.NoError(t, err)
   118  		c.Compare(0, 1, 0, 0)
   119  		nb1 := tc.proc.Mp().CurrNB()
   120  		require.Equal(t, nb0, nb1)
   121  		// XXX MPOOL
   122  		// tv.vecs[0].Free modifies tc.proc.Mp()
   123  		tc.vecs[0].Free(tc.proc.Mp())
   124  		tc.vecs[1].Free(tc.proc.Mp())
   125  	}
   126  }
   127  
   128  func newTestCase(desc bool, m *mpool.MPool, typ types.Type) testCase {
   129  	vecs := make([]*vector.Vector, 2)
   130  	vecs[0] = testutil.NewVector(Rows, typ, m, true, nil)
   131  	vecs[1] = testutil.NewVector(Rows, typ, m, true, nil)
   132  	return testCase{
   133  		desc: desc,
   134  		vecs: vecs,
   135  		proc: testutil.NewProcessWithMPool(m),
   136  	}
   137  }