From 449978bda013e6d926b8d91b1225e464f28fa6c0 Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Tue, 17 Mar 2026 19:07:01 +0300 Subject: [PATCH] revert main and more tests --- main.cpp | 21 --------- tests/thread/piprotectedvariable_test.cpp | 56 ++++++++--------------- 2 files changed, 18 insertions(+), 59 deletions(-) diff --git a/main.cpp b/main.cpp index 7dda6513..cf2f6235 100644 --- a/main.cpp +++ b/main.cpp @@ -20,27 +20,6 @@ inline PIByteArray SMBusTypeInfo_genHash(PIString n) { int main(int argc, char * argv[]) { - PIProtectedVariable 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; diff --git a/tests/thread/piprotectedvariable_test.cpp b/tests/thread/piprotectedvariable_test.cpp index 09f52d05..77c43c05 100644 --- a/tests/thread/piprotectedvariable_test.cpp +++ b/tests/thread/piprotectedvariable_test.cpp @@ -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 . -*/ - #include "piprotectedvariable.h" #include "pistring.h" #include "pithread.h" +#include "piliterals_time.h" #include "gtest/gtest.h" #include -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 pv; - atomic writeCount(0); - atomic readCount(0); - atomic invalidReads(0); + std::atomic writeCount(0); + std::atomic readCount(0); + std::atomic 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);