//////////////////////////////////////////////////////////////////////////////// /// @brief test suite for ShortestPathPriorityQueue /// /// @file /// /// DISCLAIMER /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is ArangoDB GmbH, Cologne, Germany /// /// @author Max Neunhoeffer /// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// #include "Basics/Common.h" #include "gtest/gtest.h" #include "Basics/voc-errors.h" #include "Graph/ShortestPathPriorityQueue.h" using namespace std; // ----------------------------------------------------------------------------- // --SECTION-- test suite // ----------------------------------------------------------------------------- struct MyValue { std::string _key; unsigned int _weight; unsigned int weight() const { return _weight; } std::string const& getKey() const { return _key; } void setWeight(unsigned int const w) { _weight = w; } MyValue(std::string k, unsigned int w) : _key(k), _weight(w) {} }; TEST(CPriorityQueueTest, tst_deque_case) { arangodb::graph::ShortestPathPriorityQueue pq; EXPECT_TRUE(0 == (int)pq.size()); EXPECT_TRUE(true == pq.empty()); bool b; MyValue* v = nullptr; b = pq.insert("a", std::make_unique("a", 1)); EXPECT_TRUE(b == true); b = pq.insert("b", std::make_unique("b", 2)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 2)); EXPECT_TRUE(b == true); b = pq.insert("d", std::make_unique("d", 4)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 5)); EXPECT_TRUE(b == false); EXPECT_TRUE(4 == (int)pq.size()); EXPECT_TRUE(false == pq.empty()); MyValue const* p; p = pq.find("a"); EXPECT_TRUE((int)p->_weight == 1); p = pq.find("b"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("c"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("d"); EXPECT_TRUE((int)p->_weight == 4); p = pq.find("abc"); EXPECT_TRUE(p == nullptr); std::string k; p = pq.getMinimal(); EXPECT_TRUE(p->_key == "a"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "a"); ASSERT_TRUE(v != nullptr); EXPECT_TRUE(v->_key == "a"); EXPECT_TRUE((int)v->_weight == 1); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "b"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "b"); EXPECT_TRUE(v->_key == "b"); EXPECT_TRUE((int)v->_weight == 2); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "c"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "c"); EXPECT_TRUE(v->_key == "c"); EXPECT_TRUE((int)v->_weight == 2); EXPECT_TRUE((int)pq.size() == 1); EXPECT_TRUE(pq.empty() == false); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "d"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "d"); EXPECT_TRUE(v->_key == "d"); EXPECT_TRUE((int)v->_weight == 4); EXPECT_TRUE((int)pq.size() == 0); EXPECT_TRUE(pq.empty() == true); p = pq.getMinimal(); EXPECT_TRUE(p == nullptr); b = pq.popMinimal(k, v); EXPECT_TRUE(b == false); } //////////////////////////////////////////////////////////////////////////////// /// @brief test filling in random weight order //////////////////////////////////////////////////////////////////////////////// TEST(CPriorityQueueTest, tst_heap_case) { arangodb::graph::ShortestPathPriorityQueue pq; EXPECT_TRUE(0 == (int)pq.size()); EXPECT_TRUE(true == pq.empty()); bool b; MyValue* v = nullptr; b = pq.insert("a", std::make_unique("a", 4)); EXPECT_TRUE(b == true); b = pq.insert("b", std::make_unique("b", 1)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 2)); EXPECT_TRUE(b == true); b = pq.insert("d", std::make_unique("d", 2)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 5)); EXPECT_TRUE(b == false); EXPECT_TRUE(4 == (int)pq.size()); EXPECT_TRUE(false == pq.empty()); MyValue const* p; p = pq.find("a"); EXPECT_TRUE((int)p->_weight == 4); p = pq.find("b"); EXPECT_TRUE((int)p->_weight == 1); p = pq.find("c"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("d"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("abc"); EXPECT_TRUE(p == nullptr); std::string k; p = pq.getMinimal(); EXPECT_TRUE(p->_key == "b"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "b"); EXPECT_TRUE(v->_key == "b"); EXPECT_TRUE((int)v->_weight == 1); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "d"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "d"); EXPECT_TRUE(v->_key == "d"); EXPECT_TRUE((int)v->_weight == 2); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "c"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "c"); EXPECT_TRUE(v->_key == "c"); EXPECT_TRUE((int)v->_weight == 2); EXPECT_TRUE((int)pq.size() == 1); EXPECT_TRUE(pq.empty() == false); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "a"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "a"); EXPECT_TRUE(v->_key == "a"); EXPECT_TRUE((int)v->_weight == 4); EXPECT_TRUE((int)pq.size() == 0); EXPECT_TRUE(pq.empty() == true); p = pq.getMinimal(); EXPECT_TRUE(p == nullptr); b = pq.popMinimal(k, v); EXPECT_TRUE(b == false); } //////////////////////////////////////////////////////////////////////////////// /// @brief test filling in ascending weight order, but then doing lowerWeight //////////////////////////////////////////////////////////////////////////////// TEST(CPriorityQueueTest, tst_deque_case_with_lowering) { arangodb::graph::ShortestPathPriorityQueue pq; EXPECT_TRUE(0 == (int)pq.size()); EXPECT_TRUE(true == pq.empty()); bool b; MyValue* v = nullptr; b = pq.insert("a", std::make_unique("a", 1)); EXPECT_TRUE(b == true); b = pq.insert("b", std::make_unique("b", 2)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 2)); EXPECT_TRUE(b == true); b = pq.insert("d", std::make_unique("d", 4)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 5)); EXPECT_TRUE(b == false); EXPECT_TRUE(4 == (int)pq.size()); EXPECT_TRUE(false == pq.empty()); pq.lowerWeight("d", 1); // This moves "d" before "b" and "c" MyValue const* p; p = pq.find("a"); EXPECT_TRUE((int)p->_weight == 1); p = pq.find("b"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("c"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("d"); EXPECT_TRUE((int)p->_weight == 1); p = pq.find("abc"); EXPECT_TRUE(p == nullptr); std::string k; p = pq.getMinimal(); EXPECT_TRUE(p->_key == "a"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "a"); EXPECT_TRUE(v->_key == "a"); EXPECT_TRUE((int)v->_weight == 1); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "d"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "d"); EXPECT_TRUE(v->_key == "d"); EXPECT_TRUE((int)v->_weight == 1); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "c"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "c"); EXPECT_TRUE(v->_key == "c"); EXPECT_TRUE((int)v->_weight == 2); EXPECT_TRUE((int)pq.size() == 1); EXPECT_TRUE(pq.empty() == false); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "b"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "b"); EXPECT_TRUE(v->_key == "b"); EXPECT_TRUE((int)v->_weight == 2); EXPECT_TRUE((int)pq.size() == 0); EXPECT_TRUE(pq.empty() == true); p = pq.getMinimal(); EXPECT_TRUE(p == nullptr); b = pq.popMinimal(k, v); EXPECT_TRUE(b == false); } //////////////////////////////////////////////////////////////////////////////// /// @brief test filling in random weight order, and later lowering some weight //////////////////////////////////////////////////////////////////////////////// TEST(CPriorityQueueTest, tst_heap_case_with_lowering) { arangodb::graph::ShortestPathPriorityQueue pq; EXPECT_TRUE(0 == (int)pq.size()); EXPECT_TRUE(true == pq.empty()); bool b; MyValue* v = nullptr; b = pq.insert("a", std::make_unique("a", 4)); EXPECT_TRUE(b == true); b = pq.insert("b", std::make_unique("b", 2)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 3)); EXPECT_TRUE(b == true); b = pq.insert("d", std::make_unique("d", 3)); EXPECT_TRUE(b == true); b = pq.insert("c", std::make_unique("c", 5)); EXPECT_TRUE(b == false); delete v; EXPECT_TRUE(4 == (int)pq.size()); EXPECT_TRUE(false == pq.empty()); pq.lowerWeight("a", 1); // This moves "a" before all others MyValue const* p; p = pq.find("a"); EXPECT_TRUE((int)p->_weight == 1); p = pq.find("b"); EXPECT_TRUE((int)p->_weight == 2); p = pq.find("c"); EXPECT_TRUE((int)p->_weight == 3); p = pq.find("d"); EXPECT_TRUE((int)p->_weight == 3); p = pq.find("abc"); EXPECT_TRUE(p == nullptr); std::string k; p = pq.getMinimal(); EXPECT_TRUE(p->_key == "a"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "a"); EXPECT_TRUE(v->_key == "a"); EXPECT_TRUE((int)v->_weight == 1); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "b"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "b"); EXPECT_TRUE(v->_key == "b"); EXPECT_TRUE((int)v->_weight == 2); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "c"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "c"); EXPECT_TRUE(v->_key == "c"); EXPECT_TRUE((int)v->_weight == 3); EXPECT_TRUE((int)pq.size() == 1); EXPECT_TRUE(pq.empty() == false); p = pq.getMinimal(); EXPECT_TRUE(p->_key == "d"); b = pq.popMinimal(k, v); EXPECT_TRUE(b == true); EXPECT_TRUE(k == "d"); EXPECT_TRUE(v->_key == "d"); EXPECT_TRUE((int)v->_weight == 3); EXPECT_TRUE((int)pq.size() == 0); EXPECT_TRUE(pq.empty() == true); p = pq.getMinimal(); EXPECT_TRUE(p == nullptr); b = pq.popMinimal(k, v); EXPECT_TRUE(b == false); }