Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:27

0001 // Unit test for L1GctHtMiss class.
0002 //
0003 // NOTE:  "Out-Of-Range" input test commented out due to the maximal tedium
0004 //        involved in testing with ctor out-of-range conditions...  I am weak :-(
0005 //
0006 // Author Robert Frazier
0007 
0008 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctHtMiss.h"
0009 
0010 #include <iostream>
0011 #include <cstdlib>
0012 
0013 using namespace std;
0014 
0015 // Simple class that holds test input data and expected test output data
0016 class TestIO {
0017 public:
0018   TestIO()
0019       : rawInput(0),
0020         bxInput(0),
0021         etInput(0),
0022         phiInput(0),
0023         overflowInput(0),
0024         rawOutput(0),
0025         bxOutput(0),
0026         etOutput(0),
0027         phiOutput(0),
0028         overflowOutput(0) {}
0029 
0030   uint32_t rawInput;
0031   int16_t bxInput;
0032   unsigned etInput;
0033   unsigned phiInput;
0034   bool overflowInput;
0035 
0036   uint32_t rawOutput;
0037   int16_t bxOutput;
0038   unsigned etOutput;
0039   unsigned phiOutput;
0040   bool overflowOutput;
0041 };
0042 
0043 // Function prototypes
0044 bool testL1GctHtMiss(const std::string& testLabel, const TestIO& testIO);
0045 
0046 bool testL1GctHtMissInstance(const std::string& testLabel,
0047                              L1GctHtMiss& testObj,
0048                              const TestIO& testIO,
0049                              bool bxIsZeroNotValueInTestIO);  // A hack, as I'm sick of writing this goddamn test.
0050 
0051 bool doObjTest(L1GctHtMiss& testObj,
0052                const TestIO& testIO,
0053                bool bxIsZeroNotValueInTestIO);  // A hack, as I'm sick of writing this goddamn test.
0054 
0055 int main() {
0056   cout << "---------------------------------------" << endl;
0057   cout << "RUNNING UNIT TEST FOR L1GctHtMiss CLASS" << endl;
0058   cout << "---------------------------------------" << endl;
0059 
0060   bool unitTestPassed = true;  // Try and prove this wrong...
0061 
0062   // "Out-Of-Range" test input and expected output.
0063   /*  Excluded for now.  Special conditions in ctors for out of range data make testing properly a tedious nightmare.
0064   TestIO oorTestData;
0065   oorTestData.rawInput = 0xffffffff;
0066   oorTestData.bxInput = 0x3fff;
0067   oorTestData.etInput = 0xffffffff;
0068   oorTestData.phiInput = 0xffffffff;
0069   oorTestData.overflowInput = true;
0070   oorTestData.rawOutput = 0x1fff;
0071   oorTestData.bxOutput = oorTestData.bxInput;
0072   oorTestData.etOutput = 0x7f;
0073   oorTestData.phiOutput = 0x1f;
0074   oorTestData.overflowOutput = oorTestData.overflowInput;
0075   */
0076 
0077   // Max sensible test input and expected output.
0078   TestIO maxTestData;
0079   maxTestData.rawInput = 0xfffffff1;
0080   maxTestData.bxInput = 0x3fff;
0081   maxTestData.etInput = 0x7f;
0082   maxTestData.phiInput = 0x11;
0083   maxTestData.overflowInput = true;
0084   maxTestData.rawOutput = 0x1ff1;
0085   maxTestData.bxOutput = maxTestData.bxInput;
0086   maxTestData.etOutput = maxTestData.etInput;
0087   maxTestData.phiOutput = maxTestData.phiInput;
0088   maxTestData.overflowOutput = maxTestData.overflowInput;
0089 
0090   // Random test input and expected output.
0091   TestIO rndTestData;
0092   rndTestData.rawInput = 0xd3b7a88e;
0093   rndTestData.bxInput = -17;
0094   rndTestData.etInput = 0x44;         // Corresponds with value that is packed in rawInput above
0095   rndTestData.phiInput = 0xe;         // Corresponds with value that is packed in rawInput above
0096   rndTestData.overflowInput = false;  // Corresponds with value that is packed in rawInput above
0097   rndTestData.rawOutput = 0x88e;
0098   rndTestData.bxOutput = rndTestData.bxInput;
0099   rndTestData.etOutput = rndTestData.etInput;
0100   rndTestData.phiOutput = rndTestData.phiInput;
0101   rndTestData.overflowOutput = rndTestData.overflowInput;
0102 
0103   // Null test data for testing default constructor
0104   TestIO nullTestData;
0105 
0106   // NOW DO THE TESTS
0107 
0108   //if(!testL1GctHtMiss("OUT-OF-RANGE VALUES", oorTestData)) { unitTestPassed = false; }  // brushing under carpet for now...
0109   if (!testL1GctHtMiss("MAX VALUES", maxTestData)) {
0110     unitTestPassed = false;
0111   }
0112   if (!testL1GctHtMiss("RANDOM VALUES", rndTestData)) {
0113     unitTestPassed = false;
0114   }
0115 
0116   cout << "\nAND FINALLY, TEST THE DEFAULT CONSTRUCTOR..." << endl;
0117   // Default constructor test object.
0118   L1GctHtMiss defaultConstructorTestObj;
0119   if (!testL1GctHtMissInstance("DEFAULT CONSTRUCTOR", defaultConstructorTestObj, nullTestData, false)) {
0120     unitTestPassed = false;
0121   }
0122 
0123   // DISPLAY OVERALL RESULT
0124 
0125   if (!unitTestPassed) {
0126     cout << "\n\n-----------------\nUnit test FAILED!\n-----------------" << endl;
0127     return (1);
0128   }
0129 
0130   cout << "\n\n----------------\nUnit test passed\n----------------" << endl;
0131 
0132   return 0;
0133 }
0134 
0135 bool testL1GctHtMiss(const std::string& testLabel, const TestIO& testIO) {
0136   bool allTestsPassed = true;  // Try and prove wrong...
0137 
0138   cout << "\nSTART OF " << testLabel << " TESTS\n" << endl;
0139 
0140   // Constructor for the unpacker that takes only the raw data.
0141   L1GctHtMiss rawOnlyConstructorTestObj(testIO.rawInput);
0142   if (!testL1GctHtMissInstance("RAW ONLY CONSTRUCTOR", rawOnlyConstructorTestObj, testIO, true)) {
0143     allTestsPassed = false;
0144   }
0145 
0146   // Constructor for the unpacker that takes the raw data and the bx.
0147   L1GctHtMiss rawAndBxConstructorTestObj(testIO.rawInput, testIO.bxInput);
0148   if (!testL1GctHtMissInstance("RAW AND BX CONSTRUCTOR", rawAndBxConstructorTestObj, testIO, false)) {
0149     allTestsPassed = false;
0150   }
0151 
0152   // Constructor that takes Et, Phi, and overflow.
0153   L1GctHtMiss etPhiOverflowConstructorTestObj(testIO.etInput, testIO.phiInput, testIO.overflowInput);
0154   if (!testL1GctHtMissInstance("ET/PHI/OVERFLOW CONSTRUCTOR", etPhiOverflowConstructorTestObj, testIO, true)) {
0155     allTestsPassed = false;
0156   }
0157 
0158   // Constructor that takes Et, Phi, overflow, and bx.
0159   L1GctHtMiss etPhiOverflowBxConstructorTestObj(testIO.etInput, testIO.phiInput, testIO.overflowInput, testIO.bxInput);
0160   if (!testL1GctHtMissInstance("ET/PHI/OVERFLOW/BX CONSTRUCTOR", etPhiOverflowBxConstructorTestObj, testIO, false)) {
0161     allTestsPassed = false;
0162   }
0163 
0164   cout << "\n  TESTING EQUALITY OPERATOR BETWEEN DIFFERENT CONSTRUCTORS" << endl;
0165   bool equalityTestsPassed = true;
0166 
0167   equalityTestsPassed = (rawOnlyConstructorTestObj == rawAndBxConstructorTestObj);
0168   if (!equalityTestsPassed) {
0169     allTestsPassed = false;
0170   }
0171   cout << "    Equality operator test between Raw only and Raw+Bx constructors: \t"
0172        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0173 
0174   equalityTestsPassed = (rawOnlyConstructorTestObj == etPhiOverflowConstructorTestObj);
0175   if (!equalityTestsPassed) {
0176     allTestsPassed = false;
0177   }
0178   cout << "    Equality operator test between Raw only and Et+Phi+Overflow constructors: \t"
0179        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0180 
0181   equalityTestsPassed = (rawOnlyConstructorTestObj == etPhiOverflowBxConstructorTestObj);
0182   if (!equalityTestsPassed) {
0183     allTestsPassed = false;
0184   }
0185   cout << "    Equality operator test between Raw only and Et+Phi+Overflow+Bx constructors: \t"
0186        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0187 
0188   equalityTestsPassed = (rawAndBxConstructorTestObj == etPhiOverflowConstructorTestObj);
0189   if (!equalityTestsPassed) {
0190     allTestsPassed = false;
0191   }
0192   cout << "    Equality operator test between Raw+Bx and Et+Phi+Overflow constructors: \t"
0193        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0194 
0195   equalityTestsPassed = (rawAndBxConstructorTestObj == etPhiOverflowBxConstructorTestObj);
0196   if (!equalityTestsPassed) {
0197     allTestsPassed = false;
0198   }
0199   cout << "    Equality operator test between Raw+Bx and Et+Phi+Overflow+Bx constructors: \t"
0200        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0201 
0202   equalityTestsPassed = (etPhiOverflowConstructorTestObj == etPhiOverflowBxConstructorTestObj);
0203   if (!equalityTestsPassed) {
0204     allTestsPassed = false;
0205   }
0206   cout << "    Equality operator test between Et+Phi+Overflow and Et+Phi+Overflow+Bx constructors: \t"
0207        << (equalityTestsPassed ? "passed." : "FAILED!") << endl;
0208 
0209   cout << "\n  TESTING INEQUALITY OPERATOR" << endl;
0210   bool inequalityTestsPassed = true;
0211   L1GctHtMiss defaultConstructorTestObj;  // Create a default object to test against.
0212 
0213   inequalityTestsPassed = (rawOnlyConstructorTestObj != defaultConstructorTestObj);
0214   if (!inequalityTestsPassed) {
0215     allTestsPassed = false;
0216   }
0217   cout << "    Inequality operator test: \t" << (inequalityTestsPassed ? "passed." : "FAILED!") << endl;
0218 
0219   return allTestsPassed;
0220 }
0221 
0222 bool testL1GctHtMissInstance(const std::string& testLabel,
0223                              L1GctHtMiss& testObj,
0224                              const TestIO& testIO,
0225                              bool bxIsZeroNotValueInTestIO) {
0226   bool testsPassed = true;  // Try and prove wrong...
0227 
0228   cout << "\n  START OF " << testLabel << " SUB-TESTS" << endl;
0229 
0230   // For testing the the copy ctor and assignment operators.
0231   L1GctHtMiss copyCtorTestObj(testObj);
0232   L1GctHtMiss assignmentOperatorTestObj;
0233   assignmentOperatorTestObj = testObj;
0234 
0235   cout << "\n    1) Testing original object:" << endl;
0236   if (!doObjTest(testObj, testIO, bxIsZeroNotValueInTestIO)) {
0237     testsPassed = false;
0238   }
0239 
0240   cout << "\n    2) Testing copy constructed version of original object:" << endl;
0241   if (!doObjTest(copyCtorTestObj, testIO, bxIsZeroNotValueInTestIO)) {
0242     testsPassed = false;
0243   }
0244 
0245   cout << "\n    3) Test assignment operator version of original object:" << endl;
0246   if (!doObjTest(assignmentOperatorTestObj, testIO, bxIsZeroNotValueInTestIO)) {
0247     testsPassed = false;
0248   }
0249 
0250   return testsPassed;
0251 }
0252 
0253 bool doObjTest(L1GctHtMiss& testObj, const TestIO& testIO, bool bxIsZeroNotValueInTestIO) {
0254   bool allTestsPassed = true;  // Try and prove wrong...
0255 
0256   bool testPassed;  // Reused for each individual test.
0257 
0258   testPassed = (testObj.name() == "HtMiss");
0259   if (!testPassed) {
0260     allTestsPassed = false;
0261   }
0262   cout << "      Test name(): \t" << (testPassed ? "passed." : "FAILED!") << endl;
0263 
0264   testPassed = (testObj.empty() == false);
0265   if (!testPassed) {
0266     allTestsPassed = true;
0267   }
0268   cout << "      Test empty(): \t" << (testPassed ? "passed." : "FAILED!") << endl;
0269 
0270   testPassed = (testObj.raw() == testIO.rawOutput);
0271   if (!testPassed) {
0272     allTestsPassed = false;
0273   }
0274   cout << "      Test raw(): \t" << (testPassed ? "passed." : "FAILED!") << "\t(raw output = 0x" << hex << testObj.raw()
0275        << dec << ")" << endl;
0276 
0277   testPassed = (testObj.et() == testIO.etOutput);
0278   if (!testPassed) {
0279     allTestsPassed = false;
0280   }
0281   cout << "      Test et(): \t" << (testPassed ? "passed." : "FAILED!") << "\t(et output  = 0x" << hex << testObj.et()
0282        << dec << ")" << endl;
0283 
0284   testPassed = (testObj.phi() == testIO.phiOutput);
0285   if (!testPassed) {
0286     allTestsPassed = false;
0287   }
0288   cout << "      Test phi(): \t" << (testPassed ? "passed." : "FAILED!") << "\t(phi output = 0x" << hex << testObj.phi()
0289        << dec << ")" << endl;
0290 
0291   testPassed = (testObj.overFlow() == testIO.overflowOutput);
0292   if (!testPassed) {
0293     allTestsPassed = false;
0294   }
0295   cout << "      Test overFlow(): \t" << (testPassed ? "passed." : "FAILED!") << endl;
0296 
0297   if (bxIsZeroNotValueInTestIO) {
0298     testPassed = (testObj.bx() == 0);
0299   } else {
0300     testPassed = (testObj.bx() == testIO.bxOutput);
0301   }
0302   if (!testPassed) {
0303     allTestsPassed = false;
0304   }
0305   cout << "      Test bx(): \t" << (testPassed ? "passed." : "FAILED!") << "\t(bx output  = " << testObj.bx() << ")"
0306        << endl;
0307 
0308   return allTestsPassed;
0309 }