github.com/apache/arrow/go/v16@v16.1.0/arrow/array/booleanbuilder_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package array_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/apache/arrow/go/v16/arrow/array"
    23  	"github.com/apache/arrow/go/v16/arrow/internal/testing/tools"
    24  	"github.com/apache/arrow/go/v16/arrow/memory"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBooleanBuilder_AppendValues(t *testing.T) {
    29  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
    30  	defer mem.AssertSize(t, 0)
    31  
    32  	b := array.NewBooleanBuilder(mem)
    33  
    34  	exp := tools.Bools(1, 1, 0, 1, 1, 0)
    35  
    36  	b.AppendValues(exp, nil)
    37  	assert.NoError(t, b.AppendValueFromString("true"))
    38  	assert.NoError(t, b.AppendValueFromString("false"))
    39  	exp = tools.Bools(1, 1, 0, 1, 1, 0, 1, 0)
    40  
    41  	got := make([]bool, len(exp))
    42  	// make sure we can read the values directly from the builder.
    43  	for i := 0; i < b.Len(); i++ {
    44  		got[i] = b.Value(i)
    45  	}
    46  	assert.Equal(t, exp, got)
    47  
    48  	got = make([]bool, len(exp)) // reset
    49  
    50  	a := b.NewBooleanArray()
    51  	b.Release()
    52  	for i := 0; i < a.Len(); i++ {
    53  		got[i] = a.Value(i)
    54  	}
    55  	assert.Equal(t, exp, got)
    56  
    57  	a.Release()
    58  }
    59  
    60  func TestBooleanBuilder_Empty(t *testing.T) {
    61  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
    62  	defer mem.AssertSize(t, 0)
    63  
    64  	ab := array.NewBooleanBuilder(mem)
    65  	defer ab.Release()
    66  
    67  	want := tools.Bools(1, 1, 0, 1, 1, 0, 1, 0)
    68  
    69  	boolValues := func(a *array.Boolean) []bool {
    70  		vs := make([]bool, a.Len())
    71  		for i := range vs {
    72  			vs[i] = a.Value(i)
    73  		}
    74  		return vs
    75  	}
    76  
    77  	ab.AppendValues([]bool{}, nil)
    78  	a := ab.NewBooleanArray()
    79  	assert.Zero(t, a.Len())
    80  	a.Release()
    81  
    82  	ab.AppendValues(nil, nil)
    83  	a = ab.NewBooleanArray()
    84  	assert.Zero(t, a.Len())
    85  	a.Release()
    86  
    87  	ab.AppendValues(want, nil)
    88  	a = ab.NewBooleanArray()
    89  	assert.Equal(t, want, boolValues(a))
    90  	a.Release()
    91  
    92  	ab.AppendValues([]bool{}, nil)
    93  	ab.AppendValues(want, nil)
    94  	a = ab.NewBooleanArray()
    95  	assert.Equal(t, want, boolValues(a))
    96  	a.Release()
    97  
    98  	ab.AppendValues(want, nil)
    99  	ab.AppendValues([]bool{}, nil)
   100  	a = ab.NewBooleanArray()
   101  	assert.Equal(t, want, boolValues(a))
   102  	a.Release()
   103  }