From: Jannik ZANDER Date: Fri, 10 Feb 2017 15:58:12 +0000 (+0100) Subject: Include unitest_cmake X-Git-Url: https://git.zndr.dk/?a=commitdiff_plain;h=8c86af728c6186948af59a5d15777209672fbe8f;p=codility.git Include unitest_cmake --- diff --git a/.gitignore b/.gitignore index b18c2b5..72e8ffc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -googletest -build +* diff --git a/CMakeLists.txt b/CMakeLists.txt index be43dad..5f67c8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,22 +1,15 @@ -cmake_minimum_required(VERSION 2.6) -project(basic_test) - -################################ -# GTest -################################ -# Prevent GoogleTest from overriding our compiler/linker options -# when building with Visual Studio -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -add_subdirectory(googletest) -enable_testing() -include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) -################################ -# Unit Tests -################################ -# Add test cpp file -add_subdirectory( solution1 ) -add_subdirectory( solution2 ) -add_subdirectory( solution3 ) -add_subdirectory( solution4 ) -add_subdirectory( solution5 ) -add_subdirectory( solution6 ) +cmake_minimum_required (VERSION 3.2) +project(project_test) + +set(USE_GTEST "yes") + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +find_package(AddUnitTest) + +add_subdirectory( solution1 ) +add_subdirectory( solution2 ) +add_subdirectory( solution3 ) +add_subdirectory( solution4 ) +add_subdirectory( solution5 ) +add_subdirectory( solution6 ) + diff --git a/cmake/CMakeLists.txt.in b/cmake/CMakeLists.txt.in new file mode 100644 index 0000000..73ea4a6 --- /dev/null +++ b/cmake/CMakeLists.txt.in @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) + diff --git a/cmake/FindAddUnitTest.cmake b/cmake/FindAddUnitTest.cmake new file mode 100644 index 0000000..0a69117 --- /dev/null +++ b/cmake/FindAddUnitTest.cmake @@ -0,0 +1,150 @@ +cmake_minimum_required (VERSION 3.2) + +enable_testing() +set_property( GLOBAL PROPERTY USE_FOLDERS ON) + +################################ +# Add unit function +################################ +function(add_unit) + cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN}) + print_target("Component" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}") + if ("${my_SRCS}" MATCHES ".*\\.(cpp|c)") + # Add public library + add_library(${my_TARGET} "${my_SRCS}") + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}") + target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS}) + add_unitfolder(${my_TARGET} "${my_SRCS}") + else() + # Add interface library + add_library(${my_TARGET} INTERFACE) + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + target_link_libraries(${my_TARGET} INTERFACE "${my_DEPS}") + target_compile_definitions(${my_TARGET} INTERFACE ${my_DEFS}) + + # Add header files to IDE view (this has no effect on build) + foreach(src ${my_SRCS}) + target_sources(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${src}") + endforeach() + endif() +endfunction() + +################################ +# Add unittest function +################################ +function(add_unittest) + cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN}) + print_target("UnitTest" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}") + + # Add test executable + add_executable(${my_TARGET} "${my_SRCS}") + foreach(inc ${my_INCS}) + target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}") + endforeach() + if (USE_GTEST) + if(GTEST_FOUND) + target_include_directories(${my_TARGET} PUBLIC "${GTEST_INCLUDE_DIRS}") + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" ${GTEST_BOTH_LIBRARIES}) + else() + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" gtest gtest_main) + endif() + target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS} USE_GTEST) + else() + target_include_directories(${my_TARGET} PUBLIC "${CPPUNIT_INCLUDE_DIRS}") + target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" ${CPPUNIT_LIBRARIES}) + target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS}) + endif() + # Add test to test suite + add_test(${my_TARGET} ${my_TARGET}) + add_unitfolder(${my_TARGET} "${my_SRCS}") + # Run test after build + add_custom_command(TARGET ${my_TARGET} POST_BUILD COMMAND ${my_TARGET}) +endfunction() + +################################ +# Add add_unitfolder function (only for IDE) +################################ +function(add_unitfolder mytarget mysrcs) + # source_group("" FILES "${mysrcs}") + string(REGEX REPLACE "_stub*$" "" myfolder ${mytarget}) + string(REGEX REPLACE "_stubs*$" "" myfolder ${mytarget}) + string(REGEX REPLACE "_test*$" "" myfolder ${myfolder}) + string(REGEX REPLACE "_if*$" "" myfolder ${myfolder}) + string(TOUPPER ${myfolder} myfolder) + set_property(TARGET ${mytarget} PROPERTY FOLDER "${myfolder}") +endfunction() + +################################ +# Print target function +################################ +function(print_target type target srcs incs deps defs) + message("${type}:") + message(" ${target}:") + message(" Sources:") + foreach(src ${srcs}) + message(" - ${src}") + endforeach() + message(" Includes:") + foreach(inc ${incs}) + message(" - ${inc}") + endforeach() + message(" Dependencies:") + foreach(dep ${deps}) + message(" - ${dep}") + endforeach() + message(" Definitions:") + foreach(def ${defs}) + message(" - ${def}") + endforeach() + message("===================================================") +endfunction() + + +################################ +# Compiler options +################################ +if(MSVC) + add_definitions(/FC /D_CRT_SECURE_NO_WARNINGS) +endif() + +if(CMAKE_COMPILER_IS_GNUCXX) + add_definitions(-Wall -Wno-deprecated -pthread -D__PC__) +endif() + +################################ +# Configure testing framework +################################ + +if(USE_GTEST) + if(NOT GTEST_ROOT) + if(NOT $ENV{GTEST_DIR} STREQUAL "") + set(GTEST_ROOT "$ENV{GTEST_DIR}") + else() + set(GTEST_ROOT "C:/googletest-release-1.7.0") + endif() + endif() + + find_package(gtest) + if(NOT GTEST_FOUND) + # Add external google test project + find_package(git REQUIRED) + include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gtest.cmake") + endif() +else() + if(NOT CPPUNIT_ROOT_DIR) + if(NOT $ENV{CPPUNITDIR} STREQUAL "") + set(CPPUNIT_ROOT_DIR "$ENV{CPPUNITDIR}/lib") + else() + set(CPPUNIT_ROOT_DIR "C:/cppunit-1.12.1/lib") + endif() + endif() + + find_package(cppunit REQUIRED) +endif() + +message("===================================================") diff --git a/cmake/Findcppunit.cmake b/cmake/Findcppunit.cmake new file mode 100644 index 0000000..b83b67e --- /dev/null +++ b/cmake/Findcppunit.cmake @@ -0,0 +1,76 @@ +# - try to find cppunit library +# +# Cache Variables: (probably not for direct use in your scripts) +# CPPUNIT_INCLUDE_DIR +# CPPUNIT_LIBRARY +# +# Non-cache variables you might use in your CMakeLists.txt: +# CPPUNIT_FOUND +# CPPUNIT_INCLUDE_DIRS +# CPPUNIT_LIBRARIES +# +# Requires these CMake modules: +# SelectLibraryConfigurations (included with CMake >= 2.8.0) +# FindPackageHandleStandardArgs (known included with CMake >=2.6.2) +# +# Original Author: +# 2009-2011 Ryan Pavlik +# http://academic.cleardefinition.com +# Iowa State University HCI Graduate Program/VRAC +# +# Copyright Iowa State University 2009-2011. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +set(CPPUNIT_ROOT_DIR + "${CPPUNIT_ROOT_DIR}" + CACHE + PATH + "Directory to search") + +find_library(CPPUNIT_LIBRARY_RELEASE + NAMES + cppunit + HINTS + "${CPPUNIT_ROOT_DIR}") + +find_library(CPPUNIT_LIBRARY_DEBUG + NAMES + cppunitd + HINTS + "${CPPUNIT_ROOT_DIR}") + +include(SelectLibraryConfigurations) +select_library_configurations(CPPUNIT) + +# Might want to look close to the library first for the includes. +get_filename_component(_libdir "${CPPUNIT_LIBRARY_RELEASE}" PATH) + +find_path(CPPUNIT_INCLUDE_DIR + NAMES + cppunit/TestCase.h + HINTS + "${_libdir}/.." + PATHS + "${CPPUNIT_ROOT_DIR}" + PATH_SUFFIXES + include/) + + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(cppunit + DEFAULT_MSG + CPPUNIT_LIBRARY + CPPUNIT_INCLUDE_DIR) + +if(CPPUNIT_FOUND) + set(CPPUNIT_LIBRARIES ${CPPUNIT_LIBRARY} ${CMAKE_DL_LIBS}) + set(CPPUNIT_INCLUDE_DIRS "${CPPUNIT_INCLUDE_DIR}") + mark_as_advanced(CPPUNIT_ROOT_DIR) +endif() + +mark_as_advanced(CPPUNIT_INCLUDE_DIR + CPPUNIT_LIBRARY_RELEASE + CPPUNIT_LIBRARY_DEBUG) + diff --git a/cmake/gtest.cmake b/cmake/gtest.cmake new file mode 100644 index 0000000..5c92fde --- /dev/null +++ b/cmake/gtest.cmake @@ -0,0 +1,34 @@ +cmake_minimum_required (VERSION 3.2) + +find_path (my_PATH NAMES CMakeLists.txt.in HINTS ${CMAKE_MODULE_PATH}) + +# Download and unpack googletest at configure time +configure_file("${my_PATH}/CMakeLists.txt.in" googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") +endif() +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") +endif() + +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +# Add googletest directly to our build. This defines +# the gtest and gtest_main targets. +add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src + ${CMAKE_BINARY_DIR}/googletest-build) + +# The gtest/gtest_main targets carry header search path +# dependencies automatically when using CMake 2.8.11 or +# later. Otherwise we have to add them here ourselves. +if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") +endif() diff --git a/solution1/CMakeLists.txt b/solution1/CMakeLists.txt index dd8e229..ec9d2e2 100644 --- a/solution1/CMakeLists.txt +++ b/solution1/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required (VERSION 2.6) -add_executable(solution1 solution.c solution.h unittest.cpp) -add_test(solution1 solution1) -target_link_libraries(solution1 gtest_main) -add_custom_command(TARGET solution1 POST_BUILD COMMAND solution1) +add_unittest( + TARGET solution1 + SRCS solution.c solution.h unittest.cpp + INCS . +) diff --git a/solution1/solution.c b/solution1/solution.c index 1bdf3d0..661ea06 100644 --- a/solution1/solution.c +++ b/solution1/solution.c @@ -1,16 +1,16 @@ -#include "solution.h" - -int solution(int A, int B) -{ - int i; - int count = 0; - for (i = 0; i <= 100; i++) - { - if (A <= i*i && i*i <= B) - { - count++; - } - } - return count; -} - +#include "solution.h" + +int solution(int A, int B) +{ + int i; + int count = 0; + for (i = 0; i <= 100; i++) + { + if (A <= i*i && i*i <= B) + { + count++; + } + } + return count; +} + diff --git a/solution1/solution.h b/solution1/solution.h index df89de0..efc7b8c 100644 --- a/solution1/solution.h +++ b/solution1/solution.h @@ -1,7 +1,7 @@ -#ifndef SOLUTION_H_ -#define SOLUTION_H_ - - -int solution(int A, int B); - -#endif // SOLUTION_H_ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + + +int solution(int A, int B); + +#endif // SOLUTION_H_ diff --git a/solution1/unittest.cpp b/solution1/unittest.cpp index 53fcd66..08e58fc 100644 --- a/solution1/unittest.cpp +++ b/solution1/unittest.cpp @@ -1,11 +1,11 @@ -#include "gtest/gtest.h" -extern "C" { -#include "solution.h" -} - - -TEST(SolutionTestSuite, returnCorrectResult) -{ - ASSERT_EQ(3, solution(4,17)); - ASSERT_EQ(5, solution(-4,17)); -} +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + + +TEST(SolutionTestSuite, returnCorrectResult) +{ + ASSERT_EQ(3, solution(4,17)); + ASSERT_EQ(5, solution(-4,17)); +} diff --git a/solution2/CMakeLists.txt b/solution2/CMakeLists.txt index b8b6dfb..14694ef 100644 --- a/solution2/CMakeLists.txt +++ b/solution2/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required (VERSION 2.6) -add_executable(solution2 solution.c solution.h unittest.cpp) -add_test(solution2 solution2) -target_link_libraries(solution2 gtest_main) -add_custom_command(TARGET solution2 POST_BUILD COMMAND solution2) +add_unittest( + TARGET solution2 + SRCS solution.c solution.h unittest.cpp + INCS . +) diff --git a/solution2/solution.c b/solution2/solution.c index 7301690..44e2e47 100644 --- a/solution2/solution.c +++ b/solution2/solution.c @@ -1,60 +1,60 @@ -#include "solution.h" - -int solution_with_bug(int *A, int A_length) -{ - int n = A_length; - int result = 0; - int i; - for (i = 0; i < n - 1; i++) { - if (A[i] == A[i + 1]) - result = result + 1; - } - int r = 0; - for (i = 0; i < n; i++) { - int count = 0; - if (i > 0) { - if (A[i - 1] != A[i]) - count = count + 1; - else - count = count - 1; - } - if (i < n - 1) { - if (A[i + 1] != A[i]) - count = count + 1; - else - count = count - 1; - } - if (count > r) - r = count; - } - return result + r; -} - -int solution(int *A, int A_length) { - int n = A_length; - int result = 1; - int i; - for (i = 0; i < n - 1; i++) { - if (A[i] == A[i + 1] && A[i] == 1) - result = result + 1; - } - int r = 0; - for (i = 0; i < n; i++) { - int count = 0; - if (i > 0) { - if (A[i - 1] != A[i]) - count = count + 1; - else - count = count - 1; - } - if (i < n - 1) { - if (A[i + 1] != A[i]) - count = count + 1; - else - count = count - 1; - } - if (count > r) - r = count; - } - return result + r; -} +#include "solution.h" + +int solution_with_bug(int *A, int A_length) +{ + int n = A_length; + int result = 0; + int i; + for (i = 0; i < n - 1; i++) { + if (A[i] == A[i + 1]) + result = result + 1; + } + int r = 0; + for (i = 0; i < n; i++) { + int count = 0; + if (i > 0) { + if (A[i - 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (i < n - 1) { + if (A[i + 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (count > r) + r = count; + } + return result + r; +} + +int solution(int *A, int A_length) { + int n = A_length; + int result = 1; + int i; + for (i = 0; i < n - 1; i++) { + if (A[i] == A[i + 1] && A[i] == 1) + result = result + 1; + } + int r = 0; + for (i = 0; i < n; i++) { + int count = 0; + if (i > 0) { + if (A[i - 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (i < n - 1) { + if (A[i + 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (count > r) + r = count; + } + return result + r; +} diff --git a/solution2/solution.h b/solution2/solution.h index b8ca84b..64aac4a 100644 --- a/solution2/solution.h +++ b/solution2/solution.h @@ -1,6 +1,6 @@ -#ifndef SOLUTION_H_ -#define SOLUTION_H_ - -int solution(int *A, int A_length); - -#endif // SOLUTION_H_ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + +int solution(int *A, int A_length); + +#endif // SOLUTION_H_ diff --git a/solution2/unittest.cpp b/solution2/unittest.cpp index 0f11933..72a9d04 100644 --- a/solution2/unittest.cpp +++ b/solution2/unittest.cpp @@ -1,65 +1,65 @@ -#include "gtest/gtest.h" -extern "C" { -#include "solution.h" -} - - -TEST(SolutionTestSuite, returnCorrectResult) -{ - int A[1000]; - - A[0] = 1; - A[1] = 1; - A[2] = 0; - A[3] = 1; - A[4] = 0; - A[5] = 0; - - ASSERT_EQ(4, solution(A,6)); - - A[0] = 1; - A[1] = 1; - A[2] = 0; - A[3] = 1; - A[4] = 0; - A[5] = 0; - A[6] = 0; -#if 0 - ASSERT_EQ(5, solution(A,7)); - - A[0] = 1; - A[1] = 1; - - ASSERT_EQ(2, solution(A,2)); - - A[0] = 1; - A[1] = 1; - A[2] = 0; - - ASSERT_EQ(3, solution(A,3)); - - A[0] = 1; - A[1] = 1; - A[2] = 0; - A[3] = 1; - - ASSERT_EQ(4, solution(A,4)); - - A[0] = 1; - A[1] = 1; - A[2] = 0; - A[3] = 1; - A[4] = 0; - - ASSERT_EQ(4, solution(A,5)); - - A[0] = 1; - A[1] = 1; - A[2] = 0; - A[3] = 1; - A[4] = 0; - A[5] = 0; - - ASSERT_EQ(4, solution(A,6)); -#endif -} +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + + +TEST(SolutionTestSuite, returnCorrectResult) +{ + int A[1000]; + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + + ASSERT_EQ(4, solution(A,6)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + A[6] = 0; +#if 0 + ASSERT_EQ(5, solution(A,7)); + + A[0] = 1; + A[1] = 1; + + ASSERT_EQ(2, solution(A,2)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + + ASSERT_EQ(3, solution(A,3)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + + ASSERT_EQ(4, solution(A,4)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + + ASSERT_EQ(4, solution(A,5)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + + ASSERT_EQ(4, solution(A,6)); +#endif +} diff --git a/solution3/CMakeLists.txt b/solution3/CMakeLists.txt index 5dff733..d09a34a 100644 --- a/solution3/CMakeLists.txt +++ b/solution3/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required (VERSION 2.6) -add_executable(solution3 solution.c solution.h unittest.cpp) -add_test(solution1 solution3) -target_link_libraries(solution3 gtest_main) -add_custom_command(TARGET solution3 POST_BUILD COMMAND solution3) +add_unittest( + TARGET solution3 + SRCS solution.c solution.h unittest.cpp + INCS . +) diff --git a/solution3/solution.c b/solution3/solution.c index 085b3d6..b2dc230 100644 --- a/solution3/solution.c +++ b/solution3/solution.c @@ -1,40 +1,40 @@ -#include "solution.h" - -struct Results solution(int A[], int M) -{ - struct Results result; - int i; - int X = 0; - int sign = 0; - for (i = 0; i < M; i++) - { - if (i & 0x1) - { - X -= A[i]*(1<= '0' && *ch <= '9') - { - digits[num_digits++]= *ch - '0'; - } - else if (*ch == ' ' && num_digits) - { - num1 = 0; - for (i = 0; i < num_digits; i++) - { - num1 *= 10; - num1 += digits[i]; - } - num_digits = 0; - stack[top++] = num1; - } - else if (*ch == 'D' && *(ch+1)=='U' && *(ch+2)=='P') - { - if (top < 1) return -1; - num1 = stack[top-1]; - stack[top++] = num1; - } - else if (*ch == 'P' && *(ch+1)=='O' && *(ch+2)=='P') - { - if (top < 1) return -1; - num1 = stack[--top]; - } - else if (*ch=='+') - { - if (top < 2) return -1; - num1 = stack[--top]; - num2 = stack[--top]; - stack[top++] = num1 + num2; - } - else if (*ch=='-') - { - if (top < 2) return -1; - num1 = stack[--top]; - num2 = stack[--top]; - if (num1 - num2 < 0) return -1; - stack[top++] = num1 - num2; - } - ch++; - } - - - return stack[top-1]; -} +#include "Solution.h" + +int solution(char *S) +{ + int stack[10]; + int top = 0; + int i; + char *ch=S; + int num1; + int num2; + int digits[8]; + int num_digits = 0; + + while (*ch != 0) + { + if (*ch >= '0' && *ch <= '9') + { + digits[num_digits++]= *ch - '0'; + } + else if (*ch == ' ' && num_digits) + { + num1 = 0; + for (i = 0; i < num_digits; i++) + { + num1 *= 10; + num1 += digits[i]; + } + num_digits = 0; + stack[top++] = num1; + } + else if (*ch == 'D' && *(ch+1)=='U' && *(ch+2)=='P') + { + if (top < 1) return -1; + num1 = stack[top-1]; + stack[top++] = num1; + } + else if (*ch == 'P' && *(ch+1)=='O' && *(ch+2)=='P') + { + if (top < 1) return -1; + num1 = stack[--top]; + } + else if (*ch=='+') + { + if (top < 2) return -1; + num1 = stack[--top]; + num2 = stack[--top]; + stack[top++] = num1 + num2; + } + else if (*ch=='-') + { + if (top < 2) return -1; + num1 = stack[--top]; + num2 = stack[--top]; + if (num1 - num2 < 0) return -1; + stack[top++] = num1 - num2; + } + ch++; + } + + + return stack[top-1]; +} diff --git a/solution6/solution.h b/solution6/solution.h index 84ad726..a30faf2 100644 --- a/solution6/solution.h +++ b/solution6/solution.h @@ -1,6 +1,6 @@ -#ifndef SOLUTION_H_ -#define SOLUTION_H_ - -int solution(char *S); - -#endif // SOLUTION_H_ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + +int solution(char *S); + +#endif // SOLUTION_H_ diff --git a/solution6/unittest.cpp b/solution6/unittest.cpp index 8063d85..3df9234 100644 --- a/solution6/unittest.cpp +++ b/solution6/unittest.cpp @@ -1,14 +1,14 @@ -#include "gtest/gtest.h" -extern "C" { -#include "solution.h" -} - -TEST(SolutionTest, SomeOperations) -{ - char S1[]= "13 DUP 4 POP 5 DUP + DUP + -"; - char S2[] = "5 6 + -"; -// char S2[] ="3 DUP 5 - -"; - - EXPECT_EQ(7, solution(S1)); - EXPECT_EQ(-1, solution(S2)); -} +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + +TEST(SolutionTest, SomeOperations) +{ + char S1[]= "13 DUP 4 POP 5 DUP + DUP + -"; + char S2[] = "5 6 + -"; +// char S2[] ="3 DUP 5 - -"; + + EXPECT_EQ(7, solution(S1)); + EXPECT_EQ(-1, solution(S2)); +}