github.com/matrixorigin/matrixone@v1.2.0/pkg/container/batch/batch_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 batch
    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/stretchr/testify/require"
    24  )
    25  
    26  const (
    27  	Rows = 10 // default rows
    28  )
    29  
    30  // add unit tests for cases
    31  type batchTestCase struct {
    32  	bat   *Batch
    33  	types []types.Type
    34  }
    35  
    36  var (
    37  	tcs []batchTestCase
    38  )
    39  
    40  func init() {
    41  	tcs = []batchTestCase{
    42  		newTestCase([]types.Type{types.T_int8.ToType()}),
    43  	}
    44  }
    45  
    46  func TestBatchMarshalAndUnmarshal(t *testing.T) {
    47  	for _, tc := range tcs {
    48  		data, err := tc.bat.MarshalBinary()
    49  		require.NoError(t, err)
    50  
    51  		rbat := new(Batch)
    52  		err = rbat.UnmarshalBinary(data)
    53  		require.NoError(t, err)
    54  		for i, vec := range rbat.Vecs {
    55  			require.Equal(t, vector.MustFixedCol[int8](tc.bat.Vecs[i]), vector.MustFixedCol[int8](vec))
    56  		}
    57  	}
    58  }
    59  
    60  func TestBatch(t *testing.T) {
    61  	for _, tc := range tcs {
    62  		data, err := types.Encode(tc.bat)
    63  		require.NoError(t, err)
    64  		rbat := new(Batch)
    65  		err = types.Decode(data, rbat)
    66  		require.NoError(t, err)
    67  		for i, vec := range rbat.Vecs {
    68  			require.Equal(t, vector.MustFixedCol[int8](tc.bat.Vecs[i]), vector.MustFixedCol[int8](vec))
    69  		}
    70  	}
    71  }
    72  
    73  func TestBatchShrink(t *testing.T) {
    74  	bat := newBatch([]types.Type{types.T_int8.ToType()}, 4)
    75  	bat.Shrink([]int64{0}, true)
    76  	require.Equal(t, 3, bat.rowCount)
    77  	bat.Shrink([]int64{0, 2}, false)
    78  	require.Equal(t, 2, bat.rowCount)
    79  }
    80  
    81  func TestBatch_ReplaceVector(t *testing.T) {
    82  	v1, v2, v3 := &vector.Vector{}, &vector.Vector{}, &vector.Vector{}
    83  	bat := &Batch{
    84  		Vecs: []*vector.Vector{
    85  			v1,
    86  			v1,
    87  			v1,
    88  			v2,
    89  			v2,
    90  		},
    91  	}
    92  	bat.ReplaceVector(bat.Vecs[0], v3)
    93  	require.Equal(t, v3, bat.Vecs[0])
    94  	require.Equal(t, v3, bat.Vecs[1])
    95  	require.Equal(t, v3, bat.Vecs[2])
    96  	require.Equal(t, v2, bat.Vecs[3])
    97  }
    98  
    99  func newTestCase(ts []types.Type) batchTestCase {
   100  	return batchTestCase{
   101  		types: ts,
   102  		bat:   newBatch(ts, Rows),
   103  	}
   104  }
   105  
   106  // create a new block based on the type information, flgs[i] == ture: has null
   107  func newBatch(ts []types.Type, rows int) *Batch {
   108  	mp := mpool.MustNewZero()
   109  	bat := NewWithSize(len(ts))
   110  	bat.SetRowCount(rows)
   111  	for i, typ := range ts {
   112  		switch typ.Oid {
   113  		case types.T_int8:
   114  			vec := vector.NewVec(typ)
   115  			err := vec.PreExtend(rows, mp)
   116  			if err != nil {
   117  				panic(err)
   118  			}
   119  			vec.SetLength(rows)
   120  			vs := vector.MustFixedCol[int8](vec)
   121  			for j := range vs {
   122  				vs[j] = int8(j)
   123  			}
   124  			bat.Vecs[i] = vec
   125  		}
   126  	}
   127  	return bat
   128  }