├── .gitattributes ├── .gitignore ├── .gitmodules ├── .travis.yml ├── AUTHORS ├── CMakeLists.txt ├── ChangeLog ├── INSTALL ├── LICENSE ├── Makefile.am ├── README.md ├── SECURITY.md ├── UnitTest++ ├── AssertException.cpp ├── AssertException.h ├── CheckMacros.h ├── Checks.cpp ├── Checks.h ├── CompositeTestReporter.cpp ├── CompositeTestReporter.h ├── Config.h ├── CurrentTest.cpp ├── CurrentTest.h ├── DeferredTestReporter.cpp ├── DeferredTestReporter.h ├── DeferredTestResult.cpp ├── DeferredTestResult.h ├── ExceptionMacros.h ├── ExecuteTest.h ├── HelperMacros.h ├── Makefile.am ├── MemoryOutStream.cpp ├── MemoryOutStream.h ├── Posix │ ├── SignalTranslator.cpp │ ├── SignalTranslator.h │ ├── TimeHelpers.cpp │ └── TimeHelpers.h ├── ReportAssert.cpp ├── ReportAssert.h ├── ReportAssertImpl.h ├── Test.cpp ├── Test.h ├── TestDetails.cpp ├── TestDetails.h ├── TestList.cpp ├── TestList.h ├── TestMacros.h ├── TestReporter.cpp ├── TestReporter.h ├── TestReporterStdout.cpp ├── TestReporterStdout.h ├── TestResults.cpp ├── TestResults.h ├── TestRunner.cpp ├── TestRunner.h ├── TestSuite.h ├── TimeConstraint.cpp ├── TimeConstraint.h ├── TimeHelpers.h ├── UnitTest++.h ├── UnitTestPP.h ├── Win32 │ ├── TimeHelpers.cpp │ └── TimeHelpers.h ├── XmlTestReporter.cpp ├── XmlTestReporter.h ├── unittestpp_vs2005.vcproj └── unittestpp_vs2008.vcproj ├── appveyor.yml ├── builds └── .gitignore ├── configure.ac └── tests ├── Main.cpp ├── Makefile.am ├── RecordingReporter.h ├── ScopedCurrentTest.h ├── TestAssertHandler.cpp ├── TestCheckMacros.cpp ├── TestChecks.cpp ├── TestCompositeTestReporter.cpp ├── TestCurrentTest.cpp ├── TestDeferredTestReporter.cpp ├── TestExceptions.cpp ├── TestMemoryOutStream.cpp ├── TestTest.cpp ├── TestTestList.cpp ├── TestTestMacros.cpp ├── TestTestResults.cpp ├── TestTestRunner.cpp ├── TestTestSuite.cpp ├── TestTimeConstraint.cpp ├── TestTimeConstraintMacro.cpp ├── TestUnitTestPP.cpp └── TestXmlTestReporter.cpp /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | 3 | *.h text 4 | *.cpp text 5 | 6 | # VS 20xx files 7 | *.sln text eol=crlf 8 | *.vcproj text eol=crlf 9 | *.vcxproj text eol=crlf 10 | *.vcxproj.filters text eol=crlf 11 | 12 | # VC6 files 13 | *.dsw text eol=crlf 14 | *.dsp text eol=crlf 15 | 16 | # Other 'windows-specific' files 17 | *.bat text eol=crlf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.a 3 | *.lo 4 | *.la 5 | 6 | # Visual Studio temp/user files 7 | *.user 8 | *.suo 9 | *.sdf 10 | *.opensdf 11 | *.ncb 12 | 13 | # Build output directories 14 | Release 15 | Debug 16 | MinSizeRel 17 | RelWithDebInfo 18 | ipch 19 | 20 | # files generated by autotools 21 | /aclocal.m4 22 | /autom4te.cache 23 | /config.guess 24 | /config.h 25 | /config.h.in 26 | /config.status 27 | /config.sub 28 | /configure 29 | /depcomp 30 | /install-sh 31 | /libtool 32 | /ltmain.sh 33 | /m4 34 | /missing 35 | /stamp-h* 36 | /test-driver 37 | /UnitTest++/TestUnitTest++ 38 | Makefile 39 | Makefile.in 40 | .deps 41 | .libs 42 | *.log 43 | *.trs 44 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "docs"] 2 | path = docs 3 | url = https://github.com/unittest-cpp/unittest-cpp.wiki.git 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | # Build with gcc and clang. 4 | compiler: 5 | - gcc 6 | - clang 7 | 8 | # Build both debug and release configurations, through use of an environment variable in the build matrix. 9 | env: 10 | - CONFIGURATION=Debug 11 | - CONFIGURATION=Release 12 | 13 | # Run cmake to generate makefiles in 'builds' directory. 14 | # No need to create it because it's part of the repo. 15 | # Pass along configuration type to cmake. 16 | before_script: 17 | - cd builds && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=$CONFIGURATION ../ && cd .. 18 | 19 | # Run make in the directory containing generated makefiles. 20 | script: 21 | - make -C builds 22 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | See README.md for the historic contributor list. 2 | 3 | The maintainer of UnitTest++ is Patrick Johnmeyer (pjohnmeyer@gmail.com). 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.1) 2 | project(UnitTest++) 3 | 4 | option(UTPP_USE_PLUS_SIGN "Set this to OFF is you with to use '-cpp' instead of '++' in lib/include paths" ON) 5 | 6 | if(MSVC14 OR MSVC12) 7 | # has the support we need 8 | else() 9 | include(CheckCXXCompilerFlag) 10 | CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) 11 | CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) 12 | if(COMPILER_SUPPORTS_CXX14) 13 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") 14 | elseif(COMPILER_SUPPORTS_CXX11) 15 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 16 | else() 17 | message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") 18 | endif() 19 | endif() 20 | 21 | # get the main sources 22 | file(GLOB headers_ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UnitTest++/*.h) 23 | file(GLOB sources_ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UnitTest++/*.cpp) 24 | source_group("" FILES ${headers_} ${sources_}) 25 | 26 | # get platform specific sources 27 | if (WIN32) 28 | add_definitions(-D_CRT_SECURE_NO_DEPRECATE) 29 | set(platformDir_ Win32) 30 | else() 31 | set(platformDir_ Posix) 32 | endif(WIN32) 33 | 34 | file(GLOB platformHeaders_ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UnitTest++/${platformDir_}/*.h) 35 | file(GLOB platformSources_ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} UnitTest++/${platformDir_}/*.cpp) 36 | source_group(${platformDir_} FILES ${platformHeaders_} ${platformSources_}) 37 | 38 | # create the lib 39 | add_library(UnitTest++ STATIC ${headers_} ${sources_} ${platformHeaders_} ${platformSources_}) 40 | 41 | if(${UTPP_USE_PLUS_SIGN}) 42 | set_target_properties(UnitTest++ PROPERTIES OUTPUT_NAME UnitTest++) 43 | endif() 44 | 45 | 46 | # build the test runner 47 | file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} tests/*.cpp tests/*.h) 48 | source_group( "" FILES ${TEST_SRCS}) 49 | add_executable(TestUnitTest++ ${TEST_SRCS}) 50 | include_directories(.) 51 | 52 | if(${UTPP_USE_PLUS_SIGN}) 53 | set_target_properties(TestUnitTest++ PROPERTIES OUTPUT_NAME TestUnitTest++) 54 | endif() 55 | 56 | target_link_libraries(TestUnitTest++ UnitTest++) 57 | 58 | # run unit tests as post build step 59 | add_custom_command(TARGET TestUnitTest++ 60 | POST_BUILD COMMAND TestUnitTest++ 61 | COMMENT "Running unit tests") 62 | 63 | # add install targets 64 | # need a custom install path? 65 | # define CMAKE_INSTALL_PREFIX to change root folder 66 | if(${UTPP_USE_PLUS_SIGN}) 67 | set (UTPP_INSTALL_DESTINATION "include/UnitTest++") 68 | else() 69 | set (UTPP_INSTALL_DESTINATION "include/UnitTestPP") 70 | endif() 71 | 72 | install(TARGETS UnitTest++ DESTINATION lib) 73 | install(FILES ${headers_} DESTINATION ${UTPP_INSTALL_DESTINATION}) 74 | install(FILES ${platformHeaders_} DESTINATION ${UTPP_INSTALL_DESTINATION}/${platformDir_}) -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/unittest-cpp/de058b8a685a0abd758de4826cb644bfc7c79d0d/ChangeLog -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006 Noel Llopis and Charles Nicholson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I m4 2 | EXTRA_DIST = docs 3 | 4 | include UnitTest++/Makefile.am 5 | include tests/Makefile.am 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/unittest-cpp/unittest-cpp.svg?branch=master)](https://travis-ci.org/unittest-cpp/unittest-cpp) 2 | [![Build status](https://ci.appveyor.com/api/projects/status/ffs2k8dddts5cyok/branch/master?svg=true)](https://ci.appveyor.com/project/pjohnmeyer/unittest-cpp/branch/master) 3 | 4 | UnitTest++ 5 | =========== 6 | 7 | UnitTest++ is a lightweight unit testing framework for C++. It was designed to do test-driven development on a wide variety of platforms. Simplicity, portability, speed, and small footprint are all very important aspects of UnitTest++. UnitTest++ is ANSI portable C++ and makes minimal use of advanced library and languages features, which means it should be easily portable to just about any platform. Out of the box, the following platforms are supported: 8 | 9 | * Windows 10 | * Linux 11 | * Mac OS X 12 | 13 | Documentation 14 | -------------- 15 | The full documentation for building and using UnitTest++ can be found on the [GitHub wiki page](https://github.com/unittest-cpp/unittest-cpp/wiki). The contents of this wiki are also included as a git submodule under the `docs` folder, so version-specific Markdown documentation is always available along with the download. 16 | 17 | Pre-requisites 18 | --------------- 19 | While there are currently some bundled makefiles and projects, UnitTest++ is primarily built and supported using [CMake](http://cmake.org). 20 | 21 | Downloading 22 | ------------ 23 | ### Latest (HEAD) ### 24 | 25 | Via git: 26 | 27 | git clone https://github.com/unittest-cpp/unittest-cpp 28 | 29 | Via svn: 30 | 31 | svn checkout https://github.com/unittest-cpp/unittest-cpp/trunk unittest-cpp 32 | 33 | ### Latest release (v1.4) ### 34 | 35 | Via git: 36 | 37 | git clone https://github.com/unittest-cpp/unittest-cpp 38 | cd unittest-cpp 39 | git checkout v1.4 40 | 41 | Via svn: 42 | 43 | svn checkout https://github.com/unittest-cpp/unittest-cpp/tags/v1.4 unittest-cpp 44 | 45 | License 46 | --------- 47 | *UnitTest++ is free software. You may copy, distribute, and modify it under 48 | the terms of the License contained in the file LICENSE distributed 49 | with this package. This license is the same as the MIT/X Consortium 50 | license.* 51 | 52 | Contributors 53 | -------------- 54 | ### [GitHub Contributor Graph](https://github.com/unittest-cpp/unittest-cpp/contributors) ### 55 | 56 | ### Current Maintainers: ### 57 | * Patrick Johnmeyer (pjohnmeyer@gmail.com) @pjohnmeyer 58 | * Charles Nicholson (charles.nicholson@gmail.com) @charlesnicholson 59 | 60 | ### Original Authors: ### 61 | * Noel Llopis (llopis@convexhull.com) 62 | * Charles Nicholson (charles.nicholson@gmail.com) 63 | 64 | ### Contributors not included in github history ### 65 | * Jim Tilander 66 | * Kim Grasman 67 | * Jonathan Jansson 68 | * Dirck Blaskey 69 | * Rory Driscoll 70 | * Dan Lind 71 | * Matt Kimmel -- Submitted with permission from Blue Fang Games 72 | * Anthony Moralez 73 | * Jeff Dixon 74 | * Randy Coulman 75 | * Lieven van der Heide 76 | 77 | Historic release notes 78 | ---------------------- 79 | 80 | ### Version 1.4 (2008-10-30) ### 81 | - CHECK macros work at arbitrary stack depth from inside TESTs. 82 | - Remove obsolete TEST_UTILITY macros 83 | - Predicated test execution (via TestRunner::RunTestsIf) 84 | - Better exception handling for fixture ctors/dtors. 85 | - VC6/7/8/9 support 86 | 87 | ### Version 1.3 (2007-4-22) ### 88 | - Removed dynamic memory allocations (other than streams) 89 | - MinGW support 90 | - Consistent (native) line endings 91 | - Minor bug fixing 92 | 93 | ### Version 1.2 (2006-10-29) ### 94 | - First pass at documentation. 95 | - More detailed error crash catching in fixtures. 96 | - Standard streams used for printing objects under check. This should allow the 97 | use of standard class types such as std::string or other custom classes with 98 | stream operators to ostream. 99 | - Standard streams can be optionally compiled off by defining UNITTEST_USE_CUSTOM_STREAMS 100 | in Config.h 101 | - Added named test suites 102 | - Added CHECK_ARRAY2D_CLOSE 103 | - Posix library name is libUnitTest++.a now 104 | - Floating point numbers are postfixed with 'f' in the failure reports 105 | 106 | ### Version 1.1 (2006-04-18) ### 107 | - CHECK macros do not have side effects even if one of the parameters changes state 108 | - Removed CHECK_ARRAY_EQUAL (too similar to CHECK_ARRAY_CLOSE) 109 | - Added local and global time constraints 110 | - Removed dependencies on strstream 111 | - Improved Posix signal to exception translator 112 | - Failing tests are added to Visual Studio's error list 113 | - Fixed Visual Studio projects to work with spaces in directories 114 | 115 | ### Version 1.0 (2006-03-15) ### 116 | - Initial release 117 | 118 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /UnitTest++/AssertException.cpp: -------------------------------------------------------------------------------- 1 | #include "AssertException.h" 2 | 3 | #ifndef UNITTEST_NO_EXCEPTIONS 4 | 5 | namespace UnitTest { 6 | 7 | AssertException::AssertException() 8 | { 9 | } 10 | 11 | AssertException::~AssertException() throw() 12 | { 13 | } 14 | 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /UnitTest++/AssertException.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_ASSERTEXCEPTION_H 2 | #define UNITTEST_ASSERTEXCEPTION_H 3 | 4 | #include "Config.h" 5 | #ifndef UNITTEST_NO_EXCEPTIONS 6 | 7 | #include "HelperMacros.h" 8 | #include 9 | 10 | namespace UnitTest { 11 | 12 | class UNITTEST_LINKAGE AssertException : public std::exception 13 | { 14 | public: 15 | AssertException(); 16 | virtual ~AssertException() throw(); 17 | }; 18 | 19 | } 20 | 21 | #endif 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /UnitTest++/CheckMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CHECKMACROS_H 2 | #define UNITTEST_CHECKMACROS_H 3 | 4 | #include "HelperMacros.h" 5 | #include "ExceptionMacros.h" 6 | #include "Checks.h" 7 | #include "AssertException.h" 8 | #include "MemoryOutStream.h" 9 | #include "TestDetails.h" 10 | #include "CurrentTest.h" 11 | #include "ReportAssertImpl.h" 12 | 13 | #ifdef CHECK 14 | #error UnitTest++ redefines CHECK 15 | #endif 16 | 17 | #ifdef CHECK_EQUAL 18 | #error UnitTest++ redefines CHECK_EQUAL 19 | #endif 20 | 21 | #ifdef CHECK_CLOSE 22 | #error UnitTest++ redefines CHECK_CLOSE 23 | #endif 24 | 25 | #ifdef CHECK_ARRAY_EQUAL 26 | #error UnitTest++ redefines CHECK_ARRAY_EQUAL 27 | #endif 28 | 29 | #ifdef CHECK_ARRAY_CLOSE 30 | #error UnitTest++ redefines CHECK_ARRAY_CLOSE 31 | #endif 32 | 33 | #ifdef CHECK_ARRAY2D_CLOSE 34 | #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE 35 | #endif 36 | 37 | #define CHECK(value) \ 38 | UNITTEST_MULTILINE_MACRO_BEGIN \ 39 | UT_TRY \ 40 | ({ \ 41 | if (!UnitTest::Check(value)) \ 42 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ 43 | }) \ 44 | UT_CATCH (std::exception, e, \ 45 | { \ 46 | UnitTest::MemoryOutStream message; \ 47 | message << "Unhandled exception (" << e.what() << ") in CHECK(" #value ")"; \ 48 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 49 | message.GetText()); \ 50 | }) \ 51 | UT_CATCH_ALL \ 52 | ({ \ 53 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 54 | "Unhandled exception in CHECK(" #value ")"); \ 55 | }) \ 56 | UNITTEST_MULTILINE_MACRO_END 57 | 58 | #define CHECK_EQUAL(expected, actual) \ 59 | UNITTEST_MULTILINE_MACRO_BEGIN \ 60 | UT_TRY \ 61 | ({ \ 62 | UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 63 | }) \ 64 | UT_CATCH (std::exception, e, \ 65 | { \ 66 | UnitTest::MemoryOutStream message; \ 67 | message << "Unhandled exception (" << e.what() << ") in CHECK_EQUAL(" #expected ", " #actual ")"; \ 68 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 69 | message.GetText()); \ 70 | }) \ 71 | UT_CATCH_ALL \ 72 | ({ \ 73 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 74 | "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ 75 | }) \ 76 | UNITTEST_MULTILINE_MACRO_END 77 | 78 | #define CHECK_CLOSE(expected, actual, tolerance) \ 79 | UNITTEST_MULTILINE_MACRO_BEGIN \ 80 | UT_TRY \ 81 | ({ \ 82 | UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 83 | }) \ 84 | UT_CATCH (std::exception, e, \ 85 | { \ 86 | UnitTest::MemoryOutStream message; \ 87 | message << "Unhandled exception (" << e.what() << ") in CHECK_CLOSE(" #expected ", " #actual ")"; \ 88 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 89 | message.GetText()); \ 90 | }) \ 91 | UT_CATCH_ALL \ 92 | ({ \ 93 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 94 | "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ 95 | }) \ 96 | UNITTEST_MULTILINE_MACRO_END 97 | 98 | #define CHECK_ARRAY_EQUAL(expected, actual, count) \ 99 | UNITTEST_MULTILINE_MACRO_BEGIN \ 100 | UT_TRY \ 101 | ({ \ 102 | UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 103 | }) \ 104 | UT_CATCH (std::exception, e, \ 105 | { \ 106 | UnitTest::MemoryOutStream message; \ 107 | message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"; \ 108 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 109 | message.GetText()); \ 110 | }) \ 111 | UT_CATCH_ALL \ 112 | ({ \ 113 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 114 | "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \ 115 | }) \ 116 | UNITTEST_MULTILINE_MACRO_END 117 | 118 | #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \ 119 | UNITTEST_MULTILINE_MACRO_BEGIN \ 120 | UT_TRY \ 121 | ({ \ 122 | UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 123 | }) \ 124 | UT_CATCH (std::exception, e, \ 125 | { \ 126 | UnitTest::MemoryOutStream message; \ 127 | message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"; \ 128 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 129 | message.GetText()); \ 130 | }) \ 131 | UT_CATCH_ALL \ 132 | ({ \ 133 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 134 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ 135 | }) \ 136 | UNITTEST_MULTILINE_MACRO_END 137 | 138 | #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \ 139 | UNITTEST_MULTILINE_MACRO_BEGIN \ 140 | UT_TRY \ 141 | ({ \ 142 | UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 143 | }) \ 144 | UT_CATCH (std::exception, e, \ 145 | { \ 146 | UnitTest::MemoryOutStream message; \ 147 | message << "Unhandled exception (" << e.what() << ") in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"; \ 148 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 149 | message.GetText()); \ 150 | }) \ 151 | UT_CATCH_ALL \ 152 | ({ \ 153 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 154 | "Unhandled exception in CHECK_ARRAY2D_CLOSE(" #expected ", " #actual ")"); \ 155 | }) \ 156 | UNITTEST_MULTILINE_MACRO_END 157 | 158 | 159 | // CHECK_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h) 160 | #ifndef UNITTEST_NO_EXCEPTIONS 161 | #define CHECK_THROW(expression, ExpectedExceptionType) \ 162 | UNITTEST_MULTILINE_MACRO_BEGIN \ 163 | bool caught_ = false; \ 164 | try { expression; } \ 165 | catch (ExpectedExceptionType const&) { caught_ = true; } \ 166 | catch (...) {} \ 167 | if (!caught_) \ 168 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ 169 | UNITTEST_MULTILINE_MACRO_END 170 | 171 | 172 | #define CHECK_ASSERT(expression) \ 173 | UNITTEST_MULTILINE_MACRO_BEGIN \ 174 | UnitTest::Detail::ExpectAssert(true); \ 175 | CHECK_THROW(expression, UnitTest::AssertException); \ 176 | UnitTest::Detail::ExpectAssert(false); \ 177 | UNITTEST_MULTILINE_MACRO_END 178 | #endif 179 | #endif 180 | -------------------------------------------------------------------------------- /UnitTest++/Checks.cpp: -------------------------------------------------------------------------------- 1 | #include "Checks.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | namespace { 7 | 8 | void CheckStringsEqual(TestResults& results, char const* expected, char const* actual, 9 | TestDetails const& details) 10 | { 11 | using namespace std; 12 | 13 | if ((expected && actual) ? strcmp(expected, actual) : (expected || actual)) 14 | { 15 | UnitTest::MemoryOutStream stream; 16 | stream << "Expected " << (expected ? expected : "") << " but was " << (actual ? actual : ""); 17 | 18 | results.OnTestFailure(details, stream.GetText()); 19 | } 20 | } 21 | 22 | } 23 | 24 | 25 | void CheckEqual(TestResults& results, char const* expected, char const* actual, 26 | TestDetails const& details) 27 | { 28 | CheckStringsEqual(results, expected, actual, details); 29 | } 30 | 31 | void CheckEqual(TestResults& results, char* expected, char* actual, 32 | TestDetails const& details) 33 | { 34 | CheckStringsEqual(results, expected, actual, details); 35 | } 36 | 37 | void CheckEqual(TestResults& results, char* expected, char const* actual, 38 | TestDetails const& details) 39 | { 40 | CheckStringsEqual(results, expected, actual, details); 41 | } 42 | 43 | void CheckEqual(TestResults& results, char const* expected, char* actual, 44 | TestDetails const& details) 45 | { 46 | CheckStringsEqual(results, expected, actual, details); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /UnitTest++/Checks.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CHECKS_H 2 | #define UNITTEST_CHECKS_H 3 | 4 | #include "Config.h" 5 | #include "TestResults.h" 6 | #include "MemoryOutStream.h" 7 | 8 | namespace UnitTest { 9 | 10 | 11 | template< typename Value > 12 | bool Check(Value const value) 13 | { 14 | return !!value; // doing double negative to avoid silly VS warnings 15 | } 16 | 17 | 18 | template< typename Expected, typename Actual > 19 | void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details) 20 | { 21 | if (!(expected == actual)) 22 | { 23 | UnitTest::MemoryOutStream stream; 24 | stream << "Expected " << expected << " but was " << actual; 25 | 26 | results.OnTestFailure(details, stream.GetText()); 27 | } 28 | } 29 | 30 | UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details); 31 | 32 | UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details); 33 | 34 | UNITTEST_LINKAGE void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details); 35 | 36 | UNITTEST_LINKAGE void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details); 37 | 38 | template< typename Expected, typename Actual, typename Tolerance > 39 | bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance) 40 | { 41 | return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance)); 42 | } 43 | 44 | template< typename Expected, typename Actual, typename Tolerance > 45 | void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance, 46 | TestDetails const& details) 47 | { 48 | if (!AreClose(expected, actual, tolerance)) 49 | { 50 | UnitTest::MemoryOutStream stream; 51 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 52 | 53 | results.OnTestFailure(details, stream.GetText()); 54 | } 55 | } 56 | 57 | 58 | template< typename Expected, typename Actual > 59 | void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual, 60 | int const count, TestDetails const& details) 61 | { 62 | bool equal = true; 63 | for (int i = 0; i < count; ++i) 64 | equal &= (expected[i] == actual[i]); 65 | 66 | if (!equal) 67 | { 68 | UnitTest::MemoryOutStream stream; 69 | 70 | stream << "Expected [ "; 71 | 72 | for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) 73 | stream << expected[expectedIndex] << " "; 74 | 75 | stream << "] but was [ "; 76 | 77 | for (int actualIndex = 0; actualIndex < count; ++actualIndex) 78 | stream << actual[actualIndex] << " "; 79 | 80 | stream << "]"; 81 | 82 | results.OnTestFailure(details, stream.GetText()); 83 | } 84 | } 85 | 86 | template< typename Expected, typename Actual, typename Tolerance > 87 | bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance) 88 | { 89 | bool equal = true; 90 | for (int i = 0; i < count; ++i) 91 | equal &= AreClose(expected[i], actual[i], tolerance); 92 | return equal; 93 | } 94 | 95 | template< typename Expected, typename Actual, typename Tolerance > 96 | void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual, 97 | int const count, Tolerance const& tolerance, TestDetails const& details) 98 | { 99 | bool equal = ArrayAreClose(expected, actual, count, tolerance); 100 | 101 | if (!equal) 102 | { 103 | UnitTest::MemoryOutStream stream; 104 | 105 | stream << "Expected [ "; 106 | for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) 107 | stream << expected[expectedIndex] << " "; 108 | stream << "] +/- " << tolerance << " but was [ "; 109 | 110 | for (int actualIndex = 0; actualIndex < count; ++actualIndex) 111 | stream << actual[actualIndex] << " "; 112 | stream << "]"; 113 | 114 | results.OnTestFailure(details, stream.GetText()); 115 | } 116 | } 117 | 118 | template< typename Expected, typename Actual, typename Tolerance > 119 | void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual, 120 | int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details) 121 | { 122 | bool equal = true; 123 | for (int i = 0; i < rows; ++i) 124 | equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance); 125 | 126 | if (!equal) 127 | { 128 | UnitTest::MemoryOutStream stream; 129 | 130 | stream << "Expected [ "; 131 | 132 | for (int expectedRow = 0; expectedRow < rows; ++expectedRow) 133 | { 134 | stream << "[ "; 135 | for (int expectedColumn = 0; expectedColumn < columns; ++expectedColumn) 136 | stream << expected[expectedRow][expectedColumn] << " "; 137 | stream << "] "; 138 | } 139 | 140 | stream << "] +/- " << tolerance << " but was [ "; 141 | 142 | for (int actualRow = 0; actualRow < rows; ++actualRow) 143 | { 144 | stream << "[ "; 145 | for (int actualColumn = 0; actualColumn < columns; ++actualColumn) 146 | stream << actual[actualRow][actualColumn] << " "; 147 | stream << "] "; 148 | } 149 | 150 | stream << "]"; 151 | 152 | results.OnTestFailure(details, stream.GetText()); 153 | } 154 | } 155 | 156 | } 157 | 158 | #endif 159 | -------------------------------------------------------------------------------- /UnitTest++/CompositeTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "CompositeTestReporter.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | CompositeTestReporter::CompositeTestReporter() 7 | : m_reporterCount(0) 8 | { 9 | } 10 | 11 | int CompositeTestReporter::GetReporterCount() const 12 | { 13 | return m_reporterCount; 14 | } 15 | 16 | bool CompositeTestReporter::AddReporter(TestReporter* reporter) 17 | { 18 | if (m_reporterCount == kMaxReporters) 19 | return false; 20 | 21 | m_reporters[m_reporterCount++] = reporter; 22 | return true; 23 | } 24 | 25 | bool CompositeTestReporter::RemoveReporter(TestReporter* reporter) 26 | { 27 | for (int index = 0; index < m_reporterCount; ++index) 28 | { 29 | if (m_reporters[index] == reporter) 30 | { 31 | m_reporters[index] = m_reporters[m_reporterCount - 1]; 32 | --m_reporterCount; 33 | return true; 34 | } 35 | } 36 | 37 | return false; 38 | } 39 | 40 | void CompositeTestReporter::ReportFailure(TestDetails const& details, char const* failure) 41 | { 42 | for (int index = 0; index < m_reporterCount; ++index) 43 | m_reporters[index]->ReportFailure(details, failure); 44 | } 45 | 46 | void CompositeTestReporter::ReportTestStart(TestDetails const& test) 47 | { 48 | for (int index = 0; index < m_reporterCount; ++index) 49 | m_reporters[index]->ReportTestStart(test); 50 | } 51 | 52 | void CompositeTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) 53 | { 54 | for (int index = 0; index < m_reporterCount; ++index) 55 | m_reporters[index]->ReportTestFinish(test, secondsElapsed); 56 | } 57 | 58 | void CompositeTestReporter::ReportSummary(int totalTestCount, 59 | int failedTestCount, 60 | int failureCount, 61 | float secondsElapsed) 62 | { 63 | for (int index = 0; index < m_reporterCount; ++index) 64 | m_reporters[index]->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /UnitTest++/CompositeTestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_COMPOSITETESTREPORTER_H 2 | #define UNITTEST_COMPOSITETESTREPORTER_H 3 | 4 | #include "TestReporter.h" 5 | 6 | namespace UnitTest { 7 | 8 | class UNITTEST_LINKAGE CompositeTestReporter : public TestReporter 9 | { 10 | public: 11 | CompositeTestReporter(); 12 | 13 | int GetReporterCount() const; 14 | bool AddReporter(TestReporter* reporter); 15 | bool RemoveReporter(TestReporter* reporter); 16 | 17 | virtual void ReportTestStart(TestDetails const& test); 18 | virtual void ReportFailure(TestDetails const& test, char const* failure); 19 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); 20 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 21 | 22 | private: 23 | enum { kMaxReporters = 16 }; 24 | TestReporter* m_reporters[kMaxReporters]; 25 | int m_reporterCount; 26 | 27 | // revoked 28 | CompositeTestReporter(const CompositeTestReporter&); 29 | CompositeTestReporter& operator =(const CompositeTestReporter&); 30 | }; 31 | 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /UnitTest++/Config.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CONFIG_H 2 | #define UNITTEST_CONFIG_H 3 | 4 | // Standard defines documented here: http://predef.sourceforge.net 5 | 6 | #if defined(_MSC_VER) 7 | #pragma warning(disable:4702) // unreachable code 8 | #pragma warning(disable:4722) // destructor never returns, potential memory leak 9 | 10 | #if (_MSC_VER == 1200) // VC6 11 | #define UNITTEST_COMPILER_IS_MSVC6 12 | #pragma warning(disable:4786) 13 | #pragma warning(disable:4290) 14 | #endif 15 | 16 | #ifdef _USRDLL 17 | #define UNITTEST_WIN32_DLL 18 | #endif 19 | #define UNITTEST_WIN32 20 | #endif 21 | 22 | #if defined(unix) || defined(__unix__) || defined(__unix) || defined(linux) || \ 23 | defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) 24 | #define UNITTEST_POSIX 25 | #endif 26 | 27 | #if defined(__MINGW32__) 28 | #define UNITTEST_MINGW 29 | #endif 30 | 31 | 32 | // By default, MemoryOutStream is implemented in terms of std::ostringstream. 33 | // This is useful if you are using the CHECK macros on objects that have something like this defined: 34 | // std::ostringstream& operator<<(std::ostringstream& s, const YourObject& value) 35 | // 36 | // On the other hand, it can be more expensive. 37 | // Un-comment this line to use the custom MemoryOutStream (no deps on std::ostringstream). 38 | 39 | // #define UNITTEST_USE_CUSTOM_STREAMS 40 | 41 | // Developer note: This dual-macro setup is to preserve compatibility with UnitTest++ 1.4 users 42 | // who may have used or defined UNITTEST_USE_CUSTOM_STREAMS outside of this configuration file, as 43 | // well as Google Code HEAD users that may have used or defined 44 | // UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM outside of this configuration file. 45 | #ifndef UNITTEST_USE_CUSTOM_STREAMS 46 | #define UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 47 | #endif 48 | 49 | // DeferredTestReporter uses the STL to collect test results for subsequent export by reporters like 50 | // XmlTestReporter. If you don't want to use this functionality, uncomment this line and no STL 51 | // headers or code will be compiled into UnitTest++ 52 | 53 | //#define UNITTEST_NO_DEFERRED_REPORTER 54 | 55 | 56 | // By default, asserts that you report via UnitTest::ReportAssert() abort the current test and 57 | // continue to the next one by throwing an exception, which unwinds the stack naturally, destroying 58 | // all auto variables on its way back down. If you don't want to (or can't) use exceptions for your 59 | // platform/compiler, uncomment this line. All exception code will be removed from UnitTest++, 60 | // assert recovery will be done via setjmp/longjmp, and NO correct stack unwinding will happen! 61 | 62 | //#define UNITTEST_NO_EXCEPTIONS 63 | 64 | 65 | // std namespace qualification: used for functions like strcpy that 66 | // may live in std:: namespace (cstring header). 67 | #if defined( UNITTEST_COMPILER_IS_MSVC6 ) 68 | #define UNIITEST_NS_QUAL_STD(x) x 69 | #else 70 | #define UNIITEST_NS_QUAL_STD(x) ::std::x 71 | #endif 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /UnitTest++/CurrentTest.cpp: -------------------------------------------------------------------------------- 1 | #include "CurrentTest.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | UNITTEST_LINKAGE TestResults*& CurrentTest::Results() 7 | { 8 | static TestResults* testResults = NULL; 9 | return testResults; 10 | } 11 | 12 | UNITTEST_LINKAGE const TestDetails*& CurrentTest::Details() 13 | { 14 | static const TestDetails* testDetails = NULL; 15 | return testDetails; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /UnitTest++/CurrentTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CURRENTTESTRESULTS_H 2 | #define UNITTEST_CURRENTTESTRESULTS_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestResults; 9 | class TestDetails; 10 | 11 | namespace CurrentTest 12 | { 13 | UNITTEST_LINKAGE TestResults*& Results(); 14 | UNITTEST_LINKAGE const TestDetails*& Details(); 15 | } 16 | 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /UnitTest++/DeferredTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 3 | 4 | #include "DeferredTestReporter.h" 5 | #include "TestDetails.h" 6 | 7 | using namespace UnitTest; 8 | 9 | void DeferredTestReporter::ReportTestStart(TestDetails const& details) 10 | { 11 | m_results.push_back(DeferredTestResult(details.suiteName, details.testName)); 12 | } 13 | 14 | void DeferredTestReporter::ReportFailure(TestDetails const& details, char const* failure) 15 | { 16 | DeferredTestResult& r = m_results.back(); 17 | r.failed = true; 18 | r.failures.push_back(DeferredTestFailure(details.lineNumber, failure)); 19 | r.failureFile = details.filename; 20 | } 21 | 22 | void DeferredTestReporter::ReportTestFinish(TestDetails const&, float secondsElapsed) 23 | { 24 | DeferredTestResult& r = m_results.back(); 25 | r.timeElapsed = secondsElapsed; 26 | } 27 | 28 | DeferredTestReporter::DeferredTestResultList& DeferredTestReporter::GetResults() 29 | { 30 | return m_results; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /UnitTest++/DeferredTestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_DEFERREDTESTREPORTER_H 2 | #define UNITTEST_DEFERREDTESTREPORTER_H 3 | 4 | #include "Config.h" 5 | 6 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 7 | 8 | #include "TestReporter.h" 9 | #include "DeferredTestResult.h" 10 | 11 | #include 12 | 13 | UNITTEST_STDVECTOR_LINKAGE(UnitTest::DeferredTestResult); 14 | 15 | namespace UnitTest 16 | { 17 | 18 | class UNITTEST_LINKAGE DeferredTestReporter : public TestReporter 19 | { 20 | public: 21 | virtual void ReportTestStart(TestDetails const& details); 22 | virtual void ReportFailure(TestDetails const& details, char const* failure); 23 | virtual void ReportTestFinish(TestDetails const& details, float secondsElapsed); 24 | 25 | typedef std::vector< DeferredTestResult > DeferredTestResultList; 26 | DeferredTestResultList& GetResults(); 27 | 28 | private: 29 | DeferredTestResultList m_results; 30 | }; 31 | 32 | } 33 | 34 | #endif 35 | #endif 36 | -------------------------------------------------------------------------------- /UnitTest++/DeferredTestResult.cpp: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 3 | 4 | #include "DeferredTestResult.h" 5 | #include 6 | 7 | namespace UnitTest 8 | { 9 | 10 | DeferredTestFailure::DeferredTestFailure() 11 | : lineNumber(-1) 12 | { 13 | failureStr[0] = '\0'; 14 | } 15 | 16 | DeferredTestFailure::DeferredTestFailure(int lineNumber_, const char* failureStr_) 17 | : lineNumber(lineNumber_) 18 | { 19 | UNIITEST_NS_QUAL_STD(strcpy)(failureStr, failureStr_); 20 | } 21 | 22 | DeferredTestResult::DeferredTestResult() 23 | : suiteName("") 24 | , testName("") 25 | , failureFile("") 26 | , timeElapsed(0.0f) 27 | , failed(false) 28 | { 29 | } 30 | 31 | DeferredTestResult::DeferredTestResult(char const* suite, char const* test) 32 | : suiteName(suite) 33 | , testName(test) 34 | , failureFile("") 35 | , timeElapsed(0.0f) 36 | , failed(false) 37 | { 38 | } 39 | 40 | DeferredTestResult::~DeferredTestResult() 41 | { 42 | } 43 | 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /UnitTest++/DeferredTestResult.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_DEFERREDTESTRESULT_H 2 | #define UNITTEST_DEFERREDTESTRESULT_H 3 | 4 | #include "Config.h" 5 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 6 | 7 | #include "HelperMacros.h" 8 | #include 9 | #include 10 | 11 | namespace UnitTest 12 | { 13 | 14 | class UNITTEST_LINKAGE DeferredTestFailure 15 | { 16 | public: 17 | DeferredTestFailure(); 18 | DeferredTestFailure(int lineNumber_, const char* failureStr_); 19 | 20 | int lineNumber; 21 | char failureStr[1024]; 22 | }; 23 | 24 | } 25 | 26 | UNITTEST_STDVECTOR_LINKAGE(UnitTest::DeferredTestFailure); 27 | 28 | namespace UnitTest 29 | { 30 | 31 | class UNITTEST_LINKAGE DeferredTestResult 32 | { 33 | public: 34 | DeferredTestResult(); 35 | DeferredTestResult(char const* suite, char const* test); 36 | ~DeferredTestResult(); 37 | 38 | std::string suiteName; 39 | std::string testName; 40 | std::string failureFile; 41 | 42 | typedef std::vector< DeferredTestFailure > FailureVec; 43 | FailureVec failures; 44 | 45 | float timeElapsed; 46 | bool failed; 47 | }; 48 | 49 | } 50 | 51 | #endif 52 | #endif 53 | -------------------------------------------------------------------------------- /UnitTest++/ExceptionMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_EXCEPTIONMACROS_H 2 | #define UNITTEST_EXCEPTIONMACROS_H 3 | 4 | #include "Config.h" 5 | 6 | #ifndef UNITTEST_NO_EXCEPTIONS 7 | #define UT_TRY(x) try x 8 | #define UT_THROW(x) throw x 9 | #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody 10 | #define UT_CATCH_ALL(CatchBody) catch(...) CatchBody 11 | #else 12 | #define UT_TRY(x) x 13 | #define UT_THROW(x) 14 | #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) 15 | #define UT_CATCH_ALL(CatchBody) 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /UnitTest++/ExecuteTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_EXECUTE_TEST_H 2 | #define UNITTEST_EXECUTE_TEST_H 3 | 4 | #include "Config.h" 5 | #include "ExceptionMacros.h" 6 | #include "TestDetails.h" 7 | #include "TestResults.h" 8 | #include "MemoryOutStream.h" 9 | #include "AssertException.h" 10 | #include "CurrentTest.h" 11 | 12 | #ifdef UNITTEST_NO_EXCEPTIONS 13 | #include "ReportAssertImpl.h" 14 | #endif 15 | 16 | #ifdef UNITTEST_POSIX 17 | #include "Posix/SignalTranslator.h" 18 | #endif 19 | 20 | namespace UnitTest { 21 | 22 | template< typename T > 23 | void ExecuteTest(T& testObject, TestDetails const& details, bool isMockTest) 24 | { 25 | if (isMockTest == false) 26 | CurrentTest::Details() = &details; 27 | 28 | #ifdef UNITTEST_NO_EXCEPTIONS 29 | if (UNITTEST_SET_ASSERT_JUMP_TARGET() == 0) 30 | { 31 | #endif 32 | #ifndef UNITTEST_POSIX 33 | UT_TRY({ testObject.RunImpl(); }) 34 | #else 35 | UT_TRY 36 | ({ 37 | UNITTEST_THROW_SIGNALS_POSIX_ONLY 38 | testObject.RunImpl(); 39 | }) 40 | #endif 41 | UT_CATCH(AssertException, e, { (void)e; }) 42 | UT_CATCH(std::exception, e, 43 | { 44 | MemoryOutStream stream; 45 | stream << "Unhandled exception: " << e.what(); 46 | CurrentTest::Results()->OnTestFailure(details, stream.GetText()); 47 | }) 48 | UT_CATCH_ALL 49 | ({ 50 | CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed"); 51 | }) 52 | #ifdef UNITTEST_NO_EXCEPTIONS 53 | } 54 | #endif 55 | } 56 | 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /UnitTest++/HelperMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_HELPERMACROS_H 2 | #define UNITTEST_HELPERMACROS_H 3 | 4 | #include "Config.h" 5 | 6 | #define UNITTEST_MULTILINE_MACRO_BEGIN do { 7 | 8 | #if defined(UNITTEST_WIN32) && !defined(UNITTEST_COMPILER_IS_MSVC6) 9 | #define UNITTEST_MULTILINE_MACRO_END \ 10 | } __pragma(warning(push)) __pragma(warning(disable:4127)) while (0) __pragma(warning(pop)) 11 | #else 12 | #define UNITTEST_MULTILINE_MACRO_END } while(0) 13 | #endif 14 | 15 | 16 | #ifdef UNITTEST_WIN32_DLL 17 | #define UNITTEST_IMPORT __declspec(dllimport) 18 | #define UNITTEST_EXPORT __declspec(dllexport) 19 | 20 | #ifdef UNITTEST_DLL_EXPORT 21 | #define UNITTEST_LINKAGE UNITTEST_EXPORT 22 | #define UNITTEST_IMPEXP_TEMPLATE 23 | #else 24 | #define UNITTEST_LINKAGE UNITTEST_IMPORT 25 | #define UNITTEST_IMPEXP_TEMPLATE extern 26 | #endif 27 | 28 | #define UNITTEST_STDVECTOR_LINKAGE(T) \ 29 | __pragma(warning(push)) \ 30 | __pragma(warning(disable:4231)) \ 31 | UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::allocator< T >; \ 32 | UNITTEST_IMPEXP_TEMPLATE template class UNITTEST_LINKAGE std::vector< T >; \ 33 | __pragma(warning(pop)) 34 | #else 35 | #define UNITTEST_IMPORT 36 | #define UNITTEST_EXPORT 37 | #define UNITTEST_LINKAGE 38 | #define UNITTEST_IMPEXP_TEMPLATE 39 | #define UNITTEST_STDVECTOR_LINKAGE(T) 40 | #endif 41 | 42 | #ifdef UNITTEST_WIN32 43 | #define UNITTEST_JMPBUF jmp_buf 44 | #define UNITTEST_SETJMP setjmp 45 | #define UNITTEST_LONGJMP longjmp 46 | #elif defined UNITTEST_POSIX 47 | #define UNITTEST_JMPBUF std::jmp_buf 48 | #define UNITTEST_SETJMP setjmp 49 | #define UNITTEST_LONGJMP std::longjmp 50 | #endif 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /UnitTest++/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = UnitTest++/libUnitTest++.la 2 | pkgincludedir = $(includedir)/UnitTest++/ 3 | nobase_pkginclude_HEADERS = UnitTest++/UnitTest++.h UnitTest++/UnitTestPP.h UnitTest++/Config.h UnitTest++/HelperMacros.h UnitTest++/Test.h UnitTest++/TestDetails.h UnitTest++/TestList.h UnitTest++/TestSuite.h UnitTest++/TestResults.h UnitTest++/TestMacros.h UnitTest++/CheckMacros.h UnitTest++/TestRunner.h UnitTest++/TimeConstraint.h UnitTest++/ExecuteTest.h UnitTest++/AssertException.h UnitTest++/MemoryOutStream.h UnitTest++/CurrentTest.h UnitTest++/Checks.h UnitTest++/TimeHelpers.h UnitTest++/ExceptionMacros.h UnitTest++/ReportAssert.h UnitTest++/ReportAssertImpl.h UnitTest++/TestReporter.h UnitTest++/TestReporterStdout.h UnitTest++/CompositeTestReporter.h UnitTest++/DeferredTestReporter.h UnitTest++/DeferredTestResult.h 4 | UnitTest___libUnitTest___la_SOURCES = UnitTest++/AssertException.cpp UnitTest++/Test.cpp UnitTest++/Checks.cpp UnitTest++/TestRunner.cpp UnitTest++/TestResults.cpp UnitTest++/TestReporter.cpp UnitTest++/TestReporterStdout.cpp UnitTest++/ReportAssert.cpp UnitTest++/TestList.cpp UnitTest++/TimeConstraint.cpp UnitTest++/TestDetails.cpp UnitTest++/MemoryOutStream.cpp UnitTest++/DeferredTestReporter.cpp UnitTest++/DeferredTestResult.cpp UnitTest++/XmlTestReporter.cpp UnitTest++/CurrentTest.cpp UnitTest++/CompositeTestReporter.cpp 5 | 6 | if WINDOWS 7 | nobase_pkginclude_HEADERS += UnitTest++/Win32/TimeHelpers.h 8 | UnitTest___libUnitTest___la_SOURCES += UnitTest++/Win32/TimeHelpers.cpp 9 | else 10 | nobase_pkginclude_HEADERS += UnitTest++/Posix/SignalTranslator.h UnitTest++/Posix/TimeHelpers.h 11 | UnitTest___libUnitTest___la_SOURCES += UnitTest++/Posix/SignalTranslator.cpp UnitTest++/Posix/TimeHelpers.cpp 12 | endif 13 | 14 | UnitTest___libUnitTest___la_LDFLAGS = -version-number @LIBUNITTEST_SO_VERSION@ 15 | 16 | 17 | -------------------------------------------------------------------------------- /UnitTest++/MemoryOutStream.cpp: -------------------------------------------------------------------------------- 1 | #include "MemoryOutStream.h" 2 | 3 | #ifdef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 4 | 5 | namespace UnitTest { 6 | 7 | char const* MemoryOutStream::GetText() const 8 | { 9 | m_text = this->str(); 10 | return m_text.c_str(); 11 | } 12 | 13 | void MemoryOutStream::Clear() 14 | { 15 | this->str(std::string()); 16 | m_text = this->str(); 17 | } 18 | 19 | #ifdef UNITTEST_COMPILER_IS_MSVC6 20 | 21 | #define snprintf _snprintf 22 | 23 | template 24 | std::ostream& FormatToStream(std::ostream& stream, char const* format, ValueType const& value) 25 | { 26 | using namespace std; 27 | 28 | const size_t BUFFER_SIZE=32; 29 | char txt[BUFFER_SIZE]; 30 | snprintf(txt, BUFFER_SIZE, format, value); 31 | return stream << txt; 32 | } 33 | 34 | std::ostream& operator<<(std::ostream& stream, __int64 const n) 35 | { 36 | return FormatToStream(stream, "%I64d", n); 37 | } 38 | 39 | std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n) 40 | { 41 | return FormatToStream(stream, "%I64u", n); 42 | } 43 | 44 | #endif 45 | 46 | } 47 | 48 | #else 49 | 50 | #include 51 | #include 52 | 53 | #if _MSC_VER 54 | #define snprintf _snprintf 55 | #endif 56 | 57 | namespace UnitTest { 58 | 59 | namespace { 60 | 61 | template 62 | void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) 63 | { 64 | using namespace std; 65 | 66 | const size_t BUFFER_SIZE=32; 67 | char txt[BUFFER_SIZE]; 68 | snprintf(txt, BUFFER_SIZE, format, value); 69 | stream << txt; 70 | } 71 | 72 | int RoundUpToMultipleOfPow2Number (int n, int pow2Number) 73 | { 74 | return (n + (pow2Number - 1)) & ~(pow2Number - 1); 75 | } 76 | 77 | } 78 | 79 | 80 | MemoryOutStream::MemoryOutStream(int const size) 81 | : m_capacity (0) 82 | , m_buffer (0) 83 | 84 | { 85 | GrowBuffer(size); 86 | } 87 | 88 | MemoryOutStream::~MemoryOutStream() 89 | { 90 | delete [] m_buffer; 91 | } 92 | 93 | void MemoryOutStream::Clear() 94 | { 95 | m_buffer[0] = '\0'; 96 | } 97 | 98 | char const* MemoryOutStream::GetText() const 99 | { 100 | return m_buffer; 101 | } 102 | 103 | MemoryOutStream& MemoryOutStream::operator <<(char const* txt) 104 | { 105 | using namespace std; 106 | 107 | int const bytesLeft = m_capacity - (int)strlen(m_buffer); 108 | int const bytesRequired = (int)strlen(txt) + 1; 109 | 110 | if (bytesRequired > bytesLeft) 111 | { 112 | int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; 113 | GrowBuffer(requiredCapacity); 114 | } 115 | 116 | strcat(m_buffer, txt); 117 | return *this; 118 | } 119 | 120 | MemoryOutStream& MemoryOutStream::operator <<(int const n) 121 | { 122 | FormatToStream(*this, "%i", n); 123 | return *this; 124 | } 125 | 126 | MemoryOutStream& MemoryOutStream::operator <<(long const n) 127 | { 128 | FormatToStream(*this, "%li", n); 129 | return *this; 130 | } 131 | 132 | MemoryOutStream& MemoryOutStream::operator <<(unsigned long const n) 133 | { 134 | FormatToStream(*this, "%lu", n); 135 | return *this; 136 | } 137 | 138 | #ifdef UNITTEST_COMPILER_IS_MSVC6 139 | MemoryOutStream& MemoryOutStream::operator <<(__int64 const n) 140 | #else 141 | MemoryOutStream& MemoryOutStream::operator <<(long long const n) 142 | #endif 143 | { 144 | #ifdef UNITTEST_WIN32 145 | FormatToStream(*this, "%I64d", n); 146 | #else 147 | FormatToStream(*this, "%lld", n); 148 | #endif 149 | 150 | return *this; 151 | } 152 | 153 | #ifdef UNITTEST_COMPILER_IS_MSVC6 154 | MemoryOutStream& MemoryOutStream::operator <<(unsigned __int64 const n) 155 | #else 156 | MemoryOutStream& MemoryOutStream::operator <<(unsigned long long const n) 157 | #endif 158 | { 159 | #ifdef UNITTEST_WIN32 160 | FormatToStream(*this, "%I64u", n); 161 | #else 162 | FormatToStream(*this, "%llu", n); 163 | #endif 164 | 165 | return *this; 166 | } 167 | 168 | MemoryOutStream& MemoryOutStream::operator <<(float const f) 169 | { 170 | FormatToStream(*this, "%0.6f", f); 171 | return *this; 172 | } 173 | 174 | MemoryOutStream& MemoryOutStream::operator <<(void const* p) 175 | { 176 | FormatToStream(*this, "%p", p); 177 | return *this; 178 | } 179 | 180 | MemoryOutStream& MemoryOutStream::operator <<(unsigned int const s) 181 | { 182 | FormatToStream(*this, "%u", s); 183 | return *this; 184 | } 185 | 186 | MemoryOutStream& MemoryOutStream::operator <<(double const d) 187 | { 188 | FormatToStream(*this, "%0.6f", d); 189 | return *this; 190 | } 191 | 192 | int MemoryOutStream::GetCapacity() const 193 | { 194 | return m_capacity; 195 | } 196 | 197 | 198 | void MemoryOutStream::GrowBuffer(int const desiredCapacity) 199 | { 200 | int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE); 201 | 202 | using namespace std; 203 | 204 | char* buffer = new char[newCapacity]; 205 | if (m_buffer) 206 | strcpy(buffer, m_buffer); 207 | else 208 | strcpy(buffer, ""); 209 | 210 | delete [] m_buffer; 211 | m_buffer = buffer; 212 | m_capacity = newCapacity; 213 | } 214 | 215 | } 216 | 217 | 218 | #endif 219 | -------------------------------------------------------------------------------- /UnitTest++/MemoryOutStream.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_MEMORYOUTSTREAM_H 2 | #define UNITTEST_MEMORYOUTSTREAM_H 3 | 4 | #include "Config.h" 5 | #include "HelperMacros.h" 6 | 7 | #ifdef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 8 | 9 | #include 10 | 11 | namespace UnitTest 12 | { 13 | 14 | class UNITTEST_LINKAGE MemoryOutStream : public std::ostringstream 15 | { 16 | public: 17 | MemoryOutStream() {} 18 | ~MemoryOutStream() {} 19 | void Clear(); 20 | char const* GetText() const; 21 | 22 | private: 23 | MemoryOutStream(MemoryOutStream const&); 24 | void operator =(MemoryOutStream const&); 25 | 26 | mutable std::string m_text; 27 | }; 28 | 29 | #ifdef UNITTEST_COMPILER_IS_MSVC6 30 | std::ostream& operator<<(std::ostream& stream, __int64 const n); 31 | std::ostream& operator<<(std::ostream& stream, unsigned __int64 const n); 32 | #endif 33 | 34 | } 35 | 36 | #else 37 | 38 | #include 39 | 40 | #ifdef UNITTEST_COMPILER_IS_MSVC6 41 | namespace std {} 42 | #endif 43 | 44 | namespace UnitTest 45 | { 46 | 47 | class UNITTEST_LINKAGE MemoryOutStream 48 | { 49 | public: 50 | explicit MemoryOutStream(int const size = 256); 51 | ~MemoryOutStream(); 52 | 53 | void Clear(); 54 | char const* GetText() const; 55 | 56 | MemoryOutStream& operator <<(char const* txt); 57 | MemoryOutStream& operator <<(int n); 58 | MemoryOutStream& operator <<(long n); 59 | MemoryOutStream& operator <<(unsigned long n); 60 | #ifdef UNITTEST_COMPILER_IS_MSVC6 61 | MemoryOutStream& operator <<(__int64 n); 62 | MemoryOutStream& operator <<(unsigned __int64 n); 63 | #else 64 | MemoryOutStream& operator <<(long long n); 65 | MemoryOutStream& operator <<(unsigned long long n); 66 | #endif 67 | MemoryOutStream& operator <<(float f); 68 | MemoryOutStream& operator <<(double d); 69 | MemoryOutStream& operator <<(void const* p); 70 | MemoryOutStream& operator <<(unsigned int s); 71 | 72 | enum { GROW_CHUNK_SIZE = 32 }; 73 | int GetCapacity() const; 74 | 75 | private: 76 | void operator= (MemoryOutStream const&); 77 | void GrowBuffer(int capacity); 78 | 79 | int m_capacity; 80 | char* m_buffer; 81 | }; 82 | 83 | } 84 | 85 | #endif 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /UnitTest++/Posix/SignalTranslator.cpp: -------------------------------------------------------------------------------- 1 | #include "SignalTranslator.h" 2 | 3 | namespace UnitTest { 4 | 5 | sigjmp_buf* SignalTranslator::s_jumpTarget = 0; 6 | 7 | namespace { 8 | 9 | void SignalHandler(int sig) 10 | { 11 | siglongjmp(*SignalTranslator::s_jumpTarget, sig ); 12 | } 13 | 14 | } 15 | 16 | 17 | SignalTranslator::SignalTranslator() 18 | { 19 | m_oldJumpTarget = s_jumpTarget; 20 | s_jumpTarget = &m_currentJumpTarget; 21 | 22 | struct sigaction action; 23 | action.sa_flags = 0; 24 | action.sa_handler = SignalHandler; 25 | sigemptyset( &action.sa_mask ); 26 | 27 | sigaction( SIGSEGV, &action, &m_old_SIGSEGV_action ); 28 | sigaction( SIGFPE , &action, &m_old_SIGFPE_action ); 29 | sigaction( SIGTRAP, &action, &m_old_SIGTRAP_action ); 30 | sigaction( SIGBUS , &action, &m_old_SIGBUS_action ); 31 | sigaction( SIGILL , &action, &m_old_SIGBUS_action ); 32 | } 33 | 34 | SignalTranslator::~SignalTranslator() 35 | { 36 | sigaction( SIGILL , &m_old_SIGBUS_action , 0 ); 37 | sigaction( SIGBUS , &m_old_SIGBUS_action , 0 ); 38 | sigaction( SIGTRAP, &m_old_SIGTRAP_action, 0 ); 39 | sigaction( SIGFPE , &m_old_SIGFPE_action , 0 ); 40 | sigaction( SIGSEGV, &m_old_SIGSEGV_action, 0 ); 41 | 42 | s_jumpTarget = m_oldJumpTarget; 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /UnitTest++/Posix/SignalTranslator.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_SIGNALTRANSLATOR_H 2 | #define UNITTEST_SIGNALTRANSLATOR_H 3 | 4 | #include 5 | #include 6 | 7 | namespace UnitTest { 8 | 9 | class SignalTranslator 10 | { 11 | public: 12 | SignalTranslator(); 13 | ~SignalTranslator(); 14 | 15 | static sigjmp_buf* s_jumpTarget; 16 | 17 | private: 18 | sigjmp_buf m_currentJumpTarget; 19 | sigjmp_buf* m_oldJumpTarget; 20 | 21 | struct sigaction m_old_SIGFPE_action; 22 | struct sigaction m_old_SIGTRAP_action; 23 | struct sigaction m_old_SIGSEGV_action; 24 | struct sigaction m_old_SIGBUS_action; 25 | struct sigaction m_old_SIGABRT_action; 26 | struct sigaction m_old_SIGALRM_action; 27 | }; 28 | 29 | #if !defined (__GNUC__) 30 | #define UNITTEST_EXTENSION 31 | #else 32 | #define UNITTEST_EXTENSION __extension__ 33 | #endif 34 | 35 | #define UNITTEST_THROW_SIGNALS_POSIX_ONLY \ 36 | UnitTest::SignalTranslator sig; \ 37 | if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \ 38 | throw ("Unhandled system exception"); 39 | 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /UnitTest++/Posix/TimeHelpers.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeHelpers.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | Timer::Timer() 7 | { 8 | m_startTime.tv_sec = 0; 9 | m_startTime.tv_usec = 0; 10 | } 11 | 12 | void Timer::Start() 13 | { 14 | gettimeofday(&m_startTime, 0); 15 | } 16 | 17 | double Timer::GetTimeInMs() const 18 | { 19 | struct timeval currentTime; 20 | gettimeofday(¤tTime, 0); 21 | 22 | double const dsecs = currentTime.tv_sec - m_startTime.tv_sec; 23 | double const dus = currentTime.tv_usec - m_startTime.tv_usec; 24 | 25 | return (dsecs * 1000.0) + (dus / 1000.0); 26 | } 27 | 28 | void TimeHelpers::SleepMs(int ms) 29 | { 30 | usleep(ms * 1000); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /UnitTest++/Posix/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMEHELPERS_H 2 | #define UNITTEST_TIMEHELPERS_H 3 | 4 | #include 5 | 6 | namespace UnitTest { 7 | 8 | class Timer 9 | { 10 | public: 11 | Timer(); 12 | void Start(); 13 | double GetTimeInMs() const; 14 | 15 | private: 16 | struct timeval m_startTime; 17 | }; 18 | 19 | 20 | namespace TimeHelpers 21 | { 22 | void SleepMs(int ms); 23 | } 24 | 25 | 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /UnitTest++/ReportAssert.cpp: -------------------------------------------------------------------------------- 1 | #include "ReportAssert.h" 2 | #include "ReportAssertImpl.h" 3 | #include "AssertException.h" 4 | #include "CurrentTest.h" 5 | #include "TestResults.h" 6 | #include "TestDetails.h" 7 | 8 | #ifdef UNITTEST_NO_EXCEPTIONS 9 | #include "ReportAssertImpl.h" 10 | #endif 11 | 12 | namespace UnitTest { 13 | 14 | namespace 15 | { 16 | bool& AssertExpectedFlag() 17 | { 18 | static bool s_assertExpected = false; 19 | return s_assertExpected; 20 | } 21 | } 22 | 23 | UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber) 24 | { 25 | Detail::ReportAssertEx(CurrentTest::Results(), CurrentTest::Details(), 26 | description, filename, lineNumber); 27 | } 28 | 29 | namespace Detail { 30 | 31 | #ifdef UNITTEST_NO_EXCEPTIONS 32 | UNITTEST_JMPBUF* GetAssertJmpBuf() 33 | { 34 | static UNITTEST_JMPBUF s_jmpBuf; 35 | return &s_jmpBuf; 36 | } 37 | #endif 38 | 39 | UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, 40 | const TestDetails* testDetails, 41 | char const* description, 42 | char const* filename, 43 | int lineNumber) 44 | { 45 | if (AssertExpectedFlag() == false) 46 | { 47 | TestDetails assertDetails(testDetails->testName, testDetails->suiteName, filename, lineNumber); 48 | testResults->OnTestFailure(assertDetails, description); 49 | } 50 | 51 | ExpectAssert(false); 52 | 53 | #ifndef UNITTEST_NO_EXCEPTIONS 54 | throw AssertException(); 55 | #else 56 | UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); 57 | #endif 58 | } 59 | 60 | UNITTEST_LINKAGE void ExpectAssert(bool expected) 61 | { 62 | AssertExpectedFlag() = expected; 63 | } 64 | 65 | UNITTEST_LINKAGE bool AssertExpected() 66 | { 67 | return AssertExpectedFlag(); 68 | } 69 | 70 | }} 71 | -------------------------------------------------------------------------------- /UnitTest++/ReportAssert.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_ASSERT_H 2 | #define UNITTEST_ASSERT_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | UNITTEST_LINKAGE void ReportAssert(char const* description, char const* filename, int lineNumber); 9 | 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /UnitTest++/ReportAssertImpl.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_REPORTASSERTIMPL_H 2 | #define UNITTEST_REPORTASSERTIMPL_H 3 | 4 | #include "Config.h" 5 | #include "HelperMacros.h" 6 | 7 | #ifdef UNITTEST_NO_EXCEPTIONS 8 | #include 9 | #endif 10 | 11 | namespace UnitTest { 12 | 13 | class TestResults; 14 | class TestDetails; 15 | 16 | namespace Detail { 17 | 18 | UNITTEST_LINKAGE void ExpectAssert(bool expected); 19 | 20 | UNITTEST_LINKAGE void ReportAssertEx(TestResults* testResults, 21 | const TestDetails* testDetails, 22 | char const* description, 23 | char const* filename, 24 | int lineNumber); 25 | 26 | UNITTEST_LINKAGE bool AssertExpected(); 27 | 28 | #ifdef UNITTEST_NO_EXCEPTIONS 29 | UNITTEST_LINKAGE UNITTEST_JMPBUF* GetAssertJmpBuf(); 30 | 31 | #ifdef UNITTEST_WIN32 32 | #define UNITTEST_SET_ASSERT_JUMP_TARGET() \ 33 | __pragma(warning(push)) __pragma(warning(disable:4611)) \ 34 | UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) \ 35 | __pragma(warning(pop)) 36 | #else 37 | #define UNITTEST_SET_ASSERT_JUMP_TARGET() UNITTEST_SETJMP(*UnitTest::Detail::GetAssertJmpBuf()) 38 | #endif 39 | 40 | #define UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET() UNITTEST_LONGJMP(*UnitTest::Detail::GetAssertJmpBuf(), 1) 41 | #endif 42 | 43 | } 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /UnitTest++/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | #include "Test.h" 3 | #include "TestList.h" 4 | #include "TestResults.h" 5 | #include "AssertException.h" 6 | #include "MemoryOutStream.h" 7 | #include "ExecuteTest.h" 8 | 9 | #ifdef UNITTEST_POSIX 10 | #include "Posix/SignalTranslator.h" 11 | #endif 12 | 13 | namespace UnitTest { 14 | 15 | TestList& Test::GetTestList() 16 | { 17 | static TestList s_list; 18 | return s_list; 19 | } 20 | 21 | Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber) 22 | : m_details(testName, suiteName, filename, lineNumber) 23 | , m_nextTest(0) 24 | , m_isMockTest(false) 25 | { 26 | } 27 | 28 | Test::~Test() 29 | { 30 | } 31 | 32 | void Test::Run() 33 | { 34 | ExecuteTest(*this, m_details, m_isMockTest); 35 | } 36 | 37 | void Test::RunImpl() const 38 | { 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /UnitTest++/Test.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TEST_H 2 | #define UNITTEST_TEST_H 3 | 4 | #include "TestDetails.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestResults; 9 | class TestList; 10 | 11 | class UNITTEST_LINKAGE Test 12 | { 13 | public: 14 | explicit Test(char const* testName, char const* suiteName = "DefaultSuite", char const* filename = "", int lineNumber = 0); 15 | virtual ~Test(); 16 | void Run(); 17 | 18 | TestDetails const m_details; 19 | Test* m_nextTest; 20 | 21 | mutable bool m_isMockTest; 22 | 23 | static TestList& GetTestList(); 24 | 25 | virtual void RunImpl() const; 26 | 27 | private: 28 | Test(Test const&); 29 | Test& operator =(Test const&); 30 | }; 31 | 32 | 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /UnitTest++/TestDetails.cpp: -------------------------------------------------------------------------------- 1 | #include "TestDetails.h" 2 | 3 | namespace UnitTest { 4 | 5 | TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_) 6 | : suiteName(suiteName_) 7 | , testName(testName_) 8 | , filename(filename_) 9 | , lineNumber(lineNumber_) 10 | , timeConstraintExempt(false) 11 | { 12 | } 13 | 14 | TestDetails::TestDetails(const TestDetails& details, int lineNumber_) 15 | : suiteName(details.suiteName) 16 | , testName(details.testName) 17 | , filename(details.filename) 18 | , lineNumber(lineNumber_) 19 | , timeConstraintExempt(details.timeConstraintExempt) 20 | { 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /UnitTest++/TestDetails.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTDETAILS_H 2 | #define UNITTEST_TESTDETAILS_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | class UNITTEST_LINKAGE TestDetails 9 | { 10 | public: 11 | TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber); 12 | TestDetails(const TestDetails& details, int lineNumber); 13 | 14 | char const* const suiteName; 15 | char const* const testName; 16 | char const* const filename; 17 | int const lineNumber; 18 | mutable bool timeConstraintExempt; 19 | 20 | TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind 21 | private: 22 | TestDetails& operator=(TestDetails const&); 23 | }; 24 | 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /UnitTest++/TestList.cpp: -------------------------------------------------------------------------------- 1 | #include "TestList.h" 2 | #include "Test.h" 3 | 4 | #include 5 | 6 | namespace UnitTest { 7 | 8 | TestList::TestList() 9 | : m_head(0) 10 | , m_tail(0) 11 | { 12 | } 13 | 14 | void TestList::Add(Test* test) 15 | { 16 | if (m_tail == 0) 17 | { 18 | assert(m_head == 0); 19 | m_head = test; 20 | m_tail = test; 21 | } 22 | else 23 | { 24 | m_tail->m_nextTest = test; 25 | m_tail = test; 26 | } 27 | } 28 | 29 | Test* TestList::GetHead() const 30 | { 31 | return m_head; 32 | } 33 | 34 | ListAdder::ListAdder(TestList& list, Test* test) 35 | { 36 | list.Add(test); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /UnitTest++/TestList.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTLIST_H 2 | #define UNITTEST_TESTLIST_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | class Test; 9 | 10 | class UNITTEST_LINKAGE TestList 11 | { 12 | public: 13 | TestList(); 14 | void Add (Test* test); 15 | 16 | Test* GetHead() const; 17 | 18 | private: 19 | Test* m_head; 20 | Test* m_tail; 21 | }; 22 | 23 | 24 | class UNITTEST_LINKAGE ListAdder 25 | { 26 | public: 27 | ListAdder(TestList& list, Test* test); 28 | }; 29 | 30 | } 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /UnitTest++/TestMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTMACROS_H 2 | #define UNITTEST_TESTMACROS_H 3 | 4 | #include "Config.h" 5 | #include "TestSuite.h" 6 | #include "ExceptionMacros.h" 7 | #include "ExecuteTest.h" 8 | #include "AssertException.h" 9 | #include "TestDetails.h" 10 | #include "MemoryOutStream.h" 11 | 12 | #ifndef UNITTEST_POSIX 13 | #define UNITTEST_THROW_SIGNALS_POSIX_ONLY 14 | #else 15 | #include "Posix/SignalTranslator.h" 16 | #endif 17 | 18 | #ifdef TEST 19 | #error UnitTest++ redefines TEST 20 | #endif 21 | 22 | #ifdef TEST_EX 23 | #error UnitTest++ redefines TEST_EX 24 | #endif 25 | 26 | #ifdef TEST_FIXTURE_EX 27 | #error UnitTest++ redefines TEST_FIXTURE_EX 28 | #endif 29 | 30 | #define SUITE(Name) \ 31 | namespace Suite##Name { \ 32 | namespace UnitTestSuite { \ 33 | inline char const* GetSuiteName () { \ 34 | return #Name ; \ 35 | } \ 36 | } \ 37 | } \ 38 | namespace Suite##Name 39 | 40 | #define TEST_EX(Name, List) \ 41 | class Test##Name : public UnitTest::Test \ 42 | { \ 43 | public: \ 44 | Test##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ 45 | private: \ 46 | virtual void RunImpl() const; \ 47 | } test##Name##Instance; \ 48 | \ 49 | UnitTest::ListAdder adder##Name (List, &test##Name##Instance); \ 50 | \ 51 | void Test##Name::RunImpl() const 52 | 53 | 54 | #define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList()) 55 | 56 | 57 | #define TEST_FIXTURE_EX(Fixture, Name, List) \ 58 | class Fixture##Name##Helper : public Fixture \ 59 | { \ 60 | public: \ 61 | explicit Fixture##Name##Helper(UnitTest::TestDetails const& details) : m_details(details) {} \ 62 | void RunImpl(); \ 63 | UnitTest::TestDetails const& m_details; \ 64 | private: \ 65 | Fixture##Name##Helper(Fixture##Name##Helper const&); \ 66 | Fixture##Name##Helper& operator =(Fixture##Name##Helper const&); \ 67 | }; \ 68 | \ 69 | class Test##Fixture##Name : public UnitTest::Test \ 70 | { \ 71 | public: \ 72 | Test##Fixture##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ 73 | private: \ 74 | virtual void RunImpl() const; \ 75 | } test##Fixture##Name##Instance; \ 76 | \ 77 | UnitTest::ListAdder adder##Fixture##Name (List, &test##Fixture##Name##Instance); \ 78 | \ 79 | void Test##Fixture##Name::RunImpl() const \ 80 | { \ 81 | volatile bool ctorOk = false; \ 82 | UT_TRY \ 83 | ({ \ 84 | Fixture##Name##Helper fixtureHelper(m_details); \ 85 | ctorOk = true; \ 86 | UnitTest::ExecuteTest(fixtureHelper, m_details, false); \ 87 | }) \ 88 | UT_CATCH (UnitTest::AssertException, e, \ 89 | { \ 90 | (void)e; \ 91 | }) \ 92 | UT_CATCH (std::exception, e, \ 93 | { \ 94 | UnitTest::MemoryOutStream stream; \ 95 | stream << "Unhandled exception: " << e.what(); \ 96 | UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \ 97 | }) \ 98 | UT_CATCH_ALL \ 99 | ({ \ 100 | if (ctorOk) \ 101 | { \ 102 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ 103 | "Unhandled exception while destroying fixture " #Fixture); \ 104 | } \ 105 | else \ 106 | { \ 107 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ 108 | "Unhandled exception while constructing fixture " #Fixture); \ 109 | } \ 110 | }) \ 111 | } \ 112 | void Fixture##Name##Helper::RunImpl() 113 | 114 | #define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList()) 115 | 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /UnitTest++/TestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "TestReporter.h" 2 | 3 | namespace UnitTest { 4 | 5 | TestReporter::~TestReporter() 6 | { 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /UnitTest++/TestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTREPORTER_H 2 | #define UNITTEST_TESTREPORTER_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestDetails; 9 | 10 | class UNITTEST_LINKAGE TestReporter 11 | { 12 | public: 13 | virtual ~TestReporter(); 14 | 15 | virtual void ReportTestStart(TestDetails const& test) = 0; 16 | virtual void ReportFailure(TestDetails const& test, char const* failure) = 0; 17 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) = 0; 18 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) = 0; 19 | }; 20 | 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /UnitTest++/TestReporterStdout.cpp: -------------------------------------------------------------------------------- 1 | #include "TestReporterStdout.h" 2 | #include 3 | 4 | #include "TestDetails.h" 5 | 6 | // cstdio doesn't pull in namespace std on VC6, so we do it here. 7 | #if defined(UNITTEST_WIN32) && (_MSC_VER == 1200) 8 | namespace std {} 9 | #endif 10 | 11 | namespace UnitTest { 12 | 13 | void TestReporterStdout::ReportFailure(TestDetails const& details, char const* failure) 14 | { 15 | using namespace std; 16 | #if defined(__APPLE__) || defined(__GNUG__) 17 | char const* const errorFormat = "%s:%d:%d: error: Failure in %s: %s\n"; 18 | fprintf(stderr, errorFormat, details.filename, details.lineNumber, 1, details.testName, failure); 19 | #else 20 | char const* const errorFormat = "%s(%d): error: Failure in %s: %s\n"; 21 | fprintf(stderr, errorFormat, details.filename, details.lineNumber, details.testName, failure); 22 | #endif 23 | } 24 | 25 | void TestReporterStdout::ReportTestStart(TestDetails const& /*test*/) 26 | { 27 | } 28 | 29 | void TestReporterStdout::ReportTestFinish(TestDetails const& /*test*/, float) 30 | { 31 | } 32 | 33 | void TestReporterStdout::ReportSummary(int const totalTestCount, int const failedTestCount, 34 | int const failureCount, float const secondsElapsed) 35 | { 36 | using namespace std; 37 | 38 | if (failureCount > 0) 39 | printf("FAILURE: %d out of %d tests failed (%d failures).\n", failedTestCount, totalTestCount, failureCount); 40 | else 41 | printf("Success: %d tests passed.\n", totalTestCount); 42 | 43 | printf("Test time: %.2f seconds.\n", secondsElapsed); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /UnitTest++/TestReporterStdout.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTREPORTERSTDOUT_H 2 | #define UNITTEST_TESTREPORTERSTDOUT_H 3 | 4 | #include "TestReporter.h" 5 | 6 | namespace UnitTest { 7 | 8 | class UNITTEST_LINKAGE TestReporterStdout : public TestReporter 9 | { 10 | private: 11 | virtual void ReportTestStart(TestDetails const& test); 12 | virtual void ReportFailure(TestDetails const& test, char const* failure); 13 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); 14 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 15 | }; 16 | 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /UnitTest++/TestResults.cpp: -------------------------------------------------------------------------------- 1 | #include "TestResults.h" 2 | #include "TestReporter.h" 3 | 4 | #include "TestDetails.h" 5 | 6 | namespace UnitTest { 7 | 8 | TestResults::TestResults(TestReporter* testReporter) 9 | : m_testReporter(testReporter) 10 | , m_totalTestCount(0) 11 | , m_failedTestCount(0) 12 | , m_failureCount(0) 13 | , m_currentTestFailed(false) 14 | { 15 | } 16 | 17 | void TestResults::OnTestStart(TestDetails const& test) 18 | { 19 | ++m_totalTestCount; 20 | m_currentTestFailed = false; 21 | if (m_testReporter) 22 | m_testReporter->ReportTestStart(test); 23 | } 24 | 25 | void TestResults::OnTestFailure(TestDetails const& test, char const* failure) 26 | { 27 | ++m_failureCount; 28 | if (!m_currentTestFailed) 29 | { 30 | ++m_failedTestCount; 31 | m_currentTestFailed = true; 32 | } 33 | 34 | if (m_testReporter) 35 | m_testReporter->ReportFailure(test, failure); 36 | } 37 | 38 | void TestResults::OnTestFinish(TestDetails const& test, float secondsElapsed) 39 | { 40 | if (m_testReporter) 41 | m_testReporter->ReportTestFinish(test, secondsElapsed); 42 | } 43 | 44 | int TestResults::GetTotalTestCount() const 45 | { 46 | return m_totalTestCount; 47 | } 48 | 49 | int TestResults::GetFailedTestCount() const 50 | { 51 | return m_failedTestCount; 52 | } 53 | 54 | int TestResults::GetFailureCount() const 55 | { 56 | return m_failureCount; 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /UnitTest++/TestResults.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTRESULTS_H 2 | #define UNITTEST_TESTRESULTS_H 3 | 4 | #include "HelperMacros.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestReporter; 9 | class TestDetails; 10 | 11 | class UNITTEST_LINKAGE TestResults 12 | { 13 | public: 14 | explicit TestResults(TestReporter* reporter = 0); 15 | 16 | void OnTestStart(TestDetails const& test); 17 | void OnTestFailure(TestDetails const& test, char const* failure); 18 | void OnTestFinish(TestDetails const& test, float secondsElapsed); 19 | 20 | int GetTotalTestCount() const; 21 | int GetFailedTestCount() const; 22 | int GetFailureCount() const; 23 | 24 | private: 25 | TestReporter* m_testReporter; 26 | int m_totalTestCount; 27 | int m_failedTestCount; 28 | int m_failureCount; 29 | 30 | bool m_currentTestFailed; 31 | 32 | TestResults(TestResults const&); 33 | TestResults& operator =(TestResults const&); 34 | }; 35 | 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /UnitTest++/TestRunner.cpp: -------------------------------------------------------------------------------- 1 | #include "TestRunner.h" 2 | #include "TestResults.h" 3 | #include "TestReporter.h" 4 | #include "TestReporterStdout.h" 5 | #include "TimeHelpers.h" 6 | #include "MemoryOutStream.h" 7 | 8 | #include 9 | 10 | 11 | namespace UnitTest { 12 | 13 | int RunAllTests() 14 | { 15 | TestReporterStdout reporter; 16 | TestRunner runner(reporter); 17 | return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0); 18 | } 19 | 20 | 21 | TestRunner::TestRunner(TestReporter& reporter) 22 | : m_reporter(&reporter) 23 | , m_result(new TestResults(&reporter)) 24 | , m_timer(new Timer) 25 | { 26 | m_timer->Start(); 27 | } 28 | 29 | TestRunner::~TestRunner() 30 | { 31 | delete m_result; 32 | delete m_timer; 33 | } 34 | 35 | TestResults* TestRunner::GetTestResults() 36 | { 37 | return m_result; 38 | } 39 | 40 | int TestRunner::Finish() const 41 | { 42 | float const secondsElapsed = static_cast(m_timer->GetTimeInMs() / 1000.0); 43 | m_reporter->ReportSummary(m_result->GetTotalTestCount(), 44 | m_result->GetFailedTestCount(), 45 | m_result->GetFailureCount(), 46 | secondsElapsed); 47 | 48 | return m_result->GetFailureCount(); 49 | } 50 | 51 | bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const 52 | { 53 | using namespace std; 54 | return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName); 55 | } 56 | 57 | void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const 58 | { 59 | if (curTest->m_isMockTest == false) 60 | CurrentTest::Results() = result; 61 | 62 | Timer testTimer; 63 | testTimer.Start(); 64 | 65 | result->OnTestStart(curTest->m_details); 66 | 67 | curTest->Run(); 68 | 69 | double const testTimeInMs = testTimer.GetTimeInMs(); 70 | if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt) 71 | { 72 | MemoryOutStream stream; 73 | stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << 74 | "ms but took " << testTimeInMs << "ms."; 75 | 76 | result->OnTestFailure(curTest->m_details, stream.GetText()); 77 | } 78 | 79 | result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0)); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /UnitTest++/TestRunner.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTRUNNER_H 2 | #define UNITTEST_TESTRUNNER_H 3 | 4 | #include "Test.h" 5 | #include "TestList.h" 6 | #include "CurrentTest.h" 7 | 8 | namespace UnitTest { 9 | 10 | class TestReporter; 11 | class TestResults; 12 | class Timer; 13 | 14 | UNITTEST_LINKAGE int RunAllTests(); 15 | 16 | struct True 17 | { 18 | bool operator()(const Test* const) const 19 | { 20 | return true; 21 | } 22 | }; 23 | 24 | class UNITTEST_LINKAGE TestRunner 25 | { 26 | public: 27 | explicit TestRunner(TestReporter& reporter); 28 | ~TestRunner(); 29 | 30 | template< class Predicate > 31 | int RunTestsIf(TestList const& list, char const* suiteName, 32 | const Predicate& predicate, int maxTestTimeInMs) const 33 | { 34 | Test* curTest = list.GetHead(); 35 | 36 | while (curTest != 0) 37 | { 38 | if (IsTestInSuite(curTest, suiteName) && predicate(curTest)) 39 | RunTest(m_result, curTest, maxTestTimeInMs); 40 | 41 | curTest = curTest->m_nextTest; 42 | } 43 | 44 | return Finish(); 45 | } 46 | 47 | TestResults* GetTestResults(); 48 | 49 | private: 50 | TestReporter* m_reporter; 51 | TestResults* m_result; 52 | Timer* m_timer; 53 | 54 | int Finish() const; 55 | bool IsTestInSuite(const Test* const curTest, char const* suiteName) const; 56 | void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const; 57 | }; 58 | 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /UnitTest++/TestSuite.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTSUITE_H 2 | #define UNITTEST_TESTSUITE_H 3 | 4 | namespace UnitTestSuite 5 | { 6 | inline char const* GetSuiteName () 7 | { 8 | return "DefaultSuite"; 9 | } 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /UnitTest++/TimeConstraint.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeConstraint.h" 2 | #include "TestResults.h" 3 | #include "MemoryOutStream.h" 4 | #include "CurrentTest.h" 5 | 6 | namespace UnitTest { 7 | 8 | 9 | TimeConstraint::TimeConstraint(int ms, TestDetails const& details, int lineNumber) 10 | : m_details(details, lineNumber) 11 | , m_maxMs(ms) 12 | { 13 | m_timer.Start(); 14 | } 15 | 16 | TimeConstraint::~TimeConstraint() 17 | { 18 | double const totalTimeInMs = m_timer.GetTimeInMs(); 19 | if (totalTimeInMs > m_maxMs) 20 | { 21 | MemoryOutStream stream; 22 | stream << "Time constraint failed. Expected to run test under " << m_maxMs << 23 | "ms but took " << totalTimeInMs << "ms."; 24 | 25 | CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /UnitTest++/TimeConstraint.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMECONSTRAINT_H 2 | #define UNITTEST_TIMECONSTRAINT_H 3 | 4 | #include "TimeHelpers.h" 5 | #include "HelperMacros.h" 6 | #include "TestDetails.h" 7 | 8 | namespace UnitTest { 9 | 10 | class TestResults; 11 | 12 | class UNITTEST_LINKAGE TimeConstraint 13 | { 14 | public: 15 | TimeConstraint(int ms, TestDetails const& details, int lineNumber); 16 | ~TimeConstraint(); 17 | 18 | private: 19 | void operator=(TimeConstraint const&); 20 | TimeConstraint(TimeConstraint const&); 21 | 22 | Timer m_timer; 23 | TestDetails const m_details; 24 | int const m_maxMs; 25 | }; 26 | 27 | #define UNITTEST_TIME_CONSTRAINT(ms) \ 28 | UnitTest::TimeConstraint unitTest__timeConstraint__(ms, m_details, __LINE__) 29 | 30 | #define UNITTEST_TIME_CONSTRAINT_EXEMPT() \ 31 | UNITTEST_MULTILINE_MACRO_BEGIN \ 32 | m_details.timeConstraintExempt = true; \ 33 | UNITTEST_MULTILINE_MACRO_END 34 | 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /UnitTest++/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | 3 | #if defined UNITTEST_POSIX 4 | #include "Posix/TimeHelpers.h" 5 | #else 6 | #include "Win32/TimeHelpers.h" 7 | #endif 8 | -------------------------------------------------------------------------------- /UnitTest++/UnitTest++.h: -------------------------------------------------------------------------------- 1 | #include "UnitTestPP.h" -------------------------------------------------------------------------------- /UnitTest++/UnitTestPP.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTESTPP_H 2 | #define UNITTESTPP_H 3 | 4 | #include "Config.h" 5 | #include "TestMacros.h" 6 | #include "CheckMacros.h" 7 | #include "TestRunner.h" 8 | #include "TimeConstraint.h" 9 | #include "ReportAssert.h" 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /UnitTest++/Win32/TimeHelpers.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeHelpers.h" 2 | 3 | #define WIN32_LEAN_AND_MEAN 4 | #include 5 | 6 | namespace UnitTest { 7 | 8 | Timer::Timer() 9 | : m_threadHandle(::GetCurrentThread()) 10 | , m_startTime(0) 11 | { 12 | #if defined(UNITTEST_WIN32) && (_MSC_VER == 1200) // VC6 doesn't have DWORD_PTR 13 | typedef unsigned long DWORD_PTR; 14 | #endif 15 | 16 | DWORD_PTR systemMask; 17 | ::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask); 18 | ::SetThreadAffinityMask(m_threadHandle, 1); 19 | ::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER* >(&m_frequency)); 20 | ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); 21 | } 22 | 23 | void Timer::Start() 24 | { 25 | m_startTime = GetTime(); 26 | } 27 | 28 | double Timer::GetTimeInMs() const 29 | { 30 | __int64 const elapsedTime = GetTime() - m_startTime; 31 | double const seconds = double(elapsedTime) / double(m_frequency); 32 | return seconds * 1000.0; 33 | } 34 | 35 | __int64 Timer::GetTime() const 36 | { 37 | LARGE_INTEGER curTime; 38 | ::SetThreadAffinityMask(m_threadHandle, 1); 39 | ::QueryPerformanceCounter(&curTime); 40 | ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); 41 | return curTime.QuadPart; 42 | } 43 | 44 | void TimeHelpers::SleepMs(int ms) 45 | { 46 | ::Sleep(ms); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /UnitTest++/Win32/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMEHELPERS_H 2 | #define UNITTEST_TIMEHELPERS_H 3 | 4 | #include "../Config.h" 5 | #include "../HelperMacros.h" 6 | 7 | #ifdef UNITTEST_MINGW 8 | #ifndef __int64 9 | #define __int64 long long 10 | #endif 11 | #endif 12 | 13 | namespace UnitTest { 14 | 15 | class UNITTEST_LINKAGE Timer 16 | { 17 | public: 18 | Timer(); 19 | void Start(); 20 | double GetTimeInMs() const; 21 | 22 | private: 23 | __int64 GetTime() const; 24 | 25 | void* m_threadHandle; 26 | 27 | #if defined(_WIN64) 28 | unsigned __int64 m_processAffinityMask; 29 | #else 30 | unsigned long m_processAffinityMask; 31 | #endif 32 | 33 | __int64 m_startTime; 34 | __int64 m_frequency; 35 | }; 36 | 37 | 38 | namespace TimeHelpers 39 | { 40 | UNITTEST_LINKAGE void SleepMs(int ms); 41 | } 42 | 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /UnitTest++/XmlTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 3 | 4 | #include "XmlTestReporter.h" 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using std::string; 11 | using std::ostringstream; 12 | using std::ostream; 13 | 14 | namespace { 15 | 16 | void ReplaceChar(string& str, char c, string const& replacement) 17 | { 18 | for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1)) 19 | str.replace(pos, 1, replacement); 20 | } 21 | 22 | string XmlEscape(string const& value) 23 | { 24 | string escaped = value; 25 | 26 | ReplaceChar(escaped, '&', "&"); 27 | ReplaceChar(escaped, '<', "<"); 28 | ReplaceChar(escaped, '>', ">"); 29 | ReplaceChar(escaped, '\'', "'"); 30 | ReplaceChar(escaped, '\"', """); 31 | 32 | return escaped; 33 | } 34 | 35 | string BuildFailureMessage(string const& file, int line, string const& message) 36 | { 37 | ostringstream failureMessage; 38 | failureMessage << file << "(" << line << ") : " << message; 39 | return failureMessage.str(); 40 | } 41 | 42 | } 43 | 44 | namespace UnitTest { 45 | 46 | XmlTestReporter::XmlTestReporter(ostream& ostream) 47 | : m_ostream(ostream) 48 | { 49 | } 50 | 51 | void XmlTestReporter::ReportSummary(int totalTestCount, int failedTestCount, 52 | int failureCount, float secondsElapsed) 53 | { 54 | AddXmlElement(m_ostream, NULL); 55 | 56 | BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); 57 | 58 | DeferredTestResultList const& results = GetResults(); 59 | for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) 60 | { 61 | BeginTest(m_ostream, *i); 62 | 63 | if (i->failed) 64 | AddFailure(m_ostream, *i); 65 | 66 | EndTest(m_ostream, *i); 67 | } 68 | 69 | EndResults(m_ostream); 70 | } 71 | 72 | void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) 73 | { 74 | os << ""; 80 | } 81 | 82 | void XmlTestReporter::BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, 83 | int failureCount, float secondsElapsed) 84 | { 85 | os << ""; 91 | } 92 | 93 | void XmlTestReporter::EndResults(std::ostream& os) 94 | { 95 | os << ""; 96 | } 97 | 98 | void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) 99 | { 100 | os << ""; 110 | else 111 | os << "/>"; 112 | } 113 | 114 | void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) 115 | { 116 | os << ">"; // close element 117 | 118 | for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); 119 | it != result.failures.end(); 120 | ++it) 121 | { 122 | string const escapedMessage = XmlEscape(std::string(it->failureStr)); 123 | string const message = BuildFailureMessage(result.failureFile, it->lineNumber, escapedMessage); 124 | 125 | os << ""; 126 | } 127 | } 128 | 129 | } 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /UnitTest++/XmlTestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_XMLTESTREPORTER_H 2 | #define UNITTEST_XMLTESTREPORTER_H 3 | 4 | #include "Config.h" 5 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 6 | 7 | #include "DeferredTestReporter.h" 8 | 9 | #include 10 | 11 | namespace UnitTest 12 | { 13 | 14 | class UNITTEST_LINKAGE XmlTestReporter : public DeferredTestReporter 15 | { 16 | public: 17 | explicit XmlTestReporter(std::ostream& ostream); 18 | 19 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 20 | 21 | private: 22 | XmlTestReporter(XmlTestReporter const&); 23 | XmlTestReporter& operator=(XmlTestReporter const&); 24 | 25 | void AddXmlElement(std::ostream& os, char const* encoding); 26 | void BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 27 | void EndResults(std::ostream& os); 28 | void BeginTest(std::ostream& os, DeferredTestResult const& result); 29 | void AddFailure(std::ostream& os, DeferredTestResult const& result); 30 | void EndTest(std::ostream& os, DeferredTestResult const& result); 31 | 32 | std::ostream& m_ostream; 33 | }; 34 | 35 | } 36 | 37 | #endif 38 | #endif 39 | -------------------------------------------------------------------------------- /UnitTest++/unittestpp_vs2005.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 25 | 28 | 31 | 34 | 37 | 40 | 53 | 56 | 59 | 62 | 66 | 69 | 72 | 75 | 78 | 81 | 82 | 90 | 93 | 96 | 99 | 102 | 105 | 116 | 119 | 122 | 125 | 129 | 132 | 135 | 138 | 141 | 144 | 145 | 152 | 155 | 158 | 161 | 164 | 167 | 180 | 183 | 186 | 189 | 194 | 197 | 200 | 203 | 206 | 209 | 212 | 215 | 218 | 219 | 227 | 230 | 233 | 236 | 239 | 242 | 253 | 256 | 259 | 262 | 267 | 270 | 273 | 276 | 279 | 282 | 285 | 288 | 291 | 292 | 299 | 302 | 305 | 308 | 311 | 314 | 327 | 330 | 333 | 336 | 340 | 343 | 346 | 349 | 352 | 355 | 356 | 364 | 367 | 370 | 373 | 376 | 379 | 390 | 393 | 396 | 399 | 403 | 406 | 409 | 412 | 415 | 418 | 419 | 420 | 421 | 422 | 423 | 426 | 429 | 430 | 433 | 434 | 435 | 438 | 439 | 442 | 443 | 446 | 447 | 450 | 451 | 454 | 455 | 458 | 459 | 462 | 463 | 466 | 467 | 470 | 471 | 474 | 475 | 478 | 479 | 482 | 483 | 486 | 487 | 490 | 491 | 494 | 495 | 498 | 499 | 502 | 503 | 506 | 507 | 510 | 511 | 514 | 515 | 518 | 519 | 522 | 523 | 526 | 527 | 530 | 531 | 534 | 535 | 538 | 539 | 542 | 543 | 546 | 547 | 550 | 551 | 554 | 555 | 558 | 559 | 562 | 563 | 566 | 567 | 570 | 571 | 574 | 575 | 578 | 579 | 582 | 583 | 586 | 587 | 590 | 591 | 594 | 595 | 598 | 599 | 602 | 603 | 606 | 607 | 610 | 611 | 612 | 613 | 614 | 615 | -------------------------------------------------------------------------------- /UnitTest++/unittestpp_vs2008.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 26 | 29 | 32 | 35 | 38 | 41 | 53 | 56 | 59 | 62 | 66 | 69 | 72 | 75 | 78 | 81 | 82 | 90 | 93 | 96 | 99 | 102 | 105 | 115 | 118 | 121 | 124 | 128 | 131 | 134 | 137 | 140 | 143 | 144 | 151 | 154 | 157 | 160 | 163 | 166 | 178 | 181 | 184 | 187 | 194 | 197 | 200 | 203 | 206 | 209 | 212 | 215 | 216 | 224 | 227 | 230 | 233 | 236 | 239 | 249 | 252 | 255 | 258 | 265 | 268 | 271 | 274 | 277 | 280 | 283 | 286 | 287 | 294 | 297 | 300 | 303 | 306 | 309 | 321 | 324 | 327 | 330 | 334 | 337 | 340 | 343 | 346 | 349 | 350 | 358 | 361 | 364 | 367 | 370 | 373 | 383 | 386 | 389 | 392 | 396 | 399 | 402 | 405 | 408 | 411 | 412 | 413 | 414 | 415 | 416 | 419 | 422 | 423 | 426 | 427 | 428 | 431 | 432 | 435 | 436 | 439 | 440 | 443 | 444 | 447 | 448 | 451 | 452 | 455 | 456 | 459 | 460 | 463 | 464 | 467 | 468 | 471 | 472 | 475 | 476 | 479 | 480 | 483 | 484 | 487 | 488 | 491 | 492 | 495 | 496 | 499 | 500 | 503 | 504 | 507 | 508 | 511 | 512 | 515 | 516 | 519 | 520 | 523 | 524 | 527 | 528 | 531 | 532 | 535 | 536 | 539 | 540 | 543 | 544 | 547 | 548 | 551 | 552 | 555 | 556 | 559 | 560 | 563 | 564 | 567 | 568 | 571 | 572 | 575 | 576 | 579 | 580 | 583 | 584 | 587 | 588 | 591 | 592 | 595 | 596 | 599 | 600 | 603 | 604 | 605 | 606 | 607 | 608 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - Windows Server 2012 R2 3 | 4 | install: 5 | # Generate solution files using cmake, in 'builds' directory. 6 | # No need to create it because it's part of the repo. 7 | - cd builds && cmake -G "Visual Studio 12" ../ && cd .. 8 | 9 | configuration: 10 | - Debug 11 | - Release 12 | 13 | build: 14 | project: builds/UnitTest++.sln 15 | -------------------------------------------------------------------------------- /builds/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.69]) 5 | AC_INIT([UnitTest++], [1.4.1], [pjohnmeyer@gmail.com]) 6 | AC_CONFIG_SRCDIR([UnitTest++/TestDetails.cpp]) 7 | AC_CONFIG_MACRO_DIR([m4]) 8 | AC_CONFIG_HEADERS([config.h]) 9 | 10 | AM_INIT_AUTOMAKE([foreign subdir-objects]) 11 | AM_CONDITIONAL([WINDOWS], 12 | [test "${host#*mingw}" != "${host}" -o "${host#*msvc}" != "${host}"]) 13 | LT_INIT() 14 | 15 | AC_SUBST([LIBUNITTEST_SO_VERSION], [1:4:1]) 16 | 17 | # Checks for programs. 18 | AC_PROG_CXX 19 | AC_PROG_CC 20 | 21 | # Checks for libraries. 22 | 23 | # Checks for header files. 24 | AC_CHECK_HEADERS([sys/time.h unistd.h setjmp.h signal.h cassert cstddef cstdio cstring exception iosfwd iostream sstream string vector]) 25 | 26 | # Checks for typedefs, structures, and compiler characteristics. 27 | AC_CHECK_HEADER_STDBOOL 28 | AC_C_INLINE 29 | AC_TYPE_SIZE_T 30 | 31 | # Checks for library functions. 32 | AC_CHECK_FUNCS([gettimeofday strstr]) 33 | 34 | AC_CONFIG_FILES([Makefile]) 35 | AC_OUTPUT 36 | -------------------------------------------------------------------------------- /tests/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | 3 | int main(int, char const *[]) 4 | { 5 | return UnitTest::RunAllTests(); 6 | } 7 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | check_PROGRAMS = UnitTest++/TestUnitTest++ 2 | UnitTest___TestUnitTest___SOURCES = tests/Main.cpp tests/TestAssertHandler.cpp tests/TestCheckMacros.cpp tests/TestChecks.cpp tests/TestCompositeTestReporter.cpp tests/TestCurrentTest.cpp tests/TestDeferredTestReporter.cpp tests/TestExceptions.cpp tests/TestMemoryOutStream.cpp tests/TestTest.cpp tests/TestTestList.cpp tests/TestTestMacros.cpp tests/TestTestResults.cpp tests/TestTestRunner.cpp tests/TestTestSuite.cpp tests/TestTimeConstraint.cpp tests/TestTimeConstraintMacro.cpp tests/TestUnitTestPP.cpp tests/TestXmlTestReporter.cpp 3 | UnitTest___TestUnitTest___LDADD = UnitTest++/libUnitTest++.la 4 | TESTS = UnitTest++/TestUnitTest++ 5 | -------------------------------------------------------------------------------- /tests/RecordingReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_RECORDINGREPORTER_H 2 | #define UNITTEST_RECORDINGREPORTER_H 3 | 4 | #include "UnitTest++/TestReporter.h" 5 | #include 6 | 7 | #include "UnitTest++/TestDetails.h" 8 | 9 | struct RecordingReporter : public UnitTest::TestReporter 10 | { 11 | private: 12 | enum { kMaxStringLength = 256 }; 13 | 14 | public: 15 | RecordingReporter() 16 | : testRunCount(0) 17 | , testFailedCount(0) 18 | , lastFailedLine(0) 19 | , testFinishedCount(0) 20 | , lastFinishedTestTime(0) 21 | , summaryTotalTestCount(0) 22 | , summaryFailedTestCount(0) 23 | , summaryFailureCount(0) 24 | , summarySecondsElapsed(0) 25 | { 26 | lastStartedSuite[0] = '\0'; 27 | lastStartedTest[0] = '\0'; 28 | lastFailedFile[0] = '\0'; 29 | lastFailedSuite[0] = '\0'; 30 | lastFailedTest[0] = '\0'; 31 | lastFailedMessage[0] = '\0'; 32 | lastFinishedSuite[0] = '\0'; 33 | lastFinishedTest[0] = '\0'; 34 | } 35 | 36 | virtual void ReportTestStart(UnitTest::TestDetails const& test) 37 | { 38 | using namespace std; 39 | 40 | ++testRunCount; 41 | strcpy(lastStartedSuite, test.suiteName); 42 | strcpy(lastStartedTest, test.testName); 43 | } 44 | 45 | virtual void ReportFailure(UnitTest::TestDetails const& test, char const* failure) 46 | { 47 | using namespace std; 48 | 49 | ++testFailedCount; 50 | strcpy(lastFailedFile, test.filename); 51 | lastFailedLine = test.lineNumber; 52 | strcpy(lastFailedSuite, test.suiteName); 53 | strcpy(lastFailedTest, test.testName); 54 | strcpy(lastFailedMessage, failure); 55 | } 56 | 57 | virtual void ReportTestFinish(UnitTest::TestDetails const& test, float testDuration) 58 | { 59 | using namespace std; 60 | 61 | ++testFinishedCount; 62 | strcpy(lastFinishedSuite, test.suiteName); 63 | strcpy(lastFinishedTest, test.testName); 64 | lastFinishedTestTime = testDuration; 65 | } 66 | 67 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) 68 | { 69 | summaryTotalTestCount = totalTestCount; 70 | summaryFailedTestCount = failedTestCount; 71 | summaryFailureCount = failureCount; 72 | summarySecondsElapsed = secondsElapsed; 73 | } 74 | 75 | int testRunCount; 76 | char lastStartedSuite[kMaxStringLength]; 77 | char lastStartedTest[kMaxStringLength]; 78 | 79 | int testFailedCount; 80 | char lastFailedFile[kMaxStringLength]; 81 | int lastFailedLine; 82 | char lastFailedSuite[kMaxStringLength]; 83 | char lastFailedTest[kMaxStringLength]; 84 | char lastFailedMessage[kMaxStringLength]; 85 | 86 | int testFinishedCount; 87 | char lastFinishedSuite[kMaxStringLength]; 88 | char lastFinishedTest[kMaxStringLength]; 89 | float lastFinishedTestTime; 90 | 91 | int summaryTotalTestCount; 92 | int summaryFailedTestCount; 93 | int summaryFailureCount; 94 | float summarySecondsElapsed; 95 | }; 96 | 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /tests/ScopedCurrentTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_SCOPEDCURRENTTEST_H 2 | #define UNITTEST_SCOPEDCURRENTTEST_H 3 | 4 | #include "UnitTest++/CurrentTest.h" 5 | #include 6 | 7 | class ScopedCurrentTest 8 | { 9 | public: 10 | ScopedCurrentTest() 11 | : m_oldTestResults(UnitTest::CurrentTest::Results()) 12 | , m_oldTestDetails(UnitTest::CurrentTest::Details()) 13 | { 14 | } 15 | 16 | explicit ScopedCurrentTest(UnitTest::TestResults& newResults, const UnitTest::TestDetails* newDetails = NULL) 17 | : m_oldTestResults(UnitTest::CurrentTest::Results()) 18 | , m_oldTestDetails(UnitTest::CurrentTest::Details()) 19 | { 20 | UnitTest::CurrentTest::Results() = &newResults; 21 | 22 | if (newDetails != NULL) 23 | UnitTest::CurrentTest::Details() = newDetails; 24 | } 25 | 26 | ~ScopedCurrentTest() 27 | { 28 | UnitTest::CurrentTest::Results() = m_oldTestResults; 29 | UnitTest::CurrentTest::Details() = m_oldTestDetails; 30 | } 31 | 32 | private: 33 | UnitTest::TestResults* m_oldTestResults; 34 | const UnitTest::TestDetails* m_oldTestDetails; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /tests/TestAssertHandler.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/Config.h" 2 | #include "UnitTest++/UnitTestPP.h" 3 | 4 | #include "UnitTest++/ReportAssert.h" 5 | #include "UnitTest++/ReportAssertImpl.h" 6 | #include "UnitTest++/AssertException.h" 7 | 8 | #include "RecordingReporter.h" 9 | #include 10 | 11 | using namespace UnitTest; 12 | 13 | namespace { 14 | 15 | TEST(CanSetAssertExpected) 16 | { 17 | Detail::ExpectAssert(true); 18 | CHECK(Detail::AssertExpected()); 19 | 20 | Detail::ExpectAssert(false); 21 | CHECK(!Detail::AssertExpected()); 22 | } 23 | 24 | #ifndef UNITTEST_NO_EXCEPTIONS 25 | 26 | TEST(ReportAssertThrowsAssertException) 27 | { 28 | bool caught = false; 29 | 30 | try 31 | { 32 | TestResults testResults; 33 | TestDetails testDetails("", "", "", 0); 34 | Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 35 | } 36 | catch(AssertException const&) 37 | { 38 | caught = true; 39 | } 40 | 41 | CHECK(true == caught); 42 | } 43 | 44 | TEST(ReportAssertClearsExpectAssertFlag) 45 | { 46 | RecordingReporter reporter; 47 | TestResults testResults(&reporter); 48 | TestDetails testDetails("", "", "", 0); 49 | 50 | try 51 | { 52 | Detail::ExpectAssert(true); 53 | Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 54 | } 55 | catch(AssertException const&) 56 | { 57 | } 58 | 59 | CHECK(Detail::AssertExpected() == false); 60 | CHECK_EQUAL(0, reporter.testFailedCount); 61 | } 62 | 63 | TEST(ReportAssertWritesFailureToResultsAndDetailsWhenAssertIsNotExpected) 64 | { 65 | const int lineNumber = 12345; 66 | const char* description = "description"; 67 | const char* filename = "filename"; 68 | 69 | RecordingReporter reporter; 70 | TestResults testResults(&reporter); 71 | TestDetails testDetails("", "", "", 0); 72 | 73 | try 74 | { 75 | Detail::ReportAssertEx(&testResults, &testDetails, description, filename, lineNumber); 76 | } 77 | catch(AssertException const&) 78 | { 79 | } 80 | 81 | CHECK_EQUAL(description, reporter.lastFailedMessage); 82 | CHECK_EQUAL(filename, reporter.lastFailedFile); 83 | CHECK_EQUAL(lineNumber, reporter.lastFailedLine); 84 | } 85 | 86 | TEST(ReportAssertReportsNoErrorsWhenAssertIsExpected) 87 | { 88 | Detail::ExpectAssert(true); 89 | 90 | RecordingReporter reporter; 91 | TestResults testResults(&reporter); 92 | TestDetails testDetails("", "", "", 0); 93 | 94 | try 95 | { 96 | Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 97 | } 98 | catch(AssertException const&) 99 | { 100 | } 101 | 102 | CHECK_EQUAL(0, reporter.testFailedCount); 103 | } 104 | 105 | TEST(CheckAssertMacroSetsAssertExpectationToFalseAfterRunning) 106 | { 107 | Detail::ExpectAssert(true); 108 | CHECK_ASSERT(ReportAssert("", "", 0)); 109 | CHECK(!Detail::AssertExpected()); 110 | Detail::ExpectAssert(false); 111 | } 112 | 113 | #else 114 | 115 | TEST(SetAssertJumpTargetReturnsFalseWhenSettingJumpTarget) 116 | { 117 | CHECK(UNITTEST_SET_ASSERT_JUMP_TARGET() == false); 118 | } 119 | 120 | TEST(JumpToAssertJumpTarget_JumpsToSetPoint_ReturnsTrue) 121 | { 122 | const volatile bool taken = !!UNITTEST_SET_ASSERT_JUMP_TARGET(); 123 | 124 | volatile bool set = false; 125 | if (taken == false) 126 | { 127 | UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); 128 | set = true; 129 | } 130 | 131 | CHECK(set == false); 132 | } 133 | 134 | #endif 135 | 136 | } 137 | -------------------------------------------------------------------------------- /tests/TestCheckMacros.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/CurrentTest.h" 3 | #include "RecordingReporter.h" 4 | #include "ScopedCurrentTest.h" 5 | 6 | using namespace std; 7 | 8 | namespace { 9 | 10 | TEST(CheckSucceedsOnTrue) 11 | { 12 | bool failure = true; 13 | { 14 | RecordingReporter reporter; 15 | UnitTest::TestResults testResults(&reporter); 16 | 17 | ScopedCurrentTest scopedResults(testResults); 18 | CHECK(true); 19 | 20 | failure = (testResults.GetFailureCount() > 0); 21 | } 22 | 23 | CHECK(!failure); 24 | } 25 | 26 | TEST(CheckFailsOnFalse) 27 | { 28 | bool failure = false; 29 | { 30 | RecordingReporter reporter; 31 | UnitTest::TestResults testResults(&reporter); 32 | ScopedCurrentTest scopedResults(testResults); 33 | CHECK(false); 34 | failure = (testResults.GetFailureCount() > 0); 35 | } 36 | 37 | CHECK(failure); 38 | } 39 | 40 | TEST(FailureReportsCorrectTestName) 41 | { 42 | RecordingReporter reporter; 43 | { 44 | UnitTest::TestResults testResults(&reporter); 45 | ScopedCurrentTest scopedResults(testResults); 46 | CHECK(false); 47 | } 48 | 49 | CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); 50 | } 51 | 52 | TEST(CheckFailureIncludesCheckContents) 53 | { 54 | RecordingReporter reporter; 55 | { 56 | UnitTest::TestResults testResults(&reporter); 57 | ScopedCurrentTest scopedResults(testResults); 58 | const bool yaddayadda = false; 59 | CHECK(yaddayadda); 60 | } 61 | 62 | CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); 63 | } 64 | 65 | TEST(CheckEqualSucceedsOnEqual) 66 | { 67 | bool failure = true; 68 | { 69 | RecordingReporter reporter; 70 | UnitTest::TestResults testResults(&reporter); 71 | ScopedCurrentTest scopedResults(testResults); 72 | CHECK_EQUAL(1, 1); 73 | failure = (testResults.GetFailureCount() > 0); 74 | } 75 | 76 | CHECK(!failure); 77 | } 78 | 79 | TEST(CheckEqualFailsOnNotEqual) 80 | { 81 | bool failure = false; 82 | { 83 | RecordingReporter reporter; 84 | UnitTest::TestResults testResults(&reporter); 85 | ScopedCurrentTest scopedResults(testResults); 86 | CHECK_EQUAL(1, 2); 87 | failure = (testResults.GetFailureCount() > 0); 88 | } 89 | 90 | CHECK(failure); 91 | } 92 | 93 | TEST(CheckEqualFailureContainsCorrectDetails) 94 | { 95 | int line = 0; 96 | RecordingReporter reporter; 97 | { 98 | UnitTest::TestResults testResults(&reporter); 99 | UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); 100 | ScopedCurrentTest scopedResults(testResults, &testDetails); 101 | 102 | CHECK_EQUAL(1, 123); line = __LINE__; 103 | } 104 | 105 | CHECK_EQUAL("testName", reporter.lastFailedTest); 106 | CHECK_EQUAL("suiteName", reporter.lastFailedSuite); 107 | CHECK_EQUAL("filename", reporter.lastFailedFile); 108 | CHECK_EQUAL(line, reporter.lastFailedLine); 109 | } 110 | 111 | int g_sideEffect = 0; 112 | int FunctionWithSideEffects() 113 | { 114 | ++g_sideEffect; 115 | return 1; 116 | } 117 | 118 | TEST(CheckEqualDoesNotHaveSideEffectsWhenPassing) 119 | { 120 | g_sideEffect = 0; 121 | { 122 | UnitTest::TestResults testResults; 123 | ScopedCurrentTest scopedResults(testResults); 124 | CHECK_EQUAL(1, FunctionWithSideEffects()); 125 | } 126 | CHECK_EQUAL(1, g_sideEffect); 127 | } 128 | 129 | TEST(CheckEqualDoesNotHaveSideEffectsWhenFailing) 130 | { 131 | g_sideEffect = 0; 132 | { 133 | UnitTest::TestResults testResults; 134 | ScopedCurrentTest scopedResults(testResults); 135 | CHECK_EQUAL(2, FunctionWithSideEffects()); 136 | } 137 | CHECK_EQUAL(1, g_sideEffect); 138 | } 139 | 140 | 141 | TEST(CheckCloseSucceedsOnEqual) 142 | { 143 | bool failure = true; 144 | { 145 | RecordingReporter reporter; 146 | UnitTest::TestResults testResults(&reporter); 147 | ScopedCurrentTest scopedResults(testResults); 148 | CHECK_CLOSE (1.0f, 1.001f, 0.01f); 149 | failure = (testResults.GetFailureCount() > 0); 150 | } 151 | 152 | CHECK(!failure); 153 | } 154 | 155 | TEST(CheckCloseFailsOnNotEqual) 156 | { 157 | bool failure = false; 158 | { 159 | RecordingReporter reporter; 160 | UnitTest::TestResults testResults(&reporter); 161 | ScopedCurrentTest scopedResults(testResults); 162 | CHECK_CLOSE (1.0f, 1.1f, 0.01f); 163 | failure = (testResults.GetFailureCount() > 0); 164 | } 165 | 166 | CHECK(failure); 167 | } 168 | 169 | TEST(CheckCloseFailureContainsCorrectDetails) 170 | { 171 | int line = 0; 172 | RecordingReporter reporter; 173 | { 174 | UnitTest::TestResults testResults(&reporter); 175 | UnitTest::TestDetails testDetails("test", "suite", "filename", -1); 176 | ScopedCurrentTest scopedResults(testResults, &testDetails); 177 | 178 | CHECK_CLOSE (1.0f, 1.1f, 0.01f); line = __LINE__; 179 | } 180 | 181 | CHECK_EQUAL("test", reporter.lastFailedTest); 182 | CHECK_EQUAL("suite", reporter.lastFailedSuite); 183 | CHECK_EQUAL("filename", reporter.lastFailedFile); 184 | CHECK_EQUAL(line, reporter.lastFailedLine); 185 | } 186 | 187 | TEST(CheckCloseDoesNotHaveSideEffectsWhenPassing) 188 | { 189 | g_sideEffect = 0; 190 | { 191 | UnitTest::TestResults testResults; 192 | ScopedCurrentTest scopedResults(testResults); 193 | CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); 194 | } 195 | CHECK_EQUAL(1, g_sideEffect); 196 | } 197 | 198 | TEST(CheckCloseDoesNotHaveSideEffectsWhenFailing) 199 | { 200 | g_sideEffect = 0; 201 | { 202 | UnitTest::TestResults testResults; 203 | ScopedCurrentTest scopedResults(testResults); 204 | CHECK_CLOSE (2, FunctionWithSideEffects(), 0.1f); 205 | } 206 | CHECK_EQUAL(1, g_sideEffect); 207 | } 208 | 209 | TEST(CheckArrayCloseSucceedsOnEqual) 210 | { 211 | bool failure = true; 212 | { 213 | RecordingReporter reporter; 214 | UnitTest::TestResults testResults(&reporter); 215 | ScopedCurrentTest scopedResults(testResults); 216 | const float data[4] = { 0, 1, 2, 3 }; 217 | CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); 218 | failure = (testResults.GetFailureCount() > 0); 219 | } 220 | 221 | CHECK(!failure); 222 | } 223 | 224 | TEST(CheckArrayCloseFailsOnNotEqual) 225 | { 226 | bool failure = false; 227 | { 228 | RecordingReporter reporter; 229 | UnitTest::TestResults testResults(&reporter); 230 | ScopedCurrentTest scopedResults(testResults); 231 | 232 | int const data1[4] = { 0, 1, 2, 3 }; 233 | int const data2[4] = { 0, 1, 3, 3 }; 234 | CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); 235 | 236 | failure = (testResults.GetFailureCount() > 0); 237 | } 238 | 239 | CHECK(failure); 240 | } 241 | 242 | TEST(CheckArrayCloseFailureIncludesCheckExpectedAndActual) 243 | { 244 | RecordingReporter reporter; 245 | { 246 | UnitTest::TestResults testResults(&reporter); 247 | ScopedCurrentTest scopedResults(testResults); 248 | 249 | int const data1[4] = { 0, 1, 2, 3 }; 250 | int const data2[4] = { 0, 1, 3, 3 }; 251 | CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); 252 | } 253 | 254 | CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); 255 | CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); 256 | } 257 | 258 | TEST(CheckArrayCloseFailureContainsCorrectDetails) 259 | { 260 | int line = 0; 261 | RecordingReporter reporter; 262 | { 263 | UnitTest::TestResults testResults(&reporter); 264 | UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); 265 | ScopedCurrentTest scopedResults(testResults, &testDetails); 266 | 267 | int const data1[4] = { 0, 1, 2, 3 }; 268 | int const data2[4] = { 0, 1, 3, 3 }; 269 | CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); line = __LINE__; 270 | } 271 | 272 | CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); 273 | CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); 274 | CHECK_EQUAL("filename", reporter.lastFailedFile); 275 | CHECK_EQUAL(line, reporter.lastFailedLine); 276 | } 277 | 278 | TEST(CheckArrayCloseFailureIncludesTolerance) 279 | { 280 | RecordingReporter reporter; 281 | { 282 | UnitTest::TestResults testResults(&reporter); 283 | ScopedCurrentTest scopedResults(testResults); 284 | 285 | float const data1[4] = { 0, 1, 2, 3 }; 286 | float const data2[4] = { 0, 1, 3, 3 }; 287 | CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); 288 | } 289 | 290 | CHECK(strstr(reporter.lastFailedMessage, "0.01")); 291 | } 292 | 293 | TEST(CheckArrayEqualSuceedsOnEqual) 294 | { 295 | bool failure = true; 296 | { 297 | RecordingReporter reporter; 298 | UnitTest::TestResults testResults(&reporter); 299 | ScopedCurrentTest scopedResults(testResults); 300 | 301 | const float data[4] = { 0, 1, 2, 3 }; 302 | CHECK_ARRAY_EQUAL (data, data, 4); 303 | 304 | failure = (testResults.GetFailureCount() > 0); 305 | } 306 | 307 | CHECK(!failure); 308 | } 309 | 310 | TEST(CheckArrayEqualFailsOnNotEqual) 311 | { 312 | bool failure = false; 313 | { 314 | RecordingReporter reporter; 315 | UnitTest::TestResults testResults(&reporter); 316 | ScopedCurrentTest scopedResults(testResults); 317 | 318 | int const data1[4] = { 0, 1, 2, 3 }; 319 | int const data2[4] = { 0, 1, 3, 3 }; 320 | CHECK_ARRAY_EQUAL (data1, data2, 4); 321 | 322 | failure = (testResults.GetFailureCount() > 0); 323 | } 324 | 325 | CHECK(failure); 326 | } 327 | 328 | TEST(CheckArrayEqualFailureIncludesCheckExpectedAndActual) 329 | { 330 | RecordingReporter reporter; 331 | { 332 | UnitTest::TestResults testResults(&reporter); 333 | ScopedCurrentTest scopedResults(testResults); 334 | 335 | int const data1[4] = { 0, 1, 2, 3 }; 336 | int const data2[4] = { 0, 1, 3, 3 }; 337 | CHECK_ARRAY_EQUAL (data1, data2, 4); 338 | } 339 | 340 | CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); 341 | CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); 342 | } 343 | 344 | TEST(CheckArrayEqualFailureContainsCorrectInfo) 345 | { 346 | int line = 0; 347 | RecordingReporter reporter; 348 | { 349 | UnitTest::TestResults testResults(&reporter); 350 | ScopedCurrentTest scopedResults(testResults); 351 | 352 | int const data1[4] = { 0, 1, 2, 3 }; 353 | int const data2[4] = { 0, 1, 3, 3 }; 354 | CHECK_ARRAY_EQUAL (data1, data2, 4); line = __LINE__; 355 | } 356 | 357 | CHECK_EQUAL("CheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); 358 | CHECK_EQUAL(__FILE__, reporter.lastFailedFile); 359 | CHECK_EQUAL(line, reporter.lastFailedLine); 360 | } 361 | 362 | float const* FunctionWithSideEffects2() 363 | { 364 | ++g_sideEffect; 365 | static float const data[] = {1,2,3,4}; 366 | return data; 367 | } 368 | 369 | TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenPassing) 370 | { 371 | g_sideEffect = 0; 372 | { 373 | UnitTest::TestResults testResults; 374 | ScopedCurrentTest scopedResults(testResults); 375 | 376 | const float data[] = { 0, 1, 2, 3 }; 377 | CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); 378 | } 379 | CHECK_EQUAL(1, g_sideEffect); 380 | } 381 | 382 | TEST(CheckArrayCloseDoesNotHaveSideEffectsWhenFailing) 383 | { 384 | g_sideEffect = 0; 385 | { 386 | UnitTest::TestResults testResults; 387 | ScopedCurrentTest scopedResults(testResults); 388 | 389 | const float data[] = { 0, 1, 3, 3 }; 390 | CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); 391 | } 392 | 393 | CHECK_EQUAL(1, g_sideEffect); 394 | } 395 | 396 | TEST(CheckArray2DCloseSucceedsOnEqual) 397 | { 398 | bool failure = true; 399 | { 400 | RecordingReporter reporter; 401 | UnitTest::TestResults testResults(&reporter); 402 | ScopedCurrentTest scopedResults(testResults); 403 | 404 | const float data[2][2] = { {0, 1}, {2, 3} }; 405 | CHECK_ARRAY2D_CLOSE (data, data, 2, 2, 0.01f); 406 | 407 | failure = (testResults.GetFailureCount() > 0); 408 | } 409 | 410 | CHECK(!failure); 411 | } 412 | 413 | TEST(CheckArray2DCloseFailsOnNotEqual) 414 | { 415 | bool failure = false; 416 | { 417 | RecordingReporter reporter; 418 | UnitTest::TestResults testResults(&reporter); 419 | ScopedCurrentTest scopedResults(testResults); 420 | 421 | int const data1[2][2] = { {0, 1}, {2, 3} }; 422 | int const data2[2][2] = { {0, 1}, {3, 3} }; 423 | CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); 424 | 425 | failure = (testResults.GetFailureCount() > 0); 426 | } 427 | 428 | CHECK(failure); 429 | } 430 | 431 | TEST(CheckArray2DCloseFailureIncludesCheckExpectedAndActual) 432 | { 433 | RecordingReporter reporter; 434 | { 435 | UnitTest::TestResults testResults(&reporter); 436 | ScopedCurrentTest scopedResults(testResults); 437 | 438 | int const data1[2][2] = { {0, 1}, {2, 3} }; 439 | int const data2[2][2] = { {0, 1}, {3, 3} }; 440 | 441 | CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); 442 | } 443 | 444 | CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); 445 | CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); 446 | } 447 | 448 | TEST(CheckArray2DCloseFailureContainsCorrectDetails) 449 | { 450 | int line = 0; 451 | RecordingReporter reporter; 452 | { 453 | UnitTest::TestResults testResults(&reporter); 454 | UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); 455 | ScopedCurrentTest scopedResults(testResults, &testDetails); 456 | 457 | int const data1[2][2] = { {0, 1}, {2, 3} }; 458 | int const data2[2][2] = { {0, 1}, {3, 3} }; 459 | CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); line = __LINE__; 460 | } 461 | 462 | CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); 463 | CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); 464 | CHECK_EQUAL("filename", reporter.lastFailedFile); 465 | CHECK_EQUAL(line, reporter.lastFailedLine); 466 | } 467 | 468 | TEST(CheckArray2DCloseFailureIncludesTolerance) 469 | { 470 | RecordingReporter reporter; 471 | { 472 | UnitTest::TestResults testResults(&reporter); 473 | ScopedCurrentTest scopedResults(testResults); 474 | 475 | float const data1[2][2] = { {0, 1}, {2, 3} }; 476 | float const data2[2][2] = { {0, 1}, {3, 3} }; 477 | CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); 478 | } 479 | 480 | CHECK(strstr(reporter.lastFailedMessage, "0.01")); 481 | } 482 | 483 | float const* const* FunctionWithSideEffects3() 484 | { 485 | ++g_sideEffect; 486 | static float const data1[] = {0,1}; 487 | static float const data2[] = {2,3}; 488 | static const float* const data[] = {data1, data2}; 489 | return data; 490 | } 491 | 492 | TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) 493 | { 494 | g_sideEffect = 0; 495 | { 496 | UnitTest::TestResults testResults; 497 | ScopedCurrentTest scopedResults(testResults); 498 | 499 | const float data[2][2] = { {0, 1}, {2, 3} }; 500 | CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); 501 | } 502 | CHECK_EQUAL(1, g_sideEffect); 503 | } 504 | 505 | TEST(CheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) 506 | { 507 | g_sideEffect = 0; 508 | { 509 | UnitTest::TestResults testResults; 510 | ScopedCurrentTest scopedResults(testResults); 511 | 512 | const float data[2][2] = { {0, 1}, {3, 3} }; 513 | CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); 514 | } 515 | CHECK_EQUAL(1, g_sideEffect); 516 | } 517 | 518 | } 519 | -------------------------------------------------------------------------------- /tests/TestChecks.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "RecordingReporter.h" 3 | 4 | #include 5 | 6 | using namespace UnitTest; 7 | 8 | 9 | namespace { 10 | 11 | 12 | TEST(CheckEqualWithUnsignedLong) 13 | { 14 | TestResults results; 15 | unsigned long something = 2; 16 | CHECK_EQUAL(something, something); 17 | } 18 | 19 | TEST(CheckEqualsWithStringsFailsOnDifferentStrings) 20 | { 21 | char txt1[] = "Hello"; 22 | char txt2[] = "Hallo"; 23 | TestResults results; 24 | CheckEqual(results, txt1, txt2, TestDetails("", "", "", 0)); 25 | CHECK_EQUAL(1, results.GetFailureCount()); 26 | } 27 | 28 | char txt1[] = "Hello"; // non-const on purpose so no folding of duplicate data 29 | char txt2[] = "Hello"; 30 | 31 | TEST(CheckEqualsWithStringsWorksOnContentsNonConstNonConst) 32 | { 33 | char const* const p1 = txt1; 34 | char const* const p2 = txt2; 35 | TestResults results; 36 | CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); 37 | CHECK_EQUAL(0, results.GetFailureCount()); 38 | } 39 | 40 | TEST(CheckEqualsWithStringsWorksOnContentsConstConst) 41 | { 42 | char* const p1 = txt1; 43 | char* const p2 = txt2; 44 | TestResults results; 45 | CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); 46 | CHECK_EQUAL(0, results.GetFailureCount()); 47 | } 48 | 49 | TEST(CheckEqualsWithStringsWorksOnContentsNonConstConst) 50 | { 51 | char* const p1 = txt1; 52 | char const* const p2 = txt2; 53 | TestResults results; 54 | CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); 55 | CHECK_EQUAL(0, results.GetFailureCount()); 56 | } 57 | 58 | TEST(CheckEqualsWithStringsWorksOnContentsConstNonConst) 59 | { 60 | char const* const p1 = txt1; 61 | char* const p2 = txt2; 62 | TestResults results; 63 | CheckEqual(results, p1, p2, TestDetails("", "", "", 0)); 64 | CHECK_EQUAL(0, results.GetFailureCount()); 65 | } 66 | 67 | TEST(CheckEqualsWithStringsWorksOnContentsWithALiteral) 68 | { 69 | char const* const p1 = txt1; 70 | TestResults results; 71 | CheckEqual(results, "Hello", p1, TestDetails("", "", "", 0)); 72 | CHECK_EQUAL(0, results.GetFailureCount()); 73 | } 74 | 75 | TEST(CheckEqualsWithStringsWorksOnNullExpected) 76 | { 77 | char const* const expected = "hi"; 78 | char const* const actual = NULL; 79 | TestResults results; 80 | CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); 81 | CHECK_EQUAL (1, results.GetFailureCount()); 82 | } 83 | 84 | TEST(CheckEqualsWithStringsWorksOnNullActual) 85 | { 86 | char const* const expected = NULL; 87 | char const* const actual = "hi"; 88 | TestResults results; 89 | CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); 90 | CHECK_EQUAL (1, results.GetFailureCount()); 91 | } 92 | 93 | TEST(CheckEqualsWithStringsWorksOnNullExpectedAndActual) 94 | { 95 | char const* const expected = NULL; 96 | char const* const actual = NULL; 97 | TestResults results; 98 | CheckEqual(results, expected, actual, TestDetails("", "", "", 0)); 99 | CHECK_EQUAL (0, results.GetFailureCount()); 100 | } 101 | 102 | TEST(CheckEqualFailureIncludesCheckExpectedAndActual) 103 | { 104 | RecordingReporter reporter; 105 | TestResults results(&reporter); 106 | const int something = 2; 107 | CheckEqual(results, 1, something, TestDetails("", "", "", 0)); 108 | 109 | using namespace std; 110 | CHECK(strstr(reporter.lastFailedMessage, "xpected 1")); 111 | CHECK(strstr(reporter.lastFailedMessage, "was 2")); 112 | } 113 | 114 | TEST(CheckEqualFailureIncludesDetails) 115 | { 116 | RecordingReporter reporter; 117 | TestResults results(&reporter); 118 | TestDetails const details("mytest", "mysuite", "file.h", 101); 119 | 120 | CheckEqual(results, 1, 2, details); 121 | 122 | CHECK_EQUAL("mytest", reporter.lastFailedTest); 123 | CHECK_EQUAL("mysuite", reporter.lastFailedSuite); 124 | CHECK_EQUAL("file.h", reporter.lastFailedFile); 125 | CHECK_EQUAL(101, reporter.lastFailedLine); 126 | } 127 | 128 | TEST(CheckCloseTrue) 129 | { 130 | TestResults results; 131 | CheckClose(results, 3.001f, 3.0f, 0.1f, TestDetails("", "", "", 0)); 132 | CHECK_EQUAL(0, results.GetFailureCount()); 133 | } 134 | 135 | TEST(CheckCloseFalse) 136 | { 137 | TestResults results; 138 | CheckClose(results, 3.12f, 3.0f, 0.1f, TestDetails("", "", "", 0)); 139 | CHECK_EQUAL(1, results.GetFailureCount()); 140 | } 141 | 142 | TEST(CheckCloseWithZeroEpsilonWorksForSameNumber) 143 | { 144 | TestResults results; 145 | CheckClose(results, 0.1f, 0.1f, 0, TestDetails("", "", "", 0)); 146 | CHECK_EQUAL(0, results.GetFailureCount()); 147 | } 148 | 149 | TEST(CheckCloseWithNaNFails) 150 | { 151 | const unsigned int bitpattern = 0xFFFFFFFF; 152 | float nan; 153 | UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); 154 | 155 | TestResults results; 156 | CheckClose(results, 3.0f, nan, 0.1f, TestDetails("", "", "", 0)); 157 | CHECK_EQUAL(1, results.GetFailureCount()); 158 | } 159 | 160 | TEST(CheckCloseWithNaNAgainstItselfFails) 161 | { 162 | const unsigned int bitpattern = 0xFFFFFFFF; 163 | float nan; 164 | UNIITEST_NS_QUAL_STD(memcpy)(&nan, &bitpattern, sizeof(bitpattern)); 165 | 166 | TestResults results; 167 | CheckClose(results, nan, nan, 0.1f, TestDetails("", "", "", 0)); 168 | CHECK_EQUAL(1, results.GetFailureCount()); 169 | } 170 | 171 | TEST(CheckCloseFailureIncludesCheckExpectedAndActual) 172 | { 173 | RecordingReporter reporter; 174 | TestResults results(&reporter); 175 | const float expected = 0.9f; 176 | const float actual = 1.1f; 177 | CheckClose(results, expected, actual, 0.01f, TestDetails("", "", "", 0)); 178 | 179 | using namespace std; 180 | CHECK(strstr(reporter.lastFailedMessage, "xpected 0.9")); 181 | CHECK(strstr(reporter.lastFailedMessage, "was 1.1")); 182 | } 183 | 184 | TEST(CheckCloseFailureIncludesTolerance) 185 | { 186 | RecordingReporter reporter; 187 | TestResults results(&reporter); 188 | CheckClose(results, 2, 3, 0.01f, TestDetails("", "", "", 0)); 189 | 190 | using namespace std; 191 | CHECK(strstr(reporter.lastFailedMessage, "0.01")); 192 | } 193 | 194 | TEST(CheckCloseFailureIncludesDetails) 195 | { 196 | RecordingReporter reporter; 197 | TestResults results(&reporter); 198 | TestDetails const details("mytest", "mysuite", "header.h", 10); 199 | 200 | CheckClose(results, 2, 3, 0.01f, details); 201 | 202 | CHECK_EQUAL("mytest", reporter.lastFailedTest); 203 | CHECK_EQUAL("mysuite", reporter.lastFailedSuite); 204 | CHECK_EQUAL("header.h", reporter.lastFailedFile); 205 | CHECK_EQUAL(10, reporter.lastFailedLine); 206 | } 207 | 208 | 209 | TEST(CheckArrayEqualTrue) 210 | { 211 | TestResults results; 212 | 213 | int const array[3] = { 1, 2, 3 }; 214 | CheckArrayEqual(results, array, array, 3, TestDetails("", "", "", 0)); 215 | CHECK_EQUAL(0, results.GetFailureCount()); 216 | } 217 | 218 | TEST(CheckArrayEqualFalse) 219 | { 220 | TestResults results; 221 | 222 | int const array1[3] = { 1, 2, 3 }; 223 | int const array2[3] = { 1, 2, 2 }; 224 | CheckArrayEqual(results, array1, array2, 3, TestDetails("", "", "", 0)); 225 | CHECK_EQUAL(1, results.GetFailureCount()); 226 | } 227 | 228 | TEST(CheckArrayCloseTrue) 229 | { 230 | TestResults results; 231 | 232 | float const array1[3] = { 1.0f, 1.5f, 2.0f }; 233 | float const array2[3] = { 1.01f, 1.51f, 2.01f }; 234 | CheckArrayClose(results, array1, array2, 3, 0.02f, TestDetails("", "", "", 0)); 235 | CHECK_EQUAL(0, results.GetFailureCount()); 236 | } 237 | 238 | TEST(CheckArrayCloseFalse) 239 | { 240 | TestResults results; 241 | 242 | float const array1[3] = { 1.0f, 1.5f, 2.0f }; 243 | float const array2[3] = { 1.01f, 1.51f, 2.01f }; 244 | CheckArrayClose(results, array1, array2, 3, 0.001f, TestDetails("", "", "", 0)); 245 | CHECK_EQUAL(1, results.GetFailureCount()); 246 | } 247 | 248 | TEST(CheckArrayCloseFailureIncludesDetails) 249 | { 250 | RecordingReporter reporter; 251 | TestResults results(&reporter); 252 | TestDetails const details("arrayCloseTest", "arrayCloseSuite", "file", 1337); 253 | 254 | float const array1[3] = { 1.0f, 1.5f, 2.0f }; 255 | float const array2[3] = { 1.01f, 1.51f, 2.01f }; 256 | CheckArrayClose(results, array1, array2, 3, 0.001f, details); 257 | 258 | CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); 259 | CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); 260 | CHECK_EQUAL("file", reporter.lastFailedFile); 261 | CHECK_EQUAL(1337, reporter.lastFailedLine); 262 | } 263 | 264 | 265 | TEST(CheckArray2DCloseTrue) 266 | { 267 | TestResults results; 268 | 269 | float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, 270 | { 2.0f, 2.5f, 3.0f }, 271 | { 3.0f, 3.5f, 4.0f } }; 272 | float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, 273 | { 2.01f, 2.51f, 3.01f }, 274 | { 3.01f, 3.51f, 4.01f } }; 275 | CheckArray2DClose(results, array1, array2, 3, 3, 0.02f, TestDetails("", "", "", 0)); 276 | CHECK_EQUAL(0, results.GetFailureCount()); 277 | } 278 | 279 | TEST(CheckArray2DCloseFalse) 280 | { 281 | TestResults results; 282 | 283 | float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, 284 | { 2.0f, 2.5f, 3.0f }, 285 | { 3.0f, 3.5f, 4.0f } }; 286 | float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, 287 | { 2.01f, 2.51f, 3.01f }, 288 | { 3.01f, 3.51f, 4.01f } }; 289 | CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, TestDetails("", "", "", 0)); 290 | CHECK_EQUAL(1, results.GetFailureCount()); 291 | } 292 | 293 | TEST(CheckCloseWithDoublesSucceeds) 294 | { 295 | CHECK_CLOSE(0.5, 0.5, 0.0001); 296 | } 297 | 298 | TEST(CheckArray2DCloseFailureIncludesDetails) 299 | { 300 | RecordingReporter reporter; 301 | TestResults results(&reporter); 302 | TestDetails const details("array2DCloseTest", "array2DCloseSuite", "file", 1234); 303 | 304 | float const array1[3][3] = { { 1.0f, 1.5f, 2.0f }, 305 | { 2.0f, 2.5f, 3.0f }, 306 | { 3.0f, 3.5f, 4.0f } }; 307 | float const array2[3][3] = { { 1.01f, 1.51f, 2.01f }, 308 | { 2.01f, 2.51f, 3.01f }, 309 | { 3.01f, 3.51f, 4.01f } }; 310 | CheckArray2DClose(results, array1, array2, 3, 3, 0.001f, details); 311 | 312 | CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); 313 | CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); 314 | CHECK_EQUAL("file", reporter.lastFailedFile); 315 | CHECK_EQUAL(1234, reporter.lastFailedLine); 316 | } 317 | 318 | } 319 | -------------------------------------------------------------------------------- /tests/TestCompositeTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/CompositeTestReporter.h" 3 | 4 | using namespace UnitTest; 5 | 6 | namespace { 7 | 8 | TEST(ZeroReportersByDefault) 9 | { 10 | CHECK_EQUAL(0, CompositeTestReporter().GetReporterCount()); 11 | } 12 | 13 | struct MockReporter : TestReporter 14 | { 15 | MockReporter() 16 | : testStartCalled(false) 17 | , testStartDetails(NULL) 18 | , failureCalled(false) 19 | , failureDetails(NULL) 20 | , failureStr(NULL) 21 | , testFinishCalled(false) 22 | , testFinishDetails(NULL) 23 | , testFinishSecondsElapsed(-1.0f) 24 | , summaryCalled(false) 25 | , summaryTotalTestCount(-1) 26 | , summaryFailureCount(-1) 27 | , summarySecondsElapsed(-1.0f) 28 | { 29 | } 30 | 31 | virtual void ReportTestStart(TestDetails const& test) 32 | { 33 | testStartCalled = true; 34 | testStartDetails = &test; 35 | } 36 | 37 | virtual void ReportFailure(TestDetails const& test, char const* failure) 38 | { 39 | failureCalled = true; 40 | failureDetails = &test; 41 | failureStr = failure; 42 | } 43 | 44 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) 45 | { 46 | testFinishCalled = true; 47 | testFinishDetails = &test; 48 | testFinishSecondsElapsed = secondsElapsed; 49 | } 50 | 51 | virtual void ReportSummary(int totalTestCount, 52 | int failedTestCount, 53 | int failureCount, 54 | float secondsElapsed) 55 | { 56 | summaryCalled = true; 57 | summaryTotalTestCount = totalTestCount; 58 | summaryFailedTestCount = failedTestCount; 59 | summaryFailureCount = failureCount; 60 | summarySecondsElapsed = secondsElapsed; 61 | } 62 | 63 | bool testStartCalled; 64 | TestDetails const* testStartDetails; 65 | 66 | bool failureCalled; 67 | TestDetails const* failureDetails; 68 | const char* failureStr; 69 | 70 | bool testFinishCalled; 71 | TestDetails const* testFinishDetails; 72 | float testFinishSecondsElapsed; 73 | 74 | bool summaryCalled; 75 | int summaryTotalTestCount; 76 | int summaryFailedTestCount; 77 | int summaryFailureCount; 78 | float summarySecondsElapsed; 79 | }; 80 | 81 | TEST(AddReporter) 82 | { 83 | MockReporter r; 84 | CompositeTestReporter c; 85 | 86 | CHECK(c.AddReporter(&r)); 87 | CHECK_EQUAL(1, c.GetReporterCount()); 88 | } 89 | 90 | TEST(RemoveReporter) 91 | { 92 | MockReporter r; 93 | CompositeTestReporter c; 94 | 95 | c.AddReporter(&r); 96 | CHECK(c.RemoveReporter(&r)); 97 | CHECK_EQUAL(0, c.GetReporterCount()); 98 | } 99 | 100 | struct Fixture 101 | { 102 | Fixture() 103 | { 104 | c.AddReporter(&r0); 105 | c.AddReporter(&r1); 106 | } 107 | 108 | MockReporter r0, r1; 109 | CompositeTestReporter c; 110 | }; 111 | 112 | TEST_FIXTURE(Fixture, ReportTestStartCallsReportTestStartOnAllAggregates) 113 | { 114 | TestDetails t("", "", "", 0); 115 | c.ReportTestStart(t); 116 | 117 | CHECK(r0.testStartCalled); 118 | CHECK_EQUAL(&t, r0.testStartDetails); 119 | CHECK(r1.testStartCalled); 120 | CHECK_EQUAL(&t, r1.testStartDetails); 121 | } 122 | 123 | TEST_FIXTURE(Fixture, ReportFailureCallsReportFailureOnAllAggregates) 124 | { 125 | TestDetails t("", "", "", 0); 126 | const char* failStr = "fail"; 127 | c.ReportFailure(t, failStr); 128 | 129 | CHECK(r0.failureCalled); 130 | CHECK_EQUAL(&t, r0.failureDetails); 131 | CHECK_EQUAL(failStr, r0.failureStr); 132 | 133 | CHECK(r1.failureCalled); 134 | CHECK_EQUAL(&t, r1.failureDetails); 135 | CHECK_EQUAL(failStr, r1.failureStr); 136 | } 137 | 138 | TEST_FIXTURE(Fixture, ReportTestFinishCallsReportTestFinishOnAllAggregates) 139 | { 140 | TestDetails t("", "", "", 0); 141 | const float s = 1.2345f; 142 | c.ReportTestFinish(t, s); 143 | 144 | CHECK(r0.testFinishCalled); 145 | CHECK_EQUAL(&t, r0.testFinishDetails); 146 | CHECK_CLOSE(s, r0.testFinishSecondsElapsed, 0.00001f); 147 | 148 | CHECK(r1.testFinishCalled); 149 | CHECK_EQUAL(&t, r1.testFinishDetails); 150 | CHECK_CLOSE(s, r1.testFinishSecondsElapsed, 0.00001f); 151 | } 152 | 153 | TEST_FIXTURE(Fixture, ReportSummaryCallsReportSummaryOnAllAggregates) 154 | { 155 | TestDetails t("", "", "", 0); 156 | const int testCount = 3; 157 | const int failedTestCount = 4; 158 | const int failureCount = 5; 159 | const float secondsElapsed = 3.14159f; 160 | 161 | c.ReportSummary(testCount, failedTestCount, failureCount, secondsElapsed); 162 | 163 | CHECK(r0.summaryCalled); 164 | CHECK_EQUAL(testCount, r0.summaryTotalTestCount); 165 | CHECK_EQUAL(failedTestCount, r0.summaryFailedTestCount); 166 | CHECK_EQUAL(failureCount, r0.summaryFailureCount); 167 | CHECK_CLOSE(secondsElapsed, r0.summarySecondsElapsed, 0.00001f); 168 | 169 | CHECK(r1.summaryCalled); 170 | CHECK_EQUAL(testCount, r1.summaryTotalTestCount); 171 | CHECK_EQUAL(failedTestCount, r1.summaryFailedTestCount); 172 | CHECK_EQUAL(failureCount, r1.summaryFailureCount); 173 | CHECK_CLOSE(secondsElapsed, r1.summarySecondsElapsed, 0.00001f); 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /tests/TestCurrentTest.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/CurrentTest.h" 3 | #include "ScopedCurrentTest.h" 4 | 5 | namespace 6 | { 7 | 8 | TEST(CanSetandGetDetails) 9 | { 10 | bool ok = false; 11 | { 12 | ScopedCurrentTest scopedTest; 13 | 14 | const UnitTest::TestDetails* details = reinterpret_cast< const UnitTest::TestDetails* >(12345); 15 | UnitTest::CurrentTest::Details() = details; 16 | 17 | ok = (UnitTest::CurrentTest::Details() == details); 18 | } 19 | 20 | CHECK(ok); 21 | } 22 | 23 | TEST(CanSetAndGetResults) 24 | { 25 | bool ok = false; 26 | { 27 | ScopedCurrentTest scopedTest; 28 | 29 | UnitTest::TestResults results; 30 | UnitTest::CurrentTest::Results() = &results; 31 | 32 | ok = (UnitTest::CurrentTest::Results() == &results); 33 | } 34 | 35 | CHECK(ok); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /tests/TestDeferredTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/Config.h" 2 | 3 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 4 | 5 | #include "UnitTest++/UnitTestPP.h" 6 | #include "UnitTest++/DeferredTestReporter.h" 7 | #include 8 | 9 | namespace UnitTest 10 | { 11 | 12 | namespace 13 | { 14 | 15 | #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 16 | MemoryOutStream& operator <<(MemoryOutStream& lhs, const std::string& rhs) 17 | { 18 | lhs << rhs.c_str(); 19 | return lhs; 20 | } 21 | #endif 22 | 23 | struct MockDeferredTestReporter : public DeferredTestReporter 24 | { 25 | virtual void ReportSummary(int, int, int, float) 26 | { 27 | } 28 | }; 29 | 30 | struct DeferredTestReporterFixture 31 | { 32 | DeferredTestReporterFixture() 33 | : testName("UniqueTestName") 34 | , testSuite("UniqueTestSuite") 35 | , fileName("filename.h") 36 | , lineNumber(12) 37 | , details(testName.c_str(), testSuite.c_str(), fileName.c_str(), lineNumber) 38 | { 39 | } 40 | 41 | MockDeferredTestReporter reporter; 42 | std::string const testName; 43 | std::string const testSuite; 44 | std::string const fileName; 45 | int const lineNumber; 46 | TestDetails const details; 47 | }; 48 | 49 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCreatesANewDeferredTest) 50 | { 51 | reporter.ReportTestStart(details); 52 | CHECK_EQUAL(1, (int)reporter.GetResults().size()); 53 | } 54 | 55 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCapturesTestNameAndSuite) 56 | { 57 | reporter.ReportTestStart(details); 58 | 59 | DeferredTestResult const& result = reporter.GetResults().at(0); 60 | CHECK_EQUAL(testName.c_str(), result.testName.c_str()); 61 | CHECK_EQUAL(testSuite.c_str(), result.suiteName.c_str()); 62 | } 63 | 64 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestEndCapturesTestTime) 65 | { 66 | float const elapsed = 123.45f; 67 | reporter.ReportTestStart(details); 68 | reporter.ReportTestFinish(details, elapsed); 69 | 70 | DeferredTestResult const& result = reporter.GetResults().at(0); 71 | CHECK_CLOSE(elapsed, result.timeElapsed, 0.0001f); 72 | } 73 | 74 | TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetails) 75 | { 76 | char const* failure = "failure"; 77 | 78 | reporter.ReportTestStart(details); 79 | reporter.ReportFailure(details, failure); 80 | 81 | DeferredTestResult const& result = reporter.GetResults().at(0); 82 | CHECK(result.failed == true); 83 | CHECK_EQUAL(fileName.c_str(), result.failureFile.c_str()); 84 | } 85 | 86 | TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetailsForMultipleFailures) 87 | { 88 | char const* failure1 = "failure 1"; 89 | char const* failure2 = "failure 2"; 90 | 91 | reporter.ReportTestStart(details); 92 | reporter.ReportFailure(details, failure1); 93 | reporter.ReportFailure(details, failure2); 94 | 95 | DeferredTestResult const& result = reporter.GetResults().at(0); 96 | CHECK_EQUAL(2, (int)result.failures.size()); 97 | CHECK_EQUAL(failure1, result.failures[0].failureStr); 98 | CHECK_EQUAL(failure2, result.failures[1].failureStr); 99 | } 100 | 101 | TEST_FIXTURE(DeferredTestReporterFixture, DeferredTestReporterTakesCopyOfFailureMessage) 102 | { 103 | reporter.ReportTestStart(details); 104 | 105 | char failureMessage[128]; 106 | char const* goodStr = "Real failure message"; 107 | char const* badStr = "Bogus failure message"; 108 | 109 | using namespace std; 110 | 111 | strcpy(failureMessage, goodStr); 112 | reporter.ReportFailure(details, failureMessage); 113 | strcpy(failureMessage, badStr); 114 | 115 | DeferredTestResult const& result = reporter.GetResults().at(0); 116 | DeferredTestFailure const& failure = result.failures.at(0); 117 | CHECK_EQUAL(goodStr, failure.failureStr); 118 | } 119 | 120 | }} 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /tests/TestMemoryOutStream.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | 3 | #include "UnitTest++/MemoryOutStream.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace UnitTest; 10 | using namespace std; 11 | 12 | namespace { 13 | 14 | const char* const maxSignedIntegralStr(size_t nBytes) 15 | { 16 | switch(nBytes) 17 | { 18 | case 8: 19 | return "9223372036854775807"; 20 | case 4: 21 | return "2147483647"; 22 | case 2: 23 | return "32767"; 24 | case 1: 25 | return "127"; 26 | default: 27 | return "Unsupported signed integral size"; 28 | } 29 | } 30 | 31 | const char* const minSignedIntegralStr(size_t nBytes) 32 | { 33 | switch(nBytes) 34 | { 35 | case 8: 36 | return "-9223372036854775808"; 37 | case 4: 38 | return "-2147483648"; 39 | case 2: 40 | return "-32768"; 41 | case 1: 42 | return "-128"; 43 | default: 44 | return "Unsupported signed integral size"; 45 | } 46 | } 47 | 48 | const char* const maxUnsignedIntegralStr(size_t nBytes) 49 | { 50 | switch(nBytes) 51 | { 52 | case 8: 53 | return "18446744073709551615"; 54 | case 4: 55 | return "4294967295"; 56 | case 2: 57 | return "65535"; 58 | case 1: 59 | return "255"; 60 | default: 61 | return "Unsupported signed integral size"; 62 | } 63 | } 64 | 65 | TEST(DefaultIsEmptyString) 66 | { 67 | MemoryOutStream const stream; 68 | CHECK(stream.GetText() != 0); 69 | CHECK_EQUAL("", stream.GetText()); 70 | } 71 | 72 | TEST(StreamingTextCopiesCharacters) 73 | { 74 | MemoryOutStream stream; 75 | stream << "Lalala"; 76 | CHECK_EQUAL("Lalala", stream.GetText()); 77 | } 78 | 79 | TEST(StreamingMultipleTimesConcatenatesResult) 80 | { 81 | MemoryOutStream stream; 82 | stream << "Bork" << "Foo" << "Bar"; 83 | CHECK_EQUAL("BorkFooBar", stream.GetText()); 84 | } 85 | 86 | TEST(StreamingIntWritesCorrectCharacters) 87 | { 88 | MemoryOutStream stream; 89 | stream << (int)123; 90 | CHECK_EQUAL("123", stream.GetText()); 91 | } 92 | 93 | TEST(StreaminMaxIntWritesCorrectCharacters) 94 | { 95 | MemoryOutStream stream; 96 | stream << INT_MAX; 97 | CHECK_EQUAL(maxSignedIntegralStr(sizeof(int)), stream.GetText()); 98 | } 99 | 100 | TEST(StreamingMinIntWritesCorrectCharacters) 101 | { 102 | MemoryOutStream stream; 103 | stream << INT_MIN; 104 | CHECK_EQUAL(minSignedIntegralStr(sizeof(int)), stream.GetText()); 105 | } 106 | 107 | TEST(StreamingUnsignedIntWritesCorrectCharacters) 108 | { 109 | MemoryOutStream stream; 110 | stream << (unsigned int)123; 111 | CHECK_EQUAL("123", stream.GetText()); 112 | } 113 | 114 | TEST(StreamingMaxUnsignedIntWritesCorrectCharacters) 115 | { 116 | MemoryOutStream stream; 117 | stream << (unsigned int)UINT_MAX; 118 | CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned int)), stream.GetText()); 119 | } 120 | 121 | TEST(StreamingMinUnsignedIntWritesCorrectCharacters) 122 | { 123 | MemoryOutStream stream; 124 | stream << (unsigned int)0; 125 | CHECK_EQUAL("0", stream.GetText()); 126 | } 127 | 128 | TEST(StreamingLongWritesCorrectCharacters) 129 | { 130 | MemoryOutStream stream; 131 | stream << (long)(-123); 132 | CHECK_EQUAL("-123", stream.GetText()); 133 | } 134 | 135 | TEST(StreamingMaxLongWritesCorrectCharacters) 136 | { 137 | MemoryOutStream stream; 138 | stream << (long)(LONG_MAX); 139 | CHECK_EQUAL(maxSignedIntegralStr(sizeof(long)), stream.GetText()); 140 | } 141 | 142 | TEST(StreamingMinLongWritesCorrectCharacters) 143 | { 144 | MemoryOutStream stream; 145 | stream << (long)(LONG_MIN); 146 | CHECK_EQUAL(minSignedIntegralStr(sizeof(long)), stream.GetText()); 147 | } 148 | 149 | TEST(StreamingUnsignedLongWritesCorrectCharacters) 150 | { 151 | MemoryOutStream stream; 152 | stream << (unsigned long)123; 153 | CHECK_EQUAL("123", stream.GetText()); 154 | } 155 | 156 | TEST(StreamingMaxUnsignedLongWritesCorrectCharacters) 157 | { 158 | MemoryOutStream stream; 159 | stream << (unsigned long)ULONG_MAX; 160 | CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long)), stream.GetText()); 161 | } 162 | 163 | TEST(StreamingMinUnsignedLongWritesCorrectCharacters) 164 | { 165 | MemoryOutStream stream; 166 | stream << (unsigned long)0ul; 167 | CHECK_EQUAL("0", stream.GetText()); 168 | } 169 | 170 | TEST(StreamingLongLongWritesCorrectCharacters) 171 | { 172 | MemoryOutStream stream; 173 | #ifdef UNITTEST_COMPILER_IS_MSVC6 174 | stream << (__int64)-12345i64; 175 | #else 176 | stream << (long long)-12345ll; 177 | #endif 178 | CHECK_EQUAL("-12345", stream.GetText()); 179 | } 180 | 181 | #ifdef LLONG_MAX 182 | TEST(StreamingMaxLongLongWritesCorrectCharacters) 183 | { 184 | MemoryOutStream stream; 185 | stream << (long long)LLONG_MAX; 186 | CHECK_EQUAL(maxSignedIntegralStr(sizeof(long long)), stream.GetText()); 187 | } 188 | #endif 189 | 190 | #ifdef LLONG_MIN 191 | TEST(StreamingMinLongLongWritesCorrectCharacters) 192 | { 193 | MemoryOutStream stream; 194 | stream << (long long)LLONG_MIN; 195 | CHECK_EQUAL(minSignedIntegralStr(sizeof(long long)), stream.GetText()); 196 | } 197 | #endif 198 | 199 | TEST(StreamingUnsignedLongLongWritesCorrectCharacters) 200 | { 201 | MemoryOutStream stream; 202 | #ifdef UNITTEST_COMPILER_IS_MSVC6 203 | stream << (unsigned __int64)85899ui64; 204 | #else 205 | stream << (unsigned long long)85899ull; 206 | #endif 207 | CHECK_EQUAL("85899", stream.GetText()); 208 | } 209 | 210 | #ifdef ULLONG_MAX 211 | TEST(StreamingMaxUnsignedLongLongWritesCorrectCharacters) 212 | { 213 | MemoryOutStream stream; 214 | stream << (unsigned long long)ULLONG_MAX; 215 | CHECK_EQUAL(maxUnsignedIntegralStr(sizeof(unsigned long long)), stream.GetText()); 216 | } 217 | #endif 218 | 219 | TEST(StreamingMinUnsignedLongLongWritesCorrectCharacters) 220 | { 221 | MemoryOutStream stream; 222 | #ifdef UNITTEST_COMPILER_IS_MSVC6 223 | stream << (unsigned __int64)0ui64; 224 | #else 225 | stream << (unsigned long long)0ull; 226 | #endif 227 | CHECK_EQUAL("0", stream.GetText()); 228 | } 229 | 230 | TEST(StreamingFloatWritesCorrectCharacters) 231 | { 232 | MemoryOutStream stream; 233 | stream << 3.1415f; 234 | CHECK(strstr(stream.GetText(), "3.1415")); 235 | } 236 | 237 | TEST(StreamingDoubleWritesCorrectCharacters) 238 | { 239 | MemoryOutStream stream; 240 | stream << 3.1415; 241 | CHECK(strstr(stream.GetText(), "3.1415")); 242 | } 243 | 244 | TEST(StreamingPointerWritesCorrectCharacters) 245 | { 246 | MemoryOutStream stream; 247 | int* p = (int*)0x1234; 248 | stream << p; 249 | CHECK(strstr(stream.GetText(), "1234")); 250 | } 251 | 252 | TEST(StreamingSizeTWritesCorrectCharacters) 253 | { 254 | MemoryOutStream stream; 255 | size_t const s = 53124; 256 | stream << s; 257 | CHECK_EQUAL("53124", stream.GetText()); 258 | } 259 | 260 | TEST(ClearEmptiesMemoryOutStreamContents) 261 | { 262 | MemoryOutStream stream; 263 | stream << "Hello world"; 264 | stream.Clear(); 265 | CHECK_EQUAL("", stream.GetText()); 266 | } 267 | 268 | #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 269 | 270 | TEST(StreamInitialCapacityIsCorrect) 271 | { 272 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 273 | CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE, stream.GetCapacity()); 274 | } 275 | 276 | TEST(StreamInitialCapacityIsMultipleOfGrowChunkSize) 277 | { 278 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE + 1); 279 | CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); 280 | } 281 | 282 | 283 | TEST(ExceedingCapacityGrowsBuffer) 284 | { 285 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 286 | stream << "012345678901234567890123456789"; 287 | char const* const oldBuffer = stream.GetText(); 288 | stream << "0123456789"; 289 | CHECK(oldBuffer != stream.GetText()); 290 | } 291 | 292 | TEST(ExceedingCapacityGrowsBufferByGrowChunk) 293 | { 294 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 295 | stream << "0123456789012345678901234567890123456789"; 296 | CHECK_EQUAL(MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); 297 | } 298 | 299 | TEST(WritingStringLongerThanCapacityFitsInNewBuffer) 300 | { 301 | MemoryOutStream stream(8); 302 | stream << "0123456789ABCDEF"; 303 | CHECK_EQUAL("0123456789ABCDEF", stream.GetText()); 304 | } 305 | 306 | TEST(WritingIntLongerThanCapacityFitsInNewBuffer) 307 | { 308 | MemoryOutStream stream(8); 309 | stream << "aaaa" << 123456; 310 | CHECK_EQUAL("aaaa123456", stream.GetText()); 311 | } 312 | 313 | TEST(WritingFloatLongerThanCapacityFitsInNewBuffer) 314 | { 315 | MemoryOutStream stream(8); 316 | stream << "aaaa" << 123456.0f; 317 | CHECK_EQUAL("aaaa123456.000000", stream.GetText()); 318 | } 319 | 320 | TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer) 321 | { 322 | MemoryOutStream stream(8); 323 | stream << "aaaa" << size_t(32145); 324 | CHECK_EQUAL("aaaa32145", stream.GetText()); 325 | } 326 | 327 | TEST(VerifyLargeDoubleCanBeStreamedWithoutCrashing) 328 | { 329 | MemoryOutStream stream(8); 330 | stream << DBL_MAX; 331 | CHECK(true); 332 | } 333 | 334 | #endif 335 | 336 | } 337 | -------------------------------------------------------------------------------- /tests/TestTest.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TestReporter.h" 3 | #include "UnitTest++/TimeHelpers.h" 4 | #include "ScopedCurrentTest.h" 5 | 6 | using namespace UnitTest; 7 | 8 | namespace { 9 | 10 | TEST(PassingTestHasNoFailures) 11 | { 12 | class PassingTest : public Test 13 | { 14 | public: 15 | PassingTest() : Test("passing") {} 16 | virtual void RunImpl() const 17 | { 18 | CHECK(true); 19 | } 20 | }; 21 | 22 | TestResults results; 23 | { 24 | ScopedCurrentTest scopedResults(results); 25 | PassingTest().Run(); 26 | } 27 | 28 | CHECK_EQUAL(0, results.GetFailureCount()); 29 | } 30 | 31 | 32 | TEST(FailingTestHasFailures) 33 | { 34 | class FailingTest : public Test 35 | { 36 | public: 37 | FailingTest() : Test("failing") {} 38 | virtual void RunImpl() const 39 | { 40 | CHECK(false); 41 | } 42 | }; 43 | 44 | TestResults results; 45 | { 46 | ScopedCurrentTest scopedResults(results); 47 | FailingTest().Run(); 48 | } 49 | 50 | CHECK_EQUAL(1, results.GetFailureCount()); 51 | } 52 | 53 | #ifndef UNITTEST_NO_EXCEPTIONS 54 | TEST(ThrowingTestsAreReportedAsFailures) 55 | { 56 | class CrashingTest : public Test 57 | { 58 | public: 59 | CrashingTest() : Test("throwing") {} 60 | virtual void RunImpl() const 61 | { 62 | throw "Blah"; 63 | } 64 | }; 65 | 66 | TestResults results; 67 | { 68 | ScopedCurrentTest scopedResult(results); 69 | CrashingTest().Run(); 70 | } 71 | 72 | CHECK_EQUAL(1, results.GetFailureCount()); 73 | } 74 | 75 | #if !defined(UNITTEST_MINGW) && !defined(UNITTEST_WIN32) 76 | // Skip this test in debug because some debuggers don't like it. 77 | #if defined(NDEBUG) 78 | TEST(CrashingTestsAreReportedAsFailures) 79 | { 80 | class CrashingTest : public Test 81 | { 82 | public: 83 | CrashingTest() : Test("crashing") {} 84 | virtual void RunImpl() const 85 | { 86 | 87 | reinterpret_cast< void (*)() >(0)(); 88 | } 89 | }; 90 | 91 | TestResults results; 92 | { 93 | ScopedCurrentTest scopedResult(results); 94 | CrashingTest().Run(); 95 | } 96 | 97 | CHECK_EQUAL(1, results.GetFailureCount()); 98 | } 99 | #endif 100 | #endif 101 | #endif 102 | 103 | TEST(TestWithUnspecifiedSuiteGetsDefaultSuite) 104 | { 105 | Test test("test"); 106 | CHECK(test.m_details.suiteName != NULL); 107 | CHECK_EQUAL("DefaultSuite", test.m_details.suiteName); 108 | } 109 | 110 | TEST(TestReflectsSpecifiedSuiteName) 111 | { 112 | Test test("test", "testSuite"); 113 | CHECK(test.m_details.suiteName != NULL); 114 | CHECK_EQUAL("testSuite", test.m_details.suiteName); 115 | } 116 | 117 | void Fail() 118 | { 119 | CHECK(false); 120 | } 121 | 122 | TEST(OutOfCoreCHECKMacrosCanFailTests) 123 | { 124 | TestResults results; 125 | { 126 | ScopedCurrentTest scopedResult(results); 127 | Fail(); 128 | } 129 | 130 | CHECK_EQUAL(1, results.GetFailureCount()); 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /tests/TestTestList.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TestList.h" 3 | 4 | using namespace UnitTest; 5 | 6 | namespace { 7 | 8 | 9 | TEST(TestListIsEmptyByDefault) 10 | { 11 | TestList list; 12 | CHECK(list.GetHead() == 0); 13 | } 14 | 15 | TEST(AddingTestSetsHeadToTest) 16 | { 17 | Test test("test"); 18 | TestList list; 19 | list.Add(&test); 20 | 21 | CHECK(list.GetHead() == &test); 22 | CHECK(test.m_nextTest == 0); 23 | } 24 | 25 | TEST(AddingSecondTestAddsItToEndOfList) 26 | { 27 | Test test1("test1"); 28 | Test test2("test2"); 29 | 30 | TestList list; 31 | list.Add(&test1); 32 | list.Add(&test2); 33 | 34 | CHECK(list.GetHead() == &test1); 35 | CHECK(test1.m_nextTest == &test2); 36 | CHECK(test2.m_nextTest == 0); 37 | } 38 | 39 | TEST(ListAdderAddsTestToList) 40 | { 41 | TestList list; 42 | 43 | Test test(""); 44 | ListAdder adder(list, &test); 45 | 46 | CHECK(list.GetHead() == &test); 47 | CHECK(test.m_nextTest == 0); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /tests/TestTestMacros.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TestMacros.h" 3 | #include "UnitTest++/TestList.h" 4 | #include "UnitTest++/TestResults.h" 5 | #include "UnitTest++/TestReporter.h" 6 | #include "UnitTest++/ReportAssert.h" 7 | #include "RecordingReporter.h" 8 | #include "ScopedCurrentTest.h" 9 | 10 | using namespace UnitTest; 11 | using namespace std; 12 | 13 | /* test for c++11 support */ 14 | #ifndef _MSC_VER 15 | 16 | /* Test for clang >= 3.3 */ 17 | #ifdef __clang__ 18 | #if (__clang__ == 1) && (__clang_major__ > 3 || (__clang_major__ == 3 && (__clang_minor__ > 2 ))) 19 | #define _NOEXCEPT_OP(x) noexcept(x) 20 | #else 21 | #define _NOEXCEPT_OP(x) 22 | #endif 23 | #endif 24 | 25 | #ifndef __clang__ 26 | /* Test for GCC >= 4.8.0 */ 27 | #ifdef __GNUC__ 28 | #if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ > 7 )) 29 | #define _NOEXCEPT_OP(x) noexcept(x) 30 | #else 31 | #define _NOEXCEPT_OP(x) 32 | #endif 33 | #endif 34 | #endif 35 | #endif 36 | 37 | namespace { 38 | 39 | TestList list1; 40 | TEST_EX(DummyTest, list1) 41 | { 42 | } 43 | 44 | TEST (TestsAreAddedToTheListThroughMacro) 45 | { 46 | CHECK(list1.GetHead() != 0); 47 | CHECK(list1.GetHead()->m_nextTest == 0); 48 | } 49 | 50 | #ifndef UNITTEST_NO_EXCEPTIONS 51 | 52 | struct ThrowingThingie 53 | { 54 | ThrowingThingie() : dummy(false) 55 | { 56 | if (!dummy) 57 | throw "Oops"; 58 | } 59 | 60 | bool dummy; 61 | }; 62 | 63 | TestList list2; 64 | TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2) 65 | { 66 | } 67 | 68 | TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture) 69 | { 70 | RecordingReporter reporter; 71 | TestResults result(&reporter); 72 | { 73 | ScopedCurrentTest scopedResults(result); 74 | list2.GetHead()->Run(); 75 | } 76 | 77 | CHECK(strstr(reporter.lastFailedMessage, "xception")); 78 | CHECK(strstr(reporter.lastFailedMessage, "fixture")); 79 | CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie")); 80 | } 81 | 82 | #endif 83 | 84 | struct DummyFixture 85 | { 86 | int x; 87 | }; 88 | 89 | // We're really testing the macros so we just want them to compile and link 90 | SUITE(TestSuite1) 91 | { 92 | TEST(SimilarlyNamedTestsInDifferentSuitesWork) 93 | { 94 | } 95 | 96 | TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork) 97 | { 98 | } 99 | } 100 | 101 | SUITE(TestSuite2) 102 | { 103 | TEST(SimilarlyNamedTestsInDifferentSuitesWork) 104 | { 105 | } 106 | 107 | TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork) 108 | { 109 | } 110 | } 111 | 112 | TestList macroTestList1; 113 | TEST_EX(MacroTestHelper1, macroTestList1) 114 | { 115 | } 116 | 117 | TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite) 118 | { 119 | CHECK(macroTestList1.GetHead() != NULL); 120 | CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName); 121 | CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName); 122 | } 123 | 124 | TestList macroTestList2; 125 | TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2) 126 | { 127 | } 128 | 129 | TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite) 130 | { 131 | CHECK(macroTestList2.GetHead() != NULL); 132 | CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName); 133 | CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName); 134 | } 135 | 136 | #ifndef UNITTEST_NO_EXCEPTIONS 137 | 138 | struct FixtureCtorThrows 139 | { 140 | FixtureCtorThrows() { throw "exception"; } 141 | }; 142 | 143 | TestList throwingFixtureTestList1; 144 | TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1) 145 | { 146 | } 147 | 148 | TEST(FixturesWithThrowingCtorsAreFailures) 149 | { 150 | CHECK(throwingFixtureTestList1.GetHead() != NULL); 151 | RecordingReporter reporter; 152 | TestResults result(&reporter); 153 | { 154 | ScopedCurrentTest scopedResult(result); 155 | throwingFixtureTestList1.GetHead()->Run(); 156 | } 157 | 158 | int const failureCount = result.GetFailedTestCount(); 159 | CHECK_EQUAL(1, failureCount); 160 | CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture")); 161 | } 162 | 163 | // Visual Studio 2015 in compliance with C++11 standard 164 | // implicitly adds a 'noexcept' to all user defined 165 | // destructors. Any exceptions thrown from destructors 166 | // cause abort() to be called on the process. 167 | #if(_MSC_VER < 1900) 168 | struct FixtureDtorThrows 169 | { 170 | ~FixtureDtorThrows() _NOEXCEPT_OP(false) { throw "exception"; } 171 | }; 172 | 173 | TestList throwingFixtureTestList2; 174 | TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2) 175 | { 176 | } 177 | 178 | TEST(FixturesWithThrowingDtorsAreFailures) 179 | { 180 | CHECK(throwingFixtureTestList2.GetHead() != NULL); 181 | 182 | RecordingReporter reporter; 183 | TestResults result(&reporter); 184 | { 185 | ScopedCurrentTest scopedResult(result); 186 | throwingFixtureTestList2.GetHead()->Run(); 187 | } 188 | 189 | int const failureCount = result.GetFailedTestCount(); 190 | CHECK_EQUAL(1, failureCount); 191 | CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture")); 192 | } 193 | #endif 194 | 195 | const int FailingLine = 123; 196 | 197 | struct FixtureCtorAsserts 198 | { 199 | FixtureCtorAsserts() 200 | { 201 | UnitTest::ReportAssert("assert failure", "file", FailingLine); 202 | } 203 | }; 204 | 205 | TestList ctorAssertFixtureTestList; 206 | TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList) 207 | { 208 | } 209 | 210 | TEST(CorrectlyReportsFixturesWithCtorsThatAssert) 211 | { 212 | RecordingReporter reporter; 213 | TestResults result(&reporter); 214 | { 215 | ScopedCurrentTest scopedResults(result); 216 | ctorAssertFixtureTestList.GetHead()->Run(); 217 | } 218 | 219 | const int failureCount = result.GetFailedTestCount(); 220 | CHECK_EQUAL(1, failureCount); 221 | CHECK_EQUAL(FailingLine, reporter.lastFailedLine); 222 | CHECK(strstr(reporter.lastFailedMessage, "assert failure")); 223 | } 224 | 225 | #endif 226 | 227 | } 228 | 229 | // We're really testing if it's possible to use the same suite in two files 230 | // to compile and link successfuly (TestTestSuite.cpp has suite with the same name) 231 | // Note: we are outside of the anonymous namespace 232 | SUITE(SameTestSuite) 233 | { 234 | TEST(DummyTest1) 235 | { 236 | } 237 | } 238 | 239 | #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo 240 | #define INNER_STRINGIFY(X) #X 241 | #define STRINGIFY(X) INNER_STRINGIFY(X) 242 | 243 | TEST(CUR_TEST_NAME) 244 | { 245 | const UnitTest::TestDetails* details = CurrentTest::Details(); 246 | CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName); 247 | } 248 | 249 | #undef CUR_TEST_NAME 250 | #undef INNER_STRINGIFY 251 | #undef STRINGIFY 252 | -------------------------------------------------------------------------------- /tests/TestTestResults.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TestResults.h" 3 | #include "RecordingReporter.h" 4 | 5 | using namespace UnitTest; 6 | 7 | namespace { 8 | 9 | TestDetails const details("testname", "suitename", "filename", 123); 10 | 11 | 12 | TEST(StartsWithNoTestsRun) 13 | { 14 | TestResults results; 15 | CHECK_EQUAL (0, results.GetTotalTestCount()); 16 | } 17 | 18 | TEST(RecordsNumbersOfTests) 19 | { 20 | TestResults results; 21 | results.OnTestStart(details); 22 | results.OnTestStart(details); 23 | results.OnTestStart(details); 24 | CHECK_EQUAL(3, results.GetTotalTestCount()); 25 | } 26 | 27 | TEST(StartsWithNoTestsFailing) 28 | { 29 | TestResults results; 30 | CHECK_EQUAL (0, results.GetFailureCount()); 31 | } 32 | 33 | TEST(RecordsNumberOfFailures) 34 | { 35 | TestResults results; 36 | results.OnTestFailure(details, ""); 37 | results.OnTestFailure(details, ""); 38 | CHECK_EQUAL(2, results.GetFailureCount()); 39 | } 40 | 41 | TEST(RecordsNumberOfFailedTests) 42 | { 43 | TestResults results; 44 | 45 | results.OnTestStart(details); 46 | results.OnTestFailure(details, ""); 47 | results.OnTestFinish(details, 0); 48 | 49 | results.OnTestStart(details); 50 | results.OnTestFailure(details, ""); 51 | results.OnTestFailure(details, ""); 52 | results.OnTestFailure(details, ""); 53 | results.OnTestFinish(details, 0); 54 | 55 | CHECK_EQUAL (2, results.GetFailedTestCount()); 56 | } 57 | 58 | TEST(NotifiesReporterOfTestStartWithCorrectInfo) 59 | { 60 | RecordingReporter reporter; 61 | TestResults results(&reporter); 62 | results.OnTestStart(details); 63 | 64 | CHECK_EQUAL (1, reporter.testRunCount); 65 | CHECK_EQUAL ("suitename", reporter.lastStartedSuite); 66 | CHECK_EQUAL ("testname", reporter.lastStartedTest); 67 | } 68 | 69 | TEST(NotifiesReporterOfTestFailureWithCorrectInfo) 70 | { 71 | RecordingReporter reporter; 72 | TestResults results(&reporter); 73 | 74 | results.OnTestFailure(details, "failurestring"); 75 | CHECK_EQUAL (1, reporter.testFailedCount); 76 | CHECK_EQUAL ("filename", reporter.lastFailedFile); 77 | CHECK_EQUAL (123, reporter.lastFailedLine); 78 | CHECK_EQUAL ("suitename", reporter.lastFailedSuite); 79 | CHECK_EQUAL ("testname", reporter.lastFailedTest); 80 | CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); 81 | } 82 | 83 | TEST(NotifiesReporterOfCheckFailureWithCorrectInfo) 84 | { 85 | RecordingReporter reporter; 86 | TestResults results(&reporter); 87 | 88 | results.OnTestFailure(details, "failurestring"); 89 | CHECK_EQUAL (1, reporter.testFailedCount); 90 | 91 | CHECK_EQUAL ("filename", reporter.lastFailedFile); 92 | CHECK_EQUAL (123, reporter.lastFailedLine); 93 | CHECK_EQUAL ("testname", reporter.lastFailedTest); 94 | CHECK_EQUAL ("suitename", reporter.lastFailedSuite); 95 | CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); 96 | } 97 | 98 | TEST(NotifiesReporterOfTestEnd) 99 | { 100 | RecordingReporter reporter; 101 | TestResults results(&reporter); 102 | 103 | results.OnTestFinish(details, 0.1234f); 104 | CHECK_EQUAL (1, reporter.testFinishedCount); 105 | CHECK_EQUAL ("testname", reporter.lastFinishedTest); 106 | CHECK_EQUAL ("suitename", reporter.lastFinishedSuite); 107 | CHECK_CLOSE (0.1234f, reporter.lastFinishedTestTime, 0.0001f); 108 | } 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /tests/TestTestRunner.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "RecordingReporter.h" 3 | #include "UnitTest++/ReportAssert.h" 4 | #include "UnitTest++/TestList.h" 5 | #include "UnitTest++/TimeHelpers.h" 6 | #include "UnitTest++/TimeConstraint.h" 7 | #include "UnitTest++/ReportAssertImpl.h" 8 | 9 | using namespace UnitTest; 10 | 11 | namespace 12 | { 13 | 14 | struct MockTest : public Test 15 | { 16 | MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1) 17 | : Test(testName) 18 | , success(success_) 19 | , asserted(assert_) 20 | , count(count_) 21 | { 22 | } 23 | 24 | virtual void RunImpl() const 25 | { 26 | TestResults& testResults_ = *CurrentTest::Results(); 27 | 28 | for (int i=0; i < count; ++i) 29 | { 30 | if (asserted) 31 | { 32 | ReportAssert("desc", "file", 0); 33 | } 34 | else if (!success) 35 | { 36 | testResults_.OnTestFailure(m_details, "message"); 37 | } 38 | } 39 | } 40 | 41 | bool const success; 42 | bool const asserted; 43 | int const count; 44 | }; 45 | 46 | struct FixtureBase 47 | { 48 | FixtureBase() 49 | : runner(reporter) 50 | { 51 | } 52 | 53 | template 54 | int RunTestsIf(TestList const& list, char const* suiteName, 55 | const Predicate& predicate, int maxTestTimeInMs) 56 | { 57 | TestResults* oldResults = CurrentTest::Results(); 58 | const TestDetails* oldDetails = CurrentTest::Details(); 59 | int result = runner.RunTestsIf(list, suiteName, predicate, maxTestTimeInMs); 60 | CurrentTest::Results() = oldResults; 61 | CurrentTest::Details() = oldDetails; 62 | return result; 63 | } 64 | 65 | TestRunner runner; 66 | RecordingReporter reporter; 67 | }; 68 | 69 | struct TestRunnerFixture : public FixtureBase 70 | { 71 | TestList list; 72 | }; 73 | 74 | TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly) 75 | { 76 | MockTest test("goodtest", true, false); 77 | list.Add(&test); 78 | 79 | RunTestsIf(list, NULL, True(), 0); 80 | CHECK_EQUAL(1, reporter.testRunCount); 81 | CHECK_EQUAL("goodtest", reporter.lastStartedTest); 82 | } 83 | 84 | TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly) 85 | { 86 | MockTest test("goodtest", true, false); 87 | list.Add(&test); 88 | 89 | RunTestsIf(list, NULL, True(), 0); 90 | CHECK_EQUAL(1, reporter.testFinishedCount); 91 | CHECK_EQUAL("goodtest", reporter.lastFinishedTest); 92 | } 93 | 94 | class SlowTest : public Test 95 | { 96 | public: 97 | SlowTest() 98 | : Test("slow", "somesuite", "filename", 123) 99 | { 100 | } 101 | 102 | virtual void RunImpl() const 103 | { 104 | TimeHelpers::SleepMs(20); 105 | } 106 | }; 107 | 108 | TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime) 109 | { 110 | SlowTest test; 111 | list.Add(&test); 112 | 113 | RunTestsIf(list, NULL, True(), 0); 114 | CHECK(reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= 0.050f); 115 | } 116 | 117 | TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun) 118 | { 119 | CHECK_EQUAL(0, RunTestsIf(list, NULL, True(), 0)); 120 | CHECK_EQUAL(0, reporter.testRunCount); 121 | CHECK_EQUAL(0, reporter.testFailedCount); 122 | } 123 | 124 | TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest) 125 | { 126 | MockTest test1("test", false, false); 127 | list.Add(&test1); 128 | MockTest test2("test", true, false); 129 | list.Add(&test2); 130 | MockTest test3("test", false, false); 131 | list.Add(&test3); 132 | 133 | CHECK_EQUAL(2, RunTestsIf(list, NULL, True(), 0)); 134 | CHECK_EQUAL(2, reporter.testFailedCount); 135 | } 136 | 137 | TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing) 138 | { 139 | MockTest test("test", true, true); 140 | list.Add(&test); 141 | 142 | RunTestsIf(list, NULL, True(), 0); 143 | CHECK_EQUAL(1, reporter.testFailedCount); 144 | } 145 | 146 | 147 | TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount) 148 | { 149 | MockTest test1("test", true, false); 150 | MockTest test2("test", true, false); 151 | MockTest test3("test", true, false); 152 | list.Add(&test1); 153 | list.Add(&test2); 154 | list.Add(&test3); 155 | 156 | RunTestsIf(list, NULL, True(), 0); 157 | CHECK_EQUAL(3, reporter.summaryTotalTestCount); 158 | } 159 | 160 | TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests) 161 | { 162 | MockTest test1("test", false, false, 2); 163 | MockTest test2("test", true, false); 164 | MockTest test3("test", false, false, 3); 165 | list.Add(&test1); 166 | list.Add(&test2); 167 | list.Add(&test3); 168 | 169 | RunTestsIf(list, NULL, True(), 0); 170 | CHECK_EQUAL(2, reporter.summaryFailedTestCount); 171 | } 172 | 173 | TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures) 174 | { 175 | MockTest test1("test", false, false, 2); 176 | MockTest test2("test", true, false); 177 | MockTest test3("test", false, false, 3); 178 | list.Add(&test1); 179 | list.Add(&test2); 180 | list.Add(&test3); 181 | 182 | RunTestsIf(list, NULL, True(), 0); 183 | CHECK_EQUAL(5, reporter.summaryFailureCount); 184 | } 185 | 186 | TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold) 187 | { 188 | SlowTest test; 189 | list.Add(&test); 190 | 191 | RunTestsIf(list, NULL, True(), 0); 192 | CHECK_EQUAL(0, reporter.testFailedCount); 193 | } 194 | 195 | TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold) 196 | { 197 | SlowTest test; 198 | list.Add(&test); 199 | 200 | RunTestsIf(list, NULL, True(), 3); 201 | CHECK_EQUAL(1, reporter.testFailedCount); 202 | } 203 | 204 | TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation) 205 | { 206 | SlowTest test; 207 | list.Add(&test); 208 | 209 | RunTestsIf(list, NULL, True(), 3); 210 | 211 | using namespace std; 212 | 213 | CHECK_EQUAL(test.m_details.testName, reporter.lastFailedTest); 214 | CHECK(strstr(test.m_details.filename, reporter.lastFailedFile)); 215 | CHECK_EQUAL(test.m_details.lineNumber, reporter.lastFailedLine); 216 | 217 | CHECK(strstr(reporter.lastFailedMessage, "Global time constraint failed")); 218 | CHECK(strstr(reporter.lastFailedMessage, "3ms")); 219 | } 220 | 221 | TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses) 222 | { 223 | class SlowExemptedTest : public Test 224 | { 225 | public: 226 | SlowExemptedTest() : Test("slowexempted", "", 0) {} 227 | virtual void RunImpl() const 228 | { 229 | UNITTEST_TIME_CONSTRAINT_EXEMPT(); 230 | TimeHelpers::SleepMs(20); 231 | } 232 | }; 233 | 234 | SlowExemptedTest test; 235 | list.Add(&test); 236 | 237 | RunTestsIf(list, NULL, True(), 3); 238 | CHECK_EQUAL(0, reporter.testFailedCount); 239 | } 240 | 241 | struct TestSuiteFixture : FixtureBase 242 | { 243 | TestSuiteFixture() 244 | : test1("TestInDefaultSuite") 245 | , test2("TestInOtherSuite", "OtherSuite") 246 | , test3("SecondTestInDefaultSuite") 247 | { 248 | list.Add(&test1); 249 | list.Add(&test2); 250 | } 251 | 252 | Test test1; 253 | Test test2; 254 | Test test3; 255 | TestList list; 256 | }; 257 | 258 | TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed) 259 | { 260 | RunTestsIf(list, NULL, True(), 0); 261 | CHECK_EQUAL(2, reporter.summaryTotalTestCount); 262 | } 263 | 264 | TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite) 265 | { 266 | RunTestsIf(list, "OtherSuite", True(), 0); 267 | CHECK_EQUAL(1, reporter.summaryTotalTestCount); 268 | CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest); 269 | } 270 | 271 | struct RunTestIfNameIs 272 | { 273 | RunTestIfNameIs(char const* name_) 274 | : name(name_) 275 | { 276 | } 277 | 278 | bool operator()(const Test* const test) const 279 | { 280 | using namespace std; 281 | return (0 == strcmp(test->m_details.testName, name)); 282 | } 283 | 284 | char const* name; 285 | }; 286 | 287 | TEST(TestMockPredicateBehavesCorrectly) 288 | { 289 | RunTestIfNameIs predicate("pass"); 290 | 291 | Test pass("pass"); 292 | Test fail("fail"); 293 | 294 | CHECK(predicate(&pass)); 295 | CHECK(!predicate(&fail)); 296 | } 297 | 298 | TEST_FIXTURE(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate) 299 | { 300 | Test should_run("goodtest"); 301 | list.Add(&should_run); 302 | 303 | Test should_not_run("badtest"); 304 | list.Add(&should_not_run); 305 | 306 | RunTestsIf(list, NULL, RunTestIfNameIs("goodtest"), 0); 307 | CHECK_EQUAL(1, reporter.testRunCount); 308 | CHECK_EQUAL("goodtest", reporter.lastStartedTest); 309 | } 310 | 311 | TEST_FIXTURE(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate) 312 | { 313 | Test runningTest1("goodtest", "suite"); 314 | Test skippedTest2("goodtest"); 315 | Test skippedTest3("badtest", "suite"); 316 | Test skippedTest4("badtest"); 317 | 318 | list.Add(&runningTest1); 319 | list.Add(&skippedTest2); 320 | list.Add(&skippedTest3); 321 | list.Add(&skippedTest4); 322 | 323 | RunTestsIf(list, "suite", RunTestIfNameIs("goodtest"), 0); 324 | 325 | CHECK_EQUAL(1, reporter.testRunCount); 326 | CHECK_EQUAL("goodtest", reporter.lastStartedTest); 327 | CHECK_EQUAL("suite", reporter.lastStartedSuite); 328 | } 329 | 330 | } -------------------------------------------------------------------------------- /tests/TestTestSuite.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | 3 | // We're really testing if it's possible to use the same suite in two files 4 | // to compile and link successfuly (TestTestSuite.cpp has suite with the same name) 5 | // Note: we are outside of the anonymous namespace 6 | SUITE(SameTestSuite) 7 | { 8 | TEST(DummyTest2) 9 | { 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /tests/TestTimeConstraint.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TestResults.h" 3 | #include "UnitTest++/TimeHelpers.h" 4 | #include "RecordingReporter.h" 5 | #include "ScopedCurrentTest.h" 6 | 7 | using namespace UnitTest; 8 | 9 | namespace 10 | { 11 | 12 | TEST(TimeConstraintSucceedsWithFastTest) 13 | { 14 | TestResults result; 15 | { 16 | ScopedCurrentTest scopedResult(result); 17 | TimeConstraint t(200, TestDetails("", "", "", 0), 0); 18 | TimeHelpers::SleepMs(5); 19 | } 20 | CHECK_EQUAL(0, result.GetFailureCount()); 21 | } 22 | 23 | TEST(TimeConstraintFailsWithSlowTest) 24 | { 25 | TestResults result; 26 | { 27 | ScopedCurrentTest scopedResult(result); 28 | TimeConstraint t(10, TestDetails("", "", "", 0),0); 29 | TimeHelpers::SleepMs(20); 30 | } 31 | CHECK_EQUAL(1, result.GetFailureCount()); 32 | } 33 | 34 | TEST(TimeConstraintFailureIncludesCorrectData) 35 | { 36 | RecordingReporter reporter; 37 | TestResults result(&reporter); 38 | { 39 | ScopedCurrentTest scopedResult(result); 40 | 41 | TestDetails const details("testname", "suitename", "filename", 10); 42 | TimeConstraint t(10, details,10); 43 | TimeHelpers::SleepMs(20); 44 | } 45 | 46 | using namespace std; 47 | 48 | CHECK(strstr(reporter.lastFailedFile, "filename")); 49 | CHECK_EQUAL(10, reporter.lastFailedLine); 50 | CHECK(strstr(reporter.lastFailedTest, "testname")); 51 | } 52 | 53 | TEST(TimeConstraintFailureIncludesTimeoutInformation) 54 | { 55 | RecordingReporter reporter; 56 | TestResults result(&reporter); 57 | { 58 | ScopedCurrentTest scopedResult(result); 59 | TimeConstraint t(10, TestDetails("", "", "", 0),0); 60 | TimeHelpers::SleepMs(20); 61 | } 62 | 63 | using namespace std; 64 | 65 | CHECK(strstr(reporter.lastFailedMessage, "ime constraint")); 66 | CHECK(strstr(reporter.lastFailedMessage, "under 10ms")); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /tests/TestTimeConstraintMacro.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "UnitTest++/TimeHelpers.h" 3 | 4 | #include "RecordingReporter.h" 5 | #include "ScopedCurrentTest.h" 6 | 7 | namespace { 8 | 9 | TEST(TimeConstraintMacroQualifiesNamespace) 10 | { 11 | // If this compiles without a "using namespace UnitTest;", all is well. 12 | UNITTEST_TIME_CONSTRAINT(1); 13 | } 14 | 15 | TEST(TimeConstraintMacroUsesCorrectInfo) 16 | { 17 | int testLine = 0; 18 | RecordingReporter reporter; 19 | 20 | { 21 | UnitTest::TestResults testResults(&reporter); 22 | ScopedCurrentTest scopedResults(testResults); 23 | 24 | UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; 25 | UnitTest::TimeHelpers::SleepMs(20); 26 | } 27 | 28 | using namespace std; 29 | 30 | CHECK_EQUAL(1, reporter.testFailedCount); 31 | CHECK(strstr(reporter.lastFailedFile, __FILE__)); 32 | CHECK_EQUAL(testLine, reporter.lastFailedLine); 33 | CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroUsesCorrectInfo")); 34 | } 35 | 36 | TEST(TimeConstraintMacroComparesAgainstPreciseActual) 37 | { 38 | int testLine = 0; 39 | RecordingReporter reporter; 40 | 41 | { 42 | UnitTest::TestResults testResults(&reporter); 43 | ScopedCurrentTest scopedResults(testResults); 44 | 45 | UNITTEST_TIME_CONSTRAINT(1); testLine = __LINE__; 46 | 47 | // start a new timer and run until we're as little over the 1 msec 48 | // threshold as we can achieve; this should guarantee that the "test" 49 | // runs in some very small amount of time > 1 msec 50 | UnitTest::Timer myTimer; 51 | myTimer.Start(); 52 | 53 | while (myTimer.GetTimeInMs() < 1.001) 54 | UnitTest::TimeHelpers::SleepMs(0); 55 | } 56 | 57 | using namespace std; 58 | 59 | CHECK_EQUAL(1, reporter.testFailedCount); 60 | CHECK(strstr(reporter.lastFailedFile, __FILE__)); 61 | CHECK_EQUAL(testLine, reporter.lastFailedLine); 62 | CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual")); 63 | } 64 | 65 | struct EmptyFixture {}; 66 | 67 | TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures) 68 | { 69 | int testLine = 0; 70 | RecordingReporter reporter; 71 | 72 | { 73 | UnitTest::TestResults testResults(&reporter); 74 | ScopedCurrentTest scopedResults(testResults); 75 | 76 | UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; 77 | UnitTest::TimeHelpers::SleepMs(20); 78 | } 79 | 80 | using namespace std; 81 | 82 | CHECK_EQUAL(1, reporter.testFailedCount); 83 | CHECK(strstr(reporter.lastFailedFile, __FILE__)); 84 | CHECK_EQUAL(testLine, reporter.lastFailedLine); 85 | CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures")); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /tests/TestUnitTestPP.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/UnitTestPP.h" 2 | #include "ScopedCurrentTest.h" 3 | 4 | // These are sample tests that show the different features of the framework 5 | 6 | namespace { 7 | 8 | TEST(ValidCheckSucceeds) 9 | { 10 | bool const b = true; 11 | CHECK(b); 12 | } 13 | 14 | TEST(CheckWorksWithPointers) 15 | { 16 | void* p = (void *)0x100; 17 | CHECK(p); 18 | CHECK(p != 0); 19 | } 20 | 21 | TEST(ValidCheckEqualSucceeds) 22 | { 23 | int const x = 3; 24 | int const y = 3; 25 | CHECK_EQUAL(x, y); 26 | } 27 | 28 | TEST(CheckEqualWorksWithPointers) 29 | { 30 | void* p = (void *)0; 31 | CHECK_EQUAL((void*)0, p); 32 | } 33 | 34 | TEST(ValidCheckCloseSucceeds) 35 | { 36 | CHECK_CLOSE(2.0f, 2.001f, 0.01f); 37 | CHECK_CLOSE(2.001f, 2.0f, 0.01f); 38 | } 39 | 40 | TEST(ArrayCloseSucceeds) 41 | { 42 | float const a1[] = {1, 2, 3}; 43 | float const a2[] = {1, 2.01f, 3}; 44 | CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f); 45 | } 46 | 47 | #ifndef UNITTEST_NO_EXCEPTIONS 48 | 49 | TEST(CheckThrowMacroSucceedsOnCorrectException) 50 | { 51 | struct TestException {}; 52 | CHECK_THROW(throw TestException(), TestException); 53 | } 54 | 55 | TEST(CheckAssertSucceeds) 56 | { 57 | CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0)); 58 | } 59 | 60 | TEST(CheckThrowMacroFailsOnMissingException) 61 | { 62 | class NoThrowTest : public UnitTest::Test 63 | { 64 | public: 65 | NoThrowTest() : Test("nothrow") {} 66 | void DontThrow() const 67 | { 68 | } 69 | 70 | virtual void RunImpl() const 71 | { 72 | CHECK_THROW(DontThrow(), int); 73 | } 74 | }; 75 | 76 | UnitTest::TestResults results; 77 | { 78 | ScopedCurrentTest scopedResults(results); 79 | 80 | NoThrowTest test; 81 | test.Run(); 82 | } 83 | 84 | CHECK_EQUAL(1, results.GetFailureCount()); 85 | } 86 | 87 | TEST(CheckThrowMacroFailsOnWrongException) 88 | { 89 | class WrongThrowTest : public UnitTest::Test 90 | { 91 | public: 92 | WrongThrowTest() : Test("wrongthrow") {} 93 | virtual void RunImpl() const 94 | { 95 | CHECK_THROW(throw "oops", int); 96 | } 97 | }; 98 | 99 | UnitTest::TestResults results; 100 | { 101 | ScopedCurrentTest scopedResults(results); 102 | 103 | WrongThrowTest test; 104 | test.Run(); 105 | } 106 | 107 | CHECK_EQUAL(1, results.GetFailureCount()); 108 | } 109 | 110 | #endif 111 | 112 | struct SimpleFixture 113 | { 114 | SimpleFixture() 115 | { 116 | ++instanceCount; 117 | } 118 | ~SimpleFixture() 119 | { 120 | --instanceCount; 121 | } 122 | 123 | static int instanceCount; 124 | }; 125 | 126 | int SimpleFixture::instanceCount = 0; 127 | 128 | TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled) 129 | { 130 | CHECK(SimpleFixture::instanceCount > 0); 131 | } 132 | 133 | TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime) 134 | { 135 | CHECK_EQUAL(1, SimpleFixture::instanceCount); 136 | } 137 | 138 | void CheckBool(const bool b) 139 | { 140 | CHECK(b); 141 | } 142 | 143 | TEST(CanCallCHECKOutsideOfTestFunction) 144 | { 145 | CheckBool(true); 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /tests/TestXmlTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "UnitTest++/Config.h" 2 | #ifndef UNITTEST_NO_DEFERRED_REPORTER 3 | 4 | #include "UnitTest++/UnitTestPP.h" 5 | #include "UnitTest++/XmlTestReporter.h" 6 | 7 | #include 8 | 9 | using namespace UnitTest; 10 | using std::ostringstream; 11 | 12 | namespace 13 | { 14 | 15 | #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM 16 | 17 | // Overload to let MemoryOutStream accept std::string 18 | MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value) 19 | { 20 | s << value.c_str(); 21 | return s; 22 | } 23 | 24 | #endif 25 | 26 | struct XmlTestReporterFixture 27 | { 28 | XmlTestReporterFixture() 29 | : reporter(output) 30 | { 31 | } 32 | 33 | ostringstream output; 34 | XmlTestReporter reporter; 35 | }; 36 | 37 | TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped) 38 | { 39 | TestDetails const details("TestName", "suite", "filename.h", 4321); 40 | 41 | reporter.ReportTestStart(details); 42 | reporter.ReportFailure(details, "\"\"\'\'&&<<>>"); 43 | reporter.ReportTestFinish(details, 0.1f); 44 | reporter.ReportSummary(1, 2, 3, 0.1f); 45 | 46 | char const* expected = 47 | "" 48 | "" 49 | "" 50 | "" 52 | "" 53 | ""; 54 | 55 | CHECK_EQUAL(expected, output.str().c_str()); 56 | } 57 | 58 | TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled) 59 | { 60 | TestDetails const details("", "", "", 0); 61 | 62 | reporter.ReportTestStart(details); 63 | reporter.ReportFailure(details, "message"); 64 | reporter.ReportTestFinish(details, 1.0F); 65 | CHECK(output.str().empty()); 66 | 67 | reporter.ReportSummary(1, 1, 1, 1.0f); 68 | CHECK(!output.str().empty()); 69 | } 70 | 71 | TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat) 72 | { 73 | reporter.ReportSummary(0, 0, 0, 0.1f); 74 | 75 | const char *expected = 76 | "" 77 | "" 78 | ""; 79 | 80 | CHECK_EQUAL(expected, output.str().c_str()); 81 | } 82 | 83 | TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat) 84 | { 85 | TestDetails const details("TestName", "DefaultSuite", "", 0); 86 | 87 | reporter.ReportTestStart(details); 88 | reporter.ReportSummary(1, 0, 0, 0.1f); 89 | 90 | const char *expected = 91 | "" 92 | "" 93 | "" 94 | ""; 95 | 96 | CHECK_EQUAL(expected, output.str().c_str()); 97 | } 98 | 99 | TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat) 100 | { 101 | TestDetails const details("A Test", "suite", "A File", 4321); 102 | 103 | reporter.ReportTestStart(details); 104 | reporter.ReportFailure(details, "A Failure"); 105 | reporter.ReportSummary(1, 1, 1, 0.1f); 106 | 107 | const char *expected = 108 | "" 109 | "" 110 | "" 111 | "" 112 | "" 113 | ""; 114 | 115 | CHECK_EQUAL(expected, output.str().c_str()); 116 | } 117 | 118 | TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped) 119 | { 120 | TestDetails const details("TestName", "suite", "filename.h", 4321); 121 | 122 | reporter.ReportTestStart(details); 123 | reporter.ReportFailure(details, "\"\'&<>"); 124 | reporter.ReportTestFinish(details, 0.1f); 125 | reporter.ReportSummary(1, 1, 1, 0.1f); 126 | 127 | char const* expected = 128 | "" 129 | "" 130 | "" 131 | "" 132 | "" 133 | ""; 134 | 135 | CHECK_EQUAL(expected, output.str().c_str()); 136 | } 137 | 138 | TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess) 139 | { 140 | TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1); 141 | reporter.ReportTestStart(failedDetails); 142 | reporter.ReportFailure(failedDetails, "expected 1 but was 2"); 143 | reporter.ReportTestFinish(failedDetails, 0.1f); 144 | 145 | TestDetails const succeededDetails("SucceededTest", "suite", "", 0); 146 | reporter.ReportTestStart(succeededDetails); 147 | reporter.ReportTestFinish(succeededDetails, 1.0f); 148 | reporter.ReportSummary(2, 1, 1, 1.1f); 149 | 150 | char const* expected = 151 | "" 152 | "" 153 | "" 154 | "" 155 | "" 156 | "" 157 | ""; 158 | 159 | CHECK_EQUAL(expected, output.str().c_str()); 160 | } 161 | 162 | TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures) 163 | { 164 | TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1); 165 | TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31); 166 | 167 | reporter.ReportTestStart(failedDetails1); 168 | reporter.ReportFailure(failedDetails1, "expected 1 but was 2"); 169 | reporter.ReportFailure(failedDetails2, "expected one but was two"); 170 | reporter.ReportTestFinish(failedDetails1, 0.1f); 171 | 172 | reporter.ReportSummary(1, 1, 2, 1.1f); 173 | 174 | char const* expected = 175 | "" 176 | "" 177 | "" 178 | "" 179 | "" 180 | "" 181 | ""; 182 | 183 | CHECK_EQUAL(expected, output.str().c_str()); 184 | } 185 | 186 | } 187 | 188 | #endif 189 | --------------------------------------------------------------------------------