github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/logical/logical_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 logical
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestAnd(t *testing.T) {
    27  	as := []bool{true, false, false, true, false, true}
    28  	bs := []bool{true, false, true, false, true, true}
    29  
    30  	cs := make([]bool, 6)
    31  	av := testutil.MakeBoolVector(as)
    32  	bv := testutil.MakeBoolVector(bs)
    33  	cv := testutil.MakeBoolVector(cs)
    34  
    35  	err := And(av, bv, cv)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	cols := vector.MustTCols[bool](cv)
    40  	require.Equal(t, []bool{true, false, false, false, false, true}, cols)
    41  }
    42  
    43  func TestAnd2(t *testing.T) {
    44  	as := make([]bool, 10)
    45  	bs := make([]bool, 10)
    46  	for i := 0; i < 10; i++ {
    47  		as[i] = true
    48  		bs[i] = false
    49  	}
    50  	cs := make([]bool, 10)
    51  
    52  	av := testutil.MakeBooleanlVector(as, []uint64{3, 7})
    53  	bv := testutil.MakeBooleanlVector(bs, []uint64{3, 5})
    54  	cv := testutil.MakeBoolVector(cs)
    55  	nulls.Or(av.Nsp, bv.Nsp, cv.Nsp)
    56  
    57  	err := And(av, bv, cv)
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	cols := vector.MustTCols[bool](cv)
    62  	require.Equal(t, []bool{false, false, false, false, false, false, false, false, false, false}, cols)
    63  	require.Equal(t, []uint64{3, 5}, cv.Nsp.Np.ToArray())
    64  }
    65  
    66  func TestOr(t *testing.T) {
    67  	as := []bool{true, false, false, true, false, true}
    68  	bs := []bool{true, false, true, false, true, true}
    69  
    70  	cs := make([]bool, 6)
    71  	av := testutil.MakeBoolVector(as)
    72  	bv := testutil.MakeBoolVector(bs)
    73  	cv := testutil.MakeBoolVector(cs)
    74  
    75  	err := Or(av, bv, cv)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	cols := vector.MustTCols[bool](cv)
    80  	require.Equal(t, []bool{true, false, true, true, true, true}, cols)
    81  }
    82  
    83  func TestOr2(t *testing.T) {
    84  	as := make([]bool, 10)
    85  	bs := make([]bool, 10)
    86  	for i := 0; i < 10; i++ {
    87  		as[i] = true
    88  		bs[i] = false
    89  	}
    90  	cs := make([]bool, 10)
    91  
    92  	av := testutil.MakeBooleanlVector(as, []uint64{3, 7})
    93  	bv := testutil.MakeBooleanlVector(bs, []uint64{3, 5})
    94  	cv := testutil.MakeBoolVector(cs)
    95  	nulls.Or(av.Nsp, bv.Nsp, cv.Nsp)
    96  
    97  	err := Or(av, bv, cv)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	cols := vector.MustTCols[bool](cv)
   102  	require.Equal(t, []bool{true, true, true, true, true, true, true, true, true, true}, cols)
   103  	require.Equal(t, []uint64{3, 7}, cv.Nsp.Np.ToArray())
   104  }
   105  
   106  func TestXor(t *testing.T) {
   107  	as := []bool{true, false, false, true, false, true}
   108  	bs := []bool{true, false, true, false, true, true}
   109  
   110  	cs := make([]bool, 6)
   111  	av := testutil.MakeBoolVector(as)
   112  	bv := testutil.MakeBoolVector(bs)
   113  	cv := testutil.MakeBoolVector(cs)
   114  
   115  	err := Xor(av, bv, cv)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	cols := vector.MustTCols[bool](cv)
   120  	require.Equal(t, []bool{false, false, true, true, true, false}, cols)
   121  }
   122  
   123  func TestXor2(t *testing.T) {
   124  	as := make([]bool, 10)
   125  	bs := make([]bool, 10)
   126  	for i := 0; i < 10; i++ {
   127  		as[i] = true
   128  		bs[i] = false
   129  	}
   130  	cs := make([]bool, 10)
   131  
   132  	av := testutil.MakeBooleanlVector(as, []uint64{3, 7})
   133  	bv := testutil.MakeBooleanlVector(bs, []uint64{3, 5})
   134  	cv := testutil.MakeBoolVector(cs)
   135  	nulls.Or(av.Nsp, bv.Nsp, cv.Nsp)
   136  
   137  	err := Xor(av, bv, cv)
   138  	if err != nil {
   139  		panic(err)
   140  	}
   141  	cols := vector.MustTCols[bool](cv)
   142  	require.Equal(t, []bool{true, true, true, true, true, true, true, true, true, true}, cols)
   143  	require.Equal(t, []uint64{3, 5, 7}, cv.Nsp.Np.ToArray())
   144  }
   145  
   146  func TestNot(t *testing.T) {
   147  	as := []bool{true, false, false, true, true}
   148  
   149  	cs := make([]bool, 5)
   150  	av := testutil.MakeBoolVector(as)
   151  	cv := testutil.MakeBoolVector(cs)
   152  
   153  	err := Not(av, cv)
   154  	if err != nil {
   155  		panic(err)
   156  	}
   157  	cols := vector.MustTCols[bool](cv)
   158  	require.Equal(t, []bool{false, true, true, false, false}, cols)
   159  }
   160  
   161  func TestNot2(t *testing.T) {
   162  	as := []bool{true, false, false, true, true}
   163  
   164  	cs := make([]bool, 5)
   165  	av := testutil.MakeBooleanlVector(as, []uint64{5})
   166  	cv := testutil.MakeBoolVector(cs)
   167  	nulls.Or(av.Nsp, nil, cv.Nsp)
   168  
   169  	err := Not(av, cv)
   170  	if err != nil {
   171  		panic(err)
   172  	}
   173  	cols := vector.MustTCols[bool](cv)
   174  	require.Equal(t, []bool{false, true, true, false, false}, cols)
   175  	require.Equal(t, []uint64{5}, cv.Nsp.Np.ToArray())
   176  }
   177  
   178  func TestNot3(t *testing.T) {
   179  	cs := make([]bool, 1)
   180  	av := testutil.MakeScalarBool(true, 5)
   181  
   182  	cv := testutil.MakeBoolVector(cs)
   183  	nulls.Or(av.Nsp, nil, cv.Nsp)
   184  
   185  	err := Not(av, cv)
   186  	if err != nil {
   187  		panic(err)
   188  	}
   189  	cols := vector.MustTCols[bool](cv)
   190  	require.Equal(t, []bool{false}, cols)
   191  }