C 程序的单元测试 [英] Unit test for a C program
问题描述
我有一个 C 程序,它给出了输入数的素数.我想测试这个程序,看看它是否给出合数.现在我需要实施测试,我觉得这很困难.如果有人能帮助我,我将不胜感激.
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.
这是我的 Checkprime.c:
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;
}
这是我的 main.c
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);
}
}
这是我的 minunit.c(测试,已实现):
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
我在网上找到了minunit.c,不知道如何实现我想要的测试,让它工作.我的目标是为我的程序做一个简单的测试.
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.
推荐答案
这是我的 minunit.c ...
And here is my minunit.c …
我猜你的意思是 minunit.h
.鉴于此,您可以像 mu_checkprime.c
一样实现测试:
I presume you meant minunit.h
. Given that, you could implement the test like mu_checkprime.c
:
#include "checkprime.c"
int Prime[MaxPrimes];
static char *test_primes()
{
mu_assert(CheckPrime(2) == 1, "2 not found to be prime");
mu_assert(CheckPrime(3) == 1, "3 not found to be prime");
mu_assert(CheckPrime(5) == 1, "5 not found to be prime");
mu_assert(CheckPrime(7) == 1, "7 not found to be prime");
mu_assert(CheckPrime(11) == 1, "11 not found to be prime");
mu_assert(CheckPrime(13) == 1, "13 not found to be prime");
return 0;
}
static char *test_composites()
{
mu_assert(CheckPrime(1) == 0, "1 found to be prime");
mu_assert(CheckPrime(4) == 0, "4 found to be prime");
mu_assert(CheckPrime(6) == 0, "6 found to be prime");
mu_assert(CheckPrime(8) == 0, "8 found to be prime");
mu_assert(CheckPrime(9) == 0, "9 found to be prime");
mu_assert(CheckPrime(10) == 0, "10 found to be prime");
mu_assert(CheckPrime(12) == 0, "12 found to be prime");
mu_assert(CheckPrime(14) == 0, "14 found to be prime");
return 0;
}
static char *all_tests()
{
mu_suite_start();
mu_run_test(test_primes);
mu_run_test(test_composites);
return 0;
}
RUN_TESTS(all_tests)
这篇关于C 程序的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!