// testComputeSplitTime4.cc
// P. Conrad 03/06/06  for CISC181
// write test cases for the computeSplitTime function

// this version uses a testing function
// this version also attempts to write correct code
// for the compute split time function

// here, inside our compute split time function, we make a
// local copy

#include <iostream>
using namespace std;

// we use "const char * const" instead of just "char *"
// because we want to be able to pass in string literals such as "1:51.0"
// The keyword const means that we cannot modify either the pointer,
// or the data pointed to.

double computeSplitTime(const char * const splitTimePtr);

void testComputeSplitTime(const char * const splitTimePtr, 
			  double expectedResult);

int main(void)
{
  testComputeSplitTime("1:51.0",111.0);
  testComputeSplitTime("2:00.0",120.0);
  testComputeSplitTime("1:51.9",111.9);  
  testComputeSplitTime("0:00.0",0.0);  

  return 0;
}


void testComputeSplitTime(const char * const splitTimePtr, 
			  double expectedResult)
{

  double result = computeSplitTime(splitTimePtr);

  if (result == expectedResult)
    cout << "Passed" << endl;
  else
    cout << "Failed" << endl;

  return; 
}


// for example, convert 1:51.9 to 111.9

double computeSplitTime(const char *  const splitTimePtr)
{
  
  // first task... make a local copy of the input string
  // that we can work with

  // an idea we'll explore in a future version... compute
  //  the exact size we need for the local copy by doing
  
  // int sizeNeeded = strlen(splitTimePtr) + 1 ;  // + 1 for null term
  // and then allocate a local copy of the exact correct size.

  // for now, we'll just over-engineer it.

  char splitTimeCopy[1024]; // over-engineered in a big way

  // Later, come back and talk about strcpy vs. strncpy vs. strpcpy
  //  buffer overruns, hacking, security, etc...

  strcpy(splitTimeCopy, splitTimePtr);
  
  // now proceed with the rest of the function...

  double result;

  // stub function @@@ finish the stub later!!!

  // use strtok to divide the string into the first part
  // which is in minutes, and the second part which is in seconds.

  // for example, 1:51.9 will be divided into 1\051.9

  char *firstPart;
  char *secondPart;
  
  firstPart = strtok(splitTimeCopy,":");
  secondPart = strtok(NULL,":");

  // compute the result by converting firstPart to a number
  // and multiplying by 60, and add to second part
  // use atof on the second part, becuase it is a double
  // (yeah, go figure... there is no atod... atof returns not
  //    a float, but rather, a double)

  result = atoi(firstPart) * 60 + atof(secondPart);

  return result;
}






