Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:22

0001 /*----------------------------------------------------------------------
0002 
0003 Test program for edm::TypeID class.
0004 Changed by Viji on 29-06-2005
0005 
0006  ----------------------------------------------------------------------*/
0007 
0008 #include <cassert>
0009 #include <cppunit/extensions/HelperMacros.h>
0010 #include "FWCore/Utilities/interface/CallOnceNoWait.h"
0011 #include "FWCore/Utilities/interface/CallNTimesNoWait.h"
0012 
0013 #include <thread>
0014 #include <atomic>
0015 
0016 class testCallXNoWait : public CppUnit::TestFixture {
0017   CPPUNIT_TEST_SUITE(testCallXNoWait);
0018 
0019   CPPUNIT_TEST(onceTest);
0020   CPPUNIT_TEST(nTimesTest);
0021   CPPUNIT_TEST(onceThreadedTest);
0022   CPPUNIT_TEST(nTimesThreadedTest);
0023 
0024   CPPUNIT_TEST_SUITE_END();
0025 
0026 public:
0027   void setUp() {}
0028   void tearDown() {}
0029 
0030   void onceTest();
0031   void nTimesTest();
0032 
0033   void onceThreadedTest();
0034   void nTimesThreadedTest();
0035 };
0036 
0037 ///registration of the test so that the runner can find it
0038 CPPUNIT_TEST_SUITE_REGISTRATION(testCallXNoWait);
0039 
0040 void testCallXNoWait::onceTest() {
0041   edm::CallOnceNoWait guard;
0042 
0043   unsigned int iCount = 0;
0044 
0045   guard([&iCount]() { ++iCount; });
0046 
0047   CPPUNIT_ASSERT(iCount == 1);
0048 
0049   guard([&iCount]() { ++iCount; });
0050   CPPUNIT_ASSERT(iCount == 1);
0051 }
0052 
0053 void testCallXNoWait::nTimesTest() {
0054   edm::CallNTimesNoWait guard{3};
0055 
0056   unsigned int iCount = 0;
0057 
0058   for (unsigned int i = 0; i < 6; ++i) {
0059     guard([&iCount]() { ++iCount; });
0060     if (i < 3) {
0061       CPPUNIT_ASSERT(iCount == i + 1);
0062     } else {
0063       CPPUNIT_ASSERT(iCount == 3);
0064     }
0065   }
0066 }
0067 
0068 void testCallXNoWait::onceThreadedTest() {
0069   edm::CallOnceNoWait guard;
0070 
0071   std::atomic<unsigned int> iCount{0};
0072 
0073   std::vector<std::thread> threads;
0074 
0075   std::atomic<bool> start{false};
0076 
0077   for (unsigned int i = 0; i < 4; ++i) {
0078     threads.emplace_back([&guard, &iCount, &start]() {
0079       while (not start) {
0080       }
0081       guard([&iCount]() { ++iCount; });
0082     });
0083   }
0084   CPPUNIT_ASSERT(iCount == 0);
0085 
0086   start = true;
0087   for (auto& t : threads) {
0088     t.join();
0089   }
0090   CPPUNIT_ASSERT(iCount == 1);
0091 }
0092 
0093 void testCallXNoWait::nTimesThreadedTest() {
0094   const unsigned short kMaxTimes = 3;
0095   edm::CallNTimesNoWait guard(kMaxTimes);
0096 
0097   std::atomic<unsigned int> iCount{0};
0098 
0099   std::vector<std::thread> threads;
0100 
0101   std::atomic<bool> start{false};
0102 
0103   for (unsigned int i = 0; i < 2 * kMaxTimes; ++i) {
0104     threads.emplace_back([&guard, &iCount, &start]() {
0105       while (not start) {
0106       }
0107       guard([&iCount]() { ++iCount; });
0108     });
0109   }
0110   CPPUNIT_ASSERT(iCount == 0);
0111 
0112   start = true;
0113   for (auto& t : threads) {
0114     t.join();
0115   }
0116   CPPUNIT_ASSERT(iCount == kMaxTimes);
0117 }