From 9e5a5970a397a993567660d9622f00c3be5a0f4c Mon Sep 17 00:00:00 2001 From: Stepan Fomenko Date: Tue, 11 Aug 2020 12:30:28 +0300 Subject: [PATCH] Improved PIBlockingDeque constructors - resolve creation from constant (see test construct_from_constant_is_max_size_eq_capacity) - add tests for constructors --- lib/main/thread/piblockingdequeue.h | 5 ++-- tests/concurrent/BlockingDequeueUnitTest.cpp | 25 +++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/main/thread/piblockingdequeue.h b/lib/main/thread/piblockingdequeue.h index c904e9fb..1b1fabbe 100644 --- a/lib/main/thread/piblockingdequeue.h +++ b/lib/main/thread/piblockingdequeue.h @@ -41,7 +41,8 @@ public: /** * @brief Copy constructor. Initialize queue with copy of other container elements. Not thread-safe for other queue. */ - template + template::value, int>::type = 0> explicit PIBlockingDequeue(const Iterable& other): PIBlockingDequeue() { mutex.lock(); for (const T& t : other) data_queue.push_back(t); @@ -51,7 +52,7 @@ public: /** * @brief Thread-safe copy constructor. Initialize queue with copy of other queue elements. */ - inline PIBlockingDequeue(PIBlockingDequeue& other): PIBlockingDequeue() { + explicit PIBlockingDequeue(PIBlockingDequeue& other): PIBlockingDequeue() { other.mutex.lock(); mutex.lock(); max_size = other.max_size; diff --git a/tests/concurrent/BlockingDequeueUnitTest.cpp b/tests/concurrent/BlockingDequeueUnitTest.cpp index f5a51332..0e7b1461 100644 --- a/tests/concurrent/BlockingDequeueUnitTest.cpp +++ b/tests/concurrent/BlockingDequeueUnitTest.cpp @@ -83,12 +83,14 @@ public: explicit PIBlockingDequeuePrepare(size_t capacity = SIZE_MAX): SuperClass(capacity) { } - template + template::value, int>::type = 0> explicit PIBlockingDequeuePrepare(const Iterable& other): SuperClass(other) { } MockConditionVar* getCondVarAdd() { return this->cond_var_add; } MockConditionVar* getCondVarRem() { return this->cond_var_rem; } MockDeque& getQueue() { return this->data_queue; } + size_t getMaxSize() { return max_size; } }; class BlockingDequeueUnitTest: public ::testing::Test { @@ -105,6 +107,27 @@ public: void take_is_wait_predicate(bool isEmpty); }; +TEST_F(BlockingDequeueUnitTest, construct_default_is_max_size_eq_size_max) { + PIBlockingDequeuePrepare dequeue; + ASSERT_EQ(dequeue.getMaxSize(), SIZE_MAX); +} + +TEST_F(BlockingDequeueUnitTest, construct_from_constant_is_max_size_eq_capacity) { + PIBlockingDequeuePrepare dequeue(2); + ASSERT_EQ(dequeue.getMaxSize(), 2); +} + +TEST_F(BlockingDequeueUnitTest, construct_from_capacity_is_max_size_eq_capacity) { + ASSERT_EQ(dequeue.getMaxSize(), capacity); +} + +TEST_F(BlockingDequeueUnitTest, construct_from_iterable) { + std::vector iterable; + iterable.emplace_back(11); + iterable.emplace_back(22); + PIBlockingDequeuePrepare dequeue(iterable); +} + void BlockingDequeueUnitTest::put_is_wait_predicate(bool isCapacityReach) { std::function conditionVarPredicate; EXPECT_CALL(*dequeue.getCondVarRem(), wait(_, _))