Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

TestManager.cpp

Committer:
ashleymills
Date:
2012-09-13
Revision:
32:8ff0b67bb58c
Parent:
30:dd2beda340c6
Child:
33:16126e029d58

File content as of revision 32:8ff0b67bb58c:

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

// adds a test into the master test index
void TestManager::addTest(VodafoneTestCase* testCase) {
   int index = testCase->_testCaseNumber;
   // make space for test
   int currentSize = _tests.size();
   if(index>=currentSize) {
      int pushCount = (index-currentSize)+1;
      while(pushCount--)
        _tests.push_back(NULL);
   }
   if(_tests[index]==NULL) {
      _tests[index] = testCase;
   }
}

TestManager::TestManager(VodafoneUSBModem *m) : _modem(m) {
   // using direct array indexing instead of searching each time
   // dummy tests are inserted by the addTest function
   addTest((VodafoneTestCase*)new Test10(_modem,10));
   addTest((VodafoneTestCase*)new Test12(_modem,12));
   addTest((VodafoneTestCase*)new Test13(_modem,13));
   addTest((VodafoneTestCase*)new Test16(_modem,16));
   addTest((VodafoneTestCase*)new Test21(_modem,21));
   addTest((VodafoneTestCase*)new Test22(_modem,22));
   addTest((VodafoneTestCase*)new Test23(_modem,23));
   addTest((VodafoneTestCase*)new Test25(_modem,25));
   addTest((VodafoneTestCase*)new Test26(_modem,26));
   addTest((VodafoneTestCase*)new Test50(_modem,50));
   addTest((VodafoneTestCase*)new Test56(_modem,56));
}

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::executeTestProfile(TestProfile profile) {
   int numPassed = 0;
   if(profile<TESTS_END) { // no test for >= 0 since TestProfile is unsigned
      numPassed = executeTestList(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) {
   int successfullTests = 0;
   for(int i=0; i<listLen; i++) {
      int testIndex = list[i];
      if(testIndex>=_tests.size()) {
         LOG("Test out of bounds. Test index must be betweeen 0 and %d!",_tests.size());
         continue;
      }
      
      VodafoneTestCase* test = _tests[testIndex];
      if(test->_lastRunOutcome==false) {
         LOG("Test %d:",list[i]);
         LOG(gTestDescriptions[list[i]]);
      }
   }
}

VodafoneTestCase* TestManager::getTest(int index) {
   for(int i=0; i<_tests.size(); i++) {
      if(index==_tests[i]->_testCaseNumber)
        return _tests[i];
   }
   return NULL;
}

int TestManager::executeTestList(const int *list, const int listLen) {
   LOG("The executeTestList is: %d", list);
   LOG("The executeTestListLength is: %d", listLen);
   int successfullTests = 0;
   LOG("Running %d tests...",listLen);
   for(int i=0; i<listLen; i++) {
      int testIndex = list[i];
      LOG("Running test %d...",testIndex);
      if(testIndex>=_tests.size()) {
         LOG("Test out of bounds. Test index must be betweeen 0 and %d!",_tests.size());
         continue;
      }
      
      VodafoneTestCase* test = _tests[testIndex];
      if(test==NULL) {
         LOG("NULL test!  This will be counted as a failure.");
         continue;
      }
      
      if(test->run()) {
         LOG("...OK");
         successfullTests++;
      } else {
         LOG("...FAIL");
      }
   }
   return successfullTests;
}

bool TestManager::runTest(int id) {
   if(id<0||id>=_tests.size()) {
      LOG("Test ID must be between 0 and %d",_tests.size());
      return false;
   }
   if(_tests[id]==NULL) {
      LOG("NULL test!  This will be counted as a failure.");
      return false;
   }
   
   return _tests[id]->run();
}


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