github.com/matrixorigin/matrixone@v1.2.0/pkg/common/hashmap/inthashmap_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 hashmap
    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  func TestIntHashMap_Iterator(t *testing.T) {
    27  	{
    28  		m := mpool.MustNewZero()
    29  		mp, err := NewIntHashMap(false, 0, 0, m)
    30  		require.NoError(t, err)
    31  		rowCount := 10
    32  		vecs := []*vector.Vector{
    33  			newVector(rowCount, types.T_int32.ToType(), m, false, []int32{
    34  				-1, -1, -1, 2, 2, 2, 3, 3, 3, 4,
    35  			}),
    36  			newVector(rowCount, types.T_uint32.ToType(), m, false, []uint32{
    37  				1, 1, 1, 2, 2, 2, 3, 3, 3, 4,
    38  			}),
    39  		}
    40  		itr := mp.NewIterator()
    41  		vs, _, err := itr.Insert(0, rowCount, vecs)
    42  		require.NoError(t, err)
    43  		require.Equal(t, []uint64{1, 1, 1, 2, 2, 2, 3, 3, 3, 4}, vs)
    44  		vs, _ = itr.Find(0, rowCount, vecs, nil)
    45  		require.Equal(t, []uint64{1, 1, 1, 2, 2, 2, 3, 3, 3, 4}, vs)
    46  		for _, vec := range vecs {
    47  			vec.Free(m)
    48  		}
    49  		mp.Free()
    50  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
    51  	}
    52  	{
    53  		m := mpool.MustNewZero()
    54  		mp, err := NewIntHashMap(true, 0, 0, m)
    55  		require.NoError(t, err)
    56  		ts := []types.Type{
    57  			types.New(types.T_int8, 0, 0),
    58  			types.New(types.T_int16, 0, 0),
    59  		}
    60  		vecs := newVectorsWithNull(ts, false, Rows, m)
    61  		itr := mp.NewIterator()
    62  		vs, _, err := itr.Insert(0, Rows, vecs)
    63  		require.NoError(t, err)
    64  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
    65  		vs, _ = itr.Find(0, Rows, vecs, nil)
    66  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
    67  		for _, vec := range vecs {
    68  			vec.Free(m)
    69  		}
    70  		mp.Free()
    71  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
    72  	}
    73  	{
    74  		m := mpool.MustNewZero()
    75  		mp, err := NewIntHashMap(true, 0, 0, m)
    76  		require.NoError(t, err)
    77  		ts := []types.Type{
    78  			types.New(types.T_int64, 0, 0),
    79  		}
    80  		vecs := newVectorsWithNull(ts, false, Rows, m)
    81  		itr := mp.NewIterator()
    82  		vs, _, err := itr.Insert(0, Rows, vecs)
    83  		require.NoError(t, err)
    84  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
    85  		vs, _ = itr.Find(0, Rows, vecs, nil)
    86  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
    87  		for _, vec := range vecs {
    88  			vec.Free(m)
    89  		}
    90  		mp.Free()
    91  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
    92  	}
    93  	{
    94  		m := mpool.MustNewZero()
    95  		mp, err := NewIntHashMap(true, 0, 0, m)
    96  		require.NoError(t, err)
    97  		ts := []types.Type{
    98  			types.New(types.T_char, 1, 0),
    99  		}
   100  		vecs := newVectorsWithNull(ts, false, Rows, m)
   101  		itr := mp.NewIterator()
   102  		vs, _, err := itr.Insert(0, Rows, vecs)
   103  		require.NoError(t, err)
   104  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
   105  		vs, _ = itr.Find(0, Rows, vecs, nil)
   106  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
   107  		for _, vec := range vecs {
   108  			vec.Free(m)
   109  		}
   110  		mp.Free()
   111  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   112  	}
   113  	{
   114  		m := mpool.MustNewZero()
   115  		mp, err := NewIntHashMap(true, 0, 0, m)
   116  		require.NoError(t, err)
   117  		ts := []types.Type{
   118  			types.New(types.T_char, 1, 0),
   119  		}
   120  		vecs := newVectors(ts, false, Rows, m)
   121  		itr := mp.NewIterator()
   122  		vs, _, err := itr.Insert(0, Rows, vecs)
   123  		require.NoError(t, err)
   124  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   125  		vs, _ = itr.Find(0, Rows, vecs, nil)
   126  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   127  		for _, vec := range vecs {
   128  			vec.Free(m)
   129  		}
   130  		mp.Free()
   131  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   132  	}
   133  	{
   134  		m := mpool.MustNewZero()
   135  		mp, err := NewIntHashMap(false, 0, 0, m)
   136  		require.NoError(t, err)
   137  		ts := []types.Type{
   138  			types.New(types.T_char, 1, 0),
   139  		}
   140  		vecs := newVectorsWithNull(ts, false, Rows, m)
   141  		itr := mp.NewIterator()
   142  		vs, _, err := itr.Insert(0, Rows, vecs)
   143  		require.NoError(t, err)
   144  		require.Equal(t, []uint64{0, 1, 0, 2, 0, 3, 0, 4, 0, 5}, vs[:Rows])
   145  		vs, _ = itr.Find(0, Rows, vecs, nil)
   146  		require.Equal(t, []uint64{0, 1, 0, 2, 0, 3, 0, 4, 0, 5}, vs[:Rows])
   147  		for _, vec := range vecs {
   148  			vec.Free(m)
   149  		}
   150  		mp.Free()
   151  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   152  	}
   153  }