TestMaxDistance.cxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * HUC - Hurna Core
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/Hurna/Hurna-Core/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #include <gtest/gtest.h>
21 #include <max_distance.hxx>
22 
23 // STD includes
24 #include <functional>
25 
26 // Testing namespace
27 using namespace huc::search;
28 
29 #ifndef DOXYGEN_SKIP
30 namespace {
31  const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366}; // Simple sorted array of integers with negative values
32  const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5}; // Simple random array of integers with negative values
33  const std::string RandomStr = "xacvgeze"; // Random string
34 
35  typedef std::vector<int> Container;
36  typedef Container::iterator IT;
37 }
38 #endif /* DOXYGEN_SKIP */
39 
40 // Test SimpleStockMarketProblem on a simple market array
41 TEST(TestSearch, MaxDistances)
42 {
43  // Should return <4,9> (largest benefice of 6)
44  {
45  Container marketPrices(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
46  const auto maxBeneficeIndexes = MaxDistance<IT>(marketPrices.begin(), marketPrices.end());
47  EXPECT_EQ(4, maxBeneficeIndexes.first);
48  EXPECT_EQ(9, maxBeneficeIndexes.second);
49  }
50 
51  // Should return <0, idxEnd> on sorted array
52  {
53  Container sortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
54  const auto indexes = MaxDistance<IT>(sortedArray.begin(), sortedArray.end());
55  EXPECT_EQ(0, indexes.first);
56  EXPECT_EQ(static_cast<int>(sortedArray.size()) - 1, indexes.second);
57  }
58 
59  // Should return <-1,-1> on insufficient array
60  {
61  Container insufficientArray = Container(1, 2);
62  const auto indexes = MaxDistance<IT>(insufficientArray.begin(), insufficientArray.end());
63  EXPECT_EQ(-1, indexes.first);
64  EXPECT_EQ(-1, indexes.second);
65  }
66 
67  // Should return <0,1> on array containing only two elements
68  {
69  Container twoElementArray = Container(2, 2);
70  const auto indexes = MaxDistance<IT>(twoElementArray.begin(), twoElementArray.end());
71  EXPECT_EQ(0, indexes.first);
72  EXPECT_EQ(1, indexes.second);
73  }
74 
75  // Should return <0,1> on array containing the same value
76  {
77  Container sameElementArray = Container(10, 2);
78  const auto indexes = MaxDistance<IT>(sameElementArray.begin(), sameElementArray.end());
79  EXPECT_EQ(0, indexes.first);
80  EXPECT_EQ(1, indexes.second);
81  }
82 
83  // String - should return <1,6> as 'a', 'z' are the most distanced letter
84  {
85  const auto indexes =
86  MaxDistance<std::string::const_iterator, std::minus<char>>(RandomStr.begin(), RandomStr.end());
87  EXPECT_EQ(1, indexes.first);
88  EXPECT_EQ(6, indexes.second);
89  }
90 }
TEST(TestSearch, MaxDistances)