Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple sample of Boost unit test
/**
* A simple sample of Boost unit test
*/
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <string>
#include "my_lib.hxx"
BOOST_AUTO_TEST_CASE(test_my_test_num)
{
/* test the mylib::my_test_num() function */
int src1 = 1;
int src2 = 3;
BOOST_CHECK((src1 + src2) == mylib::my_test_num(src1, src2));
}
BOOST_AUTO_TEST_CASE(test2)
{
std::string ret = mylib::my_test_str(std::string("123"), std::string("456"));
BOOST_CHECK_EQUAL(ret, std::string("123456"));
}
# CMake build script
cmake_minimum_required(VERSION 2.8)
# project name & version
project(ProgUnitTest)
# common settings (Boost libraries)
if (MSVC)
# Enable the static libraries on Windows
foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
# update to the static version of the run time library
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endforeach()
set(CMAKE_C_STANDARD_LIBRARIES "")
set(CMAKE_CXX_STANDARD_LIBRARIES "")
endif()
# enable boost static flag
unset(Boost_LIBRARIES)
set(Boost_USE_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
find_package(Boost REQUIRED COMPONENTS
unit_test_framework)
include_directories("${PROJECT_SOURCE_DIR}"
"${Boost_INCLUDE_DIRS}")
set(progunittest_boost_dbg_libs
"${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_DEBUG}")
set(progunittest_boost_opt_libs
"${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE}")
add_library(progunittest STATIC
my_lib.cxx)
add_executable(prog-unittest
"${PROJECT_SOURCE_DIR}/0_my_lib_test.cxx")
target_link_libraries(prog-unittest
debug "${progunittest_boost_dbg_libs}"
optimized "${progunittest_boost_opt_libs}"
progunittest)
/**
* The sample library
*/
#if defined(_MSC_VER)
# pragma warning (push)
# pragma warning (disable : 4819)
# include <boost/format.hpp>
# pragma warning (push)
# pragma warning (default : 4819)
#else
# include <boost/format.hpp>
#endif /* defined(_MSC_VER) */
#include "my_lib.hxx"
namespace mylib
{
int my_test_num(int src1, int src2)
{
return src1 + src2;
}
std::string my_test_str(const std::string &src1,
const std::string &src2)
{
boost::format fmt("%s%s");
return boost::str(fmt % src1 % src2);
}
}
/**
* The sample library
*/
#ifndef _MY_LIB_HXX_
#define _MY_LIB_HXX_ 1
#include <string>
namespace mylib
{
/**
* return = src1 + src2
*/
int my_test_num(int src1, int src2);
/**
* return = src1 + src2
*/
std::string my_test_str(const std::string &src1, const std::string &src2);
}
#endif /* !defined(_MY_LIB_HXX_)*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment