diff --git a/modules/vinichuk_t_binary_search/CMakeLists.txt b/modules/vinichuk_t_binary_search/CMakeLists.txt new file mode 100644 index 000000000..f8f467bff --- /dev/null +++ b/modules/vinichuk_t_binary_search/CMakeLists.txt @@ -0,0 +1,13 @@ +# Declare variables for binaries' names +get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +set(MODULE "${DIR_NAME}") +set(LIBRARY "lib_${MODULE}") +set(TESTS "test_${MODULE}") + + +# Include directory with public headers +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +# Add all submodules +add_subdirectory(src) +add_subdirectory(test) diff --git a/modules/vinichuk_t_binary_search/include/BinarySearch.h b/modules/vinichuk_t_binary_search/include/BinarySearch.h new file mode 100644 index 000000000..94547f901 --- /dev/null +++ b/modules/vinichuk_t_binary_search/include/BinarySearch.h @@ -0,0 +1,28 @@ +// Copyright 2024 Vinichuk Timofey + +#ifndef MODULES_VINICHUK_T_BINARY_SEARCH_INCLUDE_BINARYSEARCH_H_ +#define MODULES_VINICHUK_T_BINARY_SEARCH_INCLUDE_BINARYSEARCH_H_ + +#include +#include + +// implementing a template function implies +// its definition in the place of declaration(header file) +template +int binary_search(const std::vector& arr, const T& target) { + int left = 0; + int right = arr.size() - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (arr[mid] == target) + return mid; + else if (arr[mid] < target) + left = mid + 1; + else + right = mid - 1; + } + + return -1; +} + +#endif // MODULES_VINICHUK_T_BINARY_SEARCH_INCLUDE_BINARYSEARCH_H_ diff --git a/modules/vinichuk_t_binary_search/src/BinarySearch.cpp b/modules/vinichuk_t_binary_search/src/BinarySearch.cpp new file mode 100644 index 000000000..1154fcf00 --- /dev/null +++ b/modules/vinichuk_t_binary_search/src/BinarySearch.cpp @@ -0,0 +1,2 @@ +// Copyright 2024 Vinichuk Timofey +#include "../include/BinarySearch.h" diff --git a/modules/vinichuk_t_binary_search/src/CMakeLists.txt b/modules/vinichuk_t_binary_search/src/CMakeLists.txt new file mode 100644 index 000000000..b1dc57b01 --- /dev/null +++ b/modules/vinichuk_t_binary_search/src/CMakeLists.txt @@ -0,0 +1,18 @@ +set(target ${LIBRARY}) + +file(GLOB srcs "*.cpp") +file(GLOB hdrs "../include/*.h") +set_source_files_properties(${srcs} ${hdrs} PROPERTIES + LABELS "${MODULE};Library") + +add_library(${target} STATIC ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + OUTPUT_NAME ${MODULE} + LABELS "${MODULE};Library") + +if (UNIX) + target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) +endif (UNIX) +target_link_libraries(${target} ${LIBRARY_DEPS}) + +set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE) diff --git a/modules/vinichuk_t_binary_search/test/CMakeLists.txt b/modules/vinichuk_t_binary_search/test/CMakeLists.txt new file mode 100644 index 000000000..cd06c57bb --- /dev/null +++ b/modules/vinichuk_t_binary_search/test/CMakeLists.txt @@ -0,0 +1,27 @@ +set(target ${TESTS}) + +file(GLOB srcs "*.cpp") +set_source_files_properties(${srcs} PROPERTIES + LABELS "${MODULE};Test") + +add_executable(${target} ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + LABELS "${MODULE};Test") + +if (UNIX) + target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread) +endif (UNIX) +target_link_libraries(${target} gtest ${LIBRARY}) + +# VS2012 doesn't support correctly the tuples yet, +# see http://code.google.com/p/googletest/issues/detail?id=412 +if(MSVC) + target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10) +endif() + +add_test( + NAME ${MODULE}_gtest + COMMAND ${target} +) +set_tests_properties (${MODULE}_gtest PROPERTIES + LABELS "${MODULE}") diff --git a/modules/vinichuk_t_binary_search/test/test_Vinichuk_Timofey_binary_search.cpp b/modules/vinichuk_t_binary_search/test/test_Vinichuk_Timofey_binary_search.cpp new file mode 100644 index 000000000..c2d9c94de --- /dev/null +++ b/modules/vinichuk_t_binary_search/test/test_Vinichuk_Timofey_binary_search.cpp @@ -0,0 +1,63 @@ +// Copyright 2024 Vinichuk Timofey + +#include +#include "../../../3rdparty/gtest/gtest.h" +#include "../include/BinarySearch.h" + +TEST(BinarySearchTest, test_find_middle) { + std::vector v = {1, 2, 3, 4, 5, 6, 7}; + ASSERT_EQ(binary_search(v, 4), 3); +} + +TEST(BinarySearchTest, test_not_found) { + std::vector v = { 1, 2, 3, 4, 5, 6, 7 }; + ASSERT_EQ(binary_search(v, 8), -1); +} + +TEST(BinarySearchTest, test_empty_array) { + std::vector v1 = { }; + std::vector v2 = { }; + + ASSERT_EQ(binary_search(v1, 8), -1); + ASSERT_EQ(binary_search(v2, 5.89), -1); +} + +TEST(BinarySearchTest, test_one_element_found) { + std::vector v1 = { 1 }; + std::vector v2 = { 2.005 }; + + ASSERT_EQ(binary_search(v1, 1), 0); + ASSERT_EQ(binary_search(v2, 2.005), 0); +} + +TEST(BinarySearchTest, test_one_element_not_found) { + std::vector v = { 1 }; + + ASSERT_EQ(binary_search(v, 2), -1); + ASSERT_EQ(binary_search(v, 10), -1); +} + +TEST(BinarySearchTest, test_large_array) { + std::vector v(1000); + std::iota(v.begin(), v.end(), 0); + + ASSERT_EQ(binary_search(v, 355), 355); + ASSERT_EQ(binary_search(v, 10), 10); + ASSERT_EQ(binary_search(v, 784), 784); +} + +TEST(BinarySearchTest, test_first_element) { + std::vector v1 = { 1, 4, 6, 59, 345 }; + std::vector v2 = { 'a', 'd', 'u', 'z'}; + + ASSERT_EQ(binary_search(v1, 1), 0); + ASSERT_EQ(binary_search(v2, 'a'), 0); +} + +TEST(BinarySearchTest, test_last_element) { + std::vector v1 = { 1, 4, 6, 59, 345 }; + std::vector v2 = { 'a', 'd', 'u', 'y', 'z'}; + + ASSERT_EQ(binary_search(v1, 345), 4); + ASSERT_EQ(binary_search(v2, 'z'), 4); +} diff --git a/modules/vinichuk_t_binary_search/test/test_main.cpp b/modules/vinichuk_t_binary_search/test/test_main.cpp new file mode 100644 index 000000000..ac310cc63 --- /dev/null +++ b/modules/vinichuk_t_binary_search/test/test_main.cpp @@ -0,0 +1,8 @@ +// Copyright 2024 Kashin Stepa + +#include + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}