jorisvandenbossche commented on code in PR #38472:
URL: https://github.com/apache/arrow/pull/38472#discussion_r1410714596


##########
cpp/src/arrow/c/dlpack_test.cc:
##########
@@ -0,0 +1,124 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+
+#include "arrow/array/array_base.h"
+#include "arrow/c/dlpack.h"
+#include "arrow/c/dlpack_abi.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+
+namespace arrow {
+
+namespace dlpack {
+
+// using ExportArray = arrow::dlpack::ExportArray;
+
+class TestExportArray : public ::testing::Test {
+ public:
+  void SetUp() {}
+};
+
+static std::vector<std::shared_ptr<DataType>> 
TestExportArrayAgainstTheseTypes() {
+  return {
+      int8(),  uint8(),  int16(),   uint16(),  int32(),   uint32(),
+      int64(), uint64(), float16(), float32(), float64(),
+  };
+}
+
+static std::vector<DLDataTypeCode> TestExpectedDLPackDataTypes() {
+  return {
+      DLDataTypeCode::kDLInt,   DLDataTypeCode::kDLUInt,  
DLDataTypeCode::kDLInt,
+      DLDataTypeCode::kDLUInt,  DLDataTypeCode::kDLInt,   
DLDataTypeCode::kDLUInt,
+      DLDataTypeCode::kDLInt,   DLDataTypeCode::kDLUInt,  
DLDataTypeCode::kDLFloat,
+      DLDataTypeCode::kDLFloat, DLDataTypeCode::kDLFloat,
+  };
+}
+
+auto check_dlptensor = [](const std::shared_ptr<Array>& arr,
+                          std::shared_ptr<DataType> arrow_type,
+                          DLDataTypeCode dlpack_type, int64_t length) {
+  DLManagedTensor* dlmtensor;
+  ASSERT_OK(arrow::dlpack::ExportArray(arr, &dlmtensor));
+  auto dltensor = dlmtensor->dl_tensor;
+
+  const auto byte_width = arr->type()->byte_width();
+  const auto start = arr->offset() * byte_width;
+  ASSERT_OK_AND_ASSIGN(auto sliced_buffer,
+                       SliceBufferSafe(arr->data()->buffers[1], start));
+  ASSERT_EQ(sliced_buffer->data(), dltensor.data);
+
+  ASSERT_EQ(0, dltensor.byte_offset);
+  ASSERT_EQ(NULL, dltensor.strides);
+  ASSERT_EQ(length, dltensor.shape[0]);
+  ASSERT_EQ(1, dltensor.ndim);
+
+  ASSERT_EQ(dlpack_type, dltensor.dtype.code);
+
+  ASSERT_EQ(arrow_type->bit_width(), dltensor.dtype.bits);
+  ASSERT_EQ(1, dltensor.dtype.lanes);
+  ASSERT_EQ(DLDeviceType::kDLCPU, dltensor.device.device_type);
+  ASSERT_EQ(0, dltensor.device.device_id);
+};
+
+TEST_F(TestExportArray, TestSupportedArray) {
+  random::RandomArrayGenerator gen(0);
+
+  for (int64_t i = 0; i < 11; ++i) {
+    const std::shared_ptr<Array> array =
+        gen.ArrayOf(TestExportArrayAgainstTheseTypes()[i], 10, 0);
+    check_dlptensor(array, TestExportArrayAgainstTheseTypes()[i],

Review Comment:
   You can probably define the vector once before the for loop, and then access 
the i'th element from that variable within the for loop.
   
   Also, if this is only used for one test, I assume we could also define the 
actual vector data (the list of types) here within the test, instead of calling 
a function that returns the vector?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to