From e8459e5b58dfaa29da7d8f846dfcc20915916dd8 Mon Sep 17 00:00:00 2001 From: Eliran Levi Date: Wed, 8 Jun 2016 16:17:49 +0300 Subject: [PATCH] Add a CXXTest unittest framework --- .gitmodules | 3 ++ build.Makefile | 33 ++++++++++----- cpp/Unittests/MockTCPConnection.cpp | 32 +++++++++++++++ .../TestProtocolVersionNegotiator.cpp | 38 ++++++++++++++++++ cpp/Unittests/test_osmosis_main.h | 40 +++++++++++++++++++ cxxtest | 1 + targets.Makefile | 9 ++++- 7 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 .gitmodules create mode 100644 cpp/Unittests/MockTCPConnection.cpp create mode 100644 cpp/Unittests/TestProtocolVersionNegotiator.cpp create mode 100644 cpp/Unittests/test_osmosis_main.h create mode 160000 cxxtest diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f682a13 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cxxtest"] + path = cxxtest + url = https://github.com/CxxTest/cxxtest diff --git a/build.Makefile b/build.Makefile index f94345a..2b534ab 100644 --- a/build.Makefile +++ b/build.Makefile @@ -2,7 +2,7 @@ all: all-targets CONFIGURATION ?= RELEASE SHELL ?= /bin/bash -COMMON_CXXFLAGS = -Ic -Icpp -I/usr/include/python2.7 -Ibuild/cpp-netlib-0.11.1-final -std=gnu++0x -Werror -Wall +COMMON_CXXFLAGS = -Ic -Icpp -I/usr/include/python2.7 -Ibuild/cpp-netlib-0.11.1-final -Icxxtest -std=gnu++0x -Werror -Wall COMMON_CFLAGS = -Ic -Werror -Wall DEBUG_CXXFLAGS = -ggdb -DDEBUG @@ -36,12 +36,12 @@ $(error BOOST static libraries were not found) endif BOOST_MT = $(shell if [ -e $(STATIC_BOOST_LIBS_DIR)/libboost_thread-mt.a ]; then echo '-mt'; fi) + include targets.Makefile define template_per_OBJECT -include $(1).deps -endef -#endef template_per_OBJECT +endef #endef template_per_OBJECT define template_per_TARGET template_per_TARGET_target = $$(if $$(filter %.cpp,$$($(1))), \ @@ -89,30 +89,45 @@ endif build/cpp/%.o: cpp/%.cpp @mkdir -p $(@D) - @echo 'C++ ' $@ + @echo 'C++ ' $@ $(Q)g++ $(CXXFLAGS) $($(subst /,_,$(subst .,_,$*))_o_CFLAGS) -MMD -MF $@.deps -c $< -o $@ build/c/%.o: c/%.c @mkdir -p $(@D) - @echo 'C ' $@ + @echo 'C ' $@ $(Q)gcc $(CFLAGS) $($(subst /,_,$(subst .,_,$*))_o_CFLAGS) -MMD -MF $@.deps -c $< -o $@ build/cpp/%.bin: @mkdir -p $(@D) - @echo 'LINK++ ' $@ + @echo 'LINK++ ' $@ $(Q)g++ $(LDFLAGS) -o $@ $^ $($*_LDFLAGS) $($*_LIBRARIES) build/c/%.bin: @mkdir -p $(@D) - @echo 'LINK ' $@ + @echo 'LINK ' $@ $(Q)gcc $(LDFLAGS) -o $@ $^ $($*_LDFLAGS) $($*_LIBRARIES) build/cpp/%.so: @mkdir -p $(@D) - @echo 'LINK++ ' $@ + @echo 'LINK++ ' $@ $(Q)g++ -shared $(LDFLAGS) -o $@ $^ $($*_LDFLAGS) $($*_LIBRARIES) build/c/%.so: @mkdir -p $(@D) - @echo 'LINK ' $@ + @echo 'LINK ' $@ $(Q)gcc -shared $(LDFLAGS) -o $@ $^ $($*_LDFLAGS) $($*_LIBRARIES) + +build/cpp/Unittests/runner_Test%.o: build/cpp/Unittests/runner_%.cpp + @mkdir -p $(@D) + @echo 'C++ ' $@ + $(Q)g++ $(CXXFLAGS) $($(subst /,_,$(subst .,_,$*))_o_CFLAGS) -MMD -MF $@.deps -c $< -o $@ + +build/cpp/Unittests/runner_Test%.cpp: cpp/Unittests/Test%.cpp + @mkdir -p $(@D) + @echo 'CXXTESTGEN ' $@ + $(Q)cxxtest/bin/cxxtestgen --error-printer -o $@ $^ + +build/cpp/runner_Test%.bin: + @mkdir -p $(@D) + @echo 'LINKTEST++ ' $@ + $(Q)g++ $(LDFLAGS) -o $@ $^ $($*_LDFLAGS) $(runner_Test$*_LIBRARIES) $(CXXFLAGS) diff --git a/cpp/Unittests/MockTCPConnection.cpp b/cpp/Unittests/MockTCPConnection.cpp new file mode 100644 index 0000000..14e3ef4 --- /dev/null +++ b/cpp/Unittests/MockTCPConnection.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +namespace Osmosis +{ +TCPConnection::TCPConnection( const std::string & hostname, unsigned short port ): + _socket( _ioService ), + _endpoint(), + _tcpSocket( _socket ) +{ + std::cout << "Nothing happens here"; +} + +TCPSocket & TCPConnection::socket() +{ + return _tcpSocket; +} + +void TCPConnection::connect() +{ +} + +void TCPConnection::sendHandshake() +{ + Stream::AckOps( _tcpSocket ).wait( "Handshake" ); +} + +void TCPConnection::setTCPNoDelay() +{ +} +} diff --git a/cpp/Unittests/TestProtocolVersionNegotiator.cpp b/cpp/Unittests/TestProtocolVersionNegotiator.cpp new file mode 100644 index 0000000..a4158c2 --- /dev/null +++ b/cpp/Unittests/TestProtocolVersionNegotiator.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include + +std::mutex globalTraceLock; + +namespace Osmosis { +namespace Stream +{ + +AckOps::AckOps( TCPSocket & socket ): + _socket( socket ) +{} + +void AckOps::wait( const char * waitReason ) +{ +} + +void AckOps::sendAck() +{ +} + +} +} + +class ProtocolVersionNegotiatorTest : public CxxTest::TestSuite +{ +public: + void test_ProtocolNegotiation(void) + { + boost::asio::io_service ioService; + boost::asio::ip::tcp::socket boostSocket(ioService); + } +}; diff --git a/cpp/Unittests/test_osmosis_main.h b/cpp/Unittests/test_osmosis_main.h new file mode 100644 index 0000000..6db024f --- /dev/null +++ b/cpp/Unittests/test_osmosis_main.h @@ -0,0 +1,40 @@ +#include +#include +#include + +std::mutex globalTraceLock; + +namespace Osmosis +{ +class TCPConnection +{ +public: + TCPConnection( const std::string & hostname, unsigned short port ) + { + std::cout << "TCPConnection mock constructor"; + } + + TCPSocket & socket() { + + } + + void connect(); + +private: + + void sendHandshake(); + + void setTCPNoDelay(); + + TCPConnection( const TCPConnection & rhs ) = delete; + TCPConnection & operator= ( const TCPConnection & rhs ) = delete; +}; +} // namespace Osmosis + +class ProtocolVersionNegotiatorTest : public CxxTest::TestSuite +{ +public: + void test_ProtocolNegotiation(void) + { + } +}; diff --git a/cxxtest b/cxxtest new file mode 160000 index 0000000..191addd --- /dev/null +++ b/cxxtest @@ -0,0 +1 @@ +Subproject commit 191adddb3876ab389c0c856e1c03874bf70f8ee4 diff --git a/targets.Makefile b/targets.Makefile index 5a25a3d..a5919e6 100644 --- a/targets.Makefile +++ b/targets.Makefile @@ -1,4 +1,6 @@ -TARGET_osmosis = $(shell find cpp/Osmosis/ -iname "*.cpp") +osmosis_SOURCES = $(shell find cpp/Osmosis/ -type f -iname "*.cpp" -not -iname "Test*.cpp" -not -iname "runner_*.cpp" -not -iname "*main.cpp") +#osmosis_bin_sources = $(shell find cpp/Osmosis/ -type f -iname "*.cpp" -not -iname "Test*.cpp" -not -iname "*main.cpp") +TARGET_osmosis = ${osmosis_SOURCES} cpp/Osmosis/main.cpp osmosis_LIBRARIES = -pthread \ $(STATIC_BOOST_LIBS_DIR)/libboost_regex$(BOOST_MT).a \ $(STATIC_BOOST_LIBS_DIR)/libboost_filesystem$(BOOST_MT).a \ @@ -9,3 +11,8 @@ osmosis_LIBRARIES = -pthread \ TARGET_testtaskqueue = cpp/Common/WhiteboxTests/testtaskqueue.cpp testtaskqueue_LIBRARIES = -pthread $(STATIC_BOOST_LIBS_DIR)/libboost_system$(BOOST_MT).a + +TARGET_runner_TestProtocolVersionNegotiator = \ + build/cpp/Unittests/runner_TestProtocolVersionNegotiator.cpp \ + cpp/Unittests/MockTCPConnection.cpp +runner_TestProtocolVersionNegotiator_LIBRARIES = ${osmosis_LIBRARIES}