diff --git a/.github/workflows/awesome_workflow.yml b/.github/workflows/awesome_workflow.yml index 473d7940663..44511cab361 100644 --- a/.github/workflows/awesome_workflow.yml +++ b/.github/workflows/awesome_workflow.yml @@ -53,6 +53,7 @@ jobs: needs: [MainSequence] permissions: pull-requests: write + issues: write strategy: matrix: os: [ubuntu-latest, windows-latest, macOS-latest] @@ -63,14 +64,31 @@ jobs: - run: | cmake -B ./build -S . cmake --build build --parallel 4 - - name: Label on PR fail - uses: actions/github-script@v6 - if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }} - with: - script: | - github.rest.issues.addLabels({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - labels: ['automated tests are failing'] - }) + + test: + name: Testing pull request + runs-on: ${{ matrix.os }} + needs: [build] + + permissions: + pull-requests: write + issues: write + + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + + steps: + - name: checkout + uses: actions/checkout@v4 + + - name: build + run: | + cmake -B build -S . -DBENABLE_TESTING=ON + cmake --build build --parallel 4 + + # The timeout below is to catch the files with user input in their main function. + # It should be removed eventually when all the files are clear of user input. + - name: test + run: | + ctest --test-dir build --timeout 5 --parallel 4 diff --git a/.gitignore b/.gitignore index c4951fe97b1..7556baacaca 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ a.out # Build build/ git_diff.txt + +# Testing +Testing/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fb339e475f..66aa3d6b2ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,15 @@ project(TheAlgorithms/C++ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) + +include(CTest) # for testing algorithms + +option(ENABLE_TESTING "flag to test the repo" OFF) +if(ENABLE_TESTING) + message(STATUS "Testing enabled") + enable_testing() +endif() + # Additional warnings and errors if(MSVC) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) @@ -20,13 +29,13 @@ endif() option(USE_OPENMP "flag to use OpenMP for multithreading" ON) if(USE_OPENMP) find_package(OpenMP 3.0 COMPONENTS CXX) - if (OpenMP_CXX_FOUND) + if(OpenMP_CXX_FOUND) message(STATUS "Building with OpenMP Multithreading.") else() message(STATUS "No OpenMP found, no multithreading.") endif() endif() - + add_subdirectory(backtracking) add_subdirectory(bit_manipulation) add_subdirectory(ciphers) @@ -56,7 +65,7 @@ cmake_policy(SET CMP0054 NEW) cmake_policy(SET CMP0057 NEW) find_package(Doxygen OPTIONAL_COMPONENTS dot dia) -if(DOXYGEN_FOUND) +if(DOXYGEN_FOUND) if(MSVC) set(DOXYGEN_CPP_CLI_SUPPORT YES) endif() @@ -64,11 +73,11 @@ if(DOXYGEN_FOUND) if(Doxygen_dot_FOUND) set(DOXYGEN_HAVE_DOT YES) endif() - + if(OPENMP_FOUND) set(DOXYGEN_PREDEFINED "_OPENMP=1") endif() - + if(GLUT_FOUND) set(DOXYGEN_PREDEFINED ${DOXYGEN_PREDEFINED} "GLUT_FOUND=1") endif() diff --git a/backtracking/CMakeLists.txt b/backtracking/CMakeLists.txt index f636edae503..0e1d73cc4a4 100644 --- a/backtracking/CMakeLists.txt +++ b/backtracking/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/backtracking") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/bit_manipulation/CMakeLists.txt b/bit_manipulation/CMakeLists.txt index f5e304cfbd7..c09625aff6c 100644 --- a/bit_manipulation/CMakeLists.txt +++ b/bit_manipulation/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/bit_manipulation") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/ciphers/CMakeLists.txt b/ciphers/CMakeLists.txt index 1efde308787..8a6d485740e 100644 --- a/ciphers/CMakeLists.txt +++ b/ciphers/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/ciphers") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/cpu_scheduling_algorithms/CMakeLists.txt b/cpu_scheduling_algorithms/CMakeLists.txt index ce93cef26e3..d84247f2699 100644 --- a/cpu_scheduling_algorithms/CMakeLists.txt +++ b/cpu_scheduling_algorithms/CMakeLists.txt @@ -2,15 +2,16 @@ # with full pathname. The RELATIVE flag makes it easier to extract an executable's name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) -foreach( testsourcefile ${APP_SOURCES} ) - string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp` - add_executable( ${testname} ${testsourcefile} ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) +foreach(testsourcefile ${APP_SOURCES}) + string(REPLACE ".cpp" "" testname ${testsourcefile}) # File type. Example: `.cpp` + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/cpu_scheduling_algorithms") # Folder name. Do NOT include `<>` -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/data_structures/CMakeLists.txt b/data_structures/CMakeLists.txt index 6c0555148be..51828d36e29 100644 --- a/data_structures/CMakeLists.txt +++ b/data_structures/CMakeLists.txt @@ -1,20 +1,21 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/data_structures") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) add_subdirectory(cll) diff --git a/data_structures/binary_search_tree.cpp b/data_structures/binary_search_tree.cpp index 86057c6c507..66aa49c182f 100644 --- a/data_structures/binary_search_tree.cpp +++ b/data_structures/binary_search_tree.cpp @@ -122,53 +122,8 @@ void Post(node *n) { } } -int main() { - queue.front = 0; - queue.rear = 0; - int value; - int ch; - node *root = new node; - std::cout << "\nEnter the value of root node :"; - std::cin >> value; - root->val = value; - root->left = NULL; - root->right = NULL; - do { - std::cout << "\n1. Insert" - << "\n2. Delete" - << "\n3. Breadth First" - << "\n4. Preorder Depth First" - << "\n5. Inorder Depth First" - << "\n6. Postorder Depth First"; - - std::cout << "\nEnter Your Choice : "; - std::cin >> ch; - int x; - switch (ch) { - case 1: - std::cout << "\nEnter the value to be Inserted : "; - std::cin >> x; - Insert(root, x); - break; - case 2: - std::cout << "\nEnter the value to be Deleted : "; - std::cin >> x; - Remove(root, root, x); - break; - case 3: - BFT(root); - break; - case 4: - Pre(root); - break; - case 5: - In(root); - break; - case 6: - Post(root); - break; - } - } while (ch != 0); - - return 0; +static void tests() { + // TODO: add tests } + +int main() { return 0; } diff --git a/divide_and_conquer/CMakeLists.txt b/divide_and_conquer/CMakeLists.txt index 4d6204325cb..ba6fc7abda6 100644 --- a/divide_and_conquer/CMakeLists.txt +++ b/divide_and_conquer/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/divide_and_conquer") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/dynamic_programming/CMakeLists.txt b/dynamic_programming/CMakeLists.txt index bcf0a990013..0e6efe6bc0b 100644 --- a/dynamic_programming/CMakeLists.txt +++ b/dynamic_programming/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/dynamic_programming") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/geometry/CMakeLists.txt b/geometry/CMakeLists.txt index 72655169aab..860d9a24621 100644 --- a/geometry/CMakeLists.txt +++ b/geometry/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/geometry") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/graph/CMakeLists.txt b/graph/CMakeLists.txt index 7f6ea0e69f5..23a9539e9b1 100644 --- a/graph/CMakeLists.txt +++ b/graph/CMakeLists.txt @@ -4,10 +4,10 @@ file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) #file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX @@ -15,6 +15,7 @@ foreach( testsourcefile ${APP_SOURCES} ) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/graph") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index 23ffcfae179..2a7c831b843 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -4,17 +4,17 @@ if(OpenGL_FOUND) if(NOT GLUT_FOUND) message("FreeGLUT library will be downloaded and built.") include(ExternalProject) - ExternalProject_Add ( + ExternalProject_Add( FREEGLUT-PRJ URL https://github.com/FreeGLUTProject/freeglut/releases/download/v3.4.0/freeglut-3.4.0.tar.gz URL_MD5 f1621464e6525d0368976870cab8f418 CMAKE_GENERATOR ${CMAKE_GENERATOR} CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET} CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM} - CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release - -DFREEGLUT_BUILD_SHARED_LIBS=OFF - -DFREEGLUT_BUILD_STATIC_LIBS=ON - -DFREEGLUT_BUILD_DEMOS=OFF + CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release + -DFREEGLUT_BUILD_SHARED_LIBS=OFF + -DFREEGLUT_BUILD_STATIC_LIBS=ON + -DFREEGLUT_BUILD_DEMOS=OFF PREFIX ${CMAKE_CURRENT_BINARY_DIR}/freeglut # BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/freeglut-build # BUILD_IN_SOURCE ON @@ -44,19 +44,19 @@ endif(OpenGL_FOUND) # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_CXX) endif() - + if(OpenGL_FOUND) if(NOT GLUT_FOUND) add_dependencies(${testname} FREEGLUT-PRJ) @@ -73,11 +73,11 @@ foreach( testsourcefile ${APP_SOURCES} ) endif() target_compile_definitions(${testname} PRIVATE USE_GLUT) endif(OpenGL_FOUND) - + if(APPLE) target_compile_options(${testname} PRIVATE -Wno-deprecated) endif(APPLE) - + install(TARGETS ${testname} DESTINATION "bin/graphics") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/greedy_algorithms/CMakeLists.txt b/greedy_algorithms/CMakeLists.txt index bd45da6d4e9..fa5786b3738 100644 --- a/greedy_algorithms/CMakeLists.txt +++ b/greedy_algorithms/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/greedy_algorithms") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/hashing/CMakeLists.txt b/hashing/CMakeLists.txt index d8bad16c172..d54fd38ea66 100644 --- a/hashing/CMakeLists.txt +++ b/hashing/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/hash") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/machine_learning/CMakeLists.txt b/machine_learning/CMakeLists.txt index e6d8a9af405..76946fcd242 100644 --- a/machine_learning/CMakeLists.txt +++ b/machine_learning/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/machine_learning") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/math/CMakeLists.txt b/math/CMakeLists.txt index 2b70b2d3101..9be975b3f51 100644 --- a/math/CMakeLists.txt +++ b/math/CMakeLists.txt @@ -1,18 +1,18 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) - + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/math") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/numerical_methods/CMakeLists.txt b/numerical_methods/CMakeLists.txt index fc4c343de30..0dddab2cfb8 100644 --- a/numerical_methods/CMakeLists.txt +++ b/numerical_methods/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/numerical_methods") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/operations_on_datastructures/CMakeLists.txt b/operations_on_datastructures/CMakeLists.txt index 09119bc4300..2377d5d9f7f 100644 --- a/operations_on_datastructures/CMakeLists.txt +++ b/operations_on_datastructures/CMakeLists.txt @@ -1,15 +1,16 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/operations_on_datastructures") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/others/CMakeLists.txt b/others/CMakeLists.txt index f049b5f2d31..c83dc499da3 100644 --- a/others/CMakeLists.txt +++ b/others/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/others") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/others/decimal_to_hexadecimal.cpp b/others/decimal_to_hexadecimal.cpp index a3e544f49f8..1cc551a1218 100644 --- a/others/decimal_to_hexadecimal.cpp +++ b/others/decimal_to_hexadecimal.cpp @@ -3,32 +3,46 @@ * @brief Convert decimal number to hexadecimal representation */ -#include +#include +#include +#include +#include /** - * Main program + * @brief converts a given decimal integer value to its equavalent hexadecimal + * string + * + * @param value the value to be converted + * @return std::string the hexadecimal string */ -int main(void) { - int valueToConvert = 0; // Holds user input - int hexArray[8]; // Contains hex values backwards - int i = 0; // counter - char HexValues[] = "0123456789ABCDEF"; - - std::cout << "Enter a Decimal Value" - << std::endl; // Displays request to stdout - std::cin >> - valueToConvert; // Stores value into valueToConvert via user input +std::string decimal_to_hexadecimal(std::uint32_t value) { + std::vector hexArray{}; + std::string ret = ""; + size_t i = 0; + const char HexValues[] = "0123456789ABCDEF"; - while (valueToConvert > 15) { // Dec to Hex Algorithm - hexArray[i++] = valueToConvert % 16; // Gets remainder - valueToConvert /= 16; - // valueToConvert >>= 4; // This will divide by 2^4=16 and is faster + while (value > 15) { + hexArray.at(i++) = value % 16; + value /= 16; } - hexArray[i] = valueToConvert; // Gets last value + while (i >= 0) ret.push_back(HexValues[i--]); + return ret; +} - std::cout << "Hex Value: "; - while (i >= 0) std::cout << HexValues[hexArray[i--]]; +/** + * @brief self test implementation + * @returns void + */ +static void tests() { + assert(decimal_to_hexadecimal(0) == "0"); + assert(decimal_to_hexadecimal(1) == "1"); + assert(decimal_to_hexadecimal(10) == "A"); +} - std::cout << std::endl; +/** + * Main program + */ +int main(void) { + tests(); // run self test imlementation return 0; } diff --git a/physics/CMakeLists.txt b/physics/CMakeLists.txt index 4d33e8eec4c..d8b52961644 100644 --- a/physics/CMakeLists.txt +++ b/physics/CMakeLists.txt @@ -2,15 +2,16 @@ # with full pathname. The RELATIVE flag makes it easier to extract an executable's name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) -foreach( testsourcefile ${APP_SOURCES} ) - string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp` - add_executable( ${testname} ${testsourcefile} ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) +foreach(testsourcefile ${APP_SOURCES}) + string(REPLACE ".cpp" "" testname ${testsourcefile}) # File type. Example: `.cpp` + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/physics") # Folder name. Do NOT include `<>` -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/probability/CMakeLists.txt b/probability/CMakeLists.txt index 0e0b7b7f04f..e90d82f071e 100644 --- a/probability/CMakeLists.txt +++ b/probability/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/probability") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/range_queries/CMakeLists.txt b/range_queries/CMakeLists.txt index c9f0c86f0a8..9fe5e5ffab1 100644 --- a/range_queries/CMakeLists.txt +++ b/range_queries/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/range_queries") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/search/CMakeLists.txt b/search/CMakeLists.txt index 5fd1ae59113..3e9a16246f2 100644 --- a/search/CMakeLists.txt +++ b/search/CMakeLists.txt @@ -1,18 +1,19 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/search") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/sorting/CMakeLists.txt b/sorting/CMakeLists.txt index 00877bb613a..58117c40573 100644 --- a/sorting/CMakeLists.txt +++ b/sorting/CMakeLists.txt @@ -1,13 +1,13 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX @@ -15,6 +15,8 @@ foreach( testsourcefile ${APP_SOURCES} ) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/sorting") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES}) diff --git a/strings/CMakeLists.txt b/strings/CMakeLists.txt index 3c15695cc6f..ec3be53858a 100644 --- a/strings/CMakeLists.txt +++ b/strings/CMakeLists.txt @@ -1,18 +1,20 @@ # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. -file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp ) +file(GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) -foreach( testsourcefile ${APP_SOURCES} ) +foreach(testsourcefile ${APP_SOURCES}) # I used a simple string replace, to cut off .cpp. - string( REPLACE ".cpp" "" testname ${testsourcefile} ) - add_executable( ${testname} ${testsourcefile} ) + string(REPLACE ".cpp" "" testname ${testsourcefile}) + add_executable(${testname} ${testsourcefile}) set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX) if(OpenMP_CXX_FOUND) target_link_libraries(${testname} OpenMP::OpenMP_CXX) endif() + + add_test(${testname} ${testname}) install(TARGETS ${testname} DESTINATION "bin/strings") -endforeach( testsourcefile ${APP_SOURCES} ) +endforeach(testsourcefile ${APP_SOURCES})