diff --git a/gtest/Makefile.gtest b/gtest/Makefile.gtest new file mode 100644 index 00000000000..c41c4bb583c --- /dev/null +++ b/gtest/Makefile.gtest @@ -0,0 +1,44 @@ +# Location of GTEST +GTEST_DIR = ../../googletest/googletest +GTEST_MAIN = ${GTEST_DIR}/make/gtest_main.a + +# BOINC Libraries +BOINC_DIR = .. +BOINC_LIB = ${BOINC_DIR}/sched/libsched.a ${BOINC_DIR}/lib/libboinc_crypt.a ${BOINC_DIR}/lib/libboinc.a -lpthread -L/usr/lib64/mysql -lmysqlclient -ldl -lz -L/usr/lib64 -lssl -lcrypto +BOINC_INC = -I ${BOINC_DIR}/sched -I ${BOINC_DIR}/db -I ${BOINC_DIR}/lib -I ${BOINC_DIR}/tools -I ${BOINC_DIR}/vda -I ${BOINC_DIR} + +# Additional Libraries +OTHER_INC = -I /usr/include/mysql + +# Flags passed to the preprocessor. +CPPFLAGS = -isystem $(GTEST_DIR)/include ${BOINC_INC} ${OTHER_INC} + +# Flags passed to the C++ compiler. +CXXFLAGS = -g -Wall -Wextra -Werror + +# Include all source files +#cppsrc = $(wildcard *.cpp) \ +# $(wildcard sched/*.cpp) \ +# $(wildcard lib/*.cpp) + +#obj = $(cppsrc:.cpp=.o) +obj = $(wildcard *.o) \ + $(wildcard sched/*.o) \ + $(wildcard lib/*.o) + +all: Sched Lib unitTests + +Sched: + cd sched ; make -f Makefile.gtest + +Lib: + cd lib ; make -f Makefile.gtest + +unitTests: Sched Lib + g++ $(CPPFLAGS) $(CXXFLAGS) $(wildcard sched/*.o) $(wildcard lib/*.o) -o $@ ${GTEST_MAIN} ${BOINC_LIB} + +clean: + rm -f *.o ${obj} unitTests + +test: + ./unitTests diff --git a/gtest/README.md b/gtest/README.md new file mode 100644 index 00000000000..8c93506407e --- /dev/null +++ b/gtest/README.md @@ -0,0 +1,21 @@ +This is the start gtest example for unit tests. To run, you should only need to run these commands. + +Setup: +You will need to have googletest downloaded and compiled on your local machine first. The Makefile expects it to be in the same directory level as boinc. (example: /home/user/boinc and /home/user/googletest) + + git clone https://github.com/google/googletest.git + cd googletest + git pull + cd googletest/make + make + +The next commands are done within the boinc/gtest folder. + +To compile the code. +make -f Makefile.gtest + +To run the tests. +make -f Makefile.gtest test + +To cleanup the old files. +make -f Makefile.gtest clean diff --git a/gtest/lib/Makefile.gtest b/gtest/lib/Makefile.gtest new file mode 100644 index 00000000000..d62f0b48142 --- /dev/null +++ b/gtest/lib/Makefile.gtest @@ -0,0 +1,25 @@ +# Location of GTEST +GTEST_DIR = ../../../googletest/googletest +GTEST_MAIN = ${GTEST_DIR}/make/gtest_main.a + +# BOINC Libraries +BOINC_DIR = ../.. +BOINC_LIB = ${BOINC_DIR}/sched/libsched.a ${BOINC_DIR}/lib/libboinc_crypt.a ${BOINC_DIR}/lib/libboinc.a ${BOINC_DIR}/sched/sched_types.o ${BOINC_DIR}/sched/handle_request.o -lpthread -L/usr/lib64/mysql -lmysqlclient -ldl -lz -L/usr/lib64 -lssl -lcrypto +BOINC_INC = -I ${BOINC_DIR}/sched -I ${BOINC_DIR}/db -I ${BOINC_DIR}/lib -I ${BOINC_DIR}/tools -I ${BOINC_DIR}/vda -I ${BOINC_DIR} + +# Additional Libraries +OTHER_INC = -I /usr/include/mysql + +# Flags passed to the preprocessor. +CPPFLAGS = -isystem $(GTEST_DIR)/include ${BOINC_INC} ${OTHER_INC} + +# Flags passed to the C++ compiler. +CXXFLAGS = -g -Wall -Wextra -Werror + +# Include all source files +cppsrc = $(wildcard *.cpp) + +obj = $(cppsrc:.cpp=.o) + +all: ${obj} + diff --git a/gtest/lib/test_str_util.cpp b/gtest/lib/test_str_util.cpp new file mode 100644 index 00000000000..ef2ffd8d527 --- /dev/null +++ b/gtest/lib/test_str_util.cpp @@ -0,0 +1,76 @@ +#include "gtest/gtest.h" +#include "common_defs.h" +#include "str_util.h" +#include +#include + +namespace test_str_util { + + // The fixture for testing class Foo. + + class test_str_util : public ::testing::Test { + protected: + // You can remove any or all of the following functions if its body + // is empty. + + test_str_util() { + // You can do set-up work for each test here. + } + + virtual ~test_str_util() { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + virtual void SetUp() { + // Code here will be called immediately after the constructor (right + // before each test). + } + + virtual void TearDown() { + // Code here will be called immediately after each test (right + // before the destructor). + } + + // Objects declared here can be used by all tests in the test case for Foo. + }; + + // Tests that Foo does Xyz. + + TEST_F(test_str_util, path_to_filename) { + std::string fname = ""; + ASSERT_EQ(path_to_filename("/home/blah", fname), 0); + ASSERT_EQ(fname, "blah"); + ASSERT_EQ(path_to_filename("hellokeith", fname), 0); + ASSERT_EQ(fname, "hellokeith"); + fname = ""; + ASSERT_EQ(path_to_filename("/home/blah/", fname), -2); + ASSERT_EQ(fname, ""); + ASSERT_EQ(path_to_filename("", fname), -1); + ASSERT_EQ(fname, ""); + } + + TEST_F(test_str_util, nbytes_to_string) { + char buf[256]; + nbytes_to_string(1024, 0, buf, sizeof (buf)); + ASSERT_STREQ(buf, "1.00 KB"); + nbytes_to_string(1024, 1024 * 1024, buf, sizeof (buf)); + ASSERT_STREQ(buf, "0.00/1.00 MB"); + nbytes_to_string(512, 1024, buf, sizeof (buf)); + ASSERT_STREQ(buf, "0.50/1.00 KB"); + nbytes_to_string(50000000000000, 0, buf, sizeof (buf)); + ASSERT_STREQ(buf, "45.47 TB"); + } + + TEST_F(test_str_util, strip_whitespace) { + std::string tmp = " white space "; + strip_whitespace(tmp); + ASSERT_EQ(tmp, "white space"); + tmp = "nospaces"; + strip_whitespace(tmp); + ASSERT_EQ(tmp, "nospaces"); + } + +} // namespace diff --git a/gtest/lib/test_url.cpp b/gtest/lib/test_url.cpp new file mode 100644 index 00000000000..43c87b4fb2e --- /dev/null +++ b/gtest/lib/test_url.cpp @@ -0,0 +1,49 @@ +#include "gtest/gtest.h" +#include "common_defs.h" +#include "url.h" +#include +#include + +namespace test_url { + + // The fixture for testing class Foo. + + class test_url : public ::testing::Test { + protected: + // You can remove any or all of the following functions if its body + // is empty. + + test_url() { + // You can do set-up work for each test here. + } + + virtual ~test_url() { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + virtual void SetUp() { + // Code here will be called immediately after the constructor (right + // before each test). + } + + virtual void TearDown() { + // Code here will be called immediately after each test (right + // before the destructor). + } + + // Objects declared here can be used by all tests in the test case for Foo. + }; + + // Tests that Foo does Xyz. + + TEST_F(test_url, is_https) { + ASSERT_EQ(is_https("hello"), false); + ASSERT_EQ(is_https("https://www.google.com"), true); + ASSERT_EQ(is_https("http://www.google.com"), false); + ASSERT_EQ(is_https("xhttps://www.google.com"), false); + } + +} // namespace diff --git a/gtest/sched/Makefile.gtest b/gtest/sched/Makefile.gtest new file mode 100644 index 00000000000..acc845b1963 --- /dev/null +++ b/gtest/sched/Makefile.gtest @@ -0,0 +1,33 @@ +# Location of GTEST +GTEST_DIR = ../../../googletest/googletest +GTEST_MAIN = ${GTEST_DIR}/make/gtest_main.a + +# BOINC Libraries +BOINC_DIR = ../.. +BOINC_LIB = ${BOINC_DIR}/sched/libsched.a ${BOINC_DIR}/lib/libboinc_crypt.a ${BOINC_DIR}/lib/libboinc.a ${BOINC_DIR}/sched/sched_types.o ${BOINC_DIR}/sched/handle_request.o -lpthread -L/usr/lib64/mysql -lmysqlclient -ldl -lz -L/usr/lib64 -lssl -lcrypto +BOINC_INC = -I ${BOINC_DIR}/sched -I ${BOINC_DIR}/db -I ${BOINC_DIR}/lib -I ${BOINC_DIR}/tools -I ${BOINC_DIR}/vda -I ${BOINC_DIR} + +# Additional Libraries +OTHER_INC = -I /usr/include/mysql + +# Flags passed to the preprocessor. +CPPFLAGS = -isystem $(GTEST_DIR)/include ${BOINC_INC} ${OTHER_INC} + +# Flags passed to the C++ compiler. +CXXFLAGS = -g -Wall -Wextra -Werror + +# Include all source files +cppsrc = $(wildcard *.cpp) + +obj = $(cppsrc:.cpp=.o) +#obj = $(wildcard *.o) \ +# $(wildcard sched/*.o) + +all: ${obj} + +#unitTests: ${obj} +# g++ $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ ${GTEST_MAIN} ${BOINC_LIB} + +#clean: +# rm -f *.o ${obj} unitTests + diff --git a/gtest/sched/test_credit.cpp b/gtest/sched/test_credit.cpp new file mode 100644 index 00000000000..e0b6123c799 --- /dev/null +++ b/gtest/sched/test_credit.cpp @@ -0,0 +1,50 @@ +#include "gtest/gtest.h" +#include "common_defs.h" +#include "credit.h" + +namespace test_credit { + + // The fixture for testing class Foo. + + class test_credit : public ::testing::Test { + protected: + // You can remove any or all of the following functions if its body + // is empty. + + test_credit() { + // You can do set-up work for each test here. + } + + virtual ~test_credit() { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + virtual void SetUp() { + // Code here will be called immediately after the constructor (right + // before each test). + } + + virtual void TearDown() { + // Code here will be called immediately after each test (right + // before the destructor). + } + + // Objects declared here can be used by all tests in the test case for Foo. + }; + + // Tests that Foo does Xyz. + + TEST_F(test_credit, fpops_to_credit) { + ASSERT_EQ(fpops_to_credit(1.0), COBBLESTONE_SCALE); + ASSERT_NE(fpops_to_credit(5.0), COBBLESTONE_SCALE); + ASSERT_EQ(fpops_to_credit(6000000.0), 6000000.0 * COBBLESTONE_SCALE); + } + + TEST_F(test_credit, cpu_time_to_credit) { + ASSERT_EQ(cpu_time_to_credit(1.0, 1.0), COBBLESTONE_SCALE); + } + +} // namespace