Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
V-inTim committed Apr 13, 2024
1 parent cc2c6c7 commit f701466
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/vinichuk_t_binary_search/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions modules/vinichuk_t_binary_search/include/BinarySearch.h
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>

// implementing a template function implies
// its definition in the place of declaration(header file)
template <typename T>
int binary_search(const std::vector<T>& 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_
2 changes: 2 additions & 0 deletions modules/vinichuk_t_binary_search/src/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright 2024 Vinichuk Timofey
#include "../include/BinarySearch.h"
18 changes: 18 additions & 0 deletions modules/vinichuk_t_binary_search/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions modules/vinichuk_t_binary_search/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 Vinichuk Timofey

#include <numeric>
#include "../../../3rdparty/gtest/gtest.h"
#include "../include/BinarySearch.h"

TEST(BinarySearchTest, test_find_middle) {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
ASSERT_EQ(binary_search(v, 4), 3);
}

TEST(BinarySearchTest, test_not_found) {
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7 };
ASSERT_EQ(binary_search(v, 8), -1);
}

TEST(BinarySearchTest, test_empty_array) {
std::vector<int> v1 = { };
std::vector<double> v2 = { };

ASSERT_EQ(binary_search(v1, 8), -1);
ASSERT_EQ(binary_search(v2, 5.89), -1);
}

TEST(BinarySearchTest, test_one_element_found) {
std::vector<int> v1 = { 1 };
std::vector<double> 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<int> v = { 1 };

ASSERT_EQ(binary_search(v, 2), -1);
ASSERT_EQ(binary_search(v, 10), -1);
}

TEST(BinarySearchTest, test_large_array) {
std::vector<int> 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<int> v1 = { 1, 4, 6, 59, 345 };
std::vector<char> 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<int> v1 = { 1, 4, 6, 59, 345 };
std::vector<char> v2 = { 'a', 'd', 'u', 'y', 'z'};

ASSERT_EQ(binary_search(v1, 345), 4);
ASSERT_EQ(binary_search(v2, 'z'), 4);
}
8 changes: 8 additions & 0 deletions modules/vinichuk_t_binary_search/test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Kashin Stepa

#include <gtest/gtest.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit f701466

Please sign in to comment.