Skip to content

Commit

Permalink
Merge pull request #1297 from berryzplus/feature/add_compiletest_of_c…
Browse files Browse the repository at this point in the history
…layoutint

コンパイルテストを導入する
  • Loading branch information
berryzplus committed May 16, 2020
2 parents 07736dc + 8d04f98 commit 7ac8117
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if(BUILD_GTEST)
add_subdirectory(googletest)
endif()
add_subdirectory(compiletests)
add_subdirectory(unittests)

# turn on solution folder
Expand Down
55 changes: 55 additions & 0 deletions tests/compiletests.run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
setlocal
set BUILD_BASE_DIR=%~dp1
set SOURCE_DIR=%~dp0compiletests

:: find generic tools
if not defined CMD_VSWHERE call %~dp0..\tools\find-tools.bat

set /a NUM_VSVERSION_NEXT=NUM_VSVERSION + 1

if not exist "%CMD_CMAKE%" (
echo "no cmake found."
exit /b 1
)

if not exist "%CMD_NINJA%" (
set GENERATOR="%CMAKE_G_PARAM%"
set GENERATOR_OPTS=-A %PLATFORM% "-DCMAKE_CONFIGURATION_TYPES=Debug;Release"
set "MAKE_PROGRAM=%CMD_MSBUILD%"
set "BUILD_DIR=%BUILD_BASE_DIR%compiletests\%platform%"
) else (
set GENERATOR=Ninja
set GENERATOR_OPTS=-DCMAKE_BUILD_TYPE=%CONFIGURATION%
set "MAKE_PROGRAM=%CMD_NINJA%"
set "BUILD_DIR=%BUILD_BASE_DIR%compiletests\%platform%\%configuration%"
)

mkdir %BUILD_DIR% > NUL 2>&1
pushd %BUILD_DIR%

call :run_cmake_configure || endlocal && exit /b 1

endlocal && exit /b 0

:run_cmake_configure
call :find_cl_compiler

:: replace back-slash to slash in the path.
set CL_COMPILER=%CMD_CL:\=/%

:: run cmake configuration.
"%CMD_CMAKE%" -G %GENERATOR% ^
"-DCMAKE_MAKE_PROGRAM=%MAKE_PROGRAM%" ^
"-DCMAKE_C_COMPILER=%CL_COMPILER%" ^
"-DCMAKE_CXX_COMPILER=%CL_COMPILER%" ^
%GENERATOR_OPTS% ^
%SOURCE_DIR% ^
|| endlocal && exit /b 1
goto :EOF

:find_cl_compiler
for /f "usebackq delims=" %%a in (`where cl.exe`) do (
set "CMD_CL=%%a"
goto :EOF
)
goto :EOF
12 changes: 12 additions & 0 deletions tests/compiletests.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This target depends on googletest.targets -->
<Target Name="RunCompileTests" DependsOnTargets="MakeGoogleTestBuildDir;BuildGoogleTest" BeforeTargets="ClCompile">
<PropertyGroup>
<VsVersion>$([System.Text.RegularExpressions.Regex]::Replace('$(VisualStudioVersion)', '^(\d+).*', '$1'))</VsVersion>
</PropertyGroup>
<SetEnv name="platform" value="$(Platform)" prefix="false" />
<SetEnv name="configuration" value="$(Configuration)" prefix="false" />
<SetEnv name="NUM_VSVERSION" value="$(VsVersion)" prefix="false" />
<Exec Command="$(MSBuildThisFileDirectory)compiletests.run.cmd &quot;$(GoogleTestBuildDir)&quot;" ConsoleToMSBuild="true" />
</Target>
</Project>
45 changes: 45 additions & 0 deletions tests/compiletests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.12)

enable_language(CXX)

set( SAKURA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../sakura_core)
set( SAKURA_DEFINITIONS -D_DEBUG -D_UNICODE -DUNICODE -D_WIN32_WINNT=_WIN32_WINNT_WIN7)

# コンパイルテスト実行関数
function( compile_test TEST_TEMPLATE TEST_BODY TEST_DESCRIPTION TEST_NAME )
message( STATUS "Checking ${TEST_NAME}" )

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TEST_TEMPLATE}
${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.cpp
NEWLINE_STYLE WIN32
)

try_compile(FAILED_TO_TEST ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.cpp
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${SAKURA_SOURCE_DIR}"
COMPILE_DEFINITIONS ${SAKURA_DEFINITIONS}
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)

if (NOT FAILED_TO_TEST)
message( STATUS "Checking ${TEST_NAME} - done" )
else()
message( FATAL_ERROR "Checking ${TEST_NAME} - Failed." )
endif (NOT FAILED_TO_TEST)
endfunction( compile_test )

# コンパイルテストの定義
compile_test( clayoutint_test.cpp.in "a += b;" "CLayoutInt に CLogicIntを加算代入することはできない" clayoutint_can_not_be_additive_assigned_from_clogicint )
compile_test( clayoutint_test.cpp.in "a -= b;" "CLayoutInt に CLogicIntを減算代入することはできない" clayoutint_can_not_be_subtractive_assigned_from_clogicint )
compile_test( clayoutint_test.cpp.in "a = a + b;" "CLayoutInt と CLogicIntを加算することはできない" clayoutint_can_not_be_added_by_clogicint )
compile_test( clayoutint_test.cpp.in "a = a - b;" "CLayoutInt から CLogicIntを減算することはできない" clayoutint_can_not_be_subtracted_by_clogicint )
compile_test( clayoutint_test.cpp.in "a = b;" "CLayoutInt に CLogicIntを代入することはできない" clayoutint_can_not_be_assigned_from_clogicint )
compile_test( clayoutint_test.cpp.in "a < b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_less_than )
compile_test( clayoutint_test.cpp.in "a <= b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_less_or_equal )
compile_test( clayoutint_test.cpp.in "a > b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_greater_than )
compile_test( clayoutint_test.cpp.in "a >= b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_greater_or_equal )
compile_test( clayoutint_test.cpp.in "a == b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_equal )
compile_test( clayoutint_test.cpp.in "a != b;" "CLayoutInt と CLogicIntを比較することはできない" clayoutint_can_not_be_compare_with_clogicint_not_equal )
compile_test( clayoutint_test.cpp.in "int c = a;" "CLayoutInt は キャストなしでint型に代入することはできない" clayoutint_can_not_be_assigned_to_int )
49 changes: 49 additions & 0 deletions tests/compiletests/clayoutint_test.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.

2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef NOMINMAX
#define NOMINMAX
#endif /* #ifndef NOMINMAX */

#include <tchar.h>
#include <Windows.h>

#ifndef USE_STRICT_INT
#define USE_STRICT_INT
#endif /* #ifndef USE_STRICT_INT */

#include "basis/SakuraBasis.h"

/*!
* @brief テンプレートのテスト
* このファイルはビルドエラーになる
*/
void main()
{
CLayoutInt a( 1 );
CLogicInt b( 2 );

//${TEST_DESCRIPTION}
${TEST_BODY}
}
2 changes: 1 addition & 1 deletion tests/unittests/test-clayoutint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ TEST( CLayoutInt, OperatorRemainder )
{
// コメントアウトの組み合わせは未実装。
CLayoutInt a( 2 ), b( 3 ), c( 8 );

EXPECT_TRUE( a == c % b );
EXPECT_TRUE( a == c % 3 );
//EXPECT_TRUE( a == 6 % b );
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/tests1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<IntDir>$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<Import Project="$(ProjectDir)..\googletest.targets" />
<Import Project="$(ProjectDir)..\compiletests.targets" />
<ItemDefinitionGroup Label="tests1.common">
<ClCompile>
<AdditionalIncludeDirectories>$(SolutionDir)sakura_core;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand Down

0 comments on commit 7ac8117

Please sign in to comment.