Skip to content

Commit 1f2ad36

Browse files
AbdealiLoKotqchen
authored andcommitted
Add make commands for tests
This adds the make commands required to build and run tests.
1 parent b045ccd commit 1f2ad36

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ tags
8080
target
8181
*.swp
8282

83+
# cpp tests and gcov generated files
84+
*.gcov
85+
*.gcda
86+
*.gcno
87+
build_tests
88+
/tests/cpp/xgboost_test
89+
8390
.DS_Store

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ AMALGA_OBJ = amalgamation/xgboost-all0.o
101101
LIB_DEP = $(DMLC_CORE)/libdmlc.a $(RABIT)/lib/$(LIB_RABIT)
102102
ALL_DEP = $(filter-out build/cli_main.o, $(ALL_OBJ)) $(LIB_DEP)
103103
CLI_OBJ = build/cli_main.o
104+
include tests/cpp/xgboost_test.mk
104105

105106
build/%.o: src/%.cc
106107
@mkdir -p $(@D)
@@ -145,8 +146,15 @@ lint: rcpplint
145146
pylint:
146147
flake8 --ignore E501 python-package
147148
flake8 --ignore E501 tests/python
149+
150+
test: $(ALL_TEST)
151+
152+
check: test
153+
./tests/cpp/xgboost_test
154+
148155
clean:
149156
$(RM) -rf build build_plugin lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o xgboost
157+
$(RM) -rf build_tests tests/cpp/xgboost_test
150158

151159
clean_all: clean
152160
cd $(DMLC_CORE); $(MAKE) clean; cd $(ROOTDIR)

make/config.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ LIB_RABIT = librabit.a
4747
# path to libjvm.so
4848
LIBJVM=$(JAVA_HOME)/jre/lib/amd64/server
4949

50+
# path to gtest library (only used when $BUILD_TEST=1)
51+
# there should be an include path in $GTEST_PATH/include and library in $GTEST_PATH/lib
52+
GTEST_PATH =
53+
5054
# List of additional plugins, checkout plugin folder.
5155
# uncomment the following lines to include these plugins
5256
# you can also add your own plugin like this

tests/cpp/data/test_metainfo.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright by Contributors
2+
#include <xgboost/data.h>
3+
#include <gtest/gtest.h>
4+
5+
TEST(MetaInfo, GetSet) {
6+
xgboost::MetaInfo info;
7+
8+
double double2[2] = {1.0, 2.0};
9+
EXPECT_EQ(info.GetRoot(1), 0)
10+
<< "When no root_index is given, was expecting default value 0";
11+
info.SetInfo("root_index", double2, xgboost::kDouble, 2);
12+
EXPECT_EQ(info.GetRoot(1), 2.0f);
13+
14+
EXPECT_EQ(info.labels.size(), 0);
15+
info.SetInfo("label", double2, xgboost::kFloat32, 2);
16+
EXPECT_EQ(info.labels.size(), 2);
17+
18+
float float2[2] = {1.0f, 2.0f};
19+
EXPECT_EQ(info.GetWeight(1), 1.0f)
20+
<< "When no weights are given, was expecting default value 1";
21+
info.SetInfo("weight", float2, xgboost::kFloat32, 2);
22+
EXPECT_EQ(info.GetWeight(1), 2.0f);
23+
24+
uint32_t uint32_t2[2] = {1U, 2U};
25+
EXPECT_EQ(info.base_margin.size(), 0);
26+
info.SetInfo("base_margin", uint32_t2, xgboost::kUInt32, 2);
27+
EXPECT_EQ(info.base_margin.size(), 2);
28+
29+
uint64_t uint64_t2[2] = {1U, 2U};
30+
EXPECT_EQ(info.group_ptr.size(), 0);
31+
info.SetInfo("group", uint64_t2, xgboost::kUInt64, 2);
32+
ASSERT_EQ(info.group_ptr.size(), 3);
33+
EXPECT_EQ(info.group_ptr[2], 3);
34+
}

tests/cpp/test_main.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright by Contributors
2+
#include <gtest/gtest.h>
3+
4+
int main(int argc, char ** argv) {
5+
testing::InitGoogleTest(&argc, argv);
6+
testing::FLAGS_gtest_death_test_style = "threadsafe";
7+
return RUN_ALL_TESTS();
8+
}

tests/cpp/xgboost_test.mk

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
UTEST_ROOT=tests/cpp
2+
UTEST_OBJ_ROOT=build_$(UTEST_ROOT)
3+
UNITTEST=$(UTEST_ROOT)/xgboost_test
4+
UNITTEST_SRC=$(wildcard $(UTEST_ROOT)/*.cc $(UTEST_ROOT)/*/*.cc)
5+
UNITTEST_OBJ=$(patsubst $(UTEST_ROOT)%.cc, $(UTEST_OBJ_ROOT)%.o, $(UNITTEST_SRC))
6+
7+
GTEST_LIB=$(GTEST_PATH)/lib/
8+
GTEST_INC=$(GTEST_PATH)/include/
9+
10+
UNITTEST_CFLAGS=$(CFLAGS)
11+
UNITTEST_LDFLAGS=$(LDFLAGS) -L$(GTEST_LIB) -lgtest
12+
UNITTEST_DEPS=lib/libxgboost.a $(DMLC_CORE)/libdmlc.a $(RABIT)/lib/$(LIB_RABIT)
13+
14+
$(UTEST_OBJ_ROOT)/%.o: $(UTEST_ROOT)/%.cc
15+
@mkdir -p $(@D)
16+
$(CXX) $(UNITTEST_CFLAGS) -I$(GTEST_INC) -o $@ -c $<
17+
18+
$(UNITTEST): $(UNITTEST_OBJ) $(UNITTEST_DEPS)
19+
$(CXX) $(UNITTEST_CFLAGS) -o $@ $^ $(UNITTEST_LDFLAGS)
20+
21+
22+
ALL_TEST=$(UNITTEST)
23+
ALL_TEST_OBJ=$(UNITTEST_OBJ)

0 commit comments

Comments
 (0)