|
| 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/arrow/array" |
| 23 | + "github.com/apache/arrow/go/arrow/float16" |
| 24 | + "github.com/apache/arrow/go/arrow/memory" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func float32Values(a *array.Float16) []float32 { |
| 29 | + values := make([]float32, a.Len()) |
| 30 | + for i, v := range a.Values() { |
| 31 | + values[i] = v.Float32() |
| 32 | + } |
| 33 | + return values |
| 34 | +} |
| 35 | + |
| 36 | +func TestNewFloat16Builder(t *testing.T) { |
| 37 | + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) |
| 38 | + defer mem.AssertSize(t, 0) |
| 39 | + |
| 40 | + ab := array.NewFloat16Builder(mem) |
| 41 | + |
| 42 | + ab.Append(float16.New(1)) |
| 43 | + ab.Append(float16.New(2)) |
| 44 | + ab.Append(float16.New(3)) |
| 45 | + ab.AppendNull() |
| 46 | + ab.Append(float16.New(5)) |
| 47 | + ab.Append(float16.New(6)) |
| 48 | + ab.AppendNull() |
| 49 | + ab.Append(float16.New(8)) |
| 50 | + ab.Append(float16.New(9)) |
| 51 | + ab.Append(float16.New(10)) |
| 52 | + |
| 53 | + // check state of builder before NewFloat16Array |
| 54 | + assert.Equal(t, 10, ab.Len(), "unexpected Len()") |
| 55 | + assert.Equal(t, 2, ab.NullN(), "unexpected NullN()") |
| 56 | + |
| 57 | + a := ab.NewFloat16Array() |
| 58 | + |
| 59 | + // check state of builder after NewFloat16Array |
| 60 | + assert.Zero(t, ab.Len(), "unexpected ArrayBuilder.Len(), NewFloat16Array did not reset state") |
| 61 | + assert.Zero(t, ab.Cap(), "unexpected ArrayBuilder.Cap(), NewFloat16Array did not reset state") |
| 62 | + assert.Zero(t, ab.NullN(), "unexpected ArrayBuilder.NullN(), NewFloat16Array did not reset state") |
| 63 | + |
| 64 | + // check state of array |
| 65 | + assert.Equal(t, 2, a.NullN(), "unexpected null count") |
| 66 | + |
| 67 | + assert.Equal(t, []float32{1, 2, 3, 0, 5, 6, 0, 8, 9, 10}, float32Values(a), "unexpected Float16Values") |
| 68 | + assert.Equal(t, []byte{0xb7}, a.NullBitmapBytes()[:1]) // 4 bytes due to minBuilderCapacity |
| 69 | + assert.Len(t, a.Values(), 10, "unexpected length of Float16Values") |
| 70 | + |
| 71 | + a.Release() |
| 72 | + ab.Append(float16.New(7)) |
| 73 | + ab.Append(float16.New(8)) |
| 74 | + |
| 75 | + a = ab.NewFloat16Array() |
| 76 | + |
| 77 | + assert.Equal(t, 0, a.NullN()) |
| 78 | + assert.Equal(t, []float32{7, 8}, float32Values(a)) |
| 79 | + assert.Len(t, a.Values(), 2) |
| 80 | + |
| 81 | + a.Release() |
| 82 | +} |
| 83 | + |
| 84 | +func TestFloat16Builder_Empty(t *testing.T) { |
| 85 | + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) |
| 86 | + defer mem.AssertSize(t, 0) |
| 87 | + |
| 88 | + ab := array.NewFloat16Builder(mem) |
| 89 | + defer ab.Release() |
| 90 | + |
| 91 | + want := []float16.Num{float16.New(3), float16.New(4)} |
| 92 | + |
| 93 | + ab.AppendValues([]float16.Num{}, nil) |
| 94 | + a := ab.NewFloat16Array() |
| 95 | + assert.Zero(t, a.Len()) |
| 96 | + a.Release() |
| 97 | + |
| 98 | + ab.AppendValues(nil, nil) |
| 99 | + a = ab.NewFloat16Array() |
| 100 | + assert.Zero(t, a.Len()) |
| 101 | + a.Release() |
| 102 | + |
| 103 | + ab.AppendValues(want, nil) |
| 104 | + a = ab.NewFloat16Array() |
| 105 | + assert.Equal(t, want, a.Values()) |
| 106 | + a.Release() |
| 107 | + |
| 108 | + ab.AppendValues([]float16.Num{}, nil) |
| 109 | + ab.AppendValues(want, nil) |
| 110 | + a = ab.NewFloat16Array() |
| 111 | + assert.Equal(t, want, a.Values()) |
| 112 | + a.Release() |
| 113 | + |
| 114 | + ab.AppendValues(want, nil) |
| 115 | + ab.AppendValues([]float16.Num{}, nil) |
| 116 | + a = ab.NewFloat16Array() |
| 117 | + assert.Equal(t, want, a.Values()) |
| 118 | + a.Release() |
| 119 | +} |
0 commit comments