revert main and more tests

This commit is contained in:
2026-03-17 19:07:01 +03:00
parent fe01c353e6
commit 449978bda0
2 changed files with 18 additions and 59 deletions

View File

@@ -20,27 +20,6 @@ inline PIByteArray SMBusTypeInfo_genHash(PIString n) {
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;
// auto ba = PIFile::readAll("logo.png");
PIString str = "hello!"_a;

View File

@@ -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 "pistring.h"
#include "pithread.h"
#include "piliterals_time.h"
#include "gtest/gtest.h"
#include <atomic>
using namespace std;
// Basic functionality tests
TEST(PIProtectedVariable_Basic, BasicFunctionality) {
// Test basic set/get with different types
@@ -65,22 +44,30 @@ TEST(PIProtectedVariable_Basic, BasicFunctionality) {
ptr->x = 100;
EXPECT_EQ(pvStruct.get().x, 100);
// Test move semantics for Pointer
// Test for Pointer
pvInt.set(42);
auto ptr1 = pvInt.getRef();
EXPECT_EQ(*ptr1, 42);
*ptr1 = 55;
EXPECT_EQ(*ptr1, 55);
auto ptr2 = std::move(ptr1);
EXPECT_EQ(*ptr2, 42);
EXPECT_EQ(*ptr2, 55);
*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
TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
PIProtectedVariable<int> pv;
atomic<int> writeCount(0);
atomic<int> readCount(0);
atomic<int> invalidReads(0);
std::atomic<int> writeCount(0);
std::atomic<int> readCount(0);
std::atomic<int> invalidReads(0);
const int NUM_ITERATIONS = 1000;
const int NUM_WRITERS = 10;
const int NUM_READERS = 20;
@@ -114,16 +101,9 @@ TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
}));
}
// Start all threads
for (auto * t: threads) {
t->startOnce();
}
// Wait for all threads to finish
for (auto * t: threads) {
t->waitForFinish(PISystemTime::fromSeconds(5));
delete t;
}
threads.forEach([](PIThread * & t) {t->startOnce();});
threads.forEach([](PIThread * & t) {t->waitForFinish(2_s);});
piDeleteAll(threads);
// Verify results
EXPECT_EQ(writeCount, TOTAL_WRITES);