Skip to content

Instantly share code, notes, and snippets.

@pabigot
Last active January 4, 2016 00:49
Show Gist options
  • Save pabigot/8543958 to your computer and use it in GitHub Desktop.
Save pabigot/8543958 to your computer and use it in GitHub Desktop.
Files supporting a unit test comparison at http://www.pabigot.com/c/c-unit-test-frameworks/
#include <boost/test/unit_test.hpp>
#include <string>
#include <cmath>
BOOST_AUTO_TEST_CASE(StringStuffBasic)
{
const std::string str("text");
const char * const cstr{"no\0no\0"};
BOOST_CHECK_EQUAL(4, str.size());
BOOST_CHECK_NE(cstr, cstr+3);
}
BOOST_AUTO_TEST_CASE(FloatStuffBasic)
{
BOOST_CHECK_CLOSE(11.045, std::sqrt(122), 0.001);
}
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <cppunit/extensions/HelperMacros.h>
#include <string>
#include <cmath>
class testStringStuff : public CppUnit::TestFixture
{
protected:
void testBasic ()
{
const char * const cstr{"no\0no\0"};
const std::string str("text");
CPPUNIT_ASSERT_EQUAL(std::size_t{4}, str.size());
CPPUNIT_ASSERT(cstr != (cstr+3));
}
private:
CPPUNIT_TEST_SUITE(testStringStuff);
CPPUNIT_TEST(testBasic);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(testStringStuff);
class testFloatStuff : public CppUnit::TestFixture
{
protected:
void testBasic ()
{
CPPUNIT_ASSERT_DOUBLES_EQUAL(11.045, std::sqrt(122.0), 0.001);
}
private:
CPPUNIT_TEST_SUITE(testFloatStuff);
CPPUNIT_TEST(testBasic);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(testFloatStuff);
#include <iostream>
#include <memory>
#include <cppunit/Test.h>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCase.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestResult.h>
#include <cppunit/TextTestRunner.h>
void dumpTests (CppUnit::Test *tp, int level=0)
{
int nc = tp->getChildTestCount();
std::cout << "[" << level << "] ";
if (dynamic_cast<CppUnit::TestSuite*>(tp)) {
std::cout << "Test suite";
} else if (dynamic_cast<CppUnit::TestCase*>(tp)) {
std::cout << "Test case";
} else {
std::cout << "?Test?";
}
std::cout << " '" << tp->getName() << "' has " << nc << std::endl;
for (int i = 0; i < nc; i++) {
dumpTests(tp->getChildTestAt(i), level+1);
}
}
int
main (int argc,
char * argv [])
{
using namespace CppUnit;
/* Get the tests in the default "All Tests" suite. */
auto& registry = TestFactoryRegistry::getRegistry();
std::unique_ptr<Test> all_tests{registry.makeTest()};
//dumpTests(all_tests.get());
/* Create a test controller, and have it record the results of the
* tests and display progress. */
TestResult controller;
TestResultCollector results;
controller.addListener(&results);
BriefTestProgressListener progress;
controller.addListener(&progress);
/* Create the test runner and transfer test ownership to it. */
TextTestRunner runner;
runner.addTest(all_tests.release());
runner.run(controller);
CompilerOutputter(&results, std::cerr).write();
return results.wasSuccessful() ? 0 : 1;
}
#include <gtest/gtest.h>
#include <string>
#include <cmath>
TEST(StringStuff, Basic)
{
const std::string str("text");
const char * const cstr{"no\0no\0"};
ASSERT_EQ(4, str.size());
ASSERT_NE(cstr, cstr+3);
}
TEST(FloatStuff, Basic)
{
ASSERT_NEAR(11.045, std::sqrt(122.0), 0.001);
}
#include <gtest/gtest.h>
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
CXX_STD=c++1y
GCC_PATH=/usr/local/gcc
GTEST_ROOT=/opt/googletest
GMOCK_ROOT=/opt/googlemock
WITH_GTEST ?= 0
WITH_CPPUT ?= 0
WITH_BUTF ?= 0
BOOST_ROOT=$(GCC_PATH)
ifeq ($(BOOST_ROOT),$(GCC_PATH))
BOOST_IPATH=${GCC_PATH}/include
else # BOOST_ROOT
ifeq ($(BOOST_ROOT),/opt/boost)
BOOST_IPATH=${BOOST_ROOT}
BOOST_LDPATH=${BOOST_ROOT}/bin.v2/libs
BOOST_TOOLCHAIN=gcc-$(GCC_VERSION)/debug
BOOST_LDFLAGS= \
$(BOOST_MODULES:%=-L$(BOOST_LDPATH)/%/$(BOOST_TOOLCHAIN)) \
$(BOOST_MODULES:%=-Wl,-rpath,$(BOOST_LDPATH)/%/$(BOOST_TOOLCHAIN))
else
BOOST_IPATH=${BOOST_ROOT}/include
BOOST_LDFLAGS=L$(BOOST_ROOT)/lib -Wl,-rpath,$(BOOST_ROOT)/lib
endif # BOOST_ROOT
endif # BOOST_ROOT
BOOST_LDLIBS=$(BOOST_MODULES:%=-lboost_%)
CPPUNIT_PATH=$(GCC_PATH)
CPPUT_LDLIBS=-lcppunit
WITH_BOOST ?= $(WITH_BUTF)
ifneq ($(WITH_BOOST),0)
CPPFLAGS += -I$(BOOST_IPATH)
LDFLAGS+=$(BOOST_LDFLAGS)
LDLIBS+=$(BOOST_LDLIBS)
endif # WITH_BOOST
WARN_CXXFLAGS=-Wall -Werror
CXX=$(GCC_PATH)/bin/g++
OPT_CXXFLAGS=-g -O2
CPPFLAGS=-I../include -DWITH_BOOST=$(WITH_BOOST) $(AUX_CPPFLAGS)
CXXFLAGS=-std=$(CXX_STD) $(WARN_CXXFLAGS) $(OPT_CXXFLAGS) $(AUX_CXXFLAGS)
LDFLAGS=-Wl,-rpath,$(GCC_PATH)/lib64:$(GCC_PATH)/lib32:$(GCC_PATH)/lib
LINK.o=$(LINK.cc)
CLEAN ?=
REALCLEAN ?=
ifneq ($(WITH_GTEST), 0)
CXXFLAGS += -pthread
CPPFLAGS += -I$(GTEST_ROOT)/include -I$(GMOCK_ROOT)/include
GT_MODULES=eval
GT_LDLIBS=libgtest.a
GT_DEPS=gt_main.o $(GT_LDLIBS)
CLEAN += $(GT_MODULES:%=gt_%.o)
REALCLEAN += $(GT_MODULES:%=gt_%)
REALCLEAN += gt
gt: $(foreach m,$(GT_MODULES),gt_$(m).o) $(GT_DEPS)
$(LINK.cc) -o $@ $^
REALCLEAN += $(GT_DEPS)
libgtest.a:
$(COMPILE.cc) -iquote $(GTEST_ROOT) $(GTEST_ROOT)/src/gtest-all.cc
$(COMPILE.cc) -iquote $(GMOCK_ROOT) $(GMOCK_ROOT)/src/gmock-all.cc
$(AR) rv $@ gtest-all.o gmock-all.o
rm gtest-all.o gmock-all.o
gt_statistics.o: gt_statistics.cc ../include/pabutils/statistics.hpp
gt_statistics: gt_statistics.o $(GT_DEPS)
$(LINK.cc) -o $@ $^
gt_string_view.o: gt_string_view.cc
gt_string_view: gt_string_view.o $(GT_DEPS)
$(LINK.cc) -o $@ $^
endif # WITH_GTEST
ifneq ($(WITH_BUTF),0)
BUTF_LDLIBS=-lboost_unit_test_framework
BUTF_MODULES=eval
CLEAN += butf_main.o
CLEAN += butf_eval.o
REALCLEAN += cpput_eval
butf_eval: butf_main.o butf_eval.o $(BUTF_LDLIBS)
REALCLEAN += butf
butf: butf_main.o $(foreach m,$(BUTF_MODULES),butf_$(m).o)
$(LINK.cc) $^ $(LDLIBS) $(BUTF_LDLIBS) -o $@
endif # WITH_BUTF
ifneq ($(WITH_CPPUT),0)
CPPUT_MODULES=eval
CLEAN += cpput_main.o
CLEAN += cpput_eval.o
cpput_eval.o: cpput_eval.cc
REALCLEAN += cpput_eval
cpput_eval: cpput_main.o cpput_eval.o
REALCLEAN += cpput
cpput: cpput_main.o $(foreach m,$(CPPUT_MODULES),cpput_$(m).o)
$(LINK.cc) $^ $(LDLIBS) $(CPPUT_LDLIBS) -o $@
endif # WITH_CPPUT
.PHONY: clean realclean
clean:
-rm -rf $(CLEAN)
realclean: clean
-rm -rf $(REALCLEAN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment