-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)\r
+project(project_test)\r
+\r
+set(USE_GTEST "yes")\r
+\r
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")\r
+find_package(AddUnitTest)\r
+\r
+add_subdirectory( solution1 )\r
+add_subdirectory( solution2 )\r
+add_subdirectory( solution3 )\r
+add_subdirectory( solution4 )\r
+add_subdirectory( solution5 )\r
+add_subdirectory( solution6 )\r
+\r
--- /dev/null
+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 ""
+)
+
--- /dev/null
+cmake_minimum_required (VERSION 3.2)\r
+\r
+enable_testing()\r
+set_property( GLOBAL PROPERTY USE_FOLDERS ON)\r
+\r
+################################\r
+# Add unit function \r
+################################\r
+function(add_unit)\r
+ cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN})\r
+ print_target("Component" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}")\r
+ if ("${my_SRCS}" MATCHES ".*\\.(cpp|c)")\r
+ # Add public library\r
+ add_library(${my_TARGET} "${my_SRCS}")\r
+ foreach(inc ${my_INCS})\r
+ target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}")\r
+ endforeach()\r
+ target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}")\r
+ target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS})\r
+ add_unitfolder(${my_TARGET} "${my_SRCS}")\r
+ else()\r
+ # Add interface library\r
+ add_library(${my_TARGET} INTERFACE)\r
+ foreach(inc ${my_INCS})\r
+ target_include_directories(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${inc}")\r
+ endforeach()\r
+ target_link_libraries(${my_TARGET} INTERFACE "${my_DEPS}")\r
+ target_compile_definitions(${my_TARGET} INTERFACE ${my_DEFS})\r
+\r
+ # Add header files to IDE view (this has no effect on build)\r
+ foreach(src ${my_SRCS})\r
+ target_sources(${my_TARGET} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/${src}")\r
+ endforeach()\r
+ endif() \r
+endfunction()\r
+\r
+################################\r
+# Add unittest function \r
+################################\r
+function(add_unittest)\r
+ cmake_parse_arguments(my "" "TARGET" "INCS;SRCS;DEPS;DEFS" ${ARGN})\r
+ print_target("UnitTest" "${my_TARGET}" "${my_SRCS}" "${my_INCS}" "${my_DEPS}" "${my_DEFS}")\r
+\r
+ # Add test executable\r
+ add_executable(${my_TARGET} "${my_SRCS}")\r
+ foreach(inc ${my_INCS})\r
+ target_include_directories(${my_TARGET} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/${inc}")\r
+ endforeach()\r
+ if (USE_GTEST)\r
+ if(GTEST_FOUND)\r
+ target_include_directories(${my_TARGET} PUBLIC "${GTEST_INCLUDE_DIRS}")\r
+ target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" ${GTEST_BOTH_LIBRARIES})\r
+ else()\r
+ target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" gtest gtest_main)\r
+ endif()\r
+ target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS} USE_GTEST)\r
+ else()\r
+ target_include_directories(${my_TARGET} PUBLIC "${CPPUNIT_INCLUDE_DIRS}")\r
+ target_link_libraries(${my_TARGET} PUBLIC "${my_DEPS}" ${CPPUNIT_LIBRARIES})\r
+ target_compile_definitions(${my_TARGET} PUBLIC ${my_DEFS})\r
+ endif()\r
+ # Add test to test suite\r
+ add_test(${my_TARGET} ${my_TARGET})\r
+ add_unitfolder(${my_TARGET} "${my_SRCS}")\r
+ # Run test after build\r
+ add_custom_command(TARGET ${my_TARGET} POST_BUILD COMMAND ${my_TARGET})\r
+endfunction()\r
+\r
+################################\r
+# Add add_unitfolder function (only for IDE)\r
+################################\r
+function(add_unitfolder mytarget mysrcs)\r
+ # source_group("" FILES "${mysrcs}")\r
+ string(REGEX REPLACE "_stub*$" "" myfolder ${mytarget})\r
+ string(REGEX REPLACE "_stubs*$" "" myfolder ${mytarget})\r
+ string(REGEX REPLACE "_test*$" "" myfolder ${myfolder})\r
+ string(REGEX REPLACE "_if*$" "" myfolder ${myfolder})\r
+ string(TOUPPER ${myfolder} myfolder)\r
+ set_property(TARGET ${mytarget} PROPERTY FOLDER "${myfolder}")\r
+endfunction()\r
+\r
+################################\r
+# Print target function \r
+################################\r
+function(print_target type target srcs incs deps defs)\r
+ message("${type}:")\r
+ message(" ${target}:")\r
+ message(" Sources:")\r
+ foreach(src ${srcs})\r
+ message(" - ${src}")\r
+ endforeach()\r
+ message(" Includes:")\r
+ foreach(inc ${incs})\r
+ message(" - ${inc}")\r
+ endforeach()\r
+ message(" Dependencies:")\r
+ foreach(dep ${deps})\r
+ message(" - ${dep}")\r
+ endforeach()\r
+ message(" Definitions:")\r
+ foreach(def ${defs})\r
+ message(" - ${def}")\r
+ endforeach()\r
+ message("===================================================")\r
+endfunction()\r
+\r
+\r
+################################\r
+# Compiler options \r
+################################\r
+if(MSVC)\r
+ add_definitions(/FC /D_CRT_SECURE_NO_WARNINGS)\r
+endif()\r
+\r
+if(CMAKE_COMPILER_IS_GNUCXX)\r
+ add_definitions(-Wall -Wno-deprecated -pthread -D__PC__)\r
+endif()\r
+\r
+################################\r
+# Configure testing framework \r
+################################\r
+\r
+if(USE_GTEST)\r
+ if(NOT GTEST_ROOT)\r
+ if(NOT $ENV{GTEST_DIR} STREQUAL "")\r
+ set(GTEST_ROOT "$ENV{GTEST_DIR}")\r
+ else()\r
+ set(GTEST_ROOT "C:/googletest-release-1.7.0")\r
+ endif()\r
+ endif()\r
+\r
+ find_package(gtest)\r
+ if(NOT GTEST_FOUND)\r
+ # Add external google test project\r
+ find_package(git REQUIRED)\r
+ include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gtest.cmake")\r
+ endif()\r
+else()\r
+ if(NOT CPPUNIT_ROOT_DIR)\r
+ if(NOT $ENV{CPPUNITDIR} STREQUAL "")\r
+ set(CPPUNIT_ROOT_DIR "$ENV{CPPUNITDIR}/lib")\r
+ else()\r
+ set(CPPUNIT_ROOT_DIR "C:/cppunit-1.12.1/lib")\r
+ endif()\r
+ endif()\r
+\r
+ find_package(cppunit REQUIRED)\r
+endif()\r
+\r
+message("===================================================")\r
--- /dev/null
+# - 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 <rpavlik@iastate.edu> <abiryan@ryand.net>
+# 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)
+
--- /dev/null
+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()
-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(\r
+ TARGET solution1\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\r
-#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"\r
+\r
+int solution(int A, int B)\r
+{\r
+ int i;\r
+ int count = 0;\r
+ for (i = 0; i <= 100; i++)\r
+ {\r
+ if (A <= i*i && i*i <= B)\r
+ {\r
+ count++;\r
+ }\r
+ }\r
+ return count;\r
+}\r
+\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-
-int solution(int A, int B);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+\r
+int solution(int A, int B);\r
+\r
+#endif // SOLUTION_H_\r
-#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"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+\r
+\r
+TEST(SolutionTestSuite, returnCorrectResult)\r
+{\r
+ ASSERT_EQ(3, solution(4,17));\r
+ ASSERT_EQ(5, solution(-4,17));\r
+}\r
-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(\r
+ TARGET solution2\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\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;
-}
+#include "solution.h"\r
+\r
+int solution_with_bug(int *A, int A_length)\r
+{\r
+ int n = A_length;\r
+ int result = 0;\r
+ int i;\r
+ for (i = 0; i < n - 1; i++) {\r
+ if (A[i] == A[i + 1])\r
+ result = result + 1;\r
+ }\r
+ int r = 0;\r
+ for (i = 0; i < n; i++) {\r
+ int count = 0;\r
+ if (i > 0) {\r
+ if (A[i - 1] != A[i])\r
+ count = count + 1;\r
+ else\r
+ count = count - 1;\r
+ }\r
+ if (i < n - 1) {\r
+ if (A[i + 1] != A[i])\r
+ count = count + 1;\r
+ else\r
+ count = count - 1;\r
+ }\r
+ if (count > r)\r
+ r = count;\r
+ }\r
+ return result + r;\r
+}\r
+\r
+int solution(int *A, int A_length) {\r
+ int n = A_length;\r
+ int result = 1;\r
+ int i;\r
+ for (i = 0; i < n - 1; i++) {\r
+ if (A[i] == A[i + 1] && A[i] == 1)\r
+ result = result + 1;\r
+ }\r
+ int r = 0;\r
+ for (i = 0; i < n; i++) {\r
+ int count = 0;\r
+ if (i > 0) {\r
+ if (A[i - 1] != A[i])\r
+ count = count + 1;\r
+ else\r
+ count = count - 1;\r
+ }\r
+ if (i < n - 1) {\r
+ if (A[i + 1] != A[i])\r
+ count = count + 1;\r
+ else\r
+ count = count - 1;\r
+ }\r
+ if (count > r)\r
+ r = count;\r
+ }\r
+ return result + r;\r
+}\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-int solution(int *A, int A_length);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+int solution(int *A, int A_length);\r
+\r
+#endif // SOLUTION_H_\r
-#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"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+\r
+\r
+TEST(SolutionTestSuite, returnCorrectResult)\r
+{\r
+ int A[1000];\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+ A[3] = 1;\r
+ A[4] = 0;\r
+ A[5] = 0;\r
+\r
+ ASSERT_EQ(4, solution(A,6));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+ A[3] = 1;\r
+ A[4] = 0;\r
+ A[5] = 0;\r
+ A[6] = 0;\r
+#if 0\r
+ ASSERT_EQ(5, solution(A,7));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+\r
+ ASSERT_EQ(2, solution(A,2));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+\r
+ ASSERT_EQ(3, solution(A,3));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+ A[3] = 1;\r
+\r
+ ASSERT_EQ(4, solution(A,4));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+ A[3] = 1;\r
+ A[4] = 0;\r
+\r
+ ASSERT_EQ(4, solution(A,5));\r
+\r
+ A[0] = 1;\r
+ A[1] = 1;\r
+ A[2] = 0;\r
+ A[3] = 1;\r
+ A[4] = 0;\r
+ A[5] = 0;\r
+\r
+ ASSERT_EQ(4, solution(A,6));\r
+#endif\r
+}\r
-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(\r
+ TARGET solution3\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\r
-#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<<i);
- }
- else
- {
- X += A[i]*(1<<i);
- }
- }
- result.N = X + 1;
- result.B = A;
-
- for (i = 0; i <= 100; i++)
- {
- if (i & 0x1)
- {
- sign = -1;
- }
- else
- {
- sign = 1;
- }
- if (X + sign*(1 << i))
- {
- }
- }
-
- return result;
-}
-
+#include "solution.h"\r
+\r
+struct Results solution(int A[], int M)\r
+{\r
+ struct Results result;\r
+ int i;\r
+ int X = 0;\r
+ int sign = 0;\r
+ for (i = 0; i < M; i++)\r
+ {\r
+ if (i & 0x1)\r
+ {\r
+ X -= A[i]*(1<<i);\r
+ }\r
+ else\r
+ {\r
+ X += A[i]*(1<<i);\r
+ }\r
+ }\r
+ result.N = X + 1;\r
+ result.B = A;\r
+\r
+ for (i = 0; i <= 100; i++)\r
+ {\r
+ if (i & 0x1)\r
+ {\r
+ sign = -1;\r
+ }\r
+ else\r
+ {\r
+ sign = 1;\r
+ }\r
+ if (X + sign*(1 << i))\r
+ {\r
+ }\r
+ }\r
+\r
+ return result;\r
+}\r
+\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-
-struct Results {
- int * B;
- int N;
-};
-
-struct Results solution(int A[], int M);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+\r
+struct Results {\r
+ int * B;\r
+ int N;\r
+};\r
+\r
+struct Results solution(int A[], int M);\r
+\r
+#endif // SOLUTION_H_\r
-#include "gtest/gtest.h"
-extern "C" {
-#include "solution.h"
-}
-TEST(SolutionTestSuite, returnCorrectResult)
-{
- struct Results res;
- int A[1000];
- A[0] = 1;
- A[1] = 0;
- A[2] = 1;
- // X = 5
-
- res = solution(A, 3);
-
- ASSERT_EQ(6, res.N);
-// ASSERT_EQ(0, res.B[0]);
-// ASSERT_EQ(1, res.B[1]);
-// ASSERT_EQ(0, res.B[3]);
-// ASSERT_EQ(1, res.B[4]);
-// ASSERT_EQ(1, res.B[5]);
-}
+#include "gtest/gtest.h"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+TEST(SolutionTestSuite, returnCorrectResult)\r
+{\r
+ struct Results res;\r
+ int A[1000];\r
+ A[0] = 1;\r
+ A[1] = 0;\r
+ A[2] = 1;\r
+ // X = 5\r
+\r
+ res = solution(A, 3);\r
+\r
+ ASSERT_EQ(6, res.N);\r
+// ASSERT_EQ(0, res.B[0]);\r
+// ASSERT_EQ(1, res.B[1]);\r
+// ASSERT_EQ(0, res.B[3]);\r
+// ASSERT_EQ(1, res.B[4]);\r
+// ASSERT_EQ(1, res.B[5]);\r
+}\r
-cmake_minimum_required (VERSION 2.6)
-add_executable(solution4 solution.c solution.h unittest.cpp)
-add_test(solution4 solution4)
-target_link_libraries(solution4 gtest_main)
-add_custom_command(TARGET solution4 POST_BUILD COMMAND solution4)
+add_unittest(\r
+ TARGET solution4\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\r
-#include "solution.h"
-
-int solution(int A, int B)
-{
- int res = -1;
- int i, j;
- int digitsA[11] = { 0 };
- int digitsB[11] = { 0 };
-
- for (i = 0; i < 10; i++)
- {
- digitsA[i] = A % 10;
- A = A / 10;
- digitsB[i] = B % 10;
- B = B / 10;
- }
- j = 0;
- for (i = 0; i < 10;i++)
- {
- if (digitsA[j] == digitsB[i] && digitsB[i] != 0)
- {
- j++;
- res = i;
- }
- if (digitsA[j] == 0 && digitsB[i] == 0)
- {
- return res - j;
- }
- }
-
- return res;
-}
-
+#include "solution.h"\r
+\r
+int solution(int A, int B)\r
+{\r
+ int res = -1;\r
+ int i, j;\r
+ int digitsA[11] = { 0 };\r
+ int digitsB[11] = { 0 };\r
+\r
+ for (i = 0; i < 10; i++)\r
+ {\r
+ digitsA[i] = A % 10;\r
+ A = A / 10;\r
+ digitsB[i] = B % 10;\r
+ B = B / 10;\r
+ }\r
+ j = 0;\r
+ for (i = 0; i < 10;i++)\r
+ {\r
+ if (digitsA[j] == digitsB[i] && digitsB[i] != 0)\r
+ {\r
+ j++;\r
+ res = i;\r
+ }\r
+ if (digitsA[j] == 0 && digitsB[i] == 0)\r
+ {\r
+ return res - j;\r
+ }\r
+ }\r
+\r
+ return res;\r
+}\r
+\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-
-int solution(int A, int B);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+\r
+int solution(int A, int B);\r
+\r
+#endif // SOLUTION_H_\r
-#include "gtest/gtest.h"
-extern "C" {
-#include "solution.h"
-}
-
-TEST(SolutionTest, SomePosition)
-{
- int A = 53;
- int B = 1953786;
-
- EXPECT_EQ(2, solution(A,B));
-}
-
+#include "gtest/gtest.h"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+\r
+TEST(SolutionTest, SomePosition)\r
+{\r
+ int A = 53;\r
+ int B = 1953786;\r
+\r
+ EXPECT_EQ(2, solution(A,B));\r
+}\r
+\r
-cmake_minimum_required (VERSION 2.6)
-add_executable(solution5 solution.c solution.h unittest.cpp)
-add_test(solution5 solution5)
-target_link_libraries(solution5 gtest_main)
-add_custom_command(TARGET solution5 POST_BUILD COMMAND solution5)
+add_unittest(\r
+ TARGET solution5\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\r
+\r
-#include "solution.h"
-
-int solution(int A, int B)
-{
- int res = -1;
- int i, j, k, l;
- int digitsA[10] = { 0 };
- int digitsB[10] = { 0 };
- int digitsC[10] = { 0 };
-
- for (i = 0; i < 9; i++)
- {
- digitsA[9-i] = A % 10;
- A = A / 10;
- digitsB[9-i] = B % 10;
- B = B / 10;
- }
- j = 9;
- k = 9;
- for (i = 0; i < 9; i++)
- {
- if (digitsA[i] != 0 && j == 9)
- {
- j = i;
- }
- if (digitsB[i] != 0 && k == 9)
- {
- k = i;
- }
- }
- l = 0;
- for (i = 0; i < 9; i++)
- {
- if (j < 10)
- {
- digitsC[l++] = digitsA[j];
- j++;
- }
- if (k < 10)
- {
- digitsC[l++] = digitsB[k];
- k++;
- }
- }
- res = 0;
- for (i = 0; i < l; i++)
- {
- res *= 10;
- res += digitsC[i];
- }
-
- return res;
-}
-
+#include "solution.h"\r
+\r
+int solution(int A, int B)\r
+{\r
+ int res = -1;\r
+ int i, j, k, l;\r
+ int digitsA[10] = { 0 };\r
+ int digitsB[10] = { 0 };\r
+ int digitsC[10] = { 0 };\r
+\r
+ for (i = 0; i < 9; i++)\r
+ {\r
+ digitsA[9-i] = A % 10;\r
+ A = A / 10;\r
+ digitsB[9-i] = B % 10;\r
+ B = B / 10;\r
+ }\r
+ j = 9;\r
+ k = 9;\r
+ for (i = 0; i < 9; i++)\r
+ {\r
+ if (digitsA[i] != 0 && j == 9)\r
+ {\r
+ j = i;\r
+ }\r
+ if (digitsB[i] != 0 && k == 9)\r
+ {\r
+ k = i;\r
+ }\r
+ }\r
+ l = 0;\r
+ for (i = 0; i < 9; i++)\r
+ {\r
+ if (j < 10)\r
+ {\r
+ digitsC[l++] = digitsA[j];\r
+ j++;\r
+ }\r
+ if (k < 10)\r
+ {\r
+ digitsC[l++] = digitsB[k];\r
+ k++;\r
+ }\r
+ }\r
+ res = 0;\r
+ for (i = 0; i < l; i++)\r
+ {\r
+ res *= 10;\r
+ res += digitsC[i];\r
+ }\r
+\r
+ return res;\r
+}\r
+\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-
-int solution(int A, int B);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+\r
+int solution(int A, int B);\r
+\r
+#endif // SOLUTION_H_\r
-#include "gtest/gtest.h"
-extern "C" {
-#include "solution.h"
-}
-
-TEST(SolutionTest, ZipSome)
-{
- EXPECT_EQ(1526, solution(12,56));
- EXPECT_EQ(5162, solution(56,12));
- EXPECT_EQ(16273845, solution(12345,678));
- EXPECT_EQ(16273890, solution(123,67890));
- EXPECT_EQ(10234, solution(1234,0));
-}
+#include "gtest/gtest.h"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+\r
+TEST(SolutionTest, ZipSome)\r
+{\r
+ EXPECT_EQ(1526, solution(12,56));\r
+ EXPECT_EQ(5162, solution(56,12));\r
+ EXPECT_EQ(16273845, solution(12345,678));\r
+ EXPECT_EQ(16273890, solution(123,67890));\r
+ EXPECT_EQ(10234, solution(1234,0));\r
+}\r
-cmake_minimum_required (VERSION 2.6)
-add_executable(solution6 unittest.cpp solution.c solution.h)
-add_test(solution6 solution6)
-target_link_libraries(solution6 gtest_main)
-add_custom_command(TARGET solution6 POST_BUILD COMMAND solution6)
+add_unittest(\r
+ TARGET solution6\r
+ SRCS solution.c solution.h unittest.cpp\r
+ INCS .\r
+)\r
-#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];
-}
+#include "Solution.h"\r
+\r
+int solution(char *S)\r
+{\r
+ int stack[10];\r
+ int top = 0;\r
+ int i;\r
+ char *ch=S;\r
+ int num1;\r
+ int num2;\r
+ int digits[8];\r
+ int num_digits = 0;\r
+\r
+ while (*ch != 0)\r
+ {\r
+ if (*ch >= '0' && *ch <= '9')\r
+ {\r
+ digits[num_digits++]= *ch - '0';\r
+ }\r
+ else if (*ch == ' ' && num_digits)\r
+ {\r
+ num1 = 0;\r
+ for (i = 0; i < num_digits; i++)\r
+ {\r
+ num1 *= 10;\r
+ num1 += digits[i];\r
+ }\r
+ num_digits = 0;\r
+ stack[top++] = num1;\r
+ }\r
+ else if (*ch == 'D' && *(ch+1)=='U' && *(ch+2)=='P')\r
+ {\r
+ if (top < 1) return -1;\r
+ num1 = stack[top-1];\r
+ stack[top++] = num1;\r
+ }\r
+ else if (*ch == 'P' && *(ch+1)=='O' && *(ch+2)=='P')\r
+ {\r
+ if (top < 1) return -1;\r
+ num1 = stack[--top];\r
+ }\r
+ else if (*ch=='+')\r
+ {\r
+ if (top < 2) return -1;\r
+ num1 = stack[--top];\r
+ num2 = stack[--top];\r
+ stack[top++] = num1 + num2;\r
+ }\r
+ else if (*ch=='-')\r
+ {\r
+ if (top < 2) return -1;\r
+ num1 = stack[--top];\r
+ num2 = stack[--top];\r
+ if (num1 - num2 < 0) return -1;\r
+ stack[top++] = num1 - num2;\r
+ }\r
+ ch++;\r
+ }\r
+\r
+\r
+ return stack[top-1];\r
+}\r
-#ifndef SOLUTION_H_
-#define SOLUTION_H_
-
-int solution(char *S);
-
-#endif // SOLUTION_H_
+#ifndef SOLUTION_H_\r
+#define SOLUTION_H_\r
+\r
+int solution(char *S);\r
+\r
+#endif // SOLUTION_H_\r
-#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"\r
+extern "C" {\r
+#include "solution.h"\r
+}\r
+\r
+TEST(SolutionTest, SomeOperations)\r
+{\r
+ char S1[]= "13 DUP 4 POP 5 DUP + DUP + -";\r
+ char S2[] = "5 6 + -";\r
+// char S2[] ="3 DUP 5 - -";\r
+\r
+ EXPECT_EQ(7, solution(S1));\r
+ EXPECT_EQ(-1, solution(S2));\r
+}\r