//*************************************************************
//* *
//* Course : CS458, Winter 1999 *
//* Project# : 3 *
//* Submitted By : Ranga Vadlamudi *
//* Mei Fun Wong *
//* Meng Yi Yee *
//* *
//*************************************************************


#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <fstream.h>


class program1
{
public:
program1();
void open_file() const;
void read_line();

private:
char generate_test_cases;

};


//***************************************************************
//* Open the input file or program to be read and categorize *
//* the decision statements *
//***************************************************************


void program1::open_file() const
{
char program_line[80];
char *tokenptr;
char *lineptr;
char *test_if = "if" ;
char *test_case = "case" ;
char *test_switch = "switch";
char *test_default= "default";
char *test_break = "break";
char test_case_input;
char *testle = "<=";
char *testge = ">=";
char *testlt = "<";
char *testgt = ">";
char *testeq = "=";
char *testne = "!=";

FILE *instrm;

int nested_if = 0;
int token_if_cnt = 0;
int token_case_cnt = 0;
int token_switch_cnt = 0;
int possible_nested_if = 0;
int token_nested_if_cnt = 0;
int case_having_break = 0;
int case_not_having_break = 0;
int prev_case = 0;
int prev_was_case = 0;
int prev_was_switch = 0;
int first_time_entry = 0;
int temp_case_count = 0;
int test_case_cnt = 0;
int sub_case_cnt = 0;
int variable_found = 0;
int prev_was_if = 0;

// opens input file for a later read
instrm = fopen("hw2.c","r");

// ofstream constructor opens file
ofstream outfile("project3.out",ios::out);

// check if input file exists
if (!instrm)
{
cerr << "Input File could not be opened\n" ;
exit(1);
}

// check if output file exists for later writing
if (!outfile)
{
cerr << "Output file could not be opened\n";
exit(1);
}
// ask user if he wants to create test cases for the program
cout << "Do You Want to create test cases" << '\n';
cin >> test_case_input;
if (test_case_input == 'y')
test_case_input = 'Y';

// do while not end of input file
while (!feof(instrm))
{

lineptr = fgets(program_line,80,instrm);
tokenptr = strtok(program_line," ");

if (*tokenptr != '/')
{
first_time_entry = 0;
while (tokenptr != NULL)
{
if (prev_was_case == 1)
{
if (test_case_input == 'Y')
{
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " << tokenptr;
outfile << '\n' << '\n';
}
}
prev_was_case = 0;
if ((prev_was_switch == 1) && (*tokenptr != '('))
{
prev_was_switch = 0;
if (test_case_input == 'Y')
{
outfile << '\n' << '\n';
test_case_cnt++;
outfile << "TEST CASE " << test_case_cnt << '\n';
outfile << "----------- " << '\n' << '\n';
outfile << "The different values in Sub Test Cases are for";
outfile << '\n' << '\n' ;
outfile << " " << tokenptr;
outfile << '\n' << '\n' ;
}
}
if ((prev_was_if == 1) && (*tokenptr == ')'))
{
prev_was_if = 0;
}
if ((prev_was_if == 1) && (*tokenptr != '('))
{
if ((test_case_input == 'Y') && (variable_found == 0))
{
variable_found = 1;
outfile << '\n' << '\n';
test_case_cnt++;
outfile << "TEST CASE " << test_case_cnt << '\n';
outfile << "----------- " << '\n' << '\n';
outfile << "The different values in Sub Test Cases are for";
outfile << '\n' << '\n' ;
outfile << " " << tokenptr;
outfile << '\n' << '\n' ;
}
else
{
if ((strcmp(tokenptr,testge) != 0)
&& (strcmp(tokenptr,testle) != 0)
&& (strcmp(tokenptr,testeq) != 0)
&& (strcmp(tokenptr,testne) != 0)
&& (strcmp(tokenptr,testlt) != 0)
&& (strcmp(tokenptr,testgt) != 0))
{
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " ;
outfile << " = " << tokenptr << '\n' << '\n';
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " ;
outfile << " != " << tokenptr << '\n' << '\n';
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " ;
outfile << " > " << tokenptr << '\n' << '\n';
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " ;
outfile << " < " << tokenptr << '\n' << '\n';
}
}
}
if ((strcmp(tokenptr,test_case) != 0) && (first_time_entry == 0))
{
prev_case = 0;
}
first_time_entry = 1;
if (nested_if > 0)
{
if (*tokenptr == '{')
{
possible_nested_if++;
}
if (*tokenptr == '}')
{
possible_nested_if = 0;
}
}

if (strcmp(tokenptr,test_if) == 0)
{
prev_was_if = 1;
variable_found = 0;
sub_case_cnt = 0;
if (possible_nested_if > 0)
{
nested_if = 0;
token_if_cnt--;
token_nested_if_cnt++;
possible_nested_if = 0;
}
else
{
nested_if++;
token_if_cnt++;
}
}

if (strcmp(tokenptr,test_switch) == 0)
{
prev_was_switch++;
token_switch_cnt++;
sub_case_cnt = 0;
}

if (strcmp(tokenptr,test_case) == 0)
{
prev_was_case++;
token_case_cnt++;
if (prev_case > 0)
{
temp_case_count++;
}
else temp_case_count = 1;
prev_case++;
}

if (strncmp(tokenptr,test_break,5) == 0)
{
case_having_break = case_having_break + temp_case_count;
}

if (strcmp(tokenptr,test_default) == 0)
{
if (test_case_input == 'Y')
{
sub_case_cnt++;
outfile << "Sub Test Case# " << sub_case_cnt << ": " << tokenptr;
outfile << "-> Give values other than the above listed. ";
outfile << '\n' << '\n';
}
token_case_cnt++;
}
tokenptr = strtok(NULL," ");
}
}
}

case_not_having_break = token_case_cnt - case_having_break;

cout << " ________________________________________________________";
cout << '\n';
cout << "| ";
cout << '\n';
cout << "| Total Number of Simple If's in the Program are : ";
cout << token_if_cnt;
cout << '\n';
cout << "| Total Number of Nested If's in the Program are : ";
cout << token_nested_if_cnt;
cout << '\n';
cout << "| Total Number of Switch Statements in the Program are: ";
cout << token_switch_cnt;
cout << '\n';
cout << "| Total Number of Case Statements in the Program are : ";
cout << token_case_cnt;
cout << '\n';
cout << "| " ;
cout << '\n';
cout << " ________________________________________________________";
cout << '\n';
cout << '\n';
cout << '\n';
cout << '\n';
cout << " ________________________________________________________";
cout << '\n';
cout << "| ";
cout << '\n';
cout << "| Total Number of Case Statements with a BREAK are : ";
cout << case_having_break;
cout << '\n';
cout << "| Total number of Case Stetements without BREAK are : ";
cout << case_not_having_break;
cout << '\n';
cout << " ________________________________________________________";

if (test_case_input == 'Y')
{
cout << '\n' << '\n';
cout << " ________________________________________________________";
cout << '\n';
cout << "The test case were generated and written to PROJECT3.OUT";
cout << '\n';
cout << "The total number of test cases generated are ";
cout << " : " << test_case_cnt;
cout << '\n';
cout << "There are sub cases for each of the test cases" << '\n';
cout << " ________________________________________________________";
}
}

//***************************************************************
//* default constructor *
//***************************************************************

program1::program1()
{
}

void main()
{
program1 program_main;

program_main.open_file();
}