github.com/apache/arrow/go/v14@v14.0.1/arrow/tensor/numeric.gen_test.go (about)

     1  // Code generated by tensor/numeric.gen_test.go.tmpl. DO NOT EDIT.
     2  
     3  // Licensed to the Apache Software Foundation (ASF) under one
     4  // or more contributor license agreements.  See the NOTICE file
     5  // distributed with this work for additional information
     6  // regarding copyright ownership.  The ASF licenses this file
     7  // to you under the Apache License, Version 2.0 (the
     8  // "License"); you may not use this file except in compliance
     9  // with the License.  You may obtain a copy of the License at
    10  //
    11  // http://www.apache.org/licenses/LICENSE-2.0
    12  //
    13  // Unless required by applicable law or agreed to in writing, software
    14  // distributed under the License is distributed on an "AS IS" BASIS,
    15  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  // See the License for the specific language governing permissions and
    17  // limitations under the License.
    18  
    19  package tensor_test
    20  
    21  import (
    22  	"fmt"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/apache/arrow/go/v14/arrow"
    27  	"github.com/apache/arrow/go/v14/arrow/array"
    28  	"github.com/apache/arrow/go/v14/arrow/memory"
    29  	"github.com/apache/arrow/go/v14/arrow/tensor"
    30  )
    31  
    32  func TestTensorInt8(t *testing.T) {
    33  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
    34  	defer mem.AssertSize(t, 0)
    35  
    36  	bld := array.NewInt8Builder(mem)
    37  	defer bld.Release()
    38  
    39  	raw := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    40  	bld.AppendValues(raw, nil)
    41  
    42  	arr := bld.NewInt8Array()
    43  	defer arr.Release()
    44  
    45  	var (
    46  		shape = []int64{2, 5}
    47  		names = []string{"x", "y"}
    48  		bw    = int64(arrow.PrimitiveTypes.Int8.(arrow.FixedWidthDataType).BitWidth()) / 8
    49  	)
    50  
    51  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Int8)
    52  	defer tsr.Release()
    53  
    54  	tsr.Retain()
    55  	tsr.Release()
    56  
    57  	if got, want := tsr.Len(), 10; got != want {
    58  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
    59  	}
    60  
    61  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
    62  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
    63  	}
    64  
    65  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
    66  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
    67  	}
    68  
    69  	if got, want := tsr.NumDims(), 2; got != want {
    70  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
    71  	}
    72  
    73  	for i, name := range names {
    74  		if got, want := tsr.DimName(i), name; got != want {
    75  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
    76  		}
    77  	}
    78  
    79  	if got, want := tsr.DataType(), arr.DataType(); got != want {
    80  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
    81  	}
    82  
    83  	if got, want := tsr.Data(), arr.Data(); got != want {
    84  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
    85  	}
    86  
    87  	if tsr.IsMutable() {
    88  		t.Fatalf("should not be mutable")
    89  	}
    90  
    91  	if !tsr.IsContiguous() {
    92  		t.Fatalf("should be contiguous")
    93  	}
    94  
    95  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
    96  		t.Fatalf("should be row-major")
    97  	}
    98  
    99  	if got, want := tsr.Int8Values(), raw; !reflect.DeepEqual(got, want) {
   100  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   101  	}
   102  
   103  	for _, tc := range []struct {
   104  		i []int64
   105  		v int8
   106  	}{
   107  		{i: []int64{0, 0}, v: 1},
   108  		{i: []int64{0, 1}, v: 2},
   109  		{i: []int64{0, 2}, v: 3},
   110  		{i: []int64{0, 3}, v: 4},
   111  		{i: []int64{0, 4}, v: 5},
   112  		{i: []int64{1, 0}, v: 6},
   113  		{i: []int64{1, 1}, v: 7},
   114  		{i: []int64{1, 2}, v: 8},
   115  		{i: []int64{1, 3}, v: 9},
   116  		{i: []int64{1, 4}, v: 10},
   117  	} {
   118  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   119  			got := tsr.Value(tc.i)
   120  			if got != tc.v {
   121  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func TestTensorInt16(t *testing.T) {
   128  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   129  	defer mem.AssertSize(t, 0)
   130  
   131  	bld := array.NewInt16Builder(mem)
   132  	defer bld.Release()
   133  
   134  	raw := []int16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   135  	bld.AppendValues(raw, nil)
   136  
   137  	arr := bld.NewInt16Array()
   138  	defer arr.Release()
   139  
   140  	var (
   141  		shape = []int64{2, 5}
   142  		names = []string{"x", "y"}
   143  		bw    = int64(arrow.PrimitiveTypes.Int16.(arrow.FixedWidthDataType).BitWidth()) / 8
   144  	)
   145  
   146  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Int16)
   147  	defer tsr.Release()
   148  
   149  	tsr.Retain()
   150  	tsr.Release()
   151  
   152  	if got, want := tsr.Len(), 10; got != want {
   153  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   154  	}
   155  
   156  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   157  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   158  	}
   159  
   160  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   161  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   162  	}
   163  
   164  	if got, want := tsr.NumDims(), 2; got != want {
   165  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   166  	}
   167  
   168  	for i, name := range names {
   169  		if got, want := tsr.DimName(i), name; got != want {
   170  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   171  		}
   172  	}
   173  
   174  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   175  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   176  	}
   177  
   178  	if got, want := tsr.Data(), arr.Data(); got != want {
   179  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   180  	}
   181  
   182  	if tsr.IsMutable() {
   183  		t.Fatalf("should not be mutable")
   184  	}
   185  
   186  	if !tsr.IsContiguous() {
   187  		t.Fatalf("should be contiguous")
   188  	}
   189  
   190  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   191  		t.Fatalf("should be row-major")
   192  	}
   193  
   194  	if got, want := tsr.Int16Values(), raw; !reflect.DeepEqual(got, want) {
   195  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   196  	}
   197  
   198  	for _, tc := range []struct {
   199  		i []int64
   200  		v int16
   201  	}{
   202  		{i: []int64{0, 0}, v: 1},
   203  		{i: []int64{0, 1}, v: 2},
   204  		{i: []int64{0, 2}, v: 3},
   205  		{i: []int64{0, 3}, v: 4},
   206  		{i: []int64{0, 4}, v: 5},
   207  		{i: []int64{1, 0}, v: 6},
   208  		{i: []int64{1, 1}, v: 7},
   209  		{i: []int64{1, 2}, v: 8},
   210  		{i: []int64{1, 3}, v: 9},
   211  		{i: []int64{1, 4}, v: 10},
   212  	} {
   213  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   214  			got := tsr.Value(tc.i)
   215  			if got != tc.v {
   216  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   217  			}
   218  		})
   219  	}
   220  }
   221  
   222  func TestTensorInt32(t *testing.T) {
   223  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   224  	defer mem.AssertSize(t, 0)
   225  
   226  	bld := array.NewInt32Builder(mem)
   227  	defer bld.Release()
   228  
   229  	raw := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   230  	bld.AppendValues(raw, nil)
   231  
   232  	arr := bld.NewInt32Array()
   233  	defer arr.Release()
   234  
   235  	var (
   236  		shape = []int64{2, 5}
   237  		names = []string{"x", "y"}
   238  		bw    = int64(arrow.PrimitiveTypes.Int32.(arrow.FixedWidthDataType).BitWidth()) / 8
   239  	)
   240  
   241  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Int32)
   242  	defer tsr.Release()
   243  
   244  	tsr.Retain()
   245  	tsr.Release()
   246  
   247  	if got, want := tsr.Len(), 10; got != want {
   248  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   249  	}
   250  
   251  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   252  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   253  	}
   254  
   255  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   256  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   257  	}
   258  
   259  	if got, want := tsr.NumDims(), 2; got != want {
   260  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   261  	}
   262  
   263  	for i, name := range names {
   264  		if got, want := tsr.DimName(i), name; got != want {
   265  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   266  		}
   267  	}
   268  
   269  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   270  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   271  	}
   272  
   273  	if got, want := tsr.Data(), arr.Data(); got != want {
   274  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   275  	}
   276  
   277  	if tsr.IsMutable() {
   278  		t.Fatalf("should not be mutable")
   279  	}
   280  
   281  	if !tsr.IsContiguous() {
   282  		t.Fatalf("should be contiguous")
   283  	}
   284  
   285  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   286  		t.Fatalf("should be row-major")
   287  	}
   288  
   289  	if got, want := tsr.Int32Values(), raw; !reflect.DeepEqual(got, want) {
   290  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   291  	}
   292  
   293  	for _, tc := range []struct {
   294  		i []int64
   295  		v int32
   296  	}{
   297  		{i: []int64{0, 0}, v: 1},
   298  		{i: []int64{0, 1}, v: 2},
   299  		{i: []int64{0, 2}, v: 3},
   300  		{i: []int64{0, 3}, v: 4},
   301  		{i: []int64{0, 4}, v: 5},
   302  		{i: []int64{1, 0}, v: 6},
   303  		{i: []int64{1, 1}, v: 7},
   304  		{i: []int64{1, 2}, v: 8},
   305  		{i: []int64{1, 3}, v: 9},
   306  		{i: []int64{1, 4}, v: 10},
   307  	} {
   308  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   309  			got := tsr.Value(tc.i)
   310  			if got != tc.v {
   311  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   312  			}
   313  		})
   314  	}
   315  }
   316  
   317  func TestTensorInt64(t *testing.T) {
   318  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   319  	defer mem.AssertSize(t, 0)
   320  
   321  	bld := array.NewInt64Builder(mem)
   322  	defer bld.Release()
   323  
   324  	raw := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   325  	bld.AppendValues(raw, nil)
   326  
   327  	arr := bld.NewInt64Array()
   328  	defer arr.Release()
   329  
   330  	var (
   331  		shape = []int64{2, 5}
   332  		names = []string{"x", "y"}
   333  		bw    = int64(arrow.PrimitiveTypes.Int64.(arrow.FixedWidthDataType).BitWidth()) / 8
   334  	)
   335  
   336  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Int64)
   337  	defer tsr.Release()
   338  
   339  	tsr.Retain()
   340  	tsr.Release()
   341  
   342  	if got, want := tsr.Len(), 10; got != want {
   343  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   344  	}
   345  
   346  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   347  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   348  	}
   349  
   350  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   351  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   352  	}
   353  
   354  	if got, want := tsr.NumDims(), 2; got != want {
   355  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   356  	}
   357  
   358  	for i, name := range names {
   359  		if got, want := tsr.DimName(i), name; got != want {
   360  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   361  		}
   362  	}
   363  
   364  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   365  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   366  	}
   367  
   368  	if got, want := tsr.Data(), arr.Data(); got != want {
   369  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   370  	}
   371  
   372  	if tsr.IsMutable() {
   373  		t.Fatalf("should not be mutable")
   374  	}
   375  
   376  	if !tsr.IsContiguous() {
   377  		t.Fatalf("should be contiguous")
   378  	}
   379  
   380  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   381  		t.Fatalf("should be row-major")
   382  	}
   383  
   384  	if got, want := tsr.Int64Values(), raw; !reflect.DeepEqual(got, want) {
   385  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   386  	}
   387  
   388  	for _, tc := range []struct {
   389  		i []int64
   390  		v int64
   391  	}{
   392  		{i: []int64{0, 0}, v: 1},
   393  		{i: []int64{0, 1}, v: 2},
   394  		{i: []int64{0, 2}, v: 3},
   395  		{i: []int64{0, 3}, v: 4},
   396  		{i: []int64{0, 4}, v: 5},
   397  		{i: []int64{1, 0}, v: 6},
   398  		{i: []int64{1, 1}, v: 7},
   399  		{i: []int64{1, 2}, v: 8},
   400  		{i: []int64{1, 3}, v: 9},
   401  		{i: []int64{1, 4}, v: 10},
   402  	} {
   403  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   404  			got := tsr.Value(tc.i)
   405  			if got != tc.v {
   406  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   407  			}
   408  		})
   409  	}
   410  }
   411  
   412  func TestTensorUint8(t *testing.T) {
   413  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   414  	defer mem.AssertSize(t, 0)
   415  
   416  	bld := array.NewUint8Builder(mem)
   417  	defer bld.Release()
   418  
   419  	raw := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   420  	bld.AppendValues(raw, nil)
   421  
   422  	arr := bld.NewUint8Array()
   423  	defer arr.Release()
   424  
   425  	var (
   426  		shape = []int64{2, 5}
   427  		names = []string{"x", "y"}
   428  		bw    = int64(arrow.PrimitiveTypes.Uint8.(arrow.FixedWidthDataType).BitWidth()) / 8
   429  	)
   430  
   431  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Uint8)
   432  	defer tsr.Release()
   433  
   434  	tsr.Retain()
   435  	tsr.Release()
   436  
   437  	if got, want := tsr.Len(), 10; got != want {
   438  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   439  	}
   440  
   441  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   442  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   443  	}
   444  
   445  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   446  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   447  	}
   448  
   449  	if got, want := tsr.NumDims(), 2; got != want {
   450  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   451  	}
   452  
   453  	for i, name := range names {
   454  		if got, want := tsr.DimName(i), name; got != want {
   455  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   456  		}
   457  	}
   458  
   459  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   460  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   461  	}
   462  
   463  	if got, want := tsr.Data(), arr.Data(); got != want {
   464  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   465  	}
   466  
   467  	if tsr.IsMutable() {
   468  		t.Fatalf("should not be mutable")
   469  	}
   470  
   471  	if !tsr.IsContiguous() {
   472  		t.Fatalf("should be contiguous")
   473  	}
   474  
   475  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   476  		t.Fatalf("should be row-major")
   477  	}
   478  
   479  	if got, want := tsr.Uint8Values(), raw; !reflect.DeepEqual(got, want) {
   480  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   481  	}
   482  
   483  	for _, tc := range []struct {
   484  		i []int64
   485  		v uint8
   486  	}{
   487  		{i: []int64{0, 0}, v: 1},
   488  		{i: []int64{0, 1}, v: 2},
   489  		{i: []int64{0, 2}, v: 3},
   490  		{i: []int64{0, 3}, v: 4},
   491  		{i: []int64{0, 4}, v: 5},
   492  		{i: []int64{1, 0}, v: 6},
   493  		{i: []int64{1, 1}, v: 7},
   494  		{i: []int64{1, 2}, v: 8},
   495  		{i: []int64{1, 3}, v: 9},
   496  		{i: []int64{1, 4}, v: 10},
   497  	} {
   498  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   499  			got := tsr.Value(tc.i)
   500  			if got != tc.v {
   501  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   502  			}
   503  		})
   504  	}
   505  }
   506  
   507  func TestTensorUint16(t *testing.T) {
   508  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   509  	defer mem.AssertSize(t, 0)
   510  
   511  	bld := array.NewUint16Builder(mem)
   512  	defer bld.Release()
   513  
   514  	raw := []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   515  	bld.AppendValues(raw, nil)
   516  
   517  	arr := bld.NewUint16Array()
   518  	defer arr.Release()
   519  
   520  	var (
   521  		shape = []int64{2, 5}
   522  		names = []string{"x", "y"}
   523  		bw    = int64(arrow.PrimitiveTypes.Uint16.(arrow.FixedWidthDataType).BitWidth()) / 8
   524  	)
   525  
   526  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Uint16)
   527  	defer tsr.Release()
   528  
   529  	tsr.Retain()
   530  	tsr.Release()
   531  
   532  	if got, want := tsr.Len(), 10; got != want {
   533  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   534  	}
   535  
   536  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   537  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   538  	}
   539  
   540  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   541  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   542  	}
   543  
   544  	if got, want := tsr.NumDims(), 2; got != want {
   545  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   546  	}
   547  
   548  	for i, name := range names {
   549  		if got, want := tsr.DimName(i), name; got != want {
   550  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   551  		}
   552  	}
   553  
   554  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   555  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   556  	}
   557  
   558  	if got, want := tsr.Data(), arr.Data(); got != want {
   559  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   560  	}
   561  
   562  	if tsr.IsMutable() {
   563  		t.Fatalf("should not be mutable")
   564  	}
   565  
   566  	if !tsr.IsContiguous() {
   567  		t.Fatalf("should be contiguous")
   568  	}
   569  
   570  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   571  		t.Fatalf("should be row-major")
   572  	}
   573  
   574  	if got, want := tsr.Uint16Values(), raw; !reflect.DeepEqual(got, want) {
   575  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   576  	}
   577  
   578  	for _, tc := range []struct {
   579  		i []int64
   580  		v uint16
   581  	}{
   582  		{i: []int64{0, 0}, v: 1},
   583  		{i: []int64{0, 1}, v: 2},
   584  		{i: []int64{0, 2}, v: 3},
   585  		{i: []int64{0, 3}, v: 4},
   586  		{i: []int64{0, 4}, v: 5},
   587  		{i: []int64{1, 0}, v: 6},
   588  		{i: []int64{1, 1}, v: 7},
   589  		{i: []int64{1, 2}, v: 8},
   590  		{i: []int64{1, 3}, v: 9},
   591  		{i: []int64{1, 4}, v: 10},
   592  	} {
   593  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   594  			got := tsr.Value(tc.i)
   595  			if got != tc.v {
   596  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   597  			}
   598  		})
   599  	}
   600  }
   601  
   602  func TestTensorUint32(t *testing.T) {
   603  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   604  	defer mem.AssertSize(t, 0)
   605  
   606  	bld := array.NewUint32Builder(mem)
   607  	defer bld.Release()
   608  
   609  	raw := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   610  	bld.AppendValues(raw, nil)
   611  
   612  	arr := bld.NewUint32Array()
   613  	defer arr.Release()
   614  
   615  	var (
   616  		shape = []int64{2, 5}
   617  		names = []string{"x", "y"}
   618  		bw    = int64(arrow.PrimitiveTypes.Uint32.(arrow.FixedWidthDataType).BitWidth()) / 8
   619  	)
   620  
   621  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Uint32)
   622  	defer tsr.Release()
   623  
   624  	tsr.Retain()
   625  	tsr.Release()
   626  
   627  	if got, want := tsr.Len(), 10; got != want {
   628  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   629  	}
   630  
   631  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   632  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   633  	}
   634  
   635  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   636  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   637  	}
   638  
   639  	if got, want := tsr.NumDims(), 2; got != want {
   640  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   641  	}
   642  
   643  	for i, name := range names {
   644  		if got, want := tsr.DimName(i), name; got != want {
   645  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   646  		}
   647  	}
   648  
   649  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   650  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   651  	}
   652  
   653  	if got, want := tsr.Data(), arr.Data(); got != want {
   654  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   655  	}
   656  
   657  	if tsr.IsMutable() {
   658  		t.Fatalf("should not be mutable")
   659  	}
   660  
   661  	if !tsr.IsContiguous() {
   662  		t.Fatalf("should be contiguous")
   663  	}
   664  
   665  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   666  		t.Fatalf("should be row-major")
   667  	}
   668  
   669  	if got, want := tsr.Uint32Values(), raw; !reflect.DeepEqual(got, want) {
   670  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   671  	}
   672  
   673  	for _, tc := range []struct {
   674  		i []int64
   675  		v uint32
   676  	}{
   677  		{i: []int64{0, 0}, v: 1},
   678  		{i: []int64{0, 1}, v: 2},
   679  		{i: []int64{0, 2}, v: 3},
   680  		{i: []int64{0, 3}, v: 4},
   681  		{i: []int64{0, 4}, v: 5},
   682  		{i: []int64{1, 0}, v: 6},
   683  		{i: []int64{1, 1}, v: 7},
   684  		{i: []int64{1, 2}, v: 8},
   685  		{i: []int64{1, 3}, v: 9},
   686  		{i: []int64{1, 4}, v: 10},
   687  	} {
   688  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   689  			got := tsr.Value(tc.i)
   690  			if got != tc.v {
   691  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   692  			}
   693  		})
   694  	}
   695  }
   696  
   697  func TestTensorUint64(t *testing.T) {
   698  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   699  	defer mem.AssertSize(t, 0)
   700  
   701  	bld := array.NewUint64Builder(mem)
   702  	defer bld.Release()
   703  
   704  	raw := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   705  	bld.AppendValues(raw, nil)
   706  
   707  	arr := bld.NewUint64Array()
   708  	defer arr.Release()
   709  
   710  	var (
   711  		shape = []int64{2, 5}
   712  		names = []string{"x", "y"}
   713  		bw    = int64(arrow.PrimitiveTypes.Uint64.(arrow.FixedWidthDataType).BitWidth()) / 8
   714  	)
   715  
   716  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Uint64)
   717  	defer tsr.Release()
   718  
   719  	tsr.Retain()
   720  	tsr.Release()
   721  
   722  	if got, want := tsr.Len(), 10; got != want {
   723  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   724  	}
   725  
   726  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   727  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   728  	}
   729  
   730  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   731  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   732  	}
   733  
   734  	if got, want := tsr.NumDims(), 2; got != want {
   735  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   736  	}
   737  
   738  	for i, name := range names {
   739  		if got, want := tsr.DimName(i), name; got != want {
   740  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   741  		}
   742  	}
   743  
   744  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   745  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   746  	}
   747  
   748  	if got, want := tsr.Data(), arr.Data(); got != want {
   749  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   750  	}
   751  
   752  	if tsr.IsMutable() {
   753  		t.Fatalf("should not be mutable")
   754  	}
   755  
   756  	if !tsr.IsContiguous() {
   757  		t.Fatalf("should be contiguous")
   758  	}
   759  
   760  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   761  		t.Fatalf("should be row-major")
   762  	}
   763  
   764  	if got, want := tsr.Uint64Values(), raw; !reflect.DeepEqual(got, want) {
   765  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   766  	}
   767  
   768  	for _, tc := range []struct {
   769  		i []int64
   770  		v uint64
   771  	}{
   772  		{i: []int64{0, 0}, v: 1},
   773  		{i: []int64{0, 1}, v: 2},
   774  		{i: []int64{0, 2}, v: 3},
   775  		{i: []int64{0, 3}, v: 4},
   776  		{i: []int64{0, 4}, v: 5},
   777  		{i: []int64{1, 0}, v: 6},
   778  		{i: []int64{1, 1}, v: 7},
   779  		{i: []int64{1, 2}, v: 8},
   780  		{i: []int64{1, 3}, v: 9},
   781  		{i: []int64{1, 4}, v: 10},
   782  	} {
   783  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   784  			got := tsr.Value(tc.i)
   785  			if got != tc.v {
   786  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   787  			}
   788  		})
   789  	}
   790  }
   791  
   792  func TestTensorFloat32(t *testing.T) {
   793  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   794  	defer mem.AssertSize(t, 0)
   795  
   796  	bld := array.NewFloat32Builder(mem)
   797  	defer bld.Release()
   798  
   799  	raw := []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   800  	bld.AppendValues(raw, nil)
   801  
   802  	arr := bld.NewFloat32Array()
   803  	defer arr.Release()
   804  
   805  	var (
   806  		shape = []int64{2, 5}
   807  		names = []string{"x", "y"}
   808  		bw    = int64(arrow.PrimitiveTypes.Float32.(arrow.FixedWidthDataType).BitWidth()) / 8
   809  	)
   810  
   811  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Float32)
   812  	defer tsr.Release()
   813  
   814  	tsr.Retain()
   815  	tsr.Release()
   816  
   817  	if got, want := tsr.Len(), 10; got != want {
   818  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   819  	}
   820  
   821  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   822  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   823  	}
   824  
   825  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   826  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   827  	}
   828  
   829  	if got, want := tsr.NumDims(), 2; got != want {
   830  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   831  	}
   832  
   833  	for i, name := range names {
   834  		if got, want := tsr.DimName(i), name; got != want {
   835  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   836  		}
   837  	}
   838  
   839  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   840  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   841  	}
   842  
   843  	if got, want := tsr.Data(), arr.Data(); got != want {
   844  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   845  	}
   846  
   847  	if tsr.IsMutable() {
   848  		t.Fatalf("should not be mutable")
   849  	}
   850  
   851  	if !tsr.IsContiguous() {
   852  		t.Fatalf("should be contiguous")
   853  	}
   854  
   855  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   856  		t.Fatalf("should be row-major")
   857  	}
   858  
   859  	if got, want := tsr.Float32Values(), raw; !reflect.DeepEqual(got, want) {
   860  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   861  	}
   862  
   863  	for _, tc := range []struct {
   864  		i []int64
   865  		v float32
   866  	}{
   867  		{i: []int64{0, 0}, v: 1},
   868  		{i: []int64{0, 1}, v: 2},
   869  		{i: []int64{0, 2}, v: 3},
   870  		{i: []int64{0, 3}, v: 4},
   871  		{i: []int64{0, 4}, v: 5},
   872  		{i: []int64{1, 0}, v: 6},
   873  		{i: []int64{1, 1}, v: 7},
   874  		{i: []int64{1, 2}, v: 8},
   875  		{i: []int64{1, 3}, v: 9},
   876  		{i: []int64{1, 4}, v: 10},
   877  	} {
   878  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   879  			got := tsr.Value(tc.i)
   880  			if got != tc.v {
   881  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   882  			}
   883  		})
   884  	}
   885  }
   886  
   887  func TestTensorFloat64(t *testing.T) {
   888  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   889  	defer mem.AssertSize(t, 0)
   890  
   891  	bld := array.NewFloat64Builder(mem)
   892  	defer bld.Release()
   893  
   894  	raw := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   895  	bld.AppendValues(raw, nil)
   896  
   897  	arr := bld.NewFloat64Array()
   898  	defer arr.Release()
   899  
   900  	var (
   901  		shape = []int64{2, 5}
   902  		names = []string{"x", "y"}
   903  		bw    = int64(arrow.PrimitiveTypes.Float64.(arrow.FixedWidthDataType).BitWidth()) / 8
   904  	)
   905  
   906  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Float64)
   907  	defer tsr.Release()
   908  
   909  	tsr.Retain()
   910  	tsr.Release()
   911  
   912  	if got, want := tsr.Len(), 10; got != want {
   913  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
   914  	}
   915  
   916  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
   917  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
   918  	}
   919  
   920  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
   921  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
   922  	}
   923  
   924  	if got, want := tsr.NumDims(), 2; got != want {
   925  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
   926  	}
   927  
   928  	for i, name := range names {
   929  		if got, want := tsr.DimName(i), name; got != want {
   930  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
   931  		}
   932  	}
   933  
   934  	if got, want := tsr.DataType(), arr.DataType(); got != want {
   935  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
   936  	}
   937  
   938  	if got, want := tsr.Data(), arr.Data(); got != want {
   939  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
   940  	}
   941  
   942  	if tsr.IsMutable() {
   943  		t.Fatalf("should not be mutable")
   944  	}
   945  
   946  	if !tsr.IsContiguous() {
   947  		t.Fatalf("should be contiguous")
   948  	}
   949  
   950  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
   951  		t.Fatalf("should be row-major")
   952  	}
   953  
   954  	if got, want := tsr.Float64Values(), raw; !reflect.DeepEqual(got, want) {
   955  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
   956  	}
   957  
   958  	for _, tc := range []struct {
   959  		i []int64
   960  		v float64
   961  	}{
   962  		{i: []int64{0, 0}, v: 1},
   963  		{i: []int64{0, 1}, v: 2},
   964  		{i: []int64{0, 2}, v: 3},
   965  		{i: []int64{0, 3}, v: 4},
   966  		{i: []int64{0, 4}, v: 5},
   967  		{i: []int64{1, 0}, v: 6},
   968  		{i: []int64{1, 1}, v: 7},
   969  		{i: []int64{1, 2}, v: 8},
   970  		{i: []int64{1, 3}, v: 9},
   971  		{i: []int64{1, 4}, v: 10},
   972  	} {
   973  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
   974  			got := tsr.Value(tc.i)
   975  			if got != tc.v {
   976  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
   977  			}
   978  		})
   979  	}
   980  }
   981  
   982  func TestTensorDate32(t *testing.T) {
   983  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
   984  	defer mem.AssertSize(t, 0)
   985  
   986  	bld := array.NewDate32Builder(mem)
   987  	defer bld.Release()
   988  
   989  	raw := []arrow.Date32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   990  	bld.AppendValues(raw, nil)
   991  
   992  	arr := bld.NewDate32Array()
   993  	defer arr.Release()
   994  
   995  	var (
   996  		shape = []int64{2, 5}
   997  		names = []string{"x", "y"}
   998  		bw    = int64(arrow.PrimitiveTypes.Date32.(arrow.FixedWidthDataType).BitWidth()) / 8
   999  	)
  1000  
  1001  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Date32)
  1002  	defer tsr.Release()
  1003  
  1004  	tsr.Retain()
  1005  	tsr.Release()
  1006  
  1007  	if got, want := tsr.Len(), 10; got != want {
  1008  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
  1009  	}
  1010  
  1011  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
  1012  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
  1013  	}
  1014  
  1015  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
  1016  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
  1017  	}
  1018  
  1019  	if got, want := tsr.NumDims(), 2; got != want {
  1020  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
  1021  	}
  1022  
  1023  	for i, name := range names {
  1024  		if got, want := tsr.DimName(i), name; got != want {
  1025  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
  1026  		}
  1027  	}
  1028  
  1029  	if got, want := tsr.DataType(), arr.DataType(); got != want {
  1030  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
  1031  	}
  1032  
  1033  	if got, want := tsr.Data(), arr.Data(); got != want {
  1034  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
  1035  	}
  1036  
  1037  	if tsr.IsMutable() {
  1038  		t.Fatalf("should not be mutable")
  1039  	}
  1040  
  1041  	if !tsr.IsContiguous() {
  1042  		t.Fatalf("should be contiguous")
  1043  	}
  1044  
  1045  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
  1046  		t.Fatalf("should be row-major")
  1047  	}
  1048  
  1049  	if got, want := tsr.Date32Values(), raw; !reflect.DeepEqual(got, want) {
  1050  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
  1051  	}
  1052  
  1053  	for _, tc := range []struct {
  1054  		i []int64
  1055  		v arrow.Date32
  1056  	}{
  1057  		{i: []int64{0, 0}, v: 1},
  1058  		{i: []int64{0, 1}, v: 2},
  1059  		{i: []int64{0, 2}, v: 3},
  1060  		{i: []int64{0, 3}, v: 4},
  1061  		{i: []int64{0, 4}, v: 5},
  1062  		{i: []int64{1, 0}, v: 6},
  1063  		{i: []int64{1, 1}, v: 7},
  1064  		{i: []int64{1, 2}, v: 8},
  1065  		{i: []int64{1, 3}, v: 9},
  1066  		{i: []int64{1, 4}, v: 10},
  1067  	} {
  1068  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
  1069  			got := tsr.Value(tc.i)
  1070  			if got != tc.v {
  1071  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
  1072  			}
  1073  		})
  1074  	}
  1075  }
  1076  
  1077  func TestTensorDate64(t *testing.T) {
  1078  	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
  1079  	defer mem.AssertSize(t, 0)
  1080  
  1081  	bld := array.NewDate64Builder(mem)
  1082  	defer bld.Release()
  1083  
  1084  	raw := []arrow.Date64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  1085  	bld.AppendValues(raw, nil)
  1086  
  1087  	arr := bld.NewDate64Array()
  1088  	defer arr.Release()
  1089  
  1090  	var (
  1091  		shape = []int64{2, 5}
  1092  		names = []string{"x", "y"}
  1093  		bw    = int64(arrow.PrimitiveTypes.Date64.(arrow.FixedWidthDataType).BitWidth()) / 8
  1094  	)
  1095  
  1096  	tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.Date64)
  1097  	defer tsr.Release()
  1098  
  1099  	tsr.Retain()
  1100  	tsr.Release()
  1101  
  1102  	if got, want := tsr.Len(), 10; got != want {
  1103  		t.Fatalf("invalid length: got=%d, want=%d", got, want)
  1104  	}
  1105  
  1106  	if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
  1107  		t.Fatalf("invalid shape: got=%v, want=%v", got, want)
  1108  	}
  1109  
  1110  	if got, want := tsr.Strides(), []int64{5 * bw, 1 * bw}; !reflect.DeepEqual(got, want) {
  1111  		t.Fatalf("invalid strides: got=%v, want=%v", got, want)
  1112  	}
  1113  
  1114  	if got, want := tsr.NumDims(), 2; got != want {
  1115  		t.Fatalf("invalid dims: got=%d, want=%d", got, want)
  1116  	}
  1117  
  1118  	for i, name := range names {
  1119  		if got, want := tsr.DimName(i), name; got != want {
  1120  			t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
  1121  		}
  1122  	}
  1123  
  1124  	if got, want := tsr.DataType(), arr.DataType(); got != want {
  1125  		t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
  1126  	}
  1127  
  1128  	if got, want := tsr.Data(), arr.Data(); got != want {
  1129  		t.Fatalf("invalid data: got=%v, want=%v", got, want)
  1130  	}
  1131  
  1132  	if tsr.IsMutable() {
  1133  		t.Fatalf("should not be mutable")
  1134  	}
  1135  
  1136  	if !tsr.IsContiguous() {
  1137  		t.Fatalf("should be contiguous")
  1138  	}
  1139  
  1140  	if !tsr.IsRowMajor() || tsr.IsColMajor() {
  1141  		t.Fatalf("should be row-major")
  1142  	}
  1143  
  1144  	if got, want := tsr.Date64Values(), raw; !reflect.DeepEqual(got, want) {
  1145  		t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
  1146  	}
  1147  
  1148  	for _, tc := range []struct {
  1149  		i []int64
  1150  		v arrow.Date64
  1151  	}{
  1152  		{i: []int64{0, 0}, v: 1},
  1153  		{i: []int64{0, 1}, v: 2},
  1154  		{i: []int64{0, 2}, v: 3},
  1155  		{i: []int64{0, 3}, v: 4},
  1156  		{i: []int64{0, 4}, v: 5},
  1157  		{i: []int64{1, 0}, v: 6},
  1158  		{i: []int64{1, 1}, v: 7},
  1159  		{i: []int64{1, 2}, v: 8},
  1160  		{i: []int64{1, 3}, v: 9},
  1161  		{i: []int64{1, 4}, v: 10},
  1162  	} {
  1163  		t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
  1164  			got := tsr.Value(tc.i)
  1165  			if got != tc.v {
  1166  				t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
  1167  			}
  1168  		})
  1169  	}
  1170  }