Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:39

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   CPPUNIT_ASSERT(neg(31) == 0x80000000);
0056 }
0057 
0058 void testBitSet::multiWordTest() {
0059   BitSet b33(33);
0060 
0061   CPPUNIT_ASSERT(b33.size() == 33);
0062   CPPUNIT_ASSERT(b33.bits() == 0);
0063 
0064   CPPUNIT_ASSERT(bool(b33.iter()) == false);
0065 
0066   for (int i = 0; i < 33; ++i) {
0067     CPPUNIT_ASSERT(b33[i] == false);
0068   }
0069 
0070   b33[1] = true;
0071   CPPUNIT_ASSERT(b33[1] == true);
0072   CPPUNIT_ASSERT(b33.bits() == 1);
0073 
0074   {
0075     auto it = b33.iter();
0076     CPPUNIT_ASSERT(bool(it) == true);
0077     CPPUNIT_ASSERT(it() == 1);
0078 
0079     ++it;
0080     CPPUNIT_ASSERT(bool(it) == false);
0081   }
0082 
0083   b33[30] = true;
0084   CPPUNIT_ASSERT(b33.bits() == 2);
0085   {
0086     auto it = b33.iter();
0087     CPPUNIT_ASSERT(bool(it) == true);
0088     CPPUNIT_ASSERT(it() == 1);
0089 
0090     ++it;
0091     CPPUNIT_ASSERT(bool(it) == true);
0092     CPPUNIT_ASSERT(it() == 30);
0093 
0094     ++it;
0095     CPPUNIT_ASSERT(bool(it) == false);
0096   }
0097 
0098   b33[32] = true;
0099   CPPUNIT_ASSERT(b33.bits() == 3);
0100   {
0101     auto it = b33.iter();
0102     CPPUNIT_ASSERT(bool(it) == true);
0103     CPPUNIT_ASSERT(it() == 1);
0104 
0105     ++it;
0106     CPPUNIT_ASSERT(bool(it) == true);
0107     CPPUNIT_ASSERT(it() == 30);
0108 
0109     ++it;
0110     CPPUNIT_ASSERT(bool(it) == true);
0111     CPPUNIT_ASSERT(it() == 32);
0112 
0113     ++it;
0114     CPPUNIT_ASSERT(bool(it) == false);
0115   }
0116 }