/*! * Copyright 2020-2021 by XGBoost Contributors */ #include #include #include "../helpers.h" #include "../../../src/data/array_interface.h" namespace xgboost { TEST(ArrayInterface, Initialize) { size_t constexpr kRows = 10, kCols = 10; HostDeviceVector storage; auto array = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); auto arr_interface = ArrayInterface<2>(StringView{array}); ASSERT_EQ(arr_interface.Shape(0), kRows); ASSERT_EQ(arr_interface.Shape(1), kCols); ASSERT_EQ(arr_interface.data, storage.ConstHostPointer()); ASSERT_EQ(arr_interface.ElementSize(), 4); ASSERT_EQ(arr_interface.type, ArrayInterfaceHandler::kF4); HostDeviceVector u64_storage(storage.Size()); std::string u64_arr_str{ArrayInterfaceStr(linalg::TensorView{ u64_storage.ConstHostSpan(), {kRows, kCols}, GenericParameter::kCpuId})}; std::copy(storage.ConstHostVector().cbegin(), storage.ConstHostVector().cend(), u64_storage.HostSpan().begin()); auto u64_arr = ArrayInterface<2>{u64_arr_str}; ASSERT_EQ(u64_arr.ElementSize(), 8); ASSERT_EQ(u64_arr.type, ArrayInterfaceHandler::kU8); } TEST(ArrayInterface, Error) { constexpr size_t kRows = 16, kCols = 10; Json column { Object() }; std::vector j_shape {Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); std::vector j_data { Json(Integer(reinterpret_cast(nullptr))), Json(Boolean(false))}; auto const& column_obj = get(column); std::string typestr{" storage; auto array = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); j_data = { Json(Integer(reinterpret_cast(storage.ConstHostPointer()))), Json(Boolean(false))}; column["data"] = j_data; EXPECT_NO_THROW(ArrayInterfaceHandler::ExtractData(column_obj, n)); } TEST(ArrayInterface, GetElement) { size_t kRows = 4, kCols = 2; HostDeviceVector storage; auto intefrace_str = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); ArrayInterface<2> array_interface{intefrace_str}; auto const& h_storage = storage.ConstHostVector(); for (size_t i = 0; i < kRows; ++i) { for (size_t j = 0; j < kCols; ++j) { float v0 = array_interface(i, j); float v1 = h_storage.at(i * kCols + j); ASSERT_EQ(v0, v1); } } } TEST(ArrayInterface, TrivialDim) { size_t kRows{1000}, kCols = 1; HostDeviceVector storage; auto interface_str = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); { ArrayInterface<1> arr_i{interface_str}; ASSERT_EQ(arr_i.n, kRows); ASSERT_EQ(arr_i.Shape(0), kRows); } std::swap(kRows, kCols); interface_str = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); { ArrayInterface<1> arr_i{interface_str}; ASSERT_EQ(arr_i.n, kCols); ASSERT_EQ(arr_i.Shape(0), kCols); } } TEST(ArrayInterface, ToDType) { static_assert(ToDType::kType == ArrayInterfaceHandler::kF4, ""); static_assert(ToDType::kType == ArrayInterfaceHandler::kF8, ""); static_assert(ToDType::kType == ArrayInterfaceHandler::kU4, ""); static_assert(ToDType::kType == ArrayInterfaceHandler::kU8, ""); static_assert(ToDType::kType == ArrayInterfaceHandler::kI4, ""); static_assert(ToDType::kType == ArrayInterfaceHandler::kI8, ""); } } // namespace xgboost