forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accept string for ArrayInterface constructor.
- Loading branch information
1 parent
b47b5ac
commit e8ecafb
Showing
4 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*! | ||
* Copyright 2020 by XGBoost Contributors | ||
*/ | ||
#include <gtest/gtest.h> | ||
#include <xgboost/host_device_vector.h> | ||
#include "../helpers.h" | ||
#include "../../../src/data/array_interface.h" | ||
|
||
namespace xgboost { | ||
TEST(ArrayInterface, Initialize) { | ||
size_t constexpr kRows = 10, kCols = 10; | ||
HostDeviceVector<float> storage; | ||
auto array = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); | ||
auto arr_interface = ArrayInterface(array); | ||
ASSERT_EQ(arr_interface.num_rows, kRows); | ||
ASSERT_EQ(arr_interface.num_cols, kCols); | ||
ASSERT_EQ(arr_interface.data, storage.ConstHostPointer()); | ||
} | ||
|
||
TEST(ArrayInterface, Error) { | ||
constexpr size_t kRows = 16, kCols = 10; | ||
Json column { Object() }; | ||
std::vector<Json> j_shape {Json(Integer(static_cast<Integer::Int>(kRows)))}; | ||
column["shape"] = Array(j_shape); | ||
std::vector<Json> j_data { | ||
Json(Integer(reinterpret_cast<Integer::Int>(nullptr))), | ||
Json(Boolean(false))}; | ||
|
||
auto const& column_obj = get<Object>(column); | ||
// missing version | ||
EXPECT_THROW(ArrayInterfaceHandler::ExtractData<float>(column_obj), dmlc::Error); | ||
column["version"] = Integer(static_cast<Integer::Int>(1)); | ||
// missing data | ||
EXPECT_THROW(ArrayInterfaceHandler::ExtractData<float>(column_obj), dmlc::Error); | ||
column["data"] = j_data; | ||
// missing typestr | ||
EXPECT_THROW(ArrayInterfaceHandler::ExtractData<float>(column_obj), dmlc::Error); | ||
column["typestr"] = String("<f4"); | ||
// nullptr is not valid | ||
EXPECT_THROW(ArrayInterfaceHandler::ExtractData<float>(column_obj), dmlc::Error); | ||
|
||
HostDeviceVector<float> storage; | ||
auto array = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage); | ||
j_data = { | ||
Json(Integer(reinterpret_cast<Integer::Int>(storage.ConstHostPointer()))), | ||
Json(Boolean(false))}; | ||
column["data"] = j_data; | ||
EXPECT_NO_THROW(ArrayInterfaceHandler::ExtractData<float>(column_obj)); | ||
} | ||
|
||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters