code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,119 +1,118 @@
#include "gtest/gtest.h"
#include "piblockingqueue.h"
#include "gtest/gtest.h"
class MockConditionVar: public PIConditionVariable {
public:
bool isWaitCalled = false;
bool isWaitForCalled = false;
bool isTrueCondition = false;
int timeout = -1;
bool isWaitCalled = false;
bool isWaitForCalled = false;
bool isTrueCondition = false;
int timeout = -1;
void wait(PIMutex& lk) override {
isWaitCalled = true;
}
void wait(PIMutex & lk) override { isWaitCalled = true; }
void wait(PIMutex& lk, const std::function<bool()>& condition) override {
isWaitCalled = true;
isTrueCondition = condition();
}
void wait(PIMutex & lk, const std::function<bool()> & condition) override {
isWaitCalled = true;
isTrueCondition = condition();
}
bool waitFor(PIMutex& lk, int timeoutMs) override {
isWaitForCalled = true;
timeout = timeoutMs;
return false;
}
bool waitFor(PIMutex & lk, int timeoutMs) override {
isWaitForCalled = true;
timeout = timeoutMs;
return false;
}
bool waitFor(PIMutex& lk, int timeoutMs, const std::function<bool()>& condition) override {
isWaitForCalled = true;
isTrueCondition = condition();
timeout = timeoutMs;
return isTrueCondition;
}
bool waitFor(PIMutex & lk, int timeoutMs, const std::function<bool()> & condition) override {
isWaitForCalled = true;
isTrueCondition = condition();
timeout = timeoutMs;
return isTrueCondition;
}
};
TEST(BlockingDequeueUnitTest, put_is_block_when_capacity_reach) {
size_t capacity = 0;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
size_t capacity = 0;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
dequeue.put(11);
ASSERT_TRUE(conditionVarRem->isWaitCalled);
ASSERT_FALSE(conditionVarRem->isTrueCondition);
dequeue.put(11);
ASSERT_TRUE(conditionVarRem->isWaitCalled);
ASSERT_FALSE(conditionVarRem->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, offer_timedout_is_false_when_capacity_reach) {
size_t capacity = 0;
int timeout = 11;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
size_t capacity = 0;
int timeout = 11;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
ASSERT_FALSE(dequeue.offer(11, timeout));
ASSERT_FALSE(dequeue.offer(11, timeout));
}
TEST(BlockingDequeueUnitTest, offer_timedout_is_block_when_capacity_reach) {
size_t capacity = 0;
int timeout = 11;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
size_t capacity = 0;
int timeout = 11;
auto conditionVarAdd = new MockConditionVar();
auto conditionVarRem = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVarAdd, conditionVarRem);
dequeue.offer(11, timeout);
EXPECT_TRUE(conditionVarRem->isWaitForCalled);
EXPECT_EQ(timeout, conditionVarRem->timeout);
ASSERT_FALSE(conditionVarRem->isTrueCondition);
dequeue.offer(11, timeout);
EXPECT_TRUE(conditionVarRem->isWaitForCalled);
EXPECT_EQ(timeout, conditionVarRem->timeout);
ASSERT_FALSE(conditionVarRem->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, offer_is_true_before_capacity_reach) {
size_t capacity = 1;
size_t capacity = 1;
PIBlockingQueue<int> dequeue(capacity);
ASSERT_TRUE(dequeue.offer(10));
}
TEST(BlockingDequeueUnitTest, offer_is_false_when_capacity_reach) {
size_t capacity = 1;
size_t capacity = 1;
PIBlockingQueue<int> dequeue(capacity);
dequeue.offer(11);
ASSERT_FALSE(dequeue.offer(10));
dequeue.offer(11);
ASSERT_FALSE(dequeue.offer(10));
}
// TODO change take_is_block_when_empty to prevent segfault
TEST(DISABLED_BlockingDequeueUnitTest, take_is_block_when_empty) {
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
// May cause segfault because take front of empty queue
dequeue.take();
EXPECT_TRUE(conditionVar->isWaitCalled);
ASSERT_FALSE(conditionVar->isTrueCondition);
// May cause segfault because take front of empty queue
dequeue.take();
EXPECT_TRUE(conditionVar->isWaitCalled);
ASSERT_FALSE(conditionVar->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, take_is_not_block_when_not_empty) {
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.offer(111);
dequeue.take();
dequeue.offer(111);
dequeue.take();
EXPECT_TRUE(conditionVar->isWaitCalled);
ASSERT_TRUE(conditionVar->isTrueCondition);
EXPECT_TRUE(conditionVar->isWaitCalled);
ASSERT_TRUE(conditionVar->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, take_is_value_eq_to_offer_value) {
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.offer(111);
ASSERT_EQ(dequeue.take(), 111);
dequeue.offer(111);
ASSERT_EQ(dequeue.take(), 111);
}
TEST(BlockingDequeueUnitTest, take_is_last) {
size_t capacity = 10;
auto conditionVar = new MockConditionVar();
size_t capacity = 10;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
EXPECT_TRUE(dequeue.offer(111));
EXPECT_TRUE(dequeue.offer(222));
ASSERT_EQ(dequeue.take(), 111);
ASSERT_EQ(dequeue.take(), 222);
EXPECT_TRUE(dequeue.offer(111));
EXPECT_TRUE(dequeue.offer(222));
ASSERT_EQ(dequeue.take(), 111);
ASSERT_EQ(dequeue.take(), 222);
}
TEST(BlockingDequeueUnitTest, poll_is_not_block_when_empty) {
@@ -143,118 +142,121 @@ TEST(BlockingDequeueUnitTest, poll_is_offer_value_when_not_empty) {
}
TEST(BlockingDequeueUnitTest, poll_timeouted_is_block_when_empty) {
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.poll(timeout, 111);
dequeue.poll(timeout, 111);
EXPECT_TRUE(conditionVar->isWaitForCalled);
EXPECT_EQ(timeout, conditionVar->timeout);
ASSERT_FALSE(conditionVar->isTrueCondition);
ASSERT_FALSE(conditionVar->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, poll_timeouted_is_default_value_when_empty) {
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
ASSERT_EQ(dequeue.poll(timeout, 111), 111);
ASSERT_EQ(dequeue.poll(timeout, 111), 111);
}
TEST(BlockingDequeueUnitTest, poll_timeouted_is_not_block_when_not_empty) {
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.offer(111);
dequeue.poll(timeout, -1);
dequeue.offer(111);
dequeue.poll(timeout, -1);
EXPECT_TRUE(conditionVar->isWaitForCalled);
ASSERT_TRUE(conditionVar->isTrueCondition);
EXPECT_TRUE(conditionVar->isWaitForCalled);
ASSERT_TRUE(conditionVar->isTrueCondition);
}
TEST(BlockingDequeueUnitTest, poll_timeouted_is_offer_value_when_not_empty) {
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
size_t capacity = 1;
int timeout = 11;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.offer(111);
ASSERT_EQ(dequeue.poll(timeout, -1), 111);
dequeue.offer(111);
ASSERT_EQ(dequeue.poll(timeout, -1), 111);
}
TEST(BlockingDequeueUnitTest, poll_timeouted_is_last) {
size_t capacity = 10;
auto conditionVar = new MockConditionVar();
size_t capacity = 10;
auto conditionVar = new MockConditionVar();
PIBlockingQueue<int> dequeue(capacity, conditionVar);
dequeue.offer(111);
dequeue.offer(222);
ASSERT_EQ(dequeue.poll(10, -1), 111);
ASSERT_EQ(dequeue.poll(10, -1), 222);
dequeue.offer(111);
dequeue.offer(222);
ASSERT_EQ(dequeue.poll(10, -1), 111);
ASSERT_EQ(dequeue.poll(10, -1), 222);
}
TEST(BlockingDequeueUnitTest, capacity_is_eq_constructor_capacity) {
size_t capacity = 10;
size_t capacity = 10;
PIBlockingQueue<int> dequeue(capacity);
ASSERT_EQ(dequeue.capacity(), capacity);
ASSERT_EQ(dequeue.capacity(), capacity);
}
TEST(BlockingDequeueUnitTest, remainingCapacity_is_dif_of_capacity_and_size) {
size_t capacity = 2;
size_t capacity = 2;
PIBlockingQueue<int> dequeue(capacity);
ASSERT_EQ(dequeue.remainingCapacity(), capacity);
dequeue.offer(111);
ASSERT_EQ(dequeue.remainingCapacity(), capacity - 1);
ASSERT_EQ(dequeue.remainingCapacity(), capacity);
dequeue.offer(111);
ASSERT_EQ(dequeue.remainingCapacity(), capacity - 1);
}
TEST(BlockingDequeueUnitTest, remainingCapacity_is_zero_when_capacity_reach) {
size_t capacity = 1;
size_t capacity = 1;
PIBlockingQueue<int> dequeue(capacity);
dequeue.offer(111);
dequeue.offer(111);
ASSERT_EQ(dequeue.remainingCapacity(), 0);
dequeue.offer(111);
dequeue.offer(111);
ASSERT_EQ(dequeue.remainingCapacity(), 0);
}
TEST(BlockingDequeueUnitTest, size_is_eq_to_num_of_elements) {
size_t capacity = 1;
size_t capacity = 1;
PIBlockingQueue<int> dequeue(capacity);
ASSERT_EQ(dequeue.size(), 0);
dequeue.offer(111);
ASSERT_EQ(dequeue.size(), 1);
ASSERT_EQ(dequeue.size(), 0);
dequeue.offer(111);
ASSERT_EQ(dequeue.size(), 1);
}
TEST(BlockingDequeueUnitTest, size_is_eq_to_capacity_when_capacity_reach) {
size_t capacity = 1;
size_t capacity = 1;
PIBlockingQueue<int> dequeue(capacity);
dequeue.offer(111);
dequeue.offer(111);
ASSERT_EQ(dequeue.size(), capacity);
dequeue.offer(111);
dequeue.offer(111);
ASSERT_EQ(dequeue.size(), capacity);
}
TEST(BlockingDequeueUnitTest, drainTo_is_elements_moved) {
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i)
refDeque.push_back(i * 10);
PIBlockingQueue<int> blockingDequeue(refDeque);
PIDeque<int> deque;
blockingDequeue.drainTo(deque);
ASSERT_EQ(blockingDequeue.size(), 0);
ASSERT_TRUE(deque == refDeque);
PIDeque<int> deque;
blockingDequeue.drainTo(deque);
ASSERT_EQ(blockingDequeue.size(), 0);
ASSERT_TRUE(deque == refDeque);
}
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_size_when_all_moved) {
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i)
refDeque.push_back(i * 10);
PIBlockingQueue<int> blockingDequeue(refDeque);
PIDeque<int> deque;
ASSERT_EQ(blockingDequeue.drainTo(deque), refDeque.size());
PIDeque<int> deque;
ASSERT_EQ(blockingDequeue.drainTo(deque), refDeque.size());
}
TEST(BlockingDequeueUnitTest, drainTo_is_ret_eq_to_maxCount) {
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i) refDeque.push_back(i * 10);
size_t capacity = 10;
PIDeque<int> refDeque;
for (size_t i = 0; i < capacity / 2; ++i)
refDeque.push_back(i * 10);
PIBlockingQueue<int> blockingDequeue(refDeque);
PIDeque<int> deque;
ASSERT_EQ(blockingDequeue.drainTo(deque, refDeque.size() - 1), refDeque.size() - 1);
PIDeque<int> deque;
ASSERT_EQ(blockingDequeue.drainTo(deque, refDeque.size() - 1), refDeque.size() - 1);
}

View File

@@ -1,13 +1,15 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "piconditionvar.h"
#include "pithread.h"
#include "testutil.h"
class ConditionLock : public ::testing::Test, public TestUtil {
#include "gmock/gmock.h"
#include "gtest/gtest.h"
class ConditionLock
: public ::testing::Test
, public TestUtil {
public:
PIMutex* m = new PIMutex();
PIMutex * m = new PIMutex();
bool isProtect;
bool isReleased;
};
@@ -16,13 +18,12 @@ public:
TEST_F(ConditionLock, DISABLED_lock_is_protect) {
m->lock();
isProtect = true;
createThread([&](){
createThread([&]() {
m->lock();
isProtect = false;
});
EXPECT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
ASSERT_TRUE(isProtect);
}
TEST_F(ConditionLock, DISABLED_unlock_is_release) {
@@ -30,7 +31,7 @@ TEST_F(ConditionLock, DISABLED_unlock_is_release) {
isReleased = false;
m->unlock();
createThread([&](){
createThread([&]() {
m->lock();
isReleased = true;
m->unlock();
@@ -39,7 +40,7 @@ TEST_F(ConditionLock, DISABLED_unlock_is_release) {
}
TEST_F(ConditionLock, tryLock_is_false_when_locked) {
createThread([&](){
createThread([&]() {
m->lock();
piMSleep(WAIT_THREAD_TIME_MS);
});

View File

@@ -1,20 +1,21 @@
#include "gtest/gtest.h"
#include "piconditionvar.h"
#include "pithread.h"
#include "testutil.h"
class ConditionVariable : public ::testing::Test, public TestUtil {
#include "gtest/gtest.h"
class ConditionVariable
: public ::testing::Test
, public TestUtil {
public:
~ConditionVariable() {
delete variable;
}
~ConditionVariable() { delete variable; }
PIMutex m;
PIConditionVariable* variable;
PIConditionVariable * variable;
protected:
void SetUp() override {
variable = new PIConditionVariable();
adapterFunctionDefault = [&](){
variable = new PIConditionVariable();
adapterFunctionDefault = [&]() {
m.lock();
variable->wait(m);
m.unlock();
@@ -46,42 +47,47 @@ TEST_F(ConditionVariable, wait_is_unblock_when_notifyOne_after_wait) {
}
TEST_F(ConditionVariable, wait_is_unblock_when_notifyAll_after_wait) {
PIVector<PIThread*> threads;
PIVector<PIThread *> threads;
for (int i = 0; i < THREAD_COUNT; ++i) {
threads.push_back(new PIThread([=](){ adapterFunctionDefault(); }));
threads.push_back(new PIThread([=]() { adapterFunctionDefault(); }));
}
piForeach(PIThread* thread, threads) thread->startOnce();
piForeach(PIThread * thread, threads)
thread->startOnce();
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
variable->notifyAll();
PITimeMeasurer measurer;
piForeach(PIThread* thread, threads) {
piForeach(PIThread * thread, threads) {
int timeout = WAIT_THREAD_TIME_MS * THREAD_COUNT - (int)measurer.elapsed_m();
thread->waitForFinish(timeout > 0 ? timeout : 0);
}
for (size_t i = 0; i < threads.size(); ++i) EXPECT_FALSE(threads[i]->isRunning()) << "Thread " << i << " still running";
piForeach(PIThread* thread, threads) delete thread;
for (size_t i = 0; i < threads.size(); ++i)
EXPECT_FALSE(threads[i]->isRunning()) << "Thread " << i << " still running";
piForeach(PIThread * thread, threads)
delete thread;
}
TEST_F(ConditionVariable, wait_is_one_unblock_when_notifyOne) {
PIVector<PIThread*> threads;
PIVector<PIThread *> threads;
for (int i = 0; i < THREAD_COUNT; ++i) {
threads.push_back(new PIThread(adapterFunctionDefault));
}
piForeach(PIThread* thread, threads) thread->startOnce();
piForeach(PIThread * thread, threads)
thread->startOnce();
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
variable->notifyOne();
piMSleep(WAIT_THREAD_TIME_MS * THREAD_COUNT);
int runningThreadCount = 0;
piForeach(PIThread* thread, threads) if (thread->isRunning()) runningThreadCount++;
piForeach(PIThread * thread, threads)
if (thread->isRunning()) runningThreadCount++;
ASSERT_EQ(runningThreadCount, THREAD_COUNT - 1);
}
TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
createThread([&](){
createThread([&]() {
m.lock();
variable->wait(m);
piMSleep(2 * WAIT_THREAD_TIME_MS);
@@ -93,9 +99,9 @@ TEST_F(ConditionVariable, wait_is_protected_unblock_when_notifyOne) {
}
TEST_F(ConditionVariable, wait_condition_is_block) {
createThread([&](){
createThread([&]() {
m.lock();
variable->wait(m, [](){ return false; });
variable->wait(m, []() { return false; });
m.unlock();
});
ASSERT_FALSE(thread->waitForFinish(WAIT_THREAD_TIME_MS));
@@ -103,9 +109,9 @@ TEST_F(ConditionVariable, wait_condition_is_block) {
TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
bool isConditionChecked = false;
createThread([&](){
createThread([&]() {
m.lock();
variable->wait(m, [&](){
variable->wait(m, [&]() {
isConditionChecked = true;
return false;
});
@@ -118,9 +124,9 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_before_block) {
TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
bool isConditionChecked;
createThread([&](){
createThread([&]() {
m.lock();
variable->wait(m, [&](){
variable->wait(m, [&]() {
isConditionChecked = true;
return false;
});
@@ -138,9 +144,9 @@ TEST_F(ConditionVariable, wait_condition_is_check_condition_when_notifyOne) {
TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne) {
bool condition = false;
createThread([&](){
createThread([&]() {
m.lock();
variable->wait(m, [&](){ return condition; });
variable->wait(m, [&]() { return condition; });
m.unlock();
});
m.lock();
@@ -151,7 +157,7 @@ TEST_F(ConditionVariable, wait_condition_is_unblock_when_condition_and_notifyOne
}
TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
createThread([&](){
createThread([&]() {
PITimeMeasurer measurer;
m.lock();
variable->waitFor(m, WAIT_THREAD_TIME_MS * 2);
@@ -164,7 +170,7 @@ TEST_F(ConditionVariable, DISABLED_waitFor_is_block_before_timeout) {
TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
std::atomic_bool isUnblock(false);
createThread([&](){
createThread([&]() {
m.lock();
variable->waitFor(m, WAIT_THREAD_TIME_MS);
isUnblock = true;
@@ -177,7 +183,7 @@ TEST_F(ConditionVariable, waitFor_is_unblock_when_timeout) {
TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
bool waitRet = true;
createThread([&](){
createThread([&]() {
m.lock();
waitRet = variable->waitFor(m, WAIT_THREAD_TIME_MS);
m.unlock();
@@ -188,9 +194,9 @@ TEST_F(ConditionVariable, waitFor_is_false_when_timeout) {
TEST_F(ConditionVariable, waitFor_is_unblock_when_condition_and_notifyOne) {
bool condition = false;
createThread([&](){
createThread([&]() {
m.lock();
variable->waitFor(m, 3 * WAIT_THREAD_TIME_MS, [&](){ return condition; });
variable->waitFor(m, 3 * WAIT_THREAD_TIME_MS, [&]() { return condition; });
m.unlock();
});
EXPECT_TRUE(thread->isRunning());

View File

@@ -1,54 +1,51 @@
#include "gtest/gtest.h"
#include "pithreadpoolexecutor.h"
#include "pimutex.h"
#include "pithreadpoolexecutor.h"
#include "gtest/gtest.h"
const int WAIT_THREAD_TIME_MS = 30;
TEST(ExcutorIntegrationTest, execute_is_runnable_invoke) {
PIMutex m;
int invokedRunnables = 0;
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() {
m.lock();
invokedRunnables++;
m.unlock();
});
piMSleep(WAIT_THREAD_TIME_MS);
ASSERT_EQ(invokedRunnables, 1);
PIMutex m;
int invokedRunnables = 0;
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() {
m.lock();
invokedRunnables++;
m.unlock();
});
piMSleep(WAIT_THREAD_TIME_MS);
ASSERT_EQ(invokedRunnables, 1);
}
TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
bool isRunnableInvoke = false;
PIThreadPoolExecutor executorService(1);
executorService.shutdown();
executorService.execute([&]() {
isRunnableInvoke = true;
});
piMSleep(WAIT_THREAD_TIME_MS);
ASSERT_FALSE(isRunnableInvoke);
bool isRunnableInvoke = false;
PIThreadPoolExecutor executorService(1);
executorService.shutdown();
executorService.execute([&]() { isRunnableInvoke = true; });
piMSleep(WAIT_THREAD_TIME_MS);
ASSERT_FALSE(isRunnableInvoke);
}
TEST(ExcutorIntegrationTest, execute_is_execute_before_shutdown) {
bool isRunnableInvoke = false;
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() {
piMSleep(WAIT_THREAD_TIME_MS);
isRunnableInvoke = true;
});
executorService.shutdown();
piMSleep(2 * WAIT_THREAD_TIME_MS);
ASSERT_TRUE(isRunnableInvoke);
bool isRunnableInvoke = false;
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() {
piMSleep(WAIT_THREAD_TIME_MS);
isRunnableInvoke = true;
});
executorService.shutdown();
piMSleep(2 * WAIT_THREAD_TIME_MS);
ASSERT_TRUE(isRunnableInvoke);
}
TEST(ExcutorIntegrationTest, execute_is_awaitTermination_wait) {
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() {
piMSleep(2 * WAIT_THREAD_TIME_MS);
});
executorService.shutdown();
PITimeMeasurer measurer;
ASSERT_TRUE(executorService.awaitTermination(3 * WAIT_THREAD_TIME_MS));
double waitTime = measurer.elapsed_m();
ASSERT_GE(waitTime, WAIT_THREAD_TIME_MS);
ASSERT_LE(waitTime, 4 * WAIT_THREAD_TIME_MS);
PIThreadPoolExecutor executorService(1);
executorService.execute([&]() { piMSleep(2 * WAIT_THREAD_TIME_MS); });
executorService.shutdown();
PITimeMeasurer measurer;
ASSERT_TRUE(executorService.awaitTermination(3 * WAIT_THREAD_TIME_MS));
double waitTime = measurer.elapsed_m();
ASSERT_GE(waitTime, WAIT_THREAD_TIME_MS);
ASSERT_LE(waitTime, 4 * WAIT_THREAD_TIME_MS);
}

View File

@@ -1,11 +1,17 @@
#include "gtest/gtest.h"
#include "pithreadnotifier.h"
#include "gtest/gtest.h"
TEST(PIThreadNotifierTest, One) {
PIThreadNotifier n;
int cnt = 0;
PIThread t1([&n, &cnt](){n.wait(); cnt++;}, true);
PIThread t1(
[&n, &cnt]() {
n.wait();
cnt++;
},
true);
piMSleep(10);
n.notifyOnce();
piMSleep(10);
@@ -21,14 +27,31 @@ TEST(PIThreadNotifierTest, Two) {
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;
PIThread t1([&n, &cnt1](){n.wait(); cnt1++; piMSleep(2);}, true);
PIThread t2([&n, &cnt2](){n.wait(); cnt2++; piMSleep(2);}, true);
PIThread t3([&n, &cnt3](){n.notifyOnce(); cnt3++; piMSleep(1);}, true);
PIThread t1(
[&n, &cnt1]() {
n.wait();
cnt1++;
piMSleep(2);
},
true);
PIThread t2(
[&n, &cnt2]() {
n.wait();
cnt2++;
piMSleep(2);
},
true);
PIThread t3(
[&n, &cnt3]() {
n.notifyOnce();
cnt3++;
piMSleep(1);
},
true);
piMSleep(20);
t3.stop(true);
piMSleep(100);
t1.stop();
t2.stop();
ASSERT_EQ(cnt1+cnt2, cnt3);
ASSERT_EQ(cnt1 + cnt2, cnt3);
}

View File

@@ -2,6 +2,7 @@
#define AWRCANFLASHER_TESTUTIL_H
#include "pithread.h"
#include <atomic>
/**
@@ -10,51 +11,52 @@
*/
const int WAIT_THREAD_TIME_MS = 400;
const int THREAD_COUNT = 5;
const int THREAD_COUNT = 5;
class TestUtil: public PIObject {
PIOBJECT(TestUtil)
PIOBJECT(TestUtil)
public:
double threadStartTime;
PIThread* thread = new PIThread();
std::atomic_bool isRunning;
std::function<void()> adapterFunctionDefault;
double threadStartTime;
PIThread * thread = new PIThread();
std::atomic_bool isRunning;
std::function<void()> adapterFunctionDefault;
TestUtil() : isRunning(false) {}
TestUtil(): isRunning(false) {}
bool createThread(const std::function<void()>& fun = nullptr, PIThread* thread_ = nullptr) {
std::function<void()> actualFun = fun == nullptr ? adapterFunctionDefault : fun;
if (thread_ == nullptr) thread_ = thread;
thread_->startOnce([=](void*){
bool createThread(const std::function<void()> & fun = nullptr, PIThread * thread_ = nullptr) {
std::function<void()> actualFun = fun == nullptr ? adapterFunctionDefault : fun;
if (thread_ == nullptr) thread_ = thread;
thread_->startOnce([=](void *) {
isRunning = true;
actualFun();
});
return waitThread(thread_);
}
actualFun();
});
return waitThread(thread_);
}
bool waitThread(PIThread* thread_, bool runningStatus = true) {
PITimeMeasurer measurer;
bool isTimeout = !thread_->waitForStart(WAIT_THREAD_TIME_MS);
while (!isRunning) {
isTimeout = WAIT_THREAD_TIME_MS <= measurer.elapsed_m();
if (isTimeout) break;
piUSleep(100);
}
bool waitThread(PIThread * thread_, bool runningStatus = true) {
PITimeMeasurer measurer;
bool isTimeout = !thread_->waitForStart(WAIT_THREAD_TIME_MS);
while (!isRunning) {
isTimeout = WAIT_THREAD_TIME_MS <= measurer.elapsed_m();
if (isTimeout) break;
piUSleep(100);
}
threadStartTime = measurer.elapsed_m();
threadStartTime = measurer.elapsed_m();
if (isTimeout) piCout << "Start thread timeout reach!";
if (isTimeout) piCout << "Start thread timeout reach!";
if (threadStartTime > 1) {
piCout << "Start time" << threadStartTime << "ms";
} else if (threadStartTime > 0.001) {
piCout << "Start time" << threadStartTime * 1000 << "mcs";
} else {
piCout << "Start time" << threadStartTime * 1000 * 1000 << "ns";
}
if (threadStartTime > 1) {
piCout << "Start time" << threadStartTime << "ms";
} else if (threadStartTime > 0.001) {
piCout << "Start time" << threadStartTime * 1000 << "mcs";
} else {
piCout << "Start time" << threadStartTime * 1000 * 1000 << "ns";
}
return !isTimeout;
}
return !isTimeout;
}
};
#endif //AWRCANFLASHER_TESTUTIL_H
#endif // AWRCANFLASHER_TESTUTIL_H

View File

@@ -1,165 +1,166 @@
#include "gtest/gtest.h"
#include "pistringlist.h"
#include "gtest/gtest.h"
using namespace std;
TEST(PIStringList_Tests, construct_empty){
TEST(PIStringList_Tests, construct_empty) {
PIStringList lis;
ASSERT_TRUE(lis.isEmpty());
}
TEST(PIStringList_Tests, construct_one_str_length){
TEST(PIStringList_Tests, construct_one_str_length) {
PIString str1 = "first string";
PIStringList lis (str1);
ASSERT_EQ(lis.length(),1);
}
TEST(PIStringList_Tests, construct_one_str_data){
PIString str1 = "first string";
PIStringList lis (str1);
ASSERT_EQ(str1, lis[0]);
}
TEST(PIStringList_Tests, construct_one_str_move_length){
PIString str1 = "first string";
PIStringList lis (move(str1));
PIStringList lis(str1);
ASSERT_EQ(lis.length(), 1);
}
TEST(PIStringList_Tests, construct_one_str_move_data){
TEST(PIStringList_Tests, construct_one_str_data) {
PIString str1 = "first string";
PIString str = str1;
PIStringList lis (move(str));
PIStringList lis(str1);
ASSERT_EQ(str1, lis[0]);
}
TEST(PIStringList_Tests, construct_two_str_length){
TEST(PIStringList_Tests, construct_one_str_move_length) {
PIString str1 = "first string";
PIStringList lis(move(str1));
ASSERT_EQ(lis.length(), 1);
}
TEST(PIStringList_Tests, construct_one_str_move_data) {
PIString str1 = "first string";
PIString str = str1;
PIStringList lis(move(str));
ASSERT_EQ(str1, lis[0]);
}
TEST(PIStringList_Tests, construct_two_str_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIStringList lis (str1, str2);
PIStringList lis(str1, str2);
ASSERT_EQ(lis.length(), 2);
}
TEST(PIStringList_Tests, construct_two_str_data){
TEST(PIStringList_Tests, construct_two_str_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIStringList lis (str1, str2);
PIStringList lis(str1, str2);
ASSERT_EQ(str1, lis[0]);
ASSERT_EQ(str2, lis[1]);
}
TEST(PIStringList_Tests, construct_two_str_move_length){
TEST(PIStringList_Tests, construct_two_str_move_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIStringList lis (move(str1), move(str2));
PIStringList lis(move(str1), move(str2));
ASSERT_EQ(lis.length(), 2);
}
TEST(PIStringList_Tests, construct_two_str_move_data){
PIString str1 = "first string";
PIString str2 = "second string";
TEST(PIStringList_Tests, construct_two_str_move_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str1_res = str1;
PIString str2_res = str2;
PIStringList lis (move(str1), move(str2));
PIStringList lis(move(str1), move(str2));
ASSERT_EQ(str1_res, lis[0]);
ASSERT_EQ(lis[1], str2_res);
}
TEST(PIStringList_Tests, construct_three_str_length){
TEST(PIStringList_Tests, construct_three_str_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIStringList lis (str1, str2, str3);
ASSERT_EQ(lis.length(),3);
PIStringList lis(str1, str2, str3);
ASSERT_EQ(lis.length(), 3);
}
TEST(PIStringList_Tests, construct_three_str_data){
TEST(PIStringList_Tests, construct_three_str_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIStringList lis (str1, str2, str3);
PIStringList lis(str1, str2, str3);
ASSERT_EQ(lis[0], str1);
ASSERT_EQ(lis[1], str2);
ASSERT_EQ(lis[2], str3);
}
TEST(PIStringList_Tests, construct_three_str_move_length){
TEST(PIStringList_Tests, construct_three_str_move_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIStringList lis (move(str1), move(str2), move(str3));
PIStringList lis(move(str1), move(str2), move(str3));
ASSERT_EQ(lis.length(), 3);
}
TEST(PIStringList_Tests, construct_three_str_move_data){
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
TEST(PIStringList_Tests, construct_three_str_move_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str1_res = str1;
PIString str2_res = str2;
PIString str3_res = str3;
PIStringList lis (move(str1), move(str2), move(str3));
PIStringList lis(move(str1), move(str2), move(str3));
ASSERT_EQ(lis[0], str1_res);
ASSERT_EQ(lis[1], str2_res);
ASSERT_EQ(lis[2], str3_res);
}
TEST(PIStringList_Tests, construct_four_str_length){
TEST(PIStringList_Tests, construct_four_str_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis (str1, str2, str3, str4);
PIStringList lis(str1, str2, str3, str4);
ASSERT_EQ(lis.length(), 4);
}
TEST(PIStringList_Tests, construct_four_str_data){
TEST(PIStringList_Tests, construct_four_str_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis (str1, str2, str3, str4);
PIStringList lis(str1, str2, str3, str4);
ASSERT_EQ(lis[0], str1);
ASSERT_EQ(lis[1], str2);
ASSERT_EQ(lis[2], str3);
ASSERT_EQ(lis[3], str4);
}
TEST(PIStringList_Tests, construct_four_str_move_length){
TEST(PIStringList_Tests, construct_four_str_move_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis (move(str1), move(str2), move(str3), move(str4));
PIStringList lis(move(str1), move(str2), move(str3), move(str4));
ASSERT_EQ(lis.length(), 4);
}
TEST(PIStringList_Tests, construct_four_str_move_data){
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
TEST(PIStringList_Tests, construct_four_str_move_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIString str1_res = str1;
PIString str2_res = str2;
PIString str3_res = str3;
PIString str4_res = str4;
PIStringList lis (move(str1), move(str2), move(str3), move(str4));
PIStringList lis(move(str1), move(str2), move(str3), move(str4));
ASSERT_EQ(lis[0], str1_res);
ASSERT_EQ(lis[1], str2_res);
ASSERT_EQ(lis[2], str3_res);
ASSERT_EQ(lis[3], str4_res);
}
TEST(PIStringList_Tests, construct_std_initializer_list_length){
PIStringList lis {"first string", "second string", "third string", "fourth string", "fourth string"};
TEST(PIStringList_Tests, construct_std_initializer_list_length) {
PIStringList lis{"first string", "second string", "third string", "fourth string", "fourth string"};
ASSERT_EQ(lis.length(), 5);
}
TEST(PIStringList_Tests, construct_std_initializer_list_data){
TEST(PIStringList_Tests, construct_std_initializer_list_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis {"first string", "second string", "third string", "fourth string", "fourth string"};
PIStringList lis{"first string", "second string", "third string", "fourth string", "fourth string"};
ASSERT_EQ(lis[0], str1);
ASSERT_EQ(lis[1], str2);
ASSERT_EQ(lis[2], str3);
@@ -167,53 +168,53 @@ TEST(PIStringList_Tests, construct_std_initializer_list_data){
ASSERT_EQ(lis[4], str4);
}
TEST(PIStringList_Tests, construct_list_length){
TEST(PIStringList_Tests, construct_list_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis {str1, str2, str3, str4};
PIStringList list (lis);
PIStringList lis{str1, str2, str3, str4};
PIStringList list(lis);
ASSERT_EQ(list.length(), 4);
}
TEST(PIStringList_Tests, construct_list_data){
TEST(PIStringList_Tests, construct_list_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis {str1, str2, str3, str4};
PIStringList list (lis);
PIStringList lis{str1, str2, str3, str4};
PIStringList list(lis);
ASSERT_EQ(list[0], str1);
ASSERT_EQ(list[1], str2);
ASSERT_EQ(list[2], str3);
ASSERT_EQ(list[3], str4);
}
TEST(PIStringList_Tests, construct_list_move_length){
TEST(PIStringList_Tests, construct_list_move_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis {"first string", "second string", "third string", "fourth string"};
PIStringList list (move(lis));
PIStringList lis{"first string", "second string", "third string", "fourth string"};
PIStringList list(move(lis));
ASSERT_EQ(list.length(), 4);
}
TEST(PIStringList_Tests, construct_list_move_data){
TEST(PIStringList_Tests, construct_list_move_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIStringList lis {"first string", "second string", "third string", "fourth string"};
PIStringList list (move(lis));
PIStringList lis{"first string", "second string", "third string", "fourth string"};
PIStringList list(move(lis));
ASSERT_EQ(list[0], str1);
ASSERT_EQ(list[1], str2);
ASSERT_EQ(list[2], str3);
ASSERT_EQ(list[3], str4);
}
TEST(PIStringList_Tests, construct_pivect_length){
TEST(PIStringList_Tests, construct_pivect_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
@@ -223,11 +224,11 @@ TEST(PIStringList_Tests, construct_pivect_length){
vect.append(str2);
vect.append(str3);
vect.append(str4);
PIStringList list (vect);
PIStringList list(vect);
ASSERT_EQ(list.length(), 4);
}
TEST(PIStringList_Tests, construct_pivect_data){
TEST(PIStringList_Tests, construct_pivect_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
@@ -237,14 +238,14 @@ TEST(PIStringList_Tests, construct_pivect_data){
vect.append(str2);
vect.append(str3);
vect.append(str4);
PIStringList list (vect);
PIStringList list(vect);
ASSERT_EQ(list[0], str1);
ASSERT_EQ(list[1], str2);
ASSERT_EQ(list[2], str3);
ASSERT_EQ(list[3], str4);
}
TEST(PIStringList_Tests, construct_list_pideq_length){
TEST(PIStringList_Tests, construct_list_pideq_length) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
@@ -254,11 +255,11 @@ TEST(PIStringList_Tests, construct_list_pideq_length){
deq.append(str2);
deq.append(str3);
deq.append(str4);
PIStringList list (deq);
PIStringList list(deq);
ASSERT_EQ(list.length(), 4);
}
TEST(PIStringList_Tests, construct_list_pideq_data){
TEST(PIStringList_Tests, construct_list_pideq_data) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
@@ -268,43 +269,42 @@ TEST(PIStringList_Tests, construct_list_pideq_data){
deq.append(str2);
deq.append(str3);
deq.append(str4);
PIStringList list (deq);
PIStringList list(deq);
ASSERT_EQ(list[0], str1);
ASSERT_EQ(list[1], str2);
ASSERT_EQ(list[2], str3);
ASSERT_EQ(list[3], str4);
}
TEST(PIStringList_Tests, join){
TEST(PIStringList_Tests, join) {
PIString str1 = "first string";
PIString str2 = "second string";
PIString str3 = "third string";
PIString str4 = "fourth string";
PIString del = ", ";
PIStringList lis {str1, str2, str3, str4};
PIString del = ", ";
PIStringList lis{str1, str2, str3, str4};
ASSERT_EQ(lis.join(del), "first string, second string, third string, fourth string");
}
TEST(PIStringList_Tests, remove_strings_length){
TEST(PIStringList_Tests, remove_strings_length) {
PIString str1 = "first string";
PIString str2 = "string";
PIString str3 = "third string";
PIString str4 = "string";
PIString val = "string";
PIStringList lis {str1, str2, str3, str4};
PIString val = "string";
PIStringList lis{str1, str2, str3, str4};
lis.removeStrings(val);
ASSERT_EQ(lis.length(), 2);
}
TEST(PIStringList_Tests, remove_strings_data){
TEST(PIStringList_Tests, remove_strings_data) {
PIString str1 = "first string";
PIString str2 = "string";
PIString str3 = "third string";
PIString str4 = "string";
PIString val = "string";
PIStringList lis {str1, str2, str3, str4};
PIString val = "string";
PIStringList lis{str1, str2, str3, str4};
lis.removeStrings(val);
ASSERT_EQ(lis[0], "first string");
ASSERT_EQ(lis[1], "third string");
}

View File

@@ -1,93 +1,95 @@
#include "gtest/gtest.h"
#include "pistring.h"
#include "pistringlist.h"
#include "math.h"
#include "pistringlist.h"
#include "gtest/gtest.h"
using namespace std;
template <typename T>
template<typename T>
bool is_equal(T x, T y) {
return std::fabs(x - y) < std::numeric_limits<T>::epsilon();
}
TEST(PIString_Tests, constructor_empty){
TEST(PIString_Tests, constructor_empty) {
PIString str1;
ASSERT_TRUE(str1.isEmpty());
}
TEST(PIString_Tests, operator_concatenation_pichar){
PIString str1 = "AB C";
TEST(PIString_Tests, operator_concatenation_pichar) {
PIString str1 = "AB C";
const PIChar str2 = " ";
str1 += str2;
PIString res = "AB C ";
ASSERT_EQ(str1, res);
}
TEST(PIString_Tests, operator_concatenation_pichar_zero1){
PIString str1 = "";
TEST(PIString_Tests, operator_concatenation_pichar_zero1) {
PIString str1 = "";
const PIChar str2 = "D";
str1 += str2;
ASSERT_EQ(str1, "D");
}
TEST(PIString_Tests, operator_concatenation_cstring){
PIString str1 = "AB C";
const char *str2 = "D D ";
TEST(PIString_Tests, operator_concatenation_cstring) {
PIString str1 = "AB C";
const char * str2 = "D D ";
str1 += str2;
ASSERT_EQ(str1, "AB CD D ");
}
TEST(PIString_Tests, operator_concatenation_cstring_zero1){
PIString str1 = "";
const char *str2 = "D D ";
TEST(PIString_Tests, operator_concatenation_cstring_zero1) {
PIString str1 = "";
const char * str2 = "D D ";
str1 += str2;
ASSERT_EQ(str1, "D D ");
}
TEST(PIString_Tests, operator_concatenation_wchar){
PIString str1= "AB C";
TEST(PIString_Tests, operator_concatenation_wchar) {
PIString str1 = "AB C";
wchar_t str2[] = L"C";
str1 += str2;
ASSERT_EQ(str1, "AB CC");
}
TEST(PIString_Tests, operator_concatenation_wchar_zero1){
PIString str1 = "";
TEST(PIString_Tests, operator_concatenation_wchar_zero1) {
PIString str1 = "";
wchar_t str2[] = L"C";
str1 += str2;
ASSERT_EQ(str1, "C");
}
TEST(PIString_Tests, operator_concatenation_pistring){
TEST(PIString_Tests, operator_concatenation_pistring) {
PIString str1 = "AB C";
PIString str2 = " CD ";
str1 += str2;
ASSERT_EQ(str1, "AB C CD ");
}
TEST(PIString_Tests, operator_concatenation_pistring_zero1){
TEST(PIString_Tests, operator_concatenation_pistring_zero1) {
PIString str1 = "";
PIString str2 = "D DD";
str1 += str2;
ASSERT_EQ(str1, "D DD");
}
TEST(PIString_Tests, operator_concatenation_pistring_zero2){
TEST(PIString_Tests, operator_concatenation_pistring_zero2) {
PIString str1 = "AB C";
PIString str2 = "";
str1 += str2;
ASSERT_EQ(str1, "AB C");
}
TEST(PIString_Tests, operator_concatenation_pistring_zero_zero){
TEST(PIString_Tests, operator_concatenation_pistring_zero_zero) {
PIString str1;
PIString str2;
str1 += str2;
ASSERT_EQ(str1, "");
}
TEST(PIString_Tests, operator_concatenation_piByteArray){
TEST(PIString_Tests, operator_concatenation_piByteArray) {
PIString str1 = "AB C";
PIByteArray str2;
str2.append('g');
@@ -95,7 +97,7 @@ TEST(PIString_Tests, operator_concatenation_piByteArray){
ASSERT_EQ(str1, "AB Cg");
}
TEST(PIString_Tests, operator_concatenation_piByteArray_zero1){
TEST(PIString_Tests, operator_concatenation_piByteArray_zero1) {
PIString str1 = "";
PIByteArray str2;
str2.append('0');
@@ -103,99 +105,99 @@ TEST(PIString_Tests, operator_concatenation_piByteArray_zero1){
ASSERT_EQ(str1, "0");
}
TEST(PIString_Tests, operator_concatenation_piByteArray_zero2){
TEST(PIString_Tests, operator_concatenation_piByteArray_zero2) {
PIString str1 = "AB C";
PIByteArray str2;
str1 += str2;
ASSERT_EQ(str1, "AB C");
}
TEST(PIString_Tests, operator_concatenation_piByteArray_zero_zero){
TEST(PIString_Tests, operator_concatenation_piByteArray_zero_zero) {
PIString str1;
PIByteArray str2;
str1 += str2;
ASSERT_EQ(str1, "");
}
TEST(PIString_Tests, construct_pistring){
TEST(PIString_Tests, construct_pistring) {
PIString str1 = "New";
PIString str(str1);
ASSERT_EQ(str1, str);
}
TEST(PIString_Tests, construct_pistring_move){
TEST(PIString_Tests, construct_pistring_move) {
PIString str1 = "New";
PIString res = str1;
PIString res = str1;
PIString str(move(str1));
ASSERT_EQ(res, str);
}
TEST(PIString_Tests, construct_from_pichar){
PIChar str1 = 'n';
TEST(PIString_Tests, construct_from_pichar) {
PIChar str1 = 'n';
PIString res = "n";
ASSERT_EQ(res, PIString(str1));
}
TEST(PIString_Tests, construct_from_char){
char str1 = 'n';
TEST(PIString_Tests, construct_from_char) {
char str1 = 'n';
PIString res = "n";
ASSERT_EQ(res, PIString(str1));
}
TEST(PIString_Tests, construct_from_cstring){
char str1[] = "mew";
TEST(PIString_Tests, construct_from_cstring) {
char str1[] = "mew";
PIString res = "mew";
ASSERT_EQ(res, PIString(str1));
}
TEST(PIString_Tests, construct_from_wchar_string){
TEST(PIString_Tests, construct_from_wchar_string) {
wchar_t str1[] = L"gav";
PIString res = "gav";
PIString res = "gav";
ASSERT_EQ(res, PIString(str1));
}
TEST(PIString_Tests, construct_from_pibyte_array){
TEST(PIString_Tests, construct_from_pibyte_array) {
PIByteArray str1;
str1.append('m');
PIString res = "m";
ASSERT_EQ(res, PIString(str1));
}
TEST(PIString_Tests, construct_from_pichar_array){
TEST(PIString_Tests, construct_from_pichar_array) {
PIChar str1[3];
str1[0] = 'n';
str1[1] = 'e';
str1[2] = 'w';
str1[0] = 'n';
str1[1] = 'e';
str1[2] = 'w';
PIString res = "new";
ASSERT_EQ(res, PIString(str1, 3));
}
TEST(PIString_Tests, construct_from_char_array){
char str1[] = "good";
TEST(PIString_Tests, construct_from_char_array) {
char str1[] = "good";
PIString res = "good";
ASSERT_EQ(res, PIString(str1, 4));
}
TEST(PIString_Tests, construct_from_char_len){
char str1 = 'n';
TEST(PIString_Tests, construct_from_char_len) {
char str1 = 'n';
PIString res = "nnn";
ASSERT_EQ(res, PIString(3, str1));
}
TEST(PIString_Tests, construct_from_pichar_len){
PIChar str1 = 'n';
TEST(PIString_Tests, construct_from_pichar_len) {
PIChar str1 = 'n';
PIString res = "nnnnn";
ASSERT_EQ(res, PIString(5, str1));
}
TEST(PIString_Tests, operator_assignment){
TEST(PIString_Tests, operator_assignment) {
PIString str1 = "testing";
PIString str;
str = str1;
ASSERT_EQ(PIString("testing"), str);
}
TEST(PIString_Tests, operator_assignment_move){
TEST(PIString_Tests, operator_assignment_move) {
PIString str1 = "testing";
PIString str;
str = move(str1);
@@ -203,301 +205,301 @@ TEST(PIString_Tests, operator_assignment_move){
ASSERT_TRUE(str1.isEmpty());
}
TEST(PIString_Tests, operator_symbol){
TEST(PIString_Tests, operator_symbol) {
PIString str1 = "testing";
PIChar symbo = "i";
PIChar symbo = "i";
ASSERT_EQ(symbo, str1[4]);
}
TEST(PIString_Tests, operator_equal_pistring_true){
TEST(PIString_Tests, operator_equal_pistring_true) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_TRUE(str1 == str2);
}
TEST(PIString_Tests, operator_equal_pistring_false){
TEST(PIString_Tests, operator_equal_pistring_false) {
PIString str1 = "tes";
PIString str2 = "testing";
ASSERT_FALSE(str1 == str2);
}
TEST(PIString_Tests, operator_equal_pichar_true){
TEST(PIString_Tests, operator_equal_pichar_true) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_TRUE(str1 == str2);
}
TEST(PIString_Tests, operator_equal_pichar_false){
TEST(PIString_Tests, operator_equal_pichar_false) {
PIString str1 = "t";
PIChar str2 = "p";
PIChar str2 = "p";
ASSERT_FALSE(str1 == str2);
}
TEST(PIString_Tests, operator_equal_cstring_true){
TEST(PIString_Tests, operator_equal_cstring_true) {
PIString str1 = "test";
char str2[] = "test";
char str2[] = "test";
ASSERT_TRUE(str1 == str2);
}
TEST(PIString_Tests, operator_equal_cstring_false){
TEST(PIString_Tests, operator_equal_cstring_false) {
PIString str1 = "t";
char str2[] = "test";
char str2[] = "test";
ASSERT_FALSE(str1 == str2);
}
TEST(PIString_Tests, operator_not_equal_pistring_false){
TEST(PIString_Tests, operator_not_equal_pistring_false) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_FALSE(str1 != str2);
}
TEST(PIString_Tests, operator_not_equal_pistring_true){
TEST(PIString_Tests, operator_not_equal_pistring_true) {
PIString str1 = "tes";
PIString str2 = "testing";
ASSERT_TRUE(str1 != str2);
}
TEST(PIString_Tests, operator_not_equal_pichar_false){
TEST(PIString_Tests, operator_not_equal_pichar_false) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_FALSE(str1 != str2);
}
TEST(PIString_Tests, operator_not_equal_pichar_true){
TEST(PIString_Tests, operator_not_equal_pichar_true) {
PIString str1 = "t";
PIChar str2 = "p";
PIChar str2 = "p";
ASSERT_TRUE(str1 != str2);
}
TEST(PIString_Tests, operator_not_equal_cstring_false){
TEST(PIString_Tests, operator_not_equal_cstring_false) {
PIString str1 = "test";
char str2[] = "test";
char str2[] = "test";
ASSERT_FALSE(str1 != str2);
}
TEST(PIString_Tests, operator_not_equal_cstring_true){
TEST(PIString_Tests, operator_not_equal_cstring_true) {
PIString str1 = "t";
char str2[] = "test";
char str2[] = "test";
ASSERT_TRUE(str1 != str2);
}
TEST(PIString_Tests, operator_less_pistring_true){
TEST(PIString_Tests, operator_less_pistring_true) {
PIString str1 = "testin";
PIString str2 = "testing";
ASSERT_TRUE(str1 < str2);
}
TEST(PIString_Tests, operator_less_pistring_false){
TEST(PIString_Tests, operator_less_pistring_false) {
PIString str1 = "testing";
PIString str2 = "testin";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_less_pistring_false_equal){
TEST(PIString_Tests, operator_less_pistring_false_equal) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_less_pichar_true){
TEST(PIString_Tests, operator_less_pichar_true) {
PIString str1 = "a";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_TRUE(str1 < str2);
}
TEST(PIString_Tests, operator_less_pichar_false){
TEST(PIString_Tests, operator_less_pichar_false) {
PIString str1 = "t";
PIChar str2 = "a";
PIChar str2 = "a";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_less_pichar_false_equal){
TEST(PIString_Tests, operator_less_pichar_false_equal) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_less_cstring_true){
TEST(PIString_Tests, operator_less_cstring_true) {
PIString str1 = "a";
char str2[] = "t";
char str2[] = "t";
ASSERT_TRUE(str1 < str2);
}
TEST(PIString_Tests, operator_less_cstring_false){
TEST(PIString_Tests, operator_less_cstring_false) {
PIString str1 = "t";
char str2[] = "a";
char str2[] = "a";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_less_cstring_false_equal){
TEST(PIString_Tests, operator_less_cstring_false_equal) {
PIString str1 = "t";
char str2[] = "t";
char str2[] = "t";
ASSERT_FALSE(str1 < str2);
}
TEST(PIString_Tests, operator_grater_pistring_true){
TEST(PIString_Tests, operator_grater_pistring_true) {
PIString str1 = "testin";
PIString str2 = "testing";
ASSERT_TRUE(str2 > str1);
}
TEST(PIString_Tests, operator_grater_pistring_false){
TEST(PIString_Tests, operator_grater_pistring_false) {
PIString str1 = "testing";
PIString str2 = "testin";
ASSERT_FALSE(str2 > str1);
}
TEST(PIString_Tests, operator_grater_pistring_false_equal){
TEST(PIString_Tests, operator_grater_pistring_false_equal) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_FALSE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_pichar_true){
TEST(PIString_Tests, operator_grater_pichar_true) {
PIString str1 = "t";
PIChar str2 = "a";
PIChar str2 = "a";
ASSERT_TRUE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_pichar_false){
TEST(PIString_Tests, operator_grater_pichar_false) {
PIString str1 = "a";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_FALSE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_pichar_false_equal){
TEST(PIString_Tests, operator_grater_pichar_false_equal) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_FALSE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_cstring_true){
TEST(PIString_Tests, operator_grater_cstring_true) {
PIString str1 = "t";
char str2[] = "a";
char str2[] = "a";
ASSERT_TRUE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_cstring_false){
TEST(PIString_Tests, operator_grater_cstring_false) {
PIString str1 = "a";
char str2[] = "t";
char str2[] = "t";
ASSERT_FALSE(str1 > str2);
}
TEST(PIString_Tests, operator_grater_cstring_false_equal){
TEST(PIString_Tests, operator_grater_cstring_false_equal) {
PIString str1 = "t";
char str2[] = "t";
char str2[] = "t";
ASSERT_FALSE(str1 > str2);
}
TEST(PIString_Tests, operator_less_eq_pistring_true_less){
TEST(PIString_Tests, operator_less_eq_pistring_true_less) {
PIString str1 = "testin";
PIString str2 = "testing";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_pistring_false){
TEST(PIString_Tests, operator_less_eq_pistring_false) {
PIString str1 = "testing";
PIString str2 = "testin";
ASSERT_FALSE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_pistring_true_equal){
TEST(PIString_Tests, operator_less_eq_pistring_true_equal) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_pichar_true){
TEST(PIString_Tests, operator_less_eq_pichar_true) {
PIString str1 = "a";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_pichar_false){
TEST(PIString_Tests, operator_less_eq_pichar_false) {
PIString str1 = "t";
PIChar str2 = "a";
PIChar str2 = "a";
ASSERT_FALSE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_pichar_true_equal){
TEST(PIString_Tests, operator_less_eq_pichar_true_equal) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_cstring_true){
TEST(PIString_Tests, operator_less_eq_cstring_true) {
PIString str1 = "a";
char str2[] = "t";
char str2[] = "t";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_cstring_false){
TEST(PIString_Tests, operator_less_eq_cstring_false) {
PIString str1 = "t";
char str2[] = "a";
char str2[] = "a";
ASSERT_FALSE(str1 <= str2);
}
TEST(PIString_Tests, operator_less_eq_cstring_true_equal){
TEST(PIString_Tests, operator_less_eq_cstring_true_equal) {
PIString str1 = "t";
char str2[] = "t";
char str2[] = "t";
ASSERT_TRUE(str1 <= str2);
}
TEST(PIString_Tests, operator_grater_eq_pistring_true){
TEST(PIString_Tests, operator_grater_eq_pistring_true) {
PIString str1 = "testin";
PIString str2 = "testing";
ASSERT_TRUE(str2 >= str1);
}
TEST(PIString_Tests, operator_grater_eq_pistring_false){
TEST(PIString_Tests, operator_grater_eq_pistring_false) {
PIString str1 = "testing";
PIString str2 = "testin";
ASSERT_FALSE(str2 >= str1);
}
TEST(PIString_Tests, operator_grater_eq_pistring_true_equal){
TEST(PIString_Tests, operator_grater_eq_pistring_true_equal) {
PIString str1 = "testing";
PIString str2 = "testing";
ASSERT_TRUE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_pichar_true){
TEST(PIString_Tests, operator_grater_eq_pichar_true) {
PIString str1 = "t";
PIChar str2 = "a";
PIChar str2 = "a";
ASSERT_TRUE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_pichar_false){
TEST(PIString_Tests, operator_grater_eq_pichar_false) {
PIString str1 = "a";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_FALSE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_pichar_true_equal){
TEST(PIString_Tests, operator_grater_eq_pichar_true_equal) {
PIString str1 = "t";
PIChar str2 = "t";
PIChar str2 = "t";
ASSERT_TRUE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_cstring_true){
TEST(PIString_Tests, operator_grater_eq_cstring_true) {
PIString str1 = "t";
char str2[] = "a";
char str2[] = "a";
ASSERT_TRUE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_cstring_false){
TEST(PIString_Tests, operator_grater_eq_cstring_false) {
PIString str1 = "a";
char str2[] = "t";
char str2[] = "t";
ASSERT_FALSE(str1 >= str2);
}
TEST(PIString_Tests, operator_grater_eq_cstring_true_equal){
TEST(PIString_Tests, operator_grater_eq_cstring_true_equal) {
PIString str1 = "t";
char str2[] = "t";
char str2[] = "t";
ASSERT_TRUE(str1 >= str2);
}
TEST(PIString_Tests, operator_shift_pistring){
TEST(PIString_Tests, operator_shift_pistring) {
PIString str1 = "shift";
PIString str2 = " good";
str1 << str2;
@@ -505,97 +507,97 @@ TEST(PIString_Tests, operator_shift_pistring){
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_pichar){
TEST(PIString_Tests, operator_shift_pichar) {
PIString str1 = "shif";
PIChar str2 = 't';
PIChar str2 = 't';
str1 << str2;
PIString res = "shift";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_cstring){
TEST(PIString_Tests, operator_shift_cstring) {
PIString str1 = "shif";
char str2[] = "t chat";
char str2[] = "t chat";
str1 << str2;
PIString res = "shift chat";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_wchar_t){
PIString str1 = "shif";
TEST(PIString_Tests, operator_shift_wchar_t) {
PIString str1 = "shif";
wchar_t str2[] = L"t cc";
str1 << str2;
PIString res = "shift cc";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_int){
TEST(PIString_Tests, operator_shift_int) {
PIString str1 = "shift ";
int numb = -2147483648;
int numb = -2147483648;
str1 << numb;
PIString res = "shift -2147483648";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_uint){
TEST(PIString_Tests, operator_shift_uint) {
PIString str1 = "shift ";
uint numb = 4294967295;
uint numb = 4294967295;
str1 << numb;
PIString res = "shift 4294967295";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_short){
TEST(PIString_Tests, operator_shift_short) {
PIString str1 = "shift ";
short numb = -32768;
short numb = -32768;
str1 << numb;
PIString res = "shift -32768";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_ushort){
TEST(PIString_Tests, operator_shift_ushort) {
PIString str1 = "shift ";
ushort numb = 65535;
ushort numb = 65535;
str1 << numb;
PIString res = "shift 65535";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_long){
TEST(PIString_Tests, operator_shift_long) {
PIString str1 = "shift ";
long numb = -2147483648;
long numb = -2147483648;
str1 << numb;
PIString res = "shift -2147483648";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_ulong){
TEST(PIString_Tests, operator_shift_ulong) {
PIString str1 = "shift ";
ulong numb = 4294967295;
ulong numb = 4294967295;
str1 << numb;
PIString res = "shift 4294967295";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_llong){
TEST(PIString_Tests, operator_shift_llong) {
PIString str1 = "shift ";
llong numb = -9223372036854775807;
llong numb = -9223372036854775807;
str1 << numb;
PIString res = "shift -9223372036854775807";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_ullong){
TEST(PIString_Tests, operator_shift_ullong) {
PIString str1 = "shift ";
ullong numb = 1844674407370955161;
ullong numb = 1844674407370955161;
str1 << numb;
PIString res = "shift 1844674407370955161";
ASSERT_EQ(res, str1);
}
TEST(PIString_Tests, operator_shift_float){
TEST(PIString_Tests, operator_shift_float) {
PIString str1 = "shift ";
float numb = -67.88999939f;
float numb = -67.88999939f;
str1 << numb;
PIString res = "shift -67.88999939";
ASSERT_EQ(res, str1);

View File

@@ -1,11 +1,12 @@
#include "gtest/gtest.h"
#include "pimathmatrix.h"
#include "gtest/gtest.h"
template<typename Type>
bool cmpSquareMatrixWithValue(PIMathMatrix<Type> matrix, Type val, int num) {
for(int i = 0; i < num; i++) {
for(int j = 0; j < num; j++) {
if(matrix.element(i, j) != val) {
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (matrix.element(i, j) != val) {
return false;
}
}
@@ -32,15 +33,14 @@ TEST(PIMathMatrix_Test, constructor3) {
TEST(PIMathMatrix_Test, identity1) {
auto matrix = PIMathMatrix<double>::identity(3, 3);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(i != j) {
if(matrix[i][j] != 0.0){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != j) {
if (matrix[i][j] != 0.0) {
FAIL();
}
}
else {
if(matrix[i][i] != 1.0){
} else {
if (matrix[i][i] != 1.0) {
FAIL();
}
}
@@ -51,20 +51,18 @@ TEST(PIMathMatrix_Test, identity1) {
TEST(PIMathMatrix_Test, identity2) {
auto matrix = PIMathMatrix<double>::identity(4, 3);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 4; j++) {
if(matrix.element(i,j) != (i == j ? 1. : 0.))
FAIL();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (matrix.element(i, j) != (i == j ? 1. : 0.)) FAIL();
}
}
}
TEST(PIMathMatrixT_Test, element) {
auto matrix = PIMathMatrix<double>::identity(3, 3);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(matrix.element(i,j) != (i == j ? 1. : 0.))
FAIL();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix.element(i, j) != (i == j ? 1. : 0.)) FAIL();
}
}
SUCCEED();
@@ -74,8 +72,8 @@ TEST(PIMathMatrix_Test, matrixRow) {
PIMathVector<double> vector;
vector.resize(3, 3.0);
auto matrix = PIMathMatrix<double>::matrixRow(vector);
for(uint i = 0; i < vector.size(); i++) {
if(matrix[0][i] != 3.0) {
for (uint i = 0; i < vector.size(); i++) {
if (matrix[0][i] != 3.0) {
FAIL();
}
}
@@ -86,8 +84,8 @@ TEST(PIMathMatrix_Test, matrixCol) {
PIMathVector<double> vector;
vector.resize(3, 3.0);
auto matrix = PIMathMatrix<double>::matrixCol(vector);
for(uint i = 0; i < vector.size(); i++) {
if(matrix[i][0] != 3.0) {
for (uint i = 0; i < vector.size(); i++) {
if (matrix[i][0] != 3.0) {
FAIL();
}
}
@@ -100,8 +98,8 @@ TEST(PIMathMatrix_Test, setCol) {
auto matrix = PIMathMatrix<double>::matrixCol(vector);
vector.fill(10.0);
matrix.setCol(0, vector);
for(uint i = 0; i < vector.size(); i++) {
if(matrix[i][0] != 10.0) {
for (uint i = 0; i < vector.size(); i++) {
if (matrix[i][0] != 10.0) {
FAIL();
}
}
@@ -114,8 +112,8 @@ TEST(PIMathMatrix_Test, setRow) {
auto matrix = PIMathMatrix<double>::matrixRow(vector);
vector.fill(10.0);
matrix.setRow(0, vector);
for(uint i = 0; i < vector.size(); i++) {
if(matrix[0][i] != 10.0) {
for (uint i = 0; i < vector.size(); i++) {
if (matrix[0][i] != 10.0) {
FAIL();
}
}
@@ -126,14 +124,15 @@ TEST(PIMathMatrix_Test, swapCols) {
PIMathMatrix<double> origMatr;
PIMathMatrix<double> matrix1;
PIMathVector<double> vector;
uint i1 = 0; uint i2 = 1;
uint i1 = 0;
uint i2 = 1;
double a1[3], a2[3], a3[3];
double b1[3], b2[3], b3[3];
vector.resize(3, 3.0);
vector[0] = 3.0;
vector[1] = 6.0;
vector[2] = 8.0;
matrix1 = origMatr.identity(3, 3);
matrix1 = origMatr.identity(3, 3);
matrix1.setCol(0, vector);
vector[0] = 2.0;
vector[1] = 1.0;
@@ -143,13 +142,13 @@ TEST(PIMathMatrix_Test, swapCols) {
vector[1] = 2.0;
vector[2] = 5.0;
matrix1.setCol(2, vector);
for(int i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
a1[i] = matrix1.element(i, 0);
a2[i] = matrix1.element(i, 1);
a3[i] = matrix1.element(i, 2);
}
matrix1.swapCols(i1, i2);
for(int i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
b1[i] = matrix1.element(i, 0);
b2[i] = matrix1.element(i, 1);
b3[i] = matrix1.element(i, 2);

View File

@@ -1,13 +1,14 @@
#include "gtest/gtest.h"
#include "pimathmatrix.h"
#include "gtest/gtest.h"
const uint rows = 3;
const uint cols = 3;
bool cmpSquareMatrixWithValue(PIMathMatrixT<rows, cols, double> matrix, double val, int num) {
for(int i = 0; i < num; i++) {
for(int j = 0; j < num; j++) {
if(matrix.at(i, j) != val) {
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (matrix.at(i, j) != val) {
return false;
}
}
@@ -17,15 +18,14 @@ bool cmpSquareMatrixWithValue(PIMathMatrixT<rows, cols, double> matrix, double v
TEST(PIMathMatrixT_Test, identity) {
auto matrix = PIMathMatrixT<rows, cols, double>::identity();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(i != j){
if(matrix[i][j] != 0.0){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != j) {
if (matrix[i][j] != 0.0) {
ASSERT_TRUE(false);
}
}
else {
if(matrix[i][i] != 1.0){
} else {
if (matrix[i][i] != 1.0) {
ASSERT_TRUE(false);
}
}
@@ -36,8 +36,8 @@ TEST(PIMathMatrixT_Test, identity) {
TEST(PIMathMatrixT_Test, at) {
auto matrix1 = PIMathMatrixT<rows, cols, double>::identity();
for(uint i = 0; i < rows; i++) {
if(matrix1.at(i,i) != 1.0) {
for (uint i = 0; i < rows; i++) {
if (matrix1.at(i, i) != 1.0) {
ASSERT_TRUE(false);
}
}
@@ -50,30 +50,30 @@ TEST(PIMathMatrixT_Test, filled) {
TEST(PIMathMatrixT_Test, cols) {
PIMathMatrixT<rows, cols, double> matr;
ASSERT_EQ(cols,matr.cols());
ASSERT_EQ(cols, matr.cols());
}
TEST(PIMathMatrixT_Test, rows) {
PIMathMatrixT<rows, cols, double> matr;
ASSERT_EQ(rows,matr.rows());
ASSERT_EQ(rows, matr.rows());
}
TEST(PIMathMatrixT_Test, col) {
PIMathMatrixT<rows, cols, double> matr;
PIMathVectorT<rows, double> vect;
uint g = 2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
vect = matr.col(g);
for(uint i = 0; i < matr.cols(); i++) {
if(matr.element(i, g) != vect[i]) {
uint g = 2;
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
vect = matr.col(g);
for (uint i = 0; i < matr.cols(); i++) {
if (matr.element(i, g) != vect[i]) {
ASSERT_TRUE(false);
}
}
@@ -83,19 +83,19 @@ TEST(PIMathMatrixT_Test, col) {
TEST(PIMathMatrixT_Test, row) {
PIMathMatrixT<rows, cols, double> matr;
PIMathVectorT<rows, double> vect;
uint g = 2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
vect = matr.row(g);
for(uint i = 0; i < matr.rows(); i++) {
if(matr.element(g, i) != vect[i]) {
uint g = 2;
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
vect = matr.row(g);
for (uint i = 0; i < matr.rows(); i++) {
if (matr.element(g, i) != vect[i]) {
ASSERT_TRUE(false);
}
}
@@ -108,10 +108,10 @@ TEST(PIMathMatrixT_Test, setCol) {
vect[0] = 1.0;
vect[1] = 3.0;
vect[2] = 5.0;
uint g = 1;
uint g = 1;
matr.setCol(g, vect);
for(uint i = 0; i < vect.size(); i++) {
if(matr.element(i, g) != vect[i]) {
for (uint i = 0; i < vect.size(); i++) {
if (matr.element(i, g) != vect[i]) {
ASSERT_TRUE(false);
}
}
@@ -124,10 +124,10 @@ TEST(PIMathMatrixT_Test, setRow) {
vect[0] = 1.0;
vect[1] = 3.0;
vect[2] = 5.0;
uint g = 1;
uint g = 1;
matr.setRow(g, vect);
for(uint i = 0; i < vect.size(); i++) {
if(matr.element(g,i) != vect[i]) {
for (uint i = 0; i < vect.size(); i++) {
if (matr.element(g, i) != vect[i]) {
ASSERT_TRUE(false);
}
}
@@ -137,24 +137,23 @@ TEST(PIMathMatrixT_Test, setRow) {
TEST(PIMathMatrixT_Test, swapCols) {
PIMathMatrixT<rows, cols, double> matr;
int g1 = 1, g2 = 2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
const PIMathVectorT<rows, double> before_Vect1 = matr.col(g1);
const PIMathVectorT<rows, double> before_Vect2 = matr.col(g2);
matr.swapCols(g1, g2);
const PIMathVectorT<rows, double> after_Vect1 = matr.col(g1);
const PIMathVectorT<rows, double> after_Vect2 = matr.col(g2);
if((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) {
if ((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) {
ASSERT_TRUE(true);
}
else {
} else {
ASSERT_TRUE(false);
}
}
@@ -162,24 +161,23 @@ TEST(PIMathMatrixT_Test, swapCols) {
TEST(PIMathMatrixT_Test, swapRows) {
PIMathMatrixT<rows, cols, double> matr;
int g1 = 1, g2 = 2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
const PIMathVectorT<rows, double> before_Vect1 = matr.row(g1);
const PIMathVectorT<rows, double> before_Vect2 = matr.row(g2);
matr.swapRows(g1, g2);
const PIMathVectorT<rows, double> after_Vect1 = matr.row(g1);
const PIMathVectorT<rows, double> after_Vect2 = matr.row(g2);
if((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) {
if ((before_Vect1 == after_Vect2) && (before_Vect2 == after_Vect1)) {
ASSERT_TRUE(true);
}
else {
} else {
ASSERT_TRUE(false);
}
}
@@ -189,9 +187,9 @@ TEST(PIMathMatrixT_Test, fill) {
PIMathMatrixT<rows, cols, double> matrix1;
double g = 1.0;
matr.fill(g);
for(uint i = 0; i < cols; i++) {
for(uint j = 0; j < rows; j++) {
matrix1.element(j,i) = g;
for (uint i = 0; i < cols; i++) {
for (uint j = 0; j < rows; j++) {
matrix1.element(j, i) = g;
}
}
ASSERT_TRUE(matr == matrix1);
@@ -231,7 +229,7 @@ TEST(PIMathMatrixT_Test, isNullFalse) {
TEST(PIMathMatrixT_Test, operator_Assignment) {
PIMathMatrixT<rows, cols, double> matrix1;
auto matrix2 = PIMathMatrixT<rows, cols, double>(6.72);
matrix1 = matrix2;
matrix1 = matrix2;
ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 6.72, rows));
}
@@ -292,8 +290,8 @@ TEST(PIMathMatrixT_Test, operator_Not_EqualFalse) {
}
TEST(PIMathMatrixT_Test, operator_Addition_Assignment) {
auto matrix1 = PIMathMatrixT<rows, cols, double>(6.72) ;
auto matrix2 = PIMathMatrixT<rows, cols, double>(1.0) ;
auto matrix1 = PIMathMatrixT<rows, cols, double>(6.72);
auto matrix2 = PIMathMatrixT<rows, cols, double>(1.0);
matrix1 += matrix2;
ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1, 7.72, rows));
}
@@ -342,16 +340,16 @@ TEST(PIMathMatrixT_Test, determinantIfSquare) {
double d;
double i = 59.0;
PIMathMatrixT<rows, cols, double> matr;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
d = matr.determinant();
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
d = matr.determinant();
ASSERT_DOUBLE_EQ(i, d);
}
@@ -361,22 +359,22 @@ TEST(PIMathMatrixT_Test, invert) {
PIMathMatrixT<rows, cols, double> matrix3;
PIMathMatrixT<rows, cols, double> matr;
double d1, d2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
matrix2 = matr;
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
matrix2 = matr;
matr.invert();
d1 = matr.determinant();
d2 = matrix2.determinant();
d1 = matr.determinant();
d2 = matrix2.determinant();
matrix3 = matrix1;
matrix1.invert();
ASSERT_TRUE((matrix1 == matrix3) && piCompare(d1, 1/d2));
ASSERT_TRUE((matrix1 == matrix3) && piCompare(d1, 1 / d2));
}
TEST(PIMathMatrixT_Test, inverted) {
@@ -385,41 +383,40 @@ TEST(PIMathMatrixT_Test, inverted) {
PIMathMatrixT<rows, cols, double> matrix3;
PIMathMatrixT<rows, cols, double> matr;
double d1, d2;
matrix1 = matr.identity();
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
matrix2 = matr.inverted();
d1 = matr.determinant();
d2 = matrix2.determinant();
matrix3 = matrix1.inverted();
ASSERT_TRUE((matrix1 == matrix3) && (round((1/d1)*10000)/10000 == round(d2*10000)/10000));
matrix1 = matr.identity();
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
matrix2 = matr.inverted();
d1 = matr.determinant();
d2 = matrix2.determinant();
matrix3 = matrix1.inverted();
ASSERT_TRUE((matrix1 == matrix3) && (round((1 / d1) * 10000) / 10000 == round(d2 * 10000) / 10000));
}
TEST(PIMathMatrixT_Test, toUpperTriangular) {
PIMathMatrixT<rows, cols, double> matrix;
double d1, d2 = 1;
PIMathMatrixT<rows, cols, double> matr;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
matrix = matr.toUpperTriangular();
d1 = matrix.determinant();
for(uint i = 0; i < cols; i++)
{
d2 = d2*matrix.at(i,i);
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
matrix = matr.toUpperTriangular();
d1 = matrix.determinant();
for (uint i = 0; i < cols; i++) {
d2 = d2 * matrix.at(i, i);
}
ASSERT_DOUBLE_EQ(d1, d2);
}
@@ -429,33 +426,32 @@ TEST(PIMathMatrixT_Test, transposed) {
PIMathMatrixT<rows, cols, double> matrix2;
PIMathMatrixT<rows, cols, double> matr;
double d1, d2;
matr.element(0,0) = 3;
matr.element(0,1) = 6;
matr.element(0,2) = 8;
matr.element(1,0) = 2;
matr.element(1,1) = 1;
matr.element(1,2) = 4;
matr.element(2,0) = 6;
matr.element(2,1) = 2;
matr.element(2,2) = 5;
d1 = matr.determinant();
matrix1 = matr.transposed();
d2 = matrix1.determinant();
matrix2 = matrix1.transposed();
matr.element(0, 0) = 3;
matr.element(0, 1) = 6;
matr.element(0, 2) = 8;
matr.element(1, 0) = 2;
matr.element(1, 1) = 1;
matr.element(1, 2) = 4;
matr.element(2, 0) = 6;
matr.element(2, 1) = 2;
matr.element(2, 2) = 5;
d1 = matr.determinant();
matrix1 = matr.transposed();
d2 = matrix1.determinant();
matrix2 = matrix1.transposed();
ASSERT_TRUE((d1 == d2) && (matr == matrix2));
}
TEST(PIMathMatrixT_Test, rotation_2x2) {
double angle = 1.0;
auto matrix = PIMathMatrixT<2u, 2u, double>::identity();
auto matrix = PIMathMatrixT<2u, 2u, double>::identity();
matrix.rotate(angle);
double c = cos(angle);
double s = sin(angle);
ASSERT_TRUE((c == matrix.at(1u,1u)) && (c == matrix.at(0u,0u)) && (-s == matrix.at(0u,1u)) && (s == matrix.at(1u,0u)));
ASSERT_TRUE((c == matrix.at(1u, 1u)) && (c == matrix.at(0u, 0u)) && (-s == matrix.at(0u, 1u)) && (s == matrix.at(1u, 0u)));
}
TEST(PIMathMatrixT_Test, matrixMultiplication)
{
TEST(PIMathMatrixT_Test, matrixMultiplication) {
auto matrix1 = PIMathMatrixT<rows, cols, double>(1.5);
auto matrix2 = PIMathMatrixT<rows, cols, double>(2.5);
ASSERT_TRUE(cmpSquareMatrixWithValue(matrix1 * matrix2, 11.25, 3));
@@ -463,9 +459,9 @@ TEST(PIMathMatrixT_Test, matrixMultiplication)
TEST(PIMathMatrixT_Test, matrixAndVectorMultiplication) {
auto matrix1 = PIMathMatrixT<rows, cols, double>(1.5);
auto vector = PIMathVectorT<rows, double>(2.5);
for(uint i = 0; i < 2; i++) {
if((matrix1 * vector)[i] != 11.25) {
auto vector = PIMathVectorT<rows, double>(2.5);
for (uint i = 0; i < 2; i++) {
if ((matrix1 * vector)[i] != 11.25) {
ASSERT_TRUE(false);
}
}
@@ -474,9 +470,9 @@ TEST(PIMathMatrixT_Test, matrixAndVectorMultiplication) {
TEST(PIMathMatrixT_Test, vectorAndMatrixMultiplication) {
auto matrix1 = PIMathMatrixT<rows, cols, double>(1.5);
auto vector = PIMathVectorT<rows, double>(2.5);
for(uint i = 0; i < 2; i++) {
if((vector * matrix1)[i] != 11.25) {
auto vector = PIMathVectorT<rows, double>(2.5);
for (uint i = 0; i < 2; i++) {
if ((vector * matrix1)[i] != 11.25) {
ASSERT_TRUE(false);
}
}
@@ -484,5 +480,5 @@ TEST(PIMathMatrixT_Test, vectorAndMatrixMultiplication) {
TEST(PIMathMatrixT_Test, valAndMatrixMultiplication) {
auto matrix1 = PIMathMatrixT<rows, cols, double>(1.5);
ASSERT_TRUE(cmpSquareMatrixWithValue(25.0*matrix1, 37.5, 3));
ASSERT_TRUE(cmpSquareMatrixWithValue(25.0 * matrix1, 37.5, 3));
}

View File

@@ -1,23 +1,24 @@
#include "gtest/gtest.h"
#include "pivector2d.h"
int ROWS_COUNT_INIT = 31;
int ROWS_COUNT_INCREASE = 41;
int ROWS_COUNT_REDUCE = 22;
#include "gtest/gtest.h"
int COLS_COUNT_INIT = 34;
int ROWS_COUNT_INIT = 31;
int ROWS_COUNT_INCREASE = 41;
int ROWS_COUNT_REDUCE = 22;
int COLS_COUNT_INIT = 34;
int COLS_COUNT_INCREASE = 44;
int COLS_COUNT_REDUCE = 13;
int COLS_COUNT_REDUCE = 13;
void assert_fill_with(PIVector2D<int> vec, int rows, int cols) {
for(int r = 0; r < rows; r++) {
for(int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
ASSERT_EQ(vec.element(r, c), r * COLS_COUNT_INIT + c);
}
}
}
class Vector2D : public ::testing::Test {
class Vector2D: public ::testing::Test {
protected:
PIVector2D<int> vec = PIVector2D<int>(ROWS_COUNT_INIT, COLS_COUNT_INIT);

View File

@@ -1,26 +1,28 @@
#include "gtest/gtest.h"
#include "piobject.h"
#include "gtest/gtest.h"
class Object : public PIObject
{
class Object: public PIObject {
PIOBJECT(Object)
public:
Object() {
x=0;
// CONNECTL(this, deleted, [](PIObject * o){
// piCout << "deteted" << o;
// });
x = 0;
// CONNECTL(this, deleted, [](PIObject * o){
// piCout << "deteted" << o;
// });
}
void test() {event_test();}
void test_val() {event_val(x);}
int getX() const {return x;}
void test() { event_test(); }
void test_val() { event_val(x); }
int getX() const { return x; }
EVENT(event_test)
EVENT1(event_val, int, arg)
EVENT_HANDLER(void, handler_test) {x++;}
EVENT_HANDLER1(void, handler_val, int, arg) {x = arg;}
EVENT_HANDLER(void, handler_test) { x++; }
EVENT_HANDLER1(void, handler_val, int, arg) { x = arg; }
private:
int x;
};
@@ -46,7 +48,7 @@ TEST(PiobjectConnections, CONNECT0) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 1);
// piCout << "================";
// piCout << "================";
}
@@ -81,7 +83,7 @@ TEST(PiobjectConnections, CONNECT0_DISCONNECT) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 1);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTU) {
@@ -104,7 +106,7 @@ TEST(PiobjectConnections, CONNECTU) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 1);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTU_DISCONNECT) {
@@ -154,7 +156,7 @@ TEST(PiobjectConnections, CONNECTU_DISCONNECT) {
a.test_val();
ASSERT_EQ(a.getX(), -1);
ASSERT_EQ(b.getX(), 99);
// piCout << "================";
// piCout << "================";
}
@@ -169,9 +171,7 @@ TEST(PiobjectConnections, CONNECTL) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 0);
CONNECTL(&a, event_test, [&b](){
b.handler_val(7);
});
CONNECTL(&a, event_test, [&b]() { b.handler_val(7); });
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 0);
a.test();
@@ -180,7 +180,7 @@ TEST(PiobjectConnections, CONNECTL) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 7);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTL_DISCONNECT) {
@@ -194,9 +194,7 @@ TEST(PiobjectConnections, CONNECTL_DISCONNECT) {
b.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 0);
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 0);
a.test();
@@ -214,21 +212,17 @@ TEST(PiobjectConnections, CONNECTL_DISCONNECT) {
a.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 2);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTL_twice) {
Object a;
Object b;
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
a.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 1);
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
a.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 3);
@@ -239,21 +233,17 @@ TEST(PiobjectConnections, CONNECTL_twice) {
a.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 0);
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
a.test();
ASSERT_EQ(a.getX(), 0);
ASSERT_EQ(b.getX(), 1);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECT_AFTER_DISCONNECT) {
Object a;
Object b;
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
a.test();
ASSERT_EQ(b.getX(), 1);
PIObject::piDisconnect(&a, "event_test");
@@ -265,15 +255,13 @@ TEST(PiobjectConnections, CONNECT_AFTER_DISCONNECT) {
a.test_val();
ASSERT_EQ(a.getX(), 8);
ASSERT_EQ(b.getX(), 8);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTL_WRONG_DISCONNECT) {
Object a;
Object b;
CONNECTL(&a, event_test, [&b](){
b.handler_test();
});
CONNECTL(&a, event_test, [&b]() { b.handler_test(); });
a.test();
ASSERT_EQ(b.getX(), 1);
PIObject::piDisconnect(&b, "event_test");
@@ -282,7 +270,7 @@ TEST(PiobjectConnections, CONNECTL_WRONG_DISCONNECT) {
PIObject::piDisconnect(&a, "event_test");
a.test();
ASSERT_EQ(b.getX(), 2);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECT_WRONG_DISCONNECT) {
@@ -298,7 +286,7 @@ TEST(PiobjectConnections, CONNECT_WRONG_DISCONNECT) {
PIObject::piDisconnect(&a, "event_test");
a.test();
ASSERT_EQ(b.getX(), 2);
// piCout << "================";
// piCout << "================";
}
TEST(PiobjectConnections, CONNECTU_WRONG_DISCONNECT) {
@@ -313,5 +301,5 @@ TEST(PiobjectConnections, CONNECTU_WRONG_DISCONNECT) {
PIObject::piDisconnect(&a, "event_test");
a.test();
ASSERT_EQ(b.getX(), 2);
// piCout << "================";
// piCout << "================";
}

View File

@@ -1,23 +1,26 @@
#include "gtest/gtest.h"
#include "piobject.h"
#include "gtest/gtest.h"
std::atomic<int> obj_cnt;
class Send: public PIObject {
PIOBJECT(Send)
public:
Send() {obj_cnt++;}
~Send() {obj_cnt--;}
EVENT1(ev, PIObject * , o)
Send() { obj_cnt++; }
~Send() { obj_cnt--; }
EVENT1(ev, PIObject *, o)
};
class Recv: public PIObject {
PIOBJECT(Recv)
public:
Recv() {obj_cnt++;}
~Recv() {obj_cnt--;}
EVENT_HANDLER1(void, eh, PIObject * , o) {
Recv() { obj_cnt++; }
~Recv() { obj_cnt--; }
EVENT_HANDLER1(void, eh, PIObject *, o) {
o->deleteLater();
piMSleep(10);
}
@@ -25,7 +28,7 @@ public:
TEST(Piobject, deleteLater) {
obj_cnt = 0;
obj_cnt = 0;
Send * s = new Send();
Recv * r = new Recv();
CONNECTU(s, ev, r, eh);
@@ -37,14 +40,16 @@ TEST(Piobject, deleteLater) {
PIVector<Send *> s2;
s2.resize(100, new Send());
for (auto o : s2) o->deleteLater();
for (auto o: s2)
o->deleteLater();
piMSleep(10);
ASSERT_EQ(obj_cnt, 0);
s2.clear();
PIVector<Recv *> r2;
r2.resize(100, [](size_t i){return new Recv();});
for (auto o : r2) o->deleteLater();
r2.resize(100, [](size_t i) { return new Recv(); });
for (auto o: r2)
o->deleteLater();
piMSleep(10);
ASSERT_EQ(obj_cnt, 0);
r2.clear();