20 #include <gtest/gtest.h> 29 const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
31 const double SortedDoubleArray[] = {-.3, 0.0, 0.12, 2.5, 8};
33 const std::string OrderedStr =
"acegmnoop";
35 typedef std::vector<int> Container;
36 typedef Container::iterator IT;
37 typedef std::vector<double>::iterator IT_DL;
42 bool operator()(
const T& a,
const T& b)
const 43 {
return std::abs(a - b) < std::numeric_limits<T>::epsilon(); }
49 bool operator()(
const T& a,
const T& b)
const {
return a == b; }
55 TEST(TestSearch, BinarySearchBasics)
57 Container sortedArray(SortedArrayInt, SortedArrayInt +
sizeof(SortedArrayInt) /
sizeof(
int));
61 Container emptyArray = Container();
62 const auto index = BinarySearch<IT, EQUAL<int>>(emptyArray.begin(), emptyArray.end(), 0);
68 const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), -3);
74 const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), 8);
80 const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), 1);
86 const auto index = BinarySearch<std::string::const_iterator, EQUAL<char>>
87 (OrderedStr.begin(), OrderedStr.end(),
'm');
93 TEST(TestSearch, BinarySearchDoubles)
95 std::vector<double> sortedDoubleArray
96 (SortedDoubleArray, SortedDoubleArray +
sizeof(SortedDoubleArray) /
sizeof(
double));
100 const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
101 (sortedDoubleArray.begin(), sortedDoubleArray.end(),
static_cast<const double>(-.3));
107 const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
108 (sortedDoubleArray.begin(), sortedDoubleArray.end(),
static_cast<const double>(0.12));
114 const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
115 (sortedDoubleArray.begin(), sortedDoubleArray.end(),
static_cast<const double>(8.1));
116 EXPECT_EQ(-1, index);
121 std::vector<double> identicalArray = std::vector<double>(10, 3.);
122 const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
123 (identicalArray.begin(), identicalArray.end(),
static_cast<const double>(3.));
TEST(TestSearch, BinarySearchBasics)