From 0cb3a05f5d14ebaa5bebc6789ff4779cea65e045 Mon Sep 17 00:00:00 2001 From: Marijn van der Werf Date: Wed, 31 Aug 2016 14:33:18 +0200 Subject: [PATCH] Include google test for cmake --- CMakeLists.txt | 60 +++++++++++++++++++++++++++++++++++++++++++-- PaintTest/gtest.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++ PaintTest/main.c | 30 +++++++++++++++++------ PaintTest/main.h | 19 ++++++++++++++ 4 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 PaintTest/gtest.cpp create mode 100644 PaintTest/main.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ebc8ba08b8..5cce470a6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -210,9 +210,14 @@ set(DEBUG_LEVEL 0 CACHE STRING "Select debug level for compilation. Use value in set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG=${DEBUG_LEVEL}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG=${DEBUG_LEVEL}") +# include lib +include_directories("lib/") # add source files -file(GLOB_RECURSE ORCT2_SOURCES "PaintTest/*.c" "PaintTest/*.cpp" "PaintTest/*.h" "PaintTest/*.hpp" "src/ride/*/*.c" "src/ride/ride_data.c" "src/ride/track_data.c" "src/ride/track_paint.c" "src/addresses.c" "src/diagnostic.c" "src/hook.c" "src/paint/map_element/map_element.c") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_VEHICLES") +file(GLOB_RECURSE ORCT2_SOURCES "src/*.c" "src/*.cpp" "src/*.h" "src/*.hpp") +if (APPLE) + file(GLOB_RECURSE ORCT2_MM_SOURCES "src/*.m") + set_source_files_properties(${ORCT2_MM_SOURCES} PROPERTIES COMPILE_FLAGS "-x objective-c -fmodules") +endif (APPLE) if (APPLE AND NOT USE_MMAP) @@ -315,6 +320,12 @@ else (WIN32) if (NOT APPLE AND NOT USE_MMAP) add_dependencies(${PROJECT} linkable_sections) endif () + add_custom_command( + OUTPUT g2.dat + COMMAND ./openrct2 sprite build ${CMAKE_BINARY_DIR}/g2.dat ${CMAKE_CURRENT_SOURCE_DIR}/resources/g2/ + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) + add_custom_target(g2 DEPENDS ${PROJECT} g2.dat) endif (WIN32) if (UNIX AND NOT APPLE) @@ -353,6 +364,51 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/g2.dat" DESTINATION share/${PROJECT}) install(DIRECTORY data/ DESTINATION share/${PROJECT}) install(FILES ${DOC_FILES} DESTINATION share/doc/${PROJECT}) +INCLUDE(ExternalProject) +# Add gtest +# http://stackoverflow.com/questions/9689183/cmake-googletest +ExternalProject_Add( + googletest + URL https://github.com/google/googletest/archive/release-1.7.0.zip + TIMEOUT 10 + # # Force separate output paths for debug and release builds to allow easy + # # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands + CMAKE_ARGS -DCMAKE_CXX_FLAGS=-m32 + # -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs + # -Dgtest_force_shared_crt=ON + # Disable install step + INSTALL_COMMAND "" + # Wrap download, configure and build steps in a script to log output + LOG_DOWNLOAD ON + LOG_CONFIGURE ON + LOG_BUILD ON) + +# Specify include dir +ExternalProject_Get_Property(googletest source_dir) +set(GTEST_INCLUDE_DIR ${source_dir}/include) + +# Library +ExternalProject_Get_Property(googletest binary_dir) +set(GTEST_LIBRARY_PATH ${binary_dir}/${CMAKE_FIND_LIBRARY_PREFIXES}gtest.a) +set(GTEST_LIBRARY gtest) +add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED) +set_property(TARGET ${GTEST_LIBRARY} PROPERTY IMPORTED_LOCATION + ${GTEST_LIBRARY_PATH} ) +add_dependencies(${GTEST_LIBRARY} googletest) + +include_directories(${GTEST_INCLUDE_DIR}) +add_definitions(-DUSE_GTEST) + +file(GLOB_RECURSE ORCT2_RIDE_SOURCES "src/ride/*/*.c") +file(GLOB_RECURSE ORCT2_RIDE_DEP_SOURCES "src/ride/ride_data.c" "src/ride/track_data.c" "src/ride/track_paint.c" "src/addresses.c" "src/diagnostic.c" "src/hook.c" "src/paint/map_element/map_element.c") +file(GLOB_RECURSE ORCT2_PAINT_TEST_SOURCES "PaintTest/*.c" "PaintTest/*.cpp" "PaintTest/*.h") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNO_VEHICLES") +string(REPLACE "-Wundef" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REPLACE "-Wundef" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +add_executable(test-paint ${ORCT2_RIDE_SOURCES} ${ORCT2_RIDE_DEP_SOURCES} ${ORCT2_PAINT_TEST_SOURCES}) +target_link_libraries(test-paint gtest) + set(CPACK_PACKAGE_VERSION_MAJOR 0) set(CPACK_PACKAGE_VERSION_MINOR 0) set(CPACK_PACKAGE_VERSION_PATCH 5) diff --git a/PaintTest/gtest.cpp b/PaintTest/gtest.cpp new file mode 100644 index 0000000000..b55e25f914 --- /dev/null +++ b/PaintTest/gtest.cpp @@ -0,0 +1,60 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#ifdef USE_GTEST +#include "gtest/gtest.h" + +extern "C" { +#include "main.h" +#include "../src/rct2.h" +} + +int main(int argc, char *argv[]) { + initHooks(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + + +class RidePaintTest : public testing::TestWithParam { +}; + +TEST_P(RidePaintTest, TestRidePainting) { + bool success = testRide(GetParam()); + + ASSERT_TRUE(success); +} + +std::vector ReadTestCasesFromDisk() { + std::vector rides; + + for (int i = 0; i < 91; i++) { + if (!rideIsImplemented(i)) { + continue; + } + rides.push_back(i); + } + + return rides; +} + +INSTANTIATE_TEST_CASE_P( + Instab, // Instantiation name can be chosen arbitrarily. + RidePaintTest, + testing::ValuesIn(ReadTestCasesFromDisk()) +); + +#endif // USE_GTEST diff --git a/PaintTest/main.c b/PaintTest/main.c index d227cf3768..a952233705 100644 --- a/PaintTest/main.c +++ b/PaintTest/main.c @@ -541,11 +541,15 @@ bool testTrackElement(uint8 rideType, uint8 trackType, utf8string *error) { return true; } -void testRide(int rideType) { +bool rideIsImplemented(int rideType) { TRACK_PAINT_FUNCTION_GETTER newPaintGetter = RideTypeTrackPaintFunctions[rideType]; - if (newPaintGetter == 0) { - return; - } + return (newPaintGetter != 0); +} + +bool testRide(int rideType) { + TRACK_PAINT_FUNCTION_GETTER newPaintGetter = RideTypeTrackPaintFunctions[rideType]; + + bool returnSuccess = true; printf("- %s (%d)\n", RideNames[rideType], rideType); for (int trackType = 0; trackType < 256; trackType++) { @@ -558,6 +562,7 @@ void testRide(int rideType) { if (!success) { printf(ANSI_COLOR_RED); + returnSuccess = false; } int sequenceCount = getTrackSequenceCount(rideType, trackType); if (ride_type_has_flag(rideType, RIDE_TYPE_FLAG_FLAT_RIDE)) { @@ -574,6 +579,8 @@ void testRide(int rideType) { printf(ANSI_COLOR_RESET "\n"); } + + return returnSuccess; } static int intercept_draw_6c(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp) { @@ -662,10 +669,19 @@ void initHooks() { addhook(0x00663584, (int) intercept_metal_b_supports, 0, (int[]) {EAX, EBX, EDX, EDI, EBP, END}, 0, EAX); } +#if !defined(USE_GTEST) + int main(int argc, const char *argv[]) { initHooks(); + + bool success = true; for (int i = 0; i < 91; i++) { - testRide(i); + if (!testRide(i)) { + success = false; + } } - return 0; -} \ No newline at end of file + + return success ? 0 : 1; +} + +#endif diff --git a/PaintTest/main.h b/PaintTest/main.h new file mode 100644 index 0000000000..8e9f9403d0 --- /dev/null +++ b/PaintTest/main.h @@ -0,0 +1,19 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +bool testRide(int rideType); +void initHooks(); +bool rideIsImplemented(int rideType);