TestBinary.cxx File Reference
#include <gtest/gtest.h>
#include <binary.hxx>
Include dependency graph for TestBinary.cxx:

Go to the source code of this file.

Functions

 TEST (TestSearch, BinarySearchBasics)
 
 TEST (TestSearch, BinarySearchDoubles)
 

Function Documentation

TEST ( TestSearch  ,
BinarySearchBasics   
)

Definition at line 55 of file TestBinary.cxx.

56 {
57  Container sortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
58 
59  // Empty array
60  {
61  Container emptyArray = Container();
62  const auto index = BinarySearch<IT, EQUAL<int>>(emptyArray.begin(), emptyArray.end(), 0);
63  EXPECT_EQ(-1, index); // Should return -1 on empty array
64  }
65 
66  // First element
67  {
68  const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), -3);
69  EXPECT_EQ(0, index);
70  }
71 
72  // Existing random value
73  {
74  const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), 8);
75  EXPECT_EQ(4, index);
76  }
77 
78  // Non-existing element
79  {
80  const auto index = BinarySearch<IT, EQUAL<int>>(sortedArray.begin(), sortedArray.end(), 1);
81  EXPECT_EQ(-1, index);
82  }
83 
84  // String collection - Find letter
85  {
86  const auto index = BinarySearch<std::string::const_iterator, EQUAL<char>>
87  (OrderedStr.begin(), OrderedStr.end(), 'm');
88  EXPECT_EQ(4, index);
89  }
90 }
TEST ( TestSearch  ,
BinarySearchDoubles   
)

Definition at line 93 of file TestBinary.cxx.

94 {
95  std::vector<double> sortedDoubleArray
96  (SortedDoubleArray, SortedDoubleArray + sizeof(SortedDoubleArray) / sizeof(double));
97 
98  // First element
99  {
100  const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
101  (sortedDoubleArray.begin(), sortedDoubleArray.end(), static_cast<const double>(-.3));
102  EXPECT_EQ(0, index);
103  }
104 
105  // Existing element
106  {
107  const auto index = BinarySearch<IT_DL, EQUIVALENT<double>>
108  (sortedDoubleArray.begin(), sortedDoubleArray.end(), static_cast<const double>(0.12));
109  EXPECT_EQ(2, index);
110  }
111 
112  // Non Existing element
113  {
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);
117  }
118 
119  // Value in the middle when identical values
120  {
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.));
124  EXPECT_EQ(5, index);
125  }
126 }