Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-27 07:20:17

0001 // -*- C++ -*-
0002 //
0003 // Package:     PhysicsTools/MVAComputer
0004 // Class  :     testBitSet.cppunit
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Fri, 23 Jan 2015 18:54:27 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include <cppunit/extensions/HelperMacros.h>
0017 
0018 #include "PhysicsTools/MVAComputer/interface/BitSet.h"
0019 
0020 class testBitSet : public CppUnit::TestFixture {
0021   CPPUNIT_TEST_SUITE(testBitSet);
0022 
0023   CPPUNIT_TEST(bitManipulationTest);
0024   CPPUNIT_TEST(multiWordTest);
0025   CPPUNIT_TEST_SUITE_END();
0026 
0027 public:
0028   void setUp() {}
0029   void tearDown() {}
0030 
0031   void multiWordTest();
0032   void bitManipulationTest();
0033 };
0034 
0035 ///registration of the test so that the runner can find it
0036 CPPUNIT_TEST_SUITE_REGISTRATION(testBitSet);
0037 
0038 using namespace PhysicsTools;
0039 
0040 namespace {
0041   unsigned int bit(int pos) { return (~(1U << pos)) + 1; }
0042 
0043   unsigned int neg(int pos) { return -(1 << pos); }
0044 }  // namespace
0045 
0046 void testBitSet::bitManipulationTest() {
0047   for (unsigned int i = 0; i < 31; ++i) {
0048     CPPUNIT_ASSERT(bit(i) == neg(i));
0049   }
0050 
0051   //bit 31 was causing UBSAN problems for neg
0052   // so if neg != bit it is from the undefined
0053   // behavior
0054   CPPUNIT_ASSERT(bit(31) == 0x80000000);
0055   //Print a warnings instead of failure due to undefined behavior
0056   //CPPUNIT_ASSERT(neg(31) == 0x80000000);
0057   if (neg(31) != 0x80000000) {
0058     std::cout << "Warning: Due to undefined behavior neg(31) != 0x80000000." << std::endl;
0059   }
0060 }
0061 
0062 void testBitSet::multiWordTest() {
0063   BitSet b33(33);
0064 
0065   CPPUNIT_ASSERT(b33.size() == 33);
0066   CPPUNIT_ASSERT(b33.bits() == 0);
0067 
0068   CPPUNIT_ASSERT(bool(b33.iter()) == false);
0069 
0070   for (int i = 0; i < 33; ++i) {
0071     CPPUNIT_ASSERT(b33[i] == false);
0072   }
0073 
0074   b33[1] = true;
0075   CPPUNIT_ASSERT(b33[1] == true);
0076   CPPUNIT_ASSERT(b33.bits() == 1);
0077 
0078   {
0079     auto it = b33.iter();
0080     CPPUNIT_ASSERT(bool(it) == true);
0081     CPPUNIT_ASSERT(it() == 1);
0082 
0083     ++it;
0084     CPPUNIT_ASSERT(bool(it) == false);
0085   }
0086 
0087   b33[30] = true;
0088   CPPUNIT_ASSERT(b33.bits() == 2);
0089   {
0090     auto it = b33.iter();
0091     CPPUNIT_ASSERT(bool(it) == true);
0092     CPPUNIT_ASSERT(it() == 1);
0093 
0094     ++it;
0095     CPPUNIT_ASSERT(bool(it) == true);
0096     CPPUNIT_ASSERT(it() == 30);
0097 
0098     ++it;
0099     CPPUNIT_ASSERT(bool(it) == false);
0100   }
0101 
0102   b33[32] = true;
0103   CPPUNIT_ASSERT(b33.bits() == 3);
0104   {
0105     auto it = b33.iter();
0106     CPPUNIT_ASSERT(bool(it) == true);
0107     CPPUNIT_ASSERT(it() == 1);
0108 
0109     ++it;
0110     CPPUNIT_ASSERT(bool(it) == true);
0111     CPPUNIT_ASSERT(it() == 30);
0112 
0113     ++it;
0114     CPPUNIT_ASSERT(bool(it) == true);
0115     CPPUNIT_ASSERT(it() == 32);
0116 
0117     ++it;
0118     CPPUNIT_ASSERT(bool(it) == false);
0119   }
0120 }