hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxReduceTaskTest.cpp
Go to the documentation of this file.
1 /*
2  * SphinxReduceTaskTest.cpp
3  *
4  * Created on: May 2, 2013
5  * Author: igor
6  */
7 
8 #include <gtest/gtest.h>
9 #include "SphinxReduceTask.hpp"
10 #include "SphinxReduceResult.hpp"
11 #include <vector>
12 
13 using namespace std;
14 using namespace Poco;
15 using namespace HCE::sphinx;
16 using namespace HCE::sphinx::reduce_task;
17 
18 TEST(SphinxReduceTask, emptyDataStorage)
19 {
20  SphinxReduceTask sphinxReduceTask;
21  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult());
22 
23 
24  sphinxReduceTask.fillReduceResult(sphinxReduceResult);
25 
26  ASSERT_TRUE(sphinxReduceResult->hasNext() == false);
27 }
28 
29 
30 class SphinxReduceTaskTest: public ::testing::Test{
31  protected:
32  virtual void SetUp(){
33  idWeightResult.push_back(make_pair("13", "12345"));
34  idWeightResult.push_back(make_pair("13", "2567891"));
35  idWeightResult.push_back(make_pair("21", "1122334"));
36  idWeightResult.push_back(make_pair("221", "99123453"));
37  idWeightResult.push_back(make_pair("221", "5525"));
38  }
39 
40 
41  bool isFindExpectData(string &expectDocId, string &expectWeight, vector<int>& bestResIndex){
42  for(size_t index = 0; index < bestResIndex.size(); ++index){
43  if (expectDocId == idWeightResult[bestResIndex[index]].first &&
44  expectWeight == idWeightResult[bestResIndex[index]].second){
45  return true;
46  }
47  }
48  return false;
49  }
50 
51  vector<pair<string, string> > idWeightResult;
52  string expectId;
53  string expectWeight;
54 };
55 
56 
58 {
59  SphinxReduceTask sphinxReduceTask;
60  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult());
61 
62  sphinxReduceTask.addKeyValue(idWeightResult[0].first, idWeightResult[0].second);
63  sphinxReduceTask.fillReduceResult(sphinxReduceResult);
64 
65  sphinxReduceResult->reset();
66  ASSERT_TRUE(sphinxReduceResult->hasNext());
67  sphinxReduceResult->getNext(expectId, expectWeight);
68 
69  EXPECT_EQ(expectId, idWeightResult[0].first);
70  EXPECT_EQ(expectWeight, idWeightResult[0].second);
71 
72  ASSERT_TRUE(sphinxReduceResult->hasNext() == false);
73 }
74 
75 
76 TEST_F(SphinxReduceTaskTest, equalDocIdsSecondGreat)
77 {
78  SphinxReduceTask sphinxReduceTask;
79  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult());
80 
81  sphinxReduceTask.addKeyValue(idWeightResult[0].first, idWeightResult[0].second);
82  sphinxReduceTask.addKeyValue(idWeightResult[1].first, idWeightResult[1].second);
83  sphinxReduceTask.fillReduceResult(sphinxReduceResult);
84 
85  sphinxReduceResult->reset();
86  ASSERT_TRUE(sphinxReduceResult->hasNext());
87  sphinxReduceResult->getNext(expectId, expectWeight);
88 
89  EXPECT_EQ(expectId, idWeightResult[1].first);
90  EXPECT_EQ(expectWeight, idWeightResult[1].second);
91 
92  ASSERT_TRUE(sphinxReduceResult->hasNext() == false);
93 }
94 
95 
96 TEST_F(SphinxReduceTaskTest, equalDocIdsSecondLess)
97 {
98  SphinxReduceTask sphinxReduceTask;
99  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult());
100 
101  sphinxReduceTask.addKeyValue(idWeightResult[3].first, idWeightResult[3].second);
102  sphinxReduceTask.addKeyValue(idWeightResult[4].first, idWeightResult[4].second);
103  sphinxReduceTask.fillReduceResult(sphinxReduceResult);
104 
105  sphinxReduceResult->reset();
106  ASSERT_TRUE(sphinxReduceResult->hasNext());
107  sphinxReduceResult->getNext(expectId, expectWeight);
108 
109  EXPECT_EQ(expectId, idWeightResult[3].first);
110  EXPECT_EQ(expectWeight, idWeightResult[3].second);
111 
112  ASSERT_TRUE(sphinxReduceResult->hasNext() == false);
113 }
114 
115 
117 {
118  SphinxReduceTask sphinxReduceTask;
119  SharedPtr<SphinxReduceResult>sphinxReduceResult(new SphinxReduceResult());
120 
121  for (size_t resIndex = 0; resIndex < idWeightResult.size(); ++resIndex){
122  sphinxReduceTask.addKeyValue(idWeightResult[resIndex].first,
123  idWeightResult[resIndex].second);
124  }
125  vector<int>bestResIndex = {1, 2, 3};
126 
127  sphinxReduceTask.fillReduceResult(sphinxReduceResult);
128 
129  sphinxReduceResult->reset();
130  int docNumber = 0;
131 
132  while (sphinxReduceResult->hasNext()){
133  sphinxReduceResult->getNext(expectId, expectWeight);
134  EXPECT_TRUE(isFindExpectData(expectId, expectWeight, bestResIndex));
135  docNumber++;
136  }
137 
138  ASSERT_EQ(docNumber, 3);
139 }