简体   繁体   English

C程序的单元测试

[英]Unit test for a C program

I've a C program that gives prime numbers up to the input number. 我有一个C程序,可以将质数指定为输入数字。 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: 这是我的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 这是我的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): 这是我的minunit.c(已实施测试):

#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. 我在互联网上找到了minunit.c,但我不知道如何实施所需的测试并使其正常运行。 My goal is to make a simple test for my program. 我的目标是为我的程序做一个简单的测试。

Ultimately, a test is nothing more than some code which exercises your code and checks your assumptions are true. 最终,测试不过是一些代码,这些代码可以练习您的代码并检查您的假设是否正确。 When you're starting out, keep it simple. 起步时,请保持简单。 Instead of using a testing framework, start with just assert() . 不要使用测试框架,而只需使用assert()

Here is how you can test CheckPrime() . 这是测试CheckPrime()

#include <assert.h>
#include "checkprime.h"

void test_CheckPrime_primes() {
    int primes[] = {
        2, 3, 5, 7, 11, 13, 0
    };

    for( int idx = 0; primes[idx] != 0; idx++ ) {
        assert( CheckPrime(primes[idx]) );
    }
}

void test_CheckPrime_composites() {
    int composites[] = {
        1, 4, 6, 8, 9, 10, 12, 14, 0
    };

    for( int idx = 0; composites[idx] != 0; idx++ ) {
        assert( !CheckPrime(composites[idx]) );
    }
}

int main() {
    test_CheckPrime_primes();
    test_CheckPrime_composites();

    return 0;
}

This simple test program will reveal problems with using CheckPrime() . 这个简单的测试程序将揭示使用CheckPrime() The two biggest are that it does not have a header file and Primes must be initialized by the caller. 最大的两个是它没有头文件,并且Primes必须由调用方初始化。 It will also allow you to learn what sort of assertions to write. 它还将使您了解要编写哪种断言。 For example, what happens with 0? 例如,0会发生什么? 1? 1个 -1? -1? INT_MAX ? INT_MAX A candidate larger than your Primes array? 大于Primes数组的候选对象?

Once you've got the basics down and understand what testing is all about, assert() can be replaced with more informative asserts and a testing framework . 一旦掌握了基础知识并了解了测试的全部内容后,可用更具参考性的assert测试框架替换assert()

And here is my minunit.c … 这是我的minunit.c…

I presume you meant minunit.h . 我想你的意思是minunit.h Given that, you could implement the test like mu_checkprime.c : 鉴于此,您可以像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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM