CUnit单元测试代码实例

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "CUnit/Basic.h"
#include "CUnit/TestDB.h"
#include "CUnit/CUnit.h"

int max(int i, int j)
{
    return i>j? 0: -1; 
}

void test1()
{
    CU_ASSERT_EQUAL(max(1,0), 0); 		//正确结果
    CU_ASSERT_EQUAL(max(0,0), -1);		//正确结果
    CU_ASSERT_EQUAL(max(-1,0), -1);		//正确结果
    CU_ASSERT_EQUAL(max(-1,-2), -1);    //错误结果                                                                      
}


CU_TestInfo testcase[] = 
{
    {"result1 is :", test1},
    CU_TEST_INFO_NULL,
};

int suite_init() {return 0;} 
int suite_clean() {return 0;} 

CU_SuiteInfo suites[]=
{
    {"suite1", suite_init, suite_clean, NULL, NULL, testcase},
    CU_SUITE_INFO_NULL,
};

void add_test(void)
{
    assert(NULL != CU_get_registry());
    assert(!CU_is_test_running());

    if(CUE_SUCCESS != CU_register_nsuites(1, suites))
    {   
        printf("error in %d\n", __LINE__);
        assert(1);
    }   
}

int main(int argc, char **argv[])
{
    if (CUE_SUCCESS != CU_initialize_registry())
              return CU_get_error();
    else
    {   
        add_test();
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
        CU_cleanup_registry();


    }   
    return 0;
}

编译命令:
gcc -o ctest main_test.c -lcunit

执行结果:
CUnit单元测试代码实例

上一篇:VS2015编译GPU程序


下一篇:HEVC编码结构简要总结