From 6bbd9039ab955017baac7de23d2ae725f8c3c846 Mon Sep 17 00:00:00 2001 From: Daniel Krawisz Date: Mon, 31 Oct 2022 20:30:43 -0500 Subject: [PATCH] fix bug in priority_queue== --- include/data/tools/priority_queue.hpp | 5 ++++- test/CMakeLists.txt | 1 + test/testPriorityQueue.cpp | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/testPriorityQueue.cpp diff --git a/include/data/tools/priority_queue.hpp b/include/data/tools/priority_queue.hpp index 80ecf8d5..c8697b10 100644 --- a/include/data/tools/priority_queue.hpp +++ b/include/data/tools/priority_queue.hpp @@ -66,7 +66,10 @@ namespace data::tool { template > bool inline operator==(const priority_queue a, const priority_queue b) { - return a.first() == b.first() && a.rest() == b.rest(); + if (data::empty(a) && data::empty(b)) return true; + if (data::empty(a) || data::empty(b)) return false; + if (a.first() != b.first()) return false; + return a.rest() == b.rest(); } template diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 16ecfa87..60e21730 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,7 @@ package_add_test(testTake testTake.cpp) package_add_test(testSort testSort.cpp) package_add_test(testLinkedTree testLinkedTree.cpp) package_add_test(testMap testMap.cpp) +package_add_test(testPriorityQueue testPriorityQueue.cpp) package_add_test(testForEach testForEach.cpp) package_add_test(testPolynomial testPolynomial.cpp) package_add_test(testPermutation testPermutation.cpp) diff --git a/test/testPriorityQueue.cpp b/test/testPriorityQueue.cpp new file mode 100644 index 00000000..8cd171e8 --- /dev/null +++ b/test/testPriorityQueue.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2019 Daniel Krawisz +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "data/tools.hpp" +#include "gtest/gtest.h" + +TEST(PriorityQueueTest, TestPriorityQueue) { + using namespace data; + + using pq = priority_queue; + + EXPECT_EQ(pq{}, pq{}); + EXPECT_EQ(first(pq{} << 1), 1); + EXPECT_NE(pq{} << 1, pq{}); + EXPECT_EQ(pq{} << 1 << 2, pq{} << 2 << 1); + EXPECT_EQ(1, first(pq{} << 2 << 1)); +} +