Skip to content

Commit

Permalink
Refactor tests with data generator. (#5439)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis authored Mar 26, 2020
1 parent 7146b91 commit 4942da6
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 259 deletions.
9 changes: 2 additions & 7 deletions tests/cpp/c_api/test_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ TEST(c_api, Version) {

TEST(c_api, ConfigIO) {
size_t constexpr kRows = 10;
auto pp_dmat = CreateDMatrix(kRows, 10, 0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatix();
std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
std::vector<bst_float> labels(kRows);
for (size_t i = 0; i < labels.size(); ++i) {
Expand All @@ -110,16 +109,13 @@ TEST(c_api, ConfigIO) {
auto config_1 = Json::Load({config_str_1.c_str(), config_str_1.size()});

ASSERT_EQ(config_0, config_1);

delete pp_dmat;
}

TEST(c_api, JsonModelIO) {
size_t constexpr kRows = 10;
dmlc::TemporaryDirectory tempdir;

auto pp_dmat = CreateDMatrix(kRows, 10, 0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(kRows, 10, 0).GenerateDMatix();
std::vector<std::shared_ptr<DMatrix>> mat {p_dmat};
std::vector<bst_float> labels(kRows);
for (size_t i = 0; i < labels.size(); ++i) {
Expand All @@ -144,6 +140,5 @@ TEST(c_api, JsonModelIO) {

ASSERT_EQ(model_str_0.front(), '{');
ASSERT_EQ(model_str_0, model_str_1);
delete pp_dmat;
}
} // namespace xgboost
21 changes: 9 additions & 12 deletions tests/cpp/common/test_column_matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ namespace xgboost {
namespace common {

TEST(DenseColumn, Test) {
auto dmat = CreateDMatrix(100, 10, 0.0);
auto dmat = RandomDataGenerator(100, 10, 0.0).GenerateDMatix();
GHistIndexMatrix gmat;
gmat.Init((*dmat).get(), 256);
gmat.Init(dmat.get(), 256);
ColumnMatrix column_matrix;
column_matrix.Init(gmat, 0.2);

for (auto i = 0ull; i < (*dmat)->Info().num_row_; i++) {
for (auto j = 0ull; j < (*dmat)->Info().num_col_; j++) {
for (auto i = 0ull; i < dmat->Info().num_row_; i++) {
for (auto j = 0ull; j < dmat->Info().num_col_; j++) {
auto col = column_matrix.GetColumn(j);
ASSERT_EQ(gmat.index[i * (*dmat)->Info().num_col_ + j],
ASSERT_EQ(gmat.index[i * dmat->Info().num_col_ + j],
col.GetGlobalBinIdx(i));
}
}
delete dmat;
}

TEST(SparseColumn, Test) {
auto dmat = CreateDMatrix(100, 1, 0.85);
auto dmat = RandomDataGenerator(100, 1, 0.85).GenerateDMatix();
GHistIndexMatrix gmat;
gmat.Init((*dmat).get(), 256);
gmat.Init(dmat.get(), 256);
ColumnMatrix column_matrix;
column_matrix.Init(gmat, 0.5);
auto col = column_matrix.GetColumn(0);
Expand All @@ -37,13 +36,12 @@ TEST(SparseColumn, Test) {
ASSERT_EQ(gmat.index[gmat.row_ptr[col.GetRowIdx(i)]],
col.GetGlobalBinIdx(i));
}
delete dmat;
}

TEST(DenseColumnWithMissing, Test) {
auto dmat = CreateDMatrix(100, 1, 0.5);
auto dmat = RandomDataGenerator(100, 1, 0.5).GenerateDMatix();
GHistIndexMatrix gmat;
gmat.Init((*dmat).get(), 256);
gmat.Init(dmat.get(), 256);
ColumnMatrix column_matrix;
column_matrix.Init(gmat, 0.2);
auto col = column_matrix.GetColumn(0);
Expand All @@ -52,7 +50,6 @@ TEST(DenseColumnWithMissing, Test) {
EXPECT_EQ(gmat.index[gmat.row_ptr[col.GetRowIdx(i)]],
col.GetGlobalBinIdx(i));
}
delete dmat;
}

void TestGHistIndexMatrixCreation(size_t nthreads) {
Expand Down
22 changes: 6 additions & 16 deletions tests/cpp/common/test_hist_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ TEST(CutsBuilder, SearchGroupInd) {
size_t constexpr kRows = 17;
size_t constexpr kCols = 15;

auto pp_dmat = CreateDMatrix(kRows, kCols, 0);
std::shared_ptr<DMatrix> p_mat {*pp_dmat};
auto p_mat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();

std::vector<bst_int> group(kNumGroups);
group[0] = 2;
Expand All @@ -149,17 +148,14 @@ TEST(CutsBuilder, SearchGroupInd) {
ASSERT_EQ(group_ind, 2);

EXPECT_ANY_THROW(CutsBuilder::SearchGroupIndFromRow(p_mat->Info().group_ptr_, 17));

delete pp_dmat;
}

TEST(SparseCuts, SingleThreadedBuild) {
size_t constexpr kRows = 267;
size_t constexpr kCols = 31;
size_t constexpr kBins = 256;

auto pp_dmat = CreateDMatrix(kRows, kCols, 0);
std::shared_ptr<DMatrix> p_fmat {*pp_dmat};
auto p_fmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();

common::GHistIndexMatrix hmat;
hmat.Init(p_fmat.get(), kBins);
Expand All @@ -173,8 +169,6 @@ TEST(SparseCuts, SingleThreadedBuild) {
ASSERT_EQ(hmat.cut.Ptrs(), cuts.Ptrs());
ASSERT_EQ(hmat.cut.Values(), cuts.Values());
ASSERT_EQ(hmat.cut.MinValues(), cuts.MinValues());

delete pp_dmat;
}

TEST(SparseCuts, MultiThreadedBuild) {
Expand Down Expand Up @@ -212,17 +206,13 @@ TEST(SparseCuts, MultiThreadedBuild) {
};

{
auto pp_mat = CreateDMatrix(kRows, kCols, 0);
DMatrix* p_fmat = (*pp_mat).get();
Compare(p_fmat);
delete pp_mat;
auto p_fmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();
Compare(p_fmat.get());
}

{
auto pp_mat = CreateDMatrix(kRows, kCols, 0.0001);
DMatrix* p_fmat = (*pp_mat).get();
Compare(p_fmat);
delete pp_mat;
auto p_fmat = RandomDataGenerator(kRows, kCols, 0.0001).GenerateDMatix();
Compare(p_fmat.get());
}

omp_set_num_threads(ori_nthreads);
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/common/test_hist_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ inline void TestRank(const std::vector<float>& cuts,
// Ignore the last cut, its special
double sum_weight = 0.0;
size_t j = 0;
for (auto i = 0; i < cuts.size() - 1; i++) {
for (size_t i = 0; i < cuts.size() - 1; i++) {
while (cuts[i] > sorted_x[j]) {
sum_weight += sorted_weights[j];
j++;
Expand All @@ -142,7 +142,7 @@ inline void TestRank(const std::vector<float>& cuts,
inline void ValidateColumn(const HistogramCuts& cuts, int column_idx,
const std::vector<float>& sorted_column,
const std::vector<float>& sorted_weights,
int num_bins) {
size_t num_bins) {

// Check the endpoints are correct
EXPECT_LT(cuts.MinValues()[column_idx], sorted_column.front());
Expand Down
5 changes: 1 addition & 4 deletions tests/cpp/data/test_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ TEST(Adapter, CSCAdapterColsMoreThanRows) {
}

TEST(c_api, DMatrixSliceAdapterFromSimpleDMatrix) {
auto pp_dmat = CreateDMatrix(6, 2, 1.0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(6, 2, 1.0).GenerateDMatix();

std::vector<int> ridx_set = {1, 3, 5};
data::DMatrixSliceAdapter adapter(p_dmat.get(),
Expand All @@ -91,8 +90,6 @@ TEST(c_api, DMatrixSliceAdapterFromSimpleDMatrix) {
}
}
}

delete pp_dmat;
}

// A mock for JVM data iterator.
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/data/test_ellpack_page.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace xgboost {
TEST(EllpackPage, EmptyDMatrix) {
constexpr int kNRows = 0, kNCols = 0, kMaxBin = 256;
constexpr float kSparsity = 0;
auto dmat = *CreateDMatrix(kNRows, kNCols, kSparsity);
auto dmat = RandomDataGenerator(kNRows, kNCols, kSparsity).GenerateDMatix();
auto& page = *dmat->GetBatches<EllpackPage>({0, kMaxBin}).begin();
auto impl = page.Impl();
ASSERT_EQ(impl->row_stride, 0);
Expand Down
5 changes: 1 addition & 4 deletions tests/cpp/data/test_simple_dmatrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ TEST(SimpleDMatrix, FromFile) {
TEST(SimpleDMatrix, Slice) {
const int kRows = 6;
const int kCols = 2;
auto pp_dmat = CreateDMatrix(kRows, kCols, 1.0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(kRows, kCols, 1.0).GenerateDMatix();
auto &labels = p_dmat->Info().labels_.HostVector();
auto &weights = p_dmat->Info().weights_.HostVector();
auto &base_margin = p_dmat->Info().base_margin_.HostVector();
Expand Down Expand Up @@ -257,8 +256,6 @@ TEST(SimpleDMatrix, Slice) {
EXPECT_EQ(old_inst[j], new_inst[j]);
}
}

delete pp_dmat;
};

TEST(SimpleDMatrix, SaveLoadBinary) {
Expand Down
14 changes: 3 additions & 11 deletions tests/cpp/gbm/test_gbtree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,22 @@ TEST(GBTree, WrongUpdater) {
size_t constexpr kRows = 17;
size_t constexpr kCols = 15;

auto pp_dmat = CreateDMatrix(kRows, kCols, 0);
std::shared_ptr<DMatrix> p_dmat {*pp_dmat};
auto p_dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();

p_dmat->Info().labels_.Resize(kRows);

auto learner = std::unique_ptr<Learner>(Learner::Create({p_dmat}));
// Hist can not be used for updating tree.
learner->SetParams(Args{{"tree_method", "hist"}, {"process_type", "update"}});
ASSERT_THROW(learner->UpdateOneIter(0, p_dmat), dmlc::Error);
delete pp_dmat;
}

#ifdef XGBOOST_USE_CUDA
TEST(GBTree, ChoosePredictor) {
size_t constexpr kRows = 17;
size_t constexpr kCols = 15;

auto pp_dmat = CreateDMatrix(kRows, kCols, 0);
std::shared_ptr<DMatrix> p_dmat {*pp_dmat};
auto p_dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();

auto& data = (*(p_dmat->GetBatches<SparsePage>().begin())).data;
p_dmat->Info().labels_.Resize(kRows);
Expand Down Expand Up @@ -117,8 +114,6 @@ TEST(GBTree, ChoosePredictor) {
}
// data is not pulled back into host
ASSERT_FALSE(data.HostCanWrite());

delete pp_dmat;
}
#endif // XGBOOST_USE_CUDA

Expand Down Expand Up @@ -200,8 +195,7 @@ TEST(Dart, JsonIO) {
TEST(Dart, Prediction) {
size_t constexpr kRows = 16, kCols = 10;

auto pp_dmat = CreateDMatrix(kRows, kCols, 0);
auto& p_mat = *pp_dmat;
auto p_mat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();

std::vector<bst_float> labels (kRows);
for (size_t i = 0; i < kRows; ++i) {
Expand Down Expand Up @@ -230,7 +224,5 @@ TEST(Dart, Prediction) {
// Inference doesn't drop tree.
ASSERT_GT(std::abs(h_predts_training[i] - h_predts_inference[i]), kRtEps);
}

delete pp_dmat;
}
} // namespace xgboost
Loading

0 comments on commit 4942da6

Please sign in to comment.