samedi 27 février 2016

Unit test for a C program

I've a C program that gives prime numbers up to the input number. I want to test this program, and see if it gives composite numbers. Now I need to implement the test, which I find difficult. So I'll appreciate if anybody can help me.

Here is my Checkprime.c:

#include "defs.h"
#include "externs.h"
#include "minunit.h"

int CheckPrime(int K){

int J;

for (J=2; J*J <= K; J++){
  if (Prime[J] == 1){
     if (K % J == 0)  {
        Prime[K] = 0;
        return 0;
     }
   }

}   

Prime[K] = 1; 
return 1;
}

This is my main.c

#include <stdio.h>
#include "defs.h"
#include "checkprime.c"

int Prime[MaxPrimes]; 

int main()
{ 
int UpperBound;
int N;
int *ba = &UpperBound;

printf("enter upper bound\n");
scanf("%d",ba);

Prime[2] = 1;

for (N = 3; N <= *ba; N+= 2){
  CheckPrime(N);
  if (Prime[N]== 1) printf("%d is a prime\n",N);
 }
}

And here is my minunit.c (test, which is implemented):

#undef NDEBUG
#ifndef _minunit_h
#define _minunit_h

#include <stdio.h>
#include <stdlib.h>

#define mu_suite_start() char *message = NULL

#define mu_assert(test, message) if (!(test)) { return message; }
#define mu_run_test(test) \                 
message = test(); tests_run++; if (message) return message;

#define RUN_TESTS(name) int main(int argc, char *argv[]) {\
argc = 1; \
    printf("----\nRUNNING: %s\n", argv[0]);\
    char *result = name();\
    if (result != 0) {\
        printf("FAILED: %s\n", result);\
    }\
    else {\
        printf("ALL TESTS PASSED\n");\
    }\
printf("Tests run: %d\n", tests_run);\
    exit(result != 0);\
}

int tests_run;

#endif

I found the minunit.c on the internet, and I don't know how to implement the test I want, and let it work. My goal is to make a simple test for my program.

Aucun commentaire:

Enregistrer un commentaire