From 1f2ad36badbee0b00267d00f92cc8f61c74f4b6e Mon Sep 17 00:00:00 2001 From: AbdealiJK Date: Fri, 2 Dec 2016 00:04:33 +0530 Subject: [PATCH] Add make commands for tests This adds the make commands required to build and run tests. --- .gitignore | 7 +++++++ Makefile | 8 ++++++++ make/config.mk | 4 ++++ tests/cpp/data/test_metainfo.cc | 34 +++++++++++++++++++++++++++++++++ tests/cpp/test_main.cc | 8 ++++++++ tests/cpp/xgboost_test.mk | 23 ++++++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 tests/cpp/data/test_metainfo.cc create mode 100644 tests/cpp/test_main.cc create mode 100644 tests/cpp/xgboost_test.mk diff --git a/.gitignore b/.gitignore index f84ce1d89..151a23603 100644 --- a/.gitignore +++ b/.gitignore @@ -80,4 +80,11 @@ tags target *.swp +# cpp tests and gcov generated files +*.gcov +*.gcda +*.gcno +build_tests +/tests/cpp/xgboost_test + .DS_Store diff --git a/Makefile b/Makefile index ea08acafd..4444088d3 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,7 @@ AMALGA_OBJ = amalgamation/xgboost-all0.o LIB_DEP = $(DMLC_CORE)/libdmlc.a $(RABIT)/lib/$(LIB_RABIT) ALL_DEP = $(filter-out build/cli_main.o, $(ALL_OBJ)) $(LIB_DEP) CLI_OBJ = build/cli_main.o +include tests/cpp/xgboost_test.mk build/%.o: src/%.cc @mkdir -p $(@D) @@ -145,8 +146,15 @@ lint: rcpplint pylint: flake8 --ignore E501 python-package flake8 --ignore E501 tests/python + +test: $(ALL_TEST) + +check: test + ./tests/cpp/xgboost_test + clean: $(RM) -rf build build_plugin lib bin *~ */*~ */*/*~ */*/*/*~ */*.o */*/*.o */*/*/*.o xgboost + $(RM) -rf build_tests tests/cpp/xgboost_test clean_all: clean cd $(DMLC_CORE); $(MAKE) clean; cd $(ROOTDIR) diff --git a/make/config.mk b/make/config.mk index 508dbec3f..522a04d49 100644 --- a/make/config.mk +++ b/make/config.mk @@ -47,6 +47,10 @@ LIB_RABIT = librabit.a # path to libjvm.so LIBJVM=$(JAVA_HOME)/jre/lib/amd64/server +# path to gtest library (only used when $BUILD_TEST=1) +# there should be an include path in $GTEST_PATH/include and library in $GTEST_PATH/lib +GTEST_PATH = + # List of additional plugins, checkout plugin folder. # uncomment the following lines to include these plugins # you can also add your own plugin like this diff --git a/tests/cpp/data/test_metainfo.cc b/tests/cpp/data/test_metainfo.cc new file mode 100644 index 000000000..c4be0e0eb --- /dev/null +++ b/tests/cpp/data/test_metainfo.cc @@ -0,0 +1,34 @@ +// Copyright by Contributors +#include +#include + +TEST(MetaInfo, GetSet) { + xgboost::MetaInfo info; + + double double2[2] = {1.0, 2.0}; + EXPECT_EQ(info.GetRoot(1), 0) + << "When no root_index is given, was expecting default value 0"; + info.SetInfo("root_index", double2, xgboost::kDouble, 2); + EXPECT_EQ(info.GetRoot(1), 2.0f); + + EXPECT_EQ(info.labels.size(), 0); + info.SetInfo("label", double2, xgboost::kFloat32, 2); + EXPECT_EQ(info.labels.size(), 2); + + float float2[2] = {1.0f, 2.0f}; + EXPECT_EQ(info.GetWeight(1), 1.0f) + << "When no weights are given, was expecting default value 1"; + info.SetInfo("weight", float2, xgboost::kFloat32, 2); + EXPECT_EQ(info.GetWeight(1), 2.0f); + + uint32_t uint32_t2[2] = {1U, 2U}; + EXPECT_EQ(info.base_margin.size(), 0); + info.SetInfo("base_margin", uint32_t2, xgboost::kUInt32, 2); + EXPECT_EQ(info.base_margin.size(), 2); + + uint64_t uint64_t2[2] = {1U, 2U}; + EXPECT_EQ(info.group_ptr.size(), 0); + info.SetInfo("group", uint64_t2, xgboost::kUInt64, 2); + ASSERT_EQ(info.group_ptr.size(), 3); + EXPECT_EQ(info.group_ptr[2], 3); +} diff --git a/tests/cpp/test_main.cc b/tests/cpp/test_main.cc new file mode 100644 index 000000000..14525c7bd --- /dev/null +++ b/tests/cpp/test_main.cc @@ -0,0 +1,8 @@ +// Copyright by Contributors +#include + +int main(int argc, char ** argv) { + testing::InitGoogleTest(&argc, argv); + testing::FLAGS_gtest_death_test_style = "threadsafe"; + return RUN_ALL_TESTS(); +} diff --git a/tests/cpp/xgboost_test.mk b/tests/cpp/xgboost_test.mk new file mode 100644 index 000000000..6d24e0abf --- /dev/null +++ b/tests/cpp/xgboost_test.mk @@ -0,0 +1,23 @@ +UTEST_ROOT=tests/cpp +UTEST_OBJ_ROOT=build_$(UTEST_ROOT) +UNITTEST=$(UTEST_ROOT)/xgboost_test +UNITTEST_SRC=$(wildcard $(UTEST_ROOT)/*.cc $(UTEST_ROOT)/*/*.cc) +UNITTEST_OBJ=$(patsubst $(UTEST_ROOT)%.cc, $(UTEST_OBJ_ROOT)%.o, $(UNITTEST_SRC)) + +GTEST_LIB=$(GTEST_PATH)/lib/ +GTEST_INC=$(GTEST_PATH)/include/ + +UNITTEST_CFLAGS=$(CFLAGS) +UNITTEST_LDFLAGS=$(LDFLAGS) -L$(GTEST_LIB) -lgtest +UNITTEST_DEPS=lib/libxgboost.a $(DMLC_CORE)/libdmlc.a $(RABIT)/lib/$(LIB_RABIT) + +$(UTEST_OBJ_ROOT)/%.o: $(UTEST_ROOT)/%.cc + @mkdir -p $(@D) + $(CXX) $(UNITTEST_CFLAGS) -I$(GTEST_INC) -o $@ -c $< + +$(UNITTEST): $(UNITTEST_OBJ) $(UNITTEST_DEPS) + $(CXX) $(UNITTEST_CFLAGS) -o $@ $^ $(UNITTEST_LDFLAGS) + + +ALL_TEST=$(UNITTEST) +ALL_TEST_OBJ=$(UNITTEST_OBJ)