github.com/matrixorigin/matrixone@v0.7.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/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  const (
    26  	Rows = 10 // default rows
    27  )
    28  
    29  // add unit tests for cases
    30  type batchTestCase struct {
    31  	bat   *Batch
    32  	types []types.Type
    33  }
    34  
    35  var (
    36  	tcs []batchTestCase
    37  )
    38  
    39  func init() {
    40  	tcs = []batchTestCase{
    41  		newTestCase([]types.Type{{Oid: types.T_int8}}),
    42  	}
    43  }
    44  
    45  func TestBatch(t *testing.T) {
    46  	for _, tc := range tcs {
    47  		data, err := types.Encode(tc.bat)
    48  		require.NoError(t, err)
    49  		rbat := new(Batch)
    50  		err = types.Decode(data, rbat)
    51  		require.NoError(t, err)
    52  		for i, vec := range rbat.Vecs {
    53  			require.Equal(t, tc.bat.Vecs[i].Col, vec.Col)
    54  		}
    55  	}
    56  }
    57  
    58  func newTestCase(ts []types.Type) batchTestCase {
    59  	return batchTestCase{
    60  		types: ts,
    61  		bat:   newBatch(ts, Rows),
    62  	}
    63  }
    64  
    65  // create a new block based on the type information, flgs[i] == ture: has null
    66  func newBatch(ts []types.Type, rows int) *Batch {
    67  	bat := NewWithSize(len(ts))
    68  	bat.InitZsOne(rows)
    69  	for i, typ := range ts {
    70  		switch typ.Oid {
    71  		case types.T_int8:
    72  			vec := vector.New(typ)
    73  			vs := make([]int8, Rows)
    74  			for j := range vs {
    75  				vs[j] = int8(j)
    76  			}
    77  			vec.Col = vs
    78  			bat.Vecs[i] = vec
    79  		}
    80  	}
    81  	return bat
    82  }