Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Creating a program to test a program (http://www.programmingforums.org/showthread.php?t=8029)

sixstringartist Jan 19th, 2006 8:15 PM

Creating a program to test a program
 
After a long absence from these boards and some more programming under my belt, I have finnally dived into C because I have my first college C class this semester. I just finished writing my first C program other than the classic "hello world". It is a program that asks for user input of an integer and decided whether or not the integer is a prime number. This program is no where near efficient and I plan on narrowing down its testing phase to only include numbers that could possibly be a prime (i.e. nothing higer than the root of the input) I would like to write a program that will test this program for each interger up to a stopping point. Also any other efficiency suggestions would be apprecieated.


:

/****************************************************
*

*  Programmer and Purdue Email Address:

*      1.Chris Cadwallader

*

*  Program Description: This program asks for user

*      input of an integer an outputs whether the

*      integer is a prime number or not.

*

*
***************************************************/


#include <stdio.h>


int main()

{

  /* Local Definitions */

 
  int testval;    // Integer to be tested

  int denom;      // Denominator used in calculations

  int testresult; // Calculation result

  int counter;    // Loop iteration counter


 
  /* Statements */



  /* Requesting an integer */


  printf("\n\nWelcome to the Prime number testing program.");

  printf("\n\nPlease Enter an integer to be tested:  ");

  scanf("%d", &testval);



  /* Verifying that the input is an integer greater than 1 */


  while (testval < 1)

  {

    printf("\nThe test value cannot be smaller than 1.\n\n");

    printf("\n\nPlease enter an integer to be tested: ");

    scanf("%d", &testval);

  } /* while */



  if (testval == 2)

    printf("The value entered is a prime number.\n\n");


 
else
 
  {

      denom = 2;

      testresult = testval % denom;

      counter = 1;


 
    /* Testing integer for prime status */


      while (denom < testval)

      {

          testresult = testval % denom;

          if (testresult == 0)

   
          denom = testval;

          else

              counter++;

              denom++;

      } /* while */


  if (testresult != 0)

        printf("The value entered is a prime number.\n\n");


  else

        printf("The value entered is not a prime number.\n\n");

    } /* else */


  printf("This program looped %d times to complete the problem.\n\n",counter);


  return 0;


} /* main */


thanks


ps..... my posts seem to have dissappeared.... now I only have 2..... thats strange.

InfoGeek Jan 19th, 2006 8:37 PM

You don't need to write a separate program to test your program. You can ask the user for a number under which you want to test the number for being prime. then you can have another loop that iterates from 3 to num and check each for being prime(you can increment the counter by 2 as only odd numbers are prime).

and for your posts disappearing I think you only posted in Lounge as the post count in Lounge has been disabled.

Arevos Jan 20th, 2006 4:18 AM

Quote:

Originally Posted by InfoGeek
You don't need to write a separate program to test your program. You can ask the user for a number under which you want to test the number for being prime. then you can have another loop that iterates from 3 to num and check each for being prime(you can increment the counter by 2 as only odd numbers are prime).

I believe he's talking about unit testing. What platform are you developing on, sixstringartist?

Also, at a guess, I'd say that sixstringartist knows a thing or two about finding primes already. As he correctly remarks, you only need to test up to the square root of the number to check if it is prime or not, which suggests that he's already familiar with how to efficiently test for primes. So I'm not sure you need to give him advice there :)

sixstringartist Jan 20th, 2006 9:25 AM

Im afraid Im not exactly sure what you mean by platform so I will ramble off some things and hope it helps. :D The previous program was written in C, using a UNIX command shell with the vi editor. compiled with gcc in UNIX.

Arevos Jan 20th, 2006 10:33 AM

Quote:

Originally Posted by sixstringartist
Im afraid Im not exactly sure what you mean by platform so I will ramble off some things and hope it helps. :D The previous program was written in C, using a UNIX command shell with the vi editor. compiled with gcc in UNIX.

Unit tests are a way of automatically checking your programs for correctness. There are a number of frameworks around that make building unit tests easier.

The first result for "C unit tests" in Google comes up with the Check framework, which seems a good place to start. Check seems to be designed with Linux/Unix in mind, which was why I was asking your platform. There might even be a precompiled Check package for your system. On the Check website, there is tutorial which seems to be fairly straightforward.

Typically, these frameworks have a number of tests, sometimes grouped together in one or more test suites so that many tests can be run at once. The frameworks also supply some form of testing the output of functions. For instance, under Check, you have the fail_unless and fail_if functions. Perhaps something like this will work:

:

START_TEST (test_primes) {
    fail_unless(your_prime_function(17), "17 should be classed as prime");
    fail_if(your_prime_function(6), "6 should be classed as not prime");
}
END_TEST

If your_prime_function returns true when passed 17, and false when passed 6, then the unit tests will pass. If they fail, then detailed information will be printed by the test runner, telling you exactly what failed and in what way. Of course, your unit tests are likely to be a little more complicated than this!

See if that helps any.

nindoja Jan 20th, 2006 3:54 PM

Another way to do it would be to use files. You could have an input file, and save the numbers there. Then you can read from the file and use those numbers in your function. If you really wanted to get fancy, you could make a batch file to run the program and send the output to a file, but that is up to you.

sixstringartist Jan 21st, 2006 12:03 PM

Quote:

Originally Posted by nindoja
Another way to do it would be to use files. You could have an input file, and save the numbers there. Then you can read from the file and use those numbers in your function. If you really wanted to get fancy, you could make a batch file to run the program and send the output to a file, but that is up to you.

Is this similar to creating functions? I have past experience using matlab but I dont konw how to write functions in C.

Polyphemus_ Jan 21st, 2006 12:26 PM

Quote:

Originally Posted by sixstringartist
ps..... my posts seem to have dissappeared.... now I only have 2..... thats strange.

Post in the lounge don't count for your postcount anymore.

Arevos Jan 21st, 2006 1:15 PM

Quote:

Originally Posted by sixstringartist
Is this similar to creating functions? I have past experience using matlab but I dont konw how to write functions in C.

You do know how to write functions in C - you just don't know it.

All code in C is encapsulated in a function. The first function called when a program executes is the function called "main".

If we take a look at the main function in your code:
:

int main()
{
    // Your code
}

The first keyword we come across is "int". This denotes the return value of the function. The main function should always return an int, which the operating system uses as the error code. If main returns 0, all is well. If it does not, an error has occurred, and the number that is returned may give a clue as to exactly what went wrong.

The second bit in the function is "main", which is the function name. Then "()". This empty pair of brackets tells C that this function has no arguments. Finally, the "{}" brackets encompass your code and tell C where the function starts, and where it ends.

A prime function could look like this:
:

#define TRUE  1
#define FALSE 0

int is_prime(int number)
{
        int i;

        for (i = 2; i < number; i++)
        {
                if ((number % i) == 0)
                {
                        return FALSE;
                }
        }
        return TRUE;
}

Note this function takes in an integer as an argument, and returns FALSE (0) if it's not a prime, and TRUE (1) if it is.

We can call this function from main() like so:
:

int main()
{
        int input;

        printf("Please enter an integer to test: ");
        scanf("%d", &input);

        if (is_prime(input))
        {
                printf("That number is prime.\n");
        }
        else
        {
                printf("That number is not prime.\n");
        }
       
        return 0;
}

There is also the issue of prototyping. Before a function in C can be used, the compiler must know about it. Thus, the following is legal:
:

int function() {
    return 0;
}
int main() {
    function();
}

But the next bit of code is not:
:

int main() {
    function();
}
int function() {
    return 0;
}

This is because "function" is referenced in main() before it is defined. You can get around this by adding in a function prototype. A prototype says, "This is what the function's inputs and outputs are, but I'll define the actual code later on":
:

int function();

int main() {
    function();
}
int function() {
    return 0;
}

In general, it's best to prototype functions in a header file. This allows functions in one file, to reference functions in another file, if both files include the same header.

Just for reference, a whole prime program could look something like the example I give below. Note it doesn't have the same input checking as yours, but it shouldn't be hard to put that in :)
:

// prime.h
#ifndef PRIME_H
#define PRIME_H

int is_prime(int number);

#endif

:

// prime.c
#include <stdio.h>
#include "prime.h"

#define TRUE  1
#define FALSE 0

int is_prime(int number)
{
        int i;

        for (i = 2; i < number; i++)
        {
                if ((number % i) == 0)
                {
                        return FALSE;
                }
        }
        return TRUE;
}

int main()
{
        int input;

        printf("Please enter an integer to test: ");
        scanf("%d", &input);

        if (is_prime(input))
        {
                printf("That number is prime.\n");
        }
        else
        {
                printf("That number is not prime.\n");
        }
       
        return 0;
}



All times are GMT -5. The time now is 5:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC