summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile10
-rw-r--r--common.mk13
-rw-r--r--debian/libreichwein-dev.install2
-rw-r--r--stringhelper.cpp (renamed from stringutil.cpp)10
-rw-r--r--stringhelper.h (renamed from stringutil.h)2
-rw-r--r--tests/Makefile87
-rw-r--r--tests/test-base64.cpp23
-rw-r--r--tests/test-file.cpp56
-rw-r--r--tests/test-mime.cpp23
-rw-r--r--tests/test-os.cpp22
-rw-r--r--tests/test-stringhelper.cpp23
-rw-r--r--tests/test-tempfile.cpp23
-rw-r--r--tests/test-url.cpp22
13 files changed, 304 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index 18725c8..391c626 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ PROGSRC=\
file.cpp \
mime.cpp \
os.cpp \
- stringutil.cpp \
+ stringhelper.cpp \
tempfile.cpp \
url.cpp
@@ -50,6 +50,8 @@ install:
$(PROJECTNAME).a: $(SRC:.cpp=.o)
ar rcs $@ $^
+dep: $(SRC:.cpp=.d)
+
%.d: %.cpp
$(CXX) $(CXXFLAGS) -MM -MP -MF $@ -c $<
@@ -68,6 +70,9 @@ $(DISTROS): deb-src
sudo pbuilder build --basetgz /var/cache/pbuilder/$@.tgz --buildresult result/$@ ../libreichwein_$(VERSION).dsc
-debsign result/$@/libreichwein_$(VERSION)_amd64.changes
+test:
+ $(MAKE) -C tests
+
# dependencies
ADD_DEP=Makefile
@@ -75,7 +80,8 @@ ADD_DEP=Makefile
# misc ---------------------------------------------------
clean:
-rm -f *.o *.a *.d $(SONAME1) $(SONAME2) $(SONAME3)
+ $(MAKE) -C tests clean
-.PHONY: clean all install
+.PHONY: clean all install test
-include $(wildcard $(SRC:.cpp=.d))
diff --git a/common.mk b/common.mk
index 9ec95e7..aa68033 100644
--- a/common.mk
+++ b/common.mk
@@ -47,20 +47,31 @@ endif
# -fprofile-instr-generate -fcoverage-mapping
# gcc:--coverage
-CXXFLAGS+=-Wall -I.
+CXXFLAGS+=-Wall
CXXFLAGS+=-Wpedantic -gdwarf-4
CXXFLAGS+=-fexceptions -Iexternal
ifeq ($(CXX),clang++-11)
CXXFLAGS+=-std=c++20 #-stdlib=libc++
+CXXTYPE=clang++
+LLVMPROFDATA=llvm-profdata-11
+LLVMCOV=llvm-cov-11
else ifeq ($(CXX),clang++-14)
CXXFLAGS+=-std=c++20 #-stdlib=libc++
+CXXTYPE=clang++
+LLVMPROFDATA=llvm-profdata-14
+LLVMCOV=llvm-cov-14
else ifeq ($(CXX),clang++-13)
CXXFLAGS+=-std=c++20 #-stdlib=libc++
+CXXTYPE=clang++
+LLVMPROFDATA=llvm-profdata-13
+LLVMCOV=llvm-cov-13
else ifeq ($(CXX),g++-11)
CXXFLAGS+=-std=c++20 #-stdlib=libc++
+CXXTYPE=g++
else
CXXFLAGS+=-std=c++17
+CXXTYPE=g++
endif
ifeq ($(CXX),clang++-11)
diff --git a/debian/libreichwein-dev.install b/debian/libreichwein-dev.install
index 6cb6588..38b90c8 100644
--- a/debian/libreichwein-dev.install
+++ b/debian/libreichwein-dev.install
@@ -2,7 +2,7 @@ usr/include/libreichwein/base64.h
usr/include/libreichwein/file.h
usr/include/libreichwein/mime.h
usr/include/libreichwein/os.h
-usr/include/libreichwein/stringutil.h
+usr/include/libreichwein/stringhelper.h
usr/include/libreichwein/tempfile.h
usr/include/libreichwein/url.h
usr/lib/libreichwein.so
diff --git a/stringutil.cpp b/stringhelper.cpp
index 6eb2899..af37499 100644
--- a/stringutil.cpp
+++ b/stringhelper.cpp
@@ -1,11 +1,11 @@
-#include "stringutil.h"
+#include "stringhelper.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <cstdarg>
-std::string Reichwein::Stringutil::strfmt(const char* fmt, ...)
+std::string Reichwein::Stringhelper::strfmt(const char* fmt, ...)
{
va_list args;
@@ -22,7 +22,7 @@ std::string Reichwein::Stringutil::strfmt(const char* fmt, ...)
return result;
}
-std::vector<std::string> Reichwein::Stringutil::split(std::string value, const std::string separators)
+std::vector<std::string> Reichwein::Stringhelper::split(std::string value, const std::string separators)
{
std::vector<std::string> result;
@@ -44,7 +44,7 @@ std::vector<std::string> Reichwein::Stringutil::split(std::string value, const s
return result;
}
-std::string Reichwein::Stringutil::join(std::vector<std::string> vs, std::string separator)
+std::string Reichwein::Stringhelper::join(std::vector<std::string> vs, std::string separator)
{
std::string s;
for (const auto& line : vs) {
@@ -56,7 +56,7 @@ std::string Reichwein::Stringutil::join(std::vector<std::string> vs, std::string
return s;
}
-bool Reichwein::Stringutil::startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list) {
+bool Reichwein::Stringhelper::startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list) {
for (const std::string& element : list) {
if (boost::algorithm::starts_with(boost::algorithm::to_lower_copy(s), boost::algorithm::to_lower_copy(element)))
return true;
diff --git a/stringutil.h b/stringhelper.h
index 510d2fd..7b27a02 100644
--- a/stringutil.h
+++ b/stringhelper.h
@@ -5,7 +5,7 @@
#define EXPORT __attribute__((visibility("default")))
-namespace Reichwein::Stringutil {
+namespace Reichwein::Stringhelper {
[[deprecated("Better use libfmt or std::format[C++20]")]]
EXPORT std::string strfmt(const char* fmt, ...);
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 <gtest/gtest.h>
+
+#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 <gtest/gtest.h>
-getFile /proc
+#include "file.h"
+
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+
+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 <gtest/gtest.h>
+
+#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 <gtest/gtest.h>
+
+#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 <gtest/gtest.h>
+
+#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 <gtest/gtest.h>
+
+#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 <gtest/gtest.h>
+
+#include "file.h"
+
+class URLTest: public ::testing::Test
+{
+protected:
+ URLTest(){
+ }
+
+ ~URLTest() override{
+ }
+
+ void SetUp() override
+ {
+ }
+
+ void TearDown() override
+ {
+ }
+
+};