revert main and more tests
This commit is contained in:
21
main.cpp
21
main.cpp
@@ -20,27 +20,6 @@ inline PIByteArray SMBusTypeInfo_genHash(PIString n) {
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
PIProtectedVariable<double> pv(3.0);
|
|
||||||
piCout << pv.get();
|
|
||||||
{
|
|
||||||
auto ref = pv.getRef();
|
|
||||||
piCout << *ref;
|
|
||||||
*ref = 11.;
|
|
||||||
piCout << *ref;
|
|
||||||
}
|
|
||||||
piCout << pv.get();
|
|
||||||
{
|
|
||||||
auto ref = pv.getRef();
|
|
||||||
piCout << *ref;
|
|
||||||
*ref = 12.;
|
|
||||||
piCout << *ref;
|
|
||||||
auto ref2 = std::move(ref);
|
|
||||||
piCout << *ref2;
|
|
||||||
}
|
|
||||||
piCout << pv.get();
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
|
|
||||||
PICrypt _crypt;
|
PICrypt _crypt;
|
||||||
// auto ba = PIFile::readAll("logo.png");
|
// auto ba = PIFile::readAll("logo.png");
|
||||||
PIString str = "hello!"_a;
|
PIString str = "hello!"_a;
|
||||||
|
|||||||
@@ -1,32 +1,11 @@
|
|||||||
//! \~\file piprotectedvariable_test.cpp
|
|
||||||
//! \~\brief Unit tests for PIProtectedVariable class
|
|
||||||
/*
|
|
||||||
PIP - Platform Independent Primitives
|
|
||||||
Unit tests for PIProtectedVariable
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "piprotectedvariable.h"
|
#include "piprotectedvariable.h"
|
||||||
#include "pistring.h"
|
#include "pistring.h"
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
|
#include "piliterals_time.h"
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
// Basic functionality tests
|
// Basic functionality tests
|
||||||
TEST(PIProtectedVariable_Basic, BasicFunctionality) {
|
TEST(PIProtectedVariable_Basic, BasicFunctionality) {
|
||||||
// Test basic set/get with different types
|
// Test basic set/get with different types
|
||||||
@@ -65,22 +44,30 @@ TEST(PIProtectedVariable_Basic, BasicFunctionality) {
|
|||||||
ptr->x = 100;
|
ptr->x = 100;
|
||||||
EXPECT_EQ(pvStruct.get().x, 100);
|
EXPECT_EQ(pvStruct.get().x, 100);
|
||||||
|
|
||||||
// Test move semantics for Pointer
|
// Test for Pointer
|
||||||
pvInt.set(42);
|
pvInt.set(42);
|
||||||
auto ptr1 = pvInt.getRef();
|
auto ptr1 = pvInt.getRef();
|
||||||
|
EXPECT_EQ(*ptr1, 42);
|
||||||
|
*ptr1 = 55;
|
||||||
|
EXPECT_EQ(*ptr1, 55);
|
||||||
auto ptr2 = std::move(ptr1);
|
auto ptr2 = std::move(ptr1);
|
||||||
EXPECT_EQ(*ptr2, 42);
|
EXPECT_EQ(*ptr2, 55);
|
||||||
*ptr2 = 100;
|
*ptr2 = 100;
|
||||||
EXPECT_EQ(pvInt.get(), 100);
|
EXPECT_EQ(*ptr2, 100);
|
||||||
|
auto ptr3 = pvInt.getRef();
|
||||||
|
EXPECT_EQ(*ptr3, 100);
|
||||||
|
*ptr3 = 333;
|
||||||
|
EXPECT_EQ(*ptr3, 333);
|
||||||
|
EXPECT_EQ(pvInt.get(), 333);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thread safety tests
|
// Thread safety tests
|
||||||
TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
|
TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
|
||||||
PIProtectedVariable<int> pv;
|
PIProtectedVariable<int> pv;
|
||||||
|
|
||||||
atomic<int> writeCount(0);
|
std::atomic<int> writeCount(0);
|
||||||
atomic<int> readCount(0);
|
std::atomic<int> readCount(0);
|
||||||
atomic<int> invalidReads(0);
|
std::atomic<int> invalidReads(0);
|
||||||
const int NUM_ITERATIONS = 1000;
|
const int NUM_ITERATIONS = 1000;
|
||||||
const int NUM_WRITERS = 10;
|
const int NUM_WRITERS = 10;
|
||||||
const int NUM_READERS = 20;
|
const int NUM_READERS = 20;
|
||||||
@@ -114,16 +101,9 @@ TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start all threads
|
threads.forEach([](PIThread * & t) {t->startOnce();});
|
||||||
for (auto * t: threads) {
|
threads.forEach([](PIThread * & t) {t->waitForFinish(2_s);});
|
||||||
t->startOnce();
|
piDeleteAll(threads);
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for all threads to finish
|
|
||||||
for (auto * t: threads) {
|
|
||||||
t->waitForFinish(PISystemTime::fromSeconds(5));
|
|
||||||
delete t;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify results
|
// Verify results
|
||||||
EXPECT_EQ(writeCount, TOTAL_WRITES);
|
EXPECT_EQ(writeCount, TOTAL_WRITES);
|
||||||
|
|||||||
Reference in New Issue
Block a user