Skip to content

Commit 7e2e3bf

Browse files
committed
fix: diff constraints test
1 parent d844a8c commit 7e2e3bf

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

Headers/0003_Graph/0012_DifferenceConstraintsShortestPaths.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace DifferenceConstraintsShortestPaths
2727
class Graph
2828
{
2929
private:
30-
Node* startingNode;
3130
map<Node*, vector<Node*>> _adjlist;
3231
map<string, Node*> _nodeMap;
3332
vector<Edge*> _edgeList;
@@ -39,6 +38,6 @@ namespace DifferenceConstraintsShortestPaths
3938
public:
4039
void PushAllDirectedEdges(vector<vector<int>> vectorA, vector<string> vectorX, vector<int> vectorB);
4140
bool FindDifferenceConstraintsSolutionBellmanFord();
42-
vector<int> GetDifferenceConstrtaintsSolution();
41+
vector<pair<string, int>> GetDifferenceConstrtaintsSolution();
4342
};
4443
}

SourceCodes/0003_Graph/0012_DifferenceConstraintsShortestPaths.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ namespace DifferenceConstraintsShortestPaths
118118
return true;
119119
}
120120

121-
vector<int> Graph::GetDifferenceConstrtaintsSolution()
121+
vector<pair<string, int>> Graph::GetDifferenceConstrtaintsSolution()
122122
{
123-
vector<int> result;
123+
vector<pair<string, int>> result;
124124
for (auto& node : this->_nodeMap)
125125
{
126126
if (node.second->data != "")
127127
{
128-
result.push_back(node.second->distance);
128+
result.push_back({ node.second->data, node.second->distance });
129129
}
130130
}
131131
return result;

Tests/0000_CommonUtilities/UnitTestHelper.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class UnitTestHelper
7373
return result;
7474
}
7575

76-
7776
// This helper method is used to sort the vector of vectors of a particular typename.
7877
// Each inner vector is sorted first.
7978
// Then each of them are sorted by their first element, in increasing order.
@@ -158,4 +157,27 @@ class UnitTestHelper
158157
});
159158
return data;
160159
}
160+
161+
template<typename T1, typename T2>
162+
string SortVectorOfPairAndSerialize(vector<pair<T1, T2>> data)
163+
{
164+
// Sorting the vector in non-decreasing order on typename T1
165+
sort(data.begin(), data.end(), [](const pair<T1, T2>& pair1, const pair<T1, T2>& pair2)
166+
{
167+
return pair1.first < pair2.first;
168+
});
169+
170+
// Serializing the vector to string
171+
string result = "";
172+
for (auto& iterator : data)
173+
{
174+
result += iterator.first + "(" + to_string(iterator.second) + ")" + " ";
175+
}
176+
177+
if (!result.empty())
178+
{
179+
result.pop_back();
180+
}
181+
return result;
182+
}
161183
};

Tests/0003_Graph/0012_DifferenceConstraintsShortestPathsTest.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ namespace DifferenceConstraintsShortestPaths
2424
};
2525
vector<string> vectorX = { "v1", "v2", "v3", "v4", "v5" };
2626
vector<int> vectorB = {0, -1, 1, 5, 4, -1, -3, -3};
27-
string expectedSolution = "-5 -3 0 -1 -4";
27+
vector<pair<string, int>> expectedSolution =
28+
{
29+
{"v2", -3},
30+
{"v5", -4},
31+
{"v1", -5},
32+
{"v4", -1},
33+
{"v3", 0},
34+
};
2835
graph.PushAllDirectedEdges(vectorA, vectorX, vectorB);
2936

3037
ASSERT_TRUE(graph.FindDifferenceConstraintsSolutionBellmanFord());
31-
ASSERT_EQ(unitTestHelper.SerializeVectorToString(graph.GetDifferenceConstrtaintsSolution()), expectedSolution);
38+
ASSERT_EQ(unitTestHelper.SortVectorOfPairAndSerialize(graph.GetDifferenceConstrtaintsSolution()), unitTestHelper.SortVectorOfPairAndSerialize(expectedSolution));
3239
}
3340
}

0 commit comments

Comments
 (0)