Skip to content

Commit

Permalink
Optional Boost.Test unit tests (default off).
Browse files Browse the repository at this point in the history
These already paid off, the previously fixed sscanf issues were found this way.

Also includes natvis file for VS Debugger pretty printing
  • Loading branch information
mrwonko committed Nov 5, 2015
1 parent c9097f0 commit b087a45
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ option(BuildJK2SPEngine "Whether to create projects for the jk2 SP engine (openj
option(BuildJK2SPGame "Whether to create projects for the jk2 sp gamecode mod (jk2gamex86.dll)" OFF)
option(BuildJK2SPRdVanilla "Whether to create projects for the jk2 sp renderer (rdjosp-vanilla_x86.dll)" OFF)

option(BuildTests "Whether to build automatic unit tests (requires Boost)" OFF)

# Configure the use of bundled libraries. By default, we assume the user is on
# a platform that does not require any bundling.
#
Expand Down Expand Up @@ -309,3 +311,6 @@ add_subdirectory(${MPDir})
if(WIN32 AND MSVC)
add_subdirectory("tools/WinSymbol")
endif()
if(BuildTests)
add_subdirectory("tests")
endif()
13 changes: 13 additions & 0 deletions shared/openjk.natvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This file tells the Visual Studio Debugger how to display types. -->
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="gsl::array_view&lt;*&gt;">
<DisplayString>{{ {begin_,[end_ - begin_]} }}</DisplayString>
<Expand>
<ArrayItems>
<Size>end_ - begin_</Size>
<ValuePointer>begin_</ValuePointer>
</ArrayItems>
</Expand>
</Type>
</AutoVisualizer>
57 changes: 57 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#============================================================================
# Copyright (C) 2013 - 2015, OpenJK contributors
#
# This file is part of the OpenJK source code.
#
# OpenJK is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#============================================================================

# Make sure the user is not executing this script directly
if(NOT InOpenJK)
message(FATAL_ERROR "Use the top-level cmake script!")
endif(NOT InOpenJK)

set(TestFiles
"main.cpp"
"safe/string.cpp"
"${SharedDir}/qcommon/safe/string.cpp"
)
if(MSVC)
set(TestFiles
${TestFiles}
"${SharedDir}/openjk.natvis"
)
endif()
source_group( "tests" REGULAR_EXPRESSION ".*")
source_group( "tests\\safe" REGULAR_EXPRESSION "safe/.*" )
source_group( "qcommon\\safe" REGULAR_EXPRESSION "${SharedDir}/qcommon/safe/.*" )

set( Boost_USE_STATIC_LIBS ON )
find_package( Boost COMPONENTS unit_test_framework REQUIRED )

set(TestTarget "UnitTests")
set(TestLibraries "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}")
set(TestIncludeDirectories
"${Boost_INCLUDE_DIRS}"
"${SharedDir}"
"${GSLIncludeDirectory}"
)
set(TestDefines "${SharedDefines}")


add_executable(${TestTarget} ${TestFiles})
set_target_properties(${TestTarget} PROPERTIES COMPILE_DEFINITIONS "${TestDefines}")
set_target_properties(${TestTarget} PROPERTIES INCLUDE_DIRECTORIES "${TestIncludeDirectories}")
set_target_properties(${TestTarget} PROPERTIES PROJECT_LABEL "Unit Tests")
target_link_libraries(${TestTarget} ${TestLibraries})
install(TARGETS ${TestTarget} DESTINATION ".")
3 changes: 3 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// create default module initialization
#define BOOST_TEST_MODULE OpenJK
#include <boost/test/unit_test.hpp>
69 changes: 69 additions & 0 deletions tests/safe/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "qcommon/safe/string.h"

#include <boost/test/unit_test.hpp>

// verifying that the compiler implements the standard correctly

BOOST_AUTO_TEST_SUITE( safe )

BOOST_AUTO_TEST_SUITE( string )

BOOST_AUTO_TEST_CASE( literals )
{
auto test = CSTRING_VIEW( "test" );
auto foo = CSTRING_VIEW( "foo" );

BOOST_CHECK_EQUAL( test, CSTRING_VIEW( "test" ) );
BOOST_CHECK_EQUAL( test.size(), 4 );

BOOST_CHECK_EQUAL( foo, CSTRING_VIEW( "foo" ) );
BOOST_CHECK_EQUAL( foo.size(), 3 );
BOOST_CHECK_NE( test, foo );
}

BOOST_AUTO_TEST_CASE( stricmp )
{
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "hello" ), CSTRING_VIEW( "HELLO" ) ), Q::Ordering::EQ );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "aaa" ), CSTRING_VIEW( "aab" ) ), Q::Ordering::LT );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "aab" ), CSTRING_VIEW( "aaa" ) ), Q::Ordering::GT );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "AAA" ), CSTRING_VIEW( "aab" ) ), Q::Ordering::LT );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "AAA" ), CSTRING_VIEW( "aab" ) ), Q::Ordering::LT );
// prefix is smaller
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "hello" ), CSTRING_VIEW( "hello world" ) ), Q::Ordering::LT );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "hello world" ), CSTRING_VIEW( "hello" ) ), Q::Ordering::GT );
// edge case: empty strings
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "" ), CSTRING_VIEW( "" ) ), Q::Ordering::EQ );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "" ), CSTRING_VIEW( "a" ) ), Q::Ordering::LT );
BOOST_CHECK_EQUAL( Q::stricmp( CSTRING_VIEW( "a" ), CSTRING_VIEW( "" ) ), Q::Ordering::GT );
}

BOOST_AUTO_TEST_CASE( svtoi )
{
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "" ) ), 0 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "asdf" ) ), 0 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "0" ) ), 0 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "1" ) ), 1 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "+1" ) ), 1 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "-1" ) ), -1 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "13foo" ) ), 13 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( " 13" ) ), 13 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "13 27" ) ), 13 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "12345" ) ), 12345 );
BOOST_CHECK_EQUAL( Q::svtoi( CSTRING_VIEW( "-12345" ) ), -12345 );
}

BOOST_AUTO_TEST_CASE( sscanf )
{
{
float x = -1, y = -1, z = -1;
BOOST_CHECK_EQUAL( Q::sscanf( CSTRING_VIEW( " 42 13.37" ), x, y, z ), 2 );
BOOST_CHECK_EQUAL( x, 42.f );
BOOST_CHECK_CLOSE_FRACTION( y, 13.37f, 0.1f ); // within 0.1% of each other
BOOST_CHECK_EQUAL( z, -1 );
}
// TODO String tests
}

BOOST_AUTO_TEST_SUITE_END() // string

BOOST_AUTO_TEST_SUITE_END() // safe

0 comments on commit b087a45

Please sign in to comment.