github.com/matrixorigin/matrixone@v0.7.0/pkg/sort/sort_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 sort
    16  
    17  import (
    18  	"sort"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  const (
    30  	Rows          = 1000
    31  	BenchmarkRows = 100000
    32  )
    33  
    34  type testCase struct {
    35  	desc bool
    36  	vec  *vector.Vector
    37  	proc *process.Process
    38  }
    39  
    40  var (
    41  	tcs []testCase
    42  )
    43  
    44  func init() {
    45  	mp := mpool.MustNewZero()
    46  	tcs = []testCase{
    47  		newTestCase(true, mp, types.New(types.T_bool, 0, 0, 0)),
    48  		newTestCase(false, mp, types.New(types.T_bool, 0, 0, 0)),
    49  
    50  		newTestCase(true, mp, types.New(types.T_int8, 0, 0, 0)),
    51  		newTestCase(false, mp, types.New(types.T_int8, 0, 0, 0)),
    52  		newTestCase(true, mp, types.New(types.T_int16, 0, 0, 0)),
    53  		newTestCase(false, mp, types.New(types.T_int16, 0, 0, 0)),
    54  		newTestCase(true, mp, types.New(types.T_int32, 0, 0, 0)),
    55  		newTestCase(false, mp, types.New(types.T_int32, 0, 0, 0)),
    56  		newTestCase(true, mp, types.New(types.T_int64, 0, 0, 0)),
    57  		newTestCase(false, mp, types.New(types.T_int64, 0, 0, 0)),
    58  
    59  		newTestCase(true, mp, types.New(types.T_uint8, 0, 0, 0)),
    60  		newTestCase(false, mp, types.New(types.T_uint8, 0, 0, 0)),
    61  		newTestCase(true, mp, types.New(types.T_uint16, 0, 0, 0)),
    62  		newTestCase(false, mp, types.New(types.T_uint16, 0, 0, 0)),
    63  		newTestCase(true, mp, types.New(types.T_uint32, 0, 0, 0)),
    64  		newTestCase(false, mp, types.New(types.T_uint32, 0, 0, 0)),
    65  		newTestCase(true, mp, types.New(types.T_uint64, 0, 0, 0)),
    66  		newTestCase(false, mp, types.New(types.T_uint64, 0, 0, 0)),
    67  
    68  		newTestCase(true, mp, types.New(types.T_float32, 0, 0, 0)),
    69  		newTestCase(false, mp, types.New(types.T_float32, 0, 0, 0)),
    70  
    71  		newTestCase(true, mp, types.New(types.T_float64, 0, 0, 0)),
    72  		newTestCase(false, mp, types.New(types.T_float64, 0, 0, 0)),
    73  
    74  		newTestCase(true, mp, types.New(types.T_date, 0, 0, 0)),
    75  		newTestCase(false, mp, types.New(types.T_date, 0, 0, 0)),
    76  
    77  		newTestCase(true, mp, types.New(types.T_datetime, 0, 0, 0)),
    78  		newTestCase(false, mp, types.New(types.T_datetime, 0, 0, 0)),
    79  
    80  		newTestCase(true, mp, types.New(types.T_timestamp, 0, 0, 0)),
    81  		newTestCase(false, mp, types.New(types.T_timestamp, 0, 0, 0)),
    82  
    83  		newTestCase(true, mp, types.New(types.T_decimal64, 0, 0, 0)),
    84  		newTestCase(false, mp, types.New(types.T_decimal64, 0, 0, 0)),
    85  
    86  		newTestCase(true, mp, types.New(types.T_decimal128, 0, 0, 0)),
    87  		newTestCase(false, mp, types.New(types.T_decimal128, 0, 0, 0)),
    88  
    89  		newTestCase(true, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0, 0)),
    90  		newTestCase(false, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0, 0)),
    91  	}
    92  }
    93  
    94  func TestSort(t *testing.T) {
    95  	for _, tc := range tcs {
    96  		os := make([]int64, vector.Length(tc.vec))
    97  		for i := range os {
    98  			os[i] = int64(i)
    99  		}
   100  		nb0 := tc.proc.Mp().CurrNB()
   101  		Sort(tc.desc, false, false, os, tc.vec, nil)
   102  		checkResult(t, tc.desc, tc.vec, os)
   103  		nb1 := tc.proc.Mp().CurrNB()
   104  		require.Equal(t, nb0, nb1)
   105  		tc.vec.Free(tc.proc.Mp())
   106  	}
   107  }
   108  
   109  func BenchmarkSortInt(b *testing.B) {
   110  	vs := make([]int, BenchmarkRows)
   111  	for i := range vs {
   112  		vs[i] = i
   113  	}
   114  	for i := 0; i < b.N; i++ {
   115  		sort.Ints(vs)
   116  	}
   117  }
   118  
   119  func BenchmarkSortIntVector(b *testing.B) {
   120  	m := mpool.MustNewZero()
   121  	vec := testutil.NewInt32Vector(BenchmarkRows, types.New(types.T_int32, 0, 0, 0), m, true, nil)
   122  	os := make([]int64, vector.Length(vec))
   123  	for i := range os {
   124  		os[i] = int64(i)
   125  	}
   126  	for i := 0; i < b.N; i++ {
   127  		Sort(false, false, false, os, vec, nil)
   128  	}
   129  }
   130  
   131  func checkResult(t *testing.T, desc bool, vec *vector.Vector, os []int64) {
   132  	switch vec.Typ.Oid {
   133  	case types.T_int32:
   134  		vs := make([]int, len(os))
   135  		col := vector.GetFixedVectorValues[int32](vec)
   136  		for i := range vs {
   137  			vs[i] = int(col[i])
   138  		}
   139  		sort.Ints(vs)
   140  		if desc {
   141  			j := len(vs) - 1
   142  			for _, v := range vs {
   143  				require.Equal(t, v, int(col[os[j]]))
   144  				j--
   145  			}
   146  		} else {
   147  			for i, v := range vs {
   148  				require.Equal(t, v, int(col[os[i]]))
   149  			}
   150  		}
   151  	case types.T_int64:
   152  		vs := make([]int, len(os))
   153  		col := vector.GetFixedVectorValues[int64](vec)
   154  		for i := range vs {
   155  			vs[i] = int(col[i])
   156  		}
   157  		sort.Ints(vs)
   158  		if desc {
   159  			j := len(vs) - 1
   160  			for _, v := range vs {
   161  				require.Equal(t, v, int(col[os[j]]))
   162  				j--
   163  			}
   164  		} else {
   165  			for i, v := range vs {
   166  				require.Equal(t, v, int(col[os[i]]))
   167  			}
   168  		}
   169  	case types.T_float32:
   170  		vs := make([]float64, len(os))
   171  		col := vector.GetFixedVectorValues[float32](vec)
   172  		for i := range vs {
   173  			vs[i] = float64(col[i])
   174  		}
   175  		sort.Float64s(vs)
   176  		if desc {
   177  			j := len(vs) - 1
   178  			for _, v := range vs {
   179  				require.Equal(t, v, float64(col[os[j]]))
   180  				j--
   181  			}
   182  		} else {
   183  			for i, v := range vs {
   184  				require.Equal(t, v, float64(col[os[i]]))
   185  			}
   186  		}
   187  	case types.T_float64:
   188  		vs := make([]float64, len(os))
   189  		col := vector.GetFixedVectorValues[float64](vec)
   190  		for i := range vs {
   191  			vs[i] = float64(col[i])
   192  		}
   193  		sort.Float64s(vs)
   194  		if desc {
   195  			j := len(vs) - 1
   196  			for _, v := range vs {
   197  				require.Equal(t, v, float64(col[os[j]]))
   198  				j--
   199  			}
   200  		} else {
   201  			for i, v := range vs {
   202  				require.Equal(t, v, float64(col[os[i]]))
   203  			}
   204  		}
   205  	}
   206  }
   207  
   208  func newTestCase(desc bool, m *mpool.MPool, typ types.Type) testCase {
   209  	return testCase{
   210  		desc: desc,
   211  		proc: testutil.NewProcessWithMPool(m),
   212  		vec:  testutil.NewVector(Rows, typ, m, true, nil),
   213  	}
   214  }