forked from UNN-ITMM-Software/devtools-course-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright 2024 Vinichuk Timofey | ||
#include "../include/BinarySearch.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
63 changes: 63 additions & 0 deletions
63
modules/vinichuk_t_binary_search/test/test_Vinichuk_Timofey_binary_search.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |