Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

TestManager.cpp

Committer:
ashleymills
Date:
2014-01-29
Revision:
74:e52ac9624f7f
Parent:
72:0e8e769fcf76

File content as of revision 74:e52ac9624f7f:

#include "TestManager.h"
#include "Tests.h"

// adds a test into the master test index
void TestManager::addTest(VodafoneTestCase* testCase) {
   _tests.push_back(testCase);
}

VodafoneTestCase* TestManager::constructTest(int testID) {
   switch(testID) {
      case  0: return (VodafoneTestCase*) new Test00(_modem);
      case  1: return (VodafoneTestCase*) new Test01(_modem);
      case  2: return (VodafoneTestCase*) new Test02(_modem);
      case  3: return (VodafoneTestCase*) new Test03(_modem);
      case  4: return (VodafoneTestCase*) new Test04(_modem);
      case  5: return (VodafoneTestCase*) new Test05(_modem);
      case  6: return (VodafoneTestCase*) new Test06(_modem);
      case  7: return (VodafoneTestCase*) new Test07(_modem);
      case  8: return (VodafoneTestCase*) new Test08(_modem);
      case  9: return (VodafoneTestCase*) new Test09(_modem);
      case 10: return (VodafoneTestCase*) new Test10(_modem);
      case 11: return (VodafoneTestCase*) new Test11(_modem);
      case 12: return (VodafoneTestCase*) new Test12(_modem);
      case 13: return (VodafoneTestCase*) new Test13(_modem);
      case 14: return (VodafoneTestCase*) new Test14(_modem);
      case 15: return (VodafoneTestCase*) new Test15(_modem);
      case 16: break;
      case 17: break;
      case 18: break;
      case 19: break;
      case 20: break;
      case 21: break;
      case 22: break;
      case 23: break;
      case 24: break;
      case 25: break;
      case 26: break;
      case 27: break;
      case 28: break;
      case 29: break;
      case 30: break;
      case 31: break;
      case 32: break;
      case 33: break;
      case 34: break;
      case 35: break;
      case 36: break;
      case 37: break;
      case 38: break;
      case 39: break;
      case 40: break;
      case 41: break;
      case 42: break;
      case 43: break;
      case 44: break;
      case 45: break;
      case 46: break;
      case 47: break;
      case 48: break;
      case 49: break;
      case 50: break;
      case 51: break;
      case 52: break;
      case 53: break;
      case 54: break;
      case 55: break;
      case 56: break;
      case 57: break;
      case 58: break;
      case 59: break;
      default: return NULL;
   };
   return NULL; //< For compiler warning
}

TestManager::TestManager(VodafoneUSBModem *m) : _modem(m) {
   _testOutcomes = (bool*)malloc(NUMBER_OF_TESTS*sizeof(bool));
   resetTestOutcomes();
}

void TestManager::resetTestOutcomes() {
   for(int i=0; i<NUMBER_OF_TESTS; i++) {
      _testOutcomes[i] = false;
   }
}

int TestManager::getTestProfileLength(TestProfile profile) {
    if(profile < TESTS_END) { // no test for >= 0 since TestProfile is unsigned
      return gTestProfileLengths[profile];
   } else {
      LOG("Test profile out of bounds. Must be between 0 and %d",TESTS_END-1);
      return 0;
   }
}

int TestManager::runTestProfile(TestProfile profile) {
   resetTestOutcomes();
   int numPassed = 0;
   if(profile<TESTS_END) { // no test for >= 0 since TestProfile is unsigned
      numPassed = runTestList(gTestProfiles[profile], gTestProfileLengths[profile]);
   } else {
      LOG("Test profile out of bounds. Must be between 0 and %d",TESTS_END-1);
      return 0;
   }
   int numTests = getTestProfileLength(profile);
   LOG("%d tests complete: %d passes and %d failures.",numTests,numPassed,numTests-numPassed);
   LOG("Listing failures:");
   listFailures(gTestProfiles[profile], gTestProfileLengths[profile]);
   return numPassed;
}

void TestManager::listFailures(const int *list, const int listLen) {
   for(int i=0; i<listLen; i++) {
      int testIndex = list[i];
      
      if(_testOutcomes[testIndex]==false) {
         LOG("Test %d:",testIndex);
         LOG(gTestDescriptions[testIndex]);
      }
   }
}

int TestManager::runTestList(const int *list, const int listLen) {
   int successfullTests = 0;
   LOG("Running %d tests...",listLen);
   for(int i=0; i<listLen; i++) {
      int testIndex = list[i];
      if(runTest(testIndex)) {
         successfullTests++;
         _testOutcomes[testIndex] = true;
      }
   }
   return successfullTests;
}

bool TestManager::runTest(int testID) {
   VodafoneTestCase* test = constructTest(testID);
   if(test==NULL) {
      LOG("Error. Test %d is not in test list! This will be counted as a failure.",testID);
      return false;
   }
      
   LOG("Running test %d...",testID);
   if(test->run()) {
      LOG("...OK");
      delete test;
      return true;
   }
   LOG("...FAIL");
   delete test;
   return false;
}

bool TestManager::runTest(int id, int numTimes) {
   for(int i=0; i<numTimes; i++) {
      if(!runTest(id))
         return false;
   }
   return true;
}