![]() |
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.
:
/****************************************************thanks ps..... my posts seem to have dissappeared.... now I only have 2..... thats strange. |
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. |
Quote:
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 :) |
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.
|
Quote:
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) {See if that helps any. |
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.
|
Quote:
|
Quote:
|
Quote:
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()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 1We can call this function from main() like so: :
int main():
int function() {:
int main() {:
int function();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:
// prime.c |
| 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