From 0f2ac0c4311e4429bfa4ede1d96ce467b5dceb5b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 5 Jan 2023 10:37:41 +0100 Subject: Added tests --- tests/Makefile | 87 +++++++++++++++++++++++++++++++++++++++++++++ tests/test-base64.cpp | 23 ++++++++++++ tests/test-file.cpp | 56 +++++++++++++++++++++++++++-- tests/test-mime.cpp | 23 ++++++++++++ tests/test-os.cpp | 22 ++++++++++++ tests/test-stringhelper.cpp | 23 ++++++++++++ tests/test-tempfile.cpp | 23 ++++++++++++ tests/test-url.cpp | 22 ++++++++++++ 8 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 tests/Makefile create mode 100644 tests/test-base64.cpp create mode 100644 tests/test-mime.cpp create mode 100644 tests/test-os.cpp create mode 100644 tests/test-stringhelper.cpp create mode 100644 tests/test-tempfile.cpp create mode 100644 tests/test-url.cpp (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..44f50dc --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,87 @@ +CXXFLAGS=-g -O0 + +include ../common.mk + +ifeq ($(CXXTYPE),clang++) +CXXFLAGS+=-fprofile-instr-generate -fcoverage-mapping +LDFLAGS+=-fprofile-instr-generate -fcoverage-mapping +else +# GCC +CXXFLAGS+=--coverage +LDFLAGS+=--coverage +endif + +UNITS=\ + base64.cpp \ + file.cpp \ + mime.cpp \ + os.cpp \ + stringhelper.cpp \ + tempfile.cpp \ + url.cpp + +UNITTESTS=\ + test-base64.cpp \ + test-file.cpp \ + test-mime.cpp \ + test-os.cpp \ + test-stringhelper.cpp \ + test-tempfile.cpp \ + test-url.cpp + +CXXFLAGS+=\ + -I/usr/src/googletest/googletest/include \ + -I/usr/src/googletest/googlemock/include \ + -I/usr/src/googletest/googletest \ + -I/usr/src/googletest/googlemock \ + -I.. + +test: unittests + # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html +ifeq ($(CXXTYPE),clang++) + LLVM_PROFILE_FILE="unittests.profraw" ./unittests + $(LLVMPROFDATA) merge -sparse unittests.profraw -o unittests.profdata + $(LLVMCOV) report --ignore-filename-regex='google' --ignore-filename-regex='test-' --ignore-filename-regex='Magick' --show-region-summary=0 -instr-profile unittests.profdata unittests +else + ./unittests + gcovr -r .. +endif + +coverage: + $(LLVMCOV) show -instr-profile unittests.profdata $(UNITS:.cpp=.o) + +unittests: libgmock.a $(UNITTESTS:.cpp=.o) $(UNITS:.cpp=.o) + $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +base64.o: ../base64.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +file.o: ../file.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +mime.o: ../mime.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +os.o: ../os.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +stringhelper.o: ../stringhelper.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +tempfile.o: ../tempfile.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +url.o: ../url.cpp + $(CXX) $(CXXFLAGS) -o $@ -c $< + +libgmock.a: + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googletest/src/gtest-all.cc + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googlemock/src/gmock-all.cc + $(CXX) $(CXXFLAGS) -c /usr/src/googletest/googlemock/src/gmock_main.cc + ar -rv libgmock.a gmock-all.o gtest-all.o gmock_main.o + +clean: + -rm -f *.o *.a unittests *.gcda *.gcno *.profraw *.profdata *.gcov diff --git a/tests/test-base64.cpp b/tests/test-base64.cpp new file mode 100644 index 0000000..03648d4 --- /dev/null +++ b/tests/test-base64.cpp @@ -0,0 +1,23 @@ +#include + +#include "file.h" + +class Base64Test: public ::testing::Test +{ +protected: + Base64Test(){ + } + + ~Base64Test() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; + diff --git a/tests/test-file.cpp b/tests/test-file.cpp index 13d0ded..7c3b752 100644 --- a/tests/test-file.cpp +++ b/tests/test-file.cpp @@ -1,3 +1,55 @@ -getFile +#include -getFile /proc +#include "file.h" + +#include +#include +#include + +namespace fs = std::filesystem; + +namespace { + const fs::path testFilename{"testfile.txt"}; +} // namespace + +class FileTest: public ::testing::Test +{ +protected: + FileTest(){ + } + + ~FileTest() override{ + } + + void SetUp() override + { + std::error_code ec; + fs::remove(testFilename, ec); + } + + void TearDown() override + { + std::error_code ec; + fs::remove(testFilename, ec); + } + +}; + +TEST_F(FileTest, getFile) +{ + { + std::ofstream of(testFilename, std::ios::binary); + of << "abc"; + } + + std::string s{Reichwein::File::getFile(testFilename)}; + + EXPECT_EQ(s, "abc"); +} + +TEST_F(FileTest, getFile_proc) +{ + std::string s{Reichwein::File::getFile("/proc/cmdline")}; + + EXPECT_GT(s.size(), 0); +} diff --git a/tests/test-mime.cpp b/tests/test-mime.cpp new file mode 100644 index 0000000..2d28a36 --- /dev/null +++ b/tests/test-mime.cpp @@ -0,0 +1,23 @@ +#include + +#include "file.h" + +class MimeTest: public ::testing::Test +{ +protected: + MimeTest(){ + } + + ~MimeTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; + diff --git a/tests/test-os.cpp b/tests/test-os.cpp new file mode 100644 index 0000000..6177bfa --- /dev/null +++ b/tests/test-os.cpp @@ -0,0 +1,22 @@ +#include + +#include "file.h" + +class OsTest: public ::testing::Test +{ +protected: + OsTest(){ + } + + ~OsTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; diff --git a/tests/test-stringhelper.cpp b/tests/test-stringhelper.cpp new file mode 100644 index 0000000..3ece784 --- /dev/null +++ b/tests/test-stringhelper.cpp @@ -0,0 +1,23 @@ +#include + +#include "file.h" + +class StringhelperTest: public ::testing::Test +{ +protected: + StringhelperTest(){ + } + + ~StringhelperTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; + diff --git a/tests/test-tempfile.cpp b/tests/test-tempfile.cpp new file mode 100644 index 0000000..b9bef05 --- /dev/null +++ b/tests/test-tempfile.cpp @@ -0,0 +1,23 @@ +#include + +#include "file.h" + +class TempfileTest: public ::testing::Test +{ +protected: + TempfileTest(){ + } + + ~TempfileTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; + diff --git a/tests/test-url.cpp b/tests/test-url.cpp new file mode 100644 index 0000000..2f5075a --- /dev/null +++ b/tests/test-url.cpp @@ -0,0 +1,22 @@ +#include + +#include "file.h" + +class URLTest: public ::testing::Test +{ +protected: + URLTest(){ + } + + ~URLTest() override{ + } + + void SetUp() override + { + } + + void TearDown() override + { + } + +}; -- cgit v1.2.3