Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

TestManager.cpp

Committer:
ashleymills
Date:
2012-09-13
Revision:
33:16126e029d58
Parent:
31:9231acdde9ff
Parent:
32:8ff0b67bb58c
Child:
35:6867af70c51c

File content as of revision 33:16126e029d58:

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

// adds a test into the master test index
void TestManager::addTest(VodafoneTestCase* testCase) {
   _tests.push_back(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));
   addTest((VodafoneTestCase*)new Test12(_modem));
   addTest((VodafoneTestCase*)new Test13(_modem));
   addTest((VodafoneTestCase*)new Test16(_modem));
   addTest((VodafoneTestCase*)new Test21(_modem));
   addTest((VodafoneTestCase*)new Test22(_modem));
   addTest((VodafoneTestCase*)new Test23(_modem));
   addTest((VodafoneTestCase*)new Test25(_modem));
   addTest((VodafoneTestCase*)new Test26(_modem));
   addTest((VodafoneTestCase*)new Test50(_modem));
   addTest((VodafoneTestCase*)new Test56(_modem));
}

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];
      VodafoneTestCase* test = getTest(testIndex);
      if(test==NULL) {
         LOG("ERROR: Test %d is not in test list!.",testIndex);
         continue;
      }
      
      if(test->_lastRunOutcome==false) {
         LOG("Test %d:",test->_testCaseNumber);
         LOG(test->_description);
      }
   }
}

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) {
   int successfullTests = 0;
   LOG("Running %d tests...",listLen);
   for(int i=0; i<listLen; i++) {
      int testIndex = list[i];
      VodafoneTestCase* test = getTest(testIndex);
      if(test==NULL) {
         LOG("Error. Test %d is not in test list! This will be counted as a failure.",testIndex);
         continue;
      }
      
      LOG("Running test %d...",testIndex);
      if(test->execute()) {
         LOG("...OK");
         successfullTests++;
      } else {
         LOG("...FAIL");
      }
   }
   return successfullTests;
}

bool TestManager::executeTest(int id) {
   VodafoneTestCase* test = getTest(id);
   if(test==NULL) {
      LOG("Error. Test %d is not in test list! This will be counted as a failure.",id);
      return false;
   }
   
   return _tests[id]->execute();
}


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