You are here:

C/addition program tester

Advertisement


Question
QUESTION: Hi Zlatko,

Although this was a simple program to test the addition (see below), what would make an effective summary report?

If you were to be running my program here, what, as the end user, and the program were to generate a log summary of the task completed, what should be included so the end user can clearly see that the program did do what it's supposed to and where the problems are (if any)? The purpose of the program was to test the number additions, and print out the entries that failed. Hope this makes sense to you. Thanks!


#include<stdio.h>
#include<string.h>

int funcAdd(char*, char*);
int StringToInt(char*);

int main(int argc, char** argv)
{
  int i, j;
  int valid_count=0;
  int invalid_count=0;
  int counter=0;
  int test_sum;
  int sum=0;
  char param1[16];
  char param2[16];

  for(i= 0; i<=100; i++)
  {
      for(j=0; j<=100; j++)
      {
         test_sum = i + j;

        /*
         The funcAdd takes strings, so in testing the program
         we must first generate the strings
         */
         sprintf(param1, "%d", i);
         sprintf(param2, "%d", j);

         sum = funcAdd(param1, param2);
        printf(" testing  %3d + %3d   ", i, j, sum);
        
         if(test_sum == sum)
        {
         valid_count++;
         printf("valid\n");
        }
        
        else
        {
         invalid_count++;
         printf("invalid\n");
        }
        counter++;
     }
  }

  printf("\n-----------------------------");
  printf("\n Total # of
  printf("\n\n%d entries tested\n", counter);
  printf("%d invalid entries found\n\n", invalid_count);

  return 0;
}

int funcAdd(char* param1, char* param2)
{
  int number1 = StringToInt(param1);
  int number2 = StringToInt(param2);

  int sum = number1 + number2;
  return sum;
}

/*function StringToInt
gets a numeric string from the user and returns the integer
representation of the string
*/
int StringToInt(char* input)
{
  int  number = 0;
  int  inputLen;
  int  i;

  inputLen = strlen(input);
  for (i = 0; i < inputLen; i++)
  {
      int digit = input[i] - '0';
      number *= 10;
      number += digit;
  }
  return number;
}

ANSWER: Hello Mike.
What is a good summary is a matter of opinion, but I would not want to read through 10,000 lines of output looking for bad additions. I would want to see a line saying what the test is doing and lines showing any failures where test_sum != sum, including the numbers that caused the failure. Maybe count the number of failures and print the count at the end of the test. Unfortunately, the log won't be very exciting unless you inject an error on purpose, like this for example

if (i == 20) test_sum = i + j + 1;
That will test your logging and error counting code, but then remember to take the error out. See, now you have to test your test code.

I hope that answers your question.
Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko,
Thanks for the info. Now, two questions:

1. what does it mean when "stack around variable1 is corrupt" type error appears when you try running your program? (I'm using the MS visual C++ 2008 express compiler, if that matters any)

2. I tried having the program save the numbers causing failures (when failures are detected) to an array and then print it out at the so there would be a list of all the values that are problematic in the summary at the end.

#include<stdio.h>
#include<string.h>

int funcAdd(char*, char*);
int StringToInt(char*);

int main(int argc, char** argv)
{
  int i, j, k;
  int valid_count=0;
  int invalid_count=0;
  int test_sum;
  int sum=0;
  char param1[16];
  char param2[16];
  int save_num1[1500];
  int save_num2[1500];

  k=0;
  for(i= 0; i<=100; i++)
  {
      for(j=0; j<=100; j++)
      {
         test_sum = i + j;

        /*
         The funcAdd takes strings, so in testing the program
         we must first generate the strings
         */
         sprintf(param1, "%d", i);
         sprintf(param2, "%d", j);

         sum = funcAdd(param1, param2);
        printf(" testing  %3d + %3d   ", i, j, sum);
        
         if(test_sum == sum)
        {
         valid_count++;
         printf("valid\n");
        }
        
        else
        {
         invalid_count++;
         printf("invalid\n");
         save_num1[k] = i;
         save_num2[k]= j;
         k++;
        }
     }
  }

  printf("\n-----------------------------");
  printf("%d\n", valid_count);
  printf("\nTesting for validity of addition of any two numbers between 0 and 99");
  printf("\n0-99 has 101 numbers, so 101 x 101 = %d possible sums available", i*j);
  printf("\n\n%d entries tested\n", i*j);
  printf("%d invalid entries found\n\n", (i*j)-valid_count);

   if(invalid_count>0)
  {
     printf("%d", save_num1[k]);
     printf("%d", save_num2[k]);
     k++;
  }
  return 0;
}

int funcAdd(char* param1, char* param2)
{
  int number1 = StringToInt(param1);
  int number2 = StringToInt(param2);

  int sum = number1 + number2;
  return -1;
}

/*function StringToInt
gets a numeric string from the user and returns the integer
representation of the string
*/
int StringToInt(char* input)
{
  int  number = 0;
  int  inputLen;
  int  i;

  inputLen = strlen(input);
  for (i = 0; i < inputLen; i++)
  {
      int digit = input[i] - '0';
      number *= 10;
      number += digit;
  }
  return number;
}

ANSWER: Hello Mike.

The stack is an area in the program's memory that is used to store all the variable you define in your functions. For example, in the main function you have int save_num1[1500]; and int save_num2[1500]; and they are on the stack. The error means that you have tried to write data to a part of the stack that you should not be writing to. This is usually caused by writing past the end of an array. In fact that is what is happening. The funcAdd is now returning -1 for each sum, so each sum is wrong. There are 100 x 100 = 10,000 sums, but the error arrays can hold only 1500 elements. You could make the error arrays larger, but you don't really need to store the errors in arrays for later printing. Just print out the errors as they are happening. Generally, when placing an unknown number of items into arrays, it is always good to check if there is space left in the array.



---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko,

You mentioned that it might be better to print out errors as it happens instead of storing in array for later printing. For the 10,000 sums, after the looping, you probably won't be able to see all the errors (like if I made the return -1 so that every sum would be invalid) so would it be recommended to save errors to text file and have user open it later to see the list of errors or is there a better way to do this?

Answer
Hello Mike.

What I meant was to print the errors to a log file, not to the screen. It is quite common to store such errors in a log file. The user probably would not be interested in such a file and by the time the user gets the program, it should be running pretty smoothly. The log file is for the benefit of the programmer. If you are programming in an industry where you are supporting the programs you write, then it is common to keep error logging turned on even when the program is delivered to the users (which are other people in your company). When the users notice problems, the programmers are called in and examine the log files to see what went wrong.

If you are writing a program for mass distribution, the log file is less useful unless you can get the program to send the log file to the programmer when there is a problem. The log file is not useful to the end user. Can you imagine looking through a log file for Microsoft Word, or Internet Explorer? Most end users are not so sophisticated, nor should they need to be.  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.