TestSuite.c(使用了CUnit的测试文件)
/*
* =====================================================================================
*
* Filename: TestSuite.c
*
* Description: test file
*
* Version: 1.0
* Created: 2012/12/3 14:03:34
* Revision: none
* Compiler: gcc
*
* Author: xhzuoxin (QQ:1126804077), xiahouzuoxin@163.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "CUnit/Console.h"
#include "BinHeap.h"
PtrHeap h = NULL;
int InitSuite(void)
{
if(NULL == (h=InitHeap(h, 30)))
{
return -1;
}
else
{
return 0;
}
return 0;
}
int EndSuite(void)
{
free(h->ele);
free(h);
return 0;
}
void TestInsertHeap(void)
{
int i = 0;
if(NULL != h)
{
CU_ASSERT(NULL != InsertHeap(h, 50));
CU_ASSERT(NULL != InsertHeap(h, 100));
CU_ASSERT(NULL != InsertHeap(h, 200));
}
printf("\n InsertHeap: ");
for(i=1; i<=h->cur; i++)
{
printf("%d ", h->ele[i]);
}
}
void TestDelMinHeap(void)
{
printf("\n DelMin: ");
while(h->cur > 0)
{
printf("%d ", DelMinHeap(h));
}
}
void TestBuildHeap(void)
{
int i = 0;
EleType x[15] = {10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, 2};
h = BuildHeap(x, 15);
printf("\n BuildHeap:");
for(i=1; i<=h->cur; i++)
{
printf("%d ", h->ele[i]);
}
}
void TestIncreaseKey(void)
{
int pos = 5;
int delta = 50;
int i = 0;
h = IncreaseKey(pos, delta, h);
printf("\n IncreaseKey:");
for(i=1; i<=h->cur; i++)
{
printf("%d ", h->ele[i]);
}
}
void TestDecreaseKey(void)
{
int pos = 5;
int delta = 1;
int i = 0;
h = DecreaseKey(pos, delta, h);
printf("\n DecreaseKey:");
for(i=1; i<=h->cur; i++)
{
printf("%d ", h->ele[i]);
}
}
void TestDelete(void)
{
int pos = 5;
EleType e = Delete(pos, h);
printf("\n Delete: %d", e);
}
int main(void)
{
// CU_pSuite suite = NULL;
//
// /* Init registry */
// if(CUE_SUCCESS != CU_initialize_registry())
// {
// return CU_get_error();
// }
//
// /* add suite */
// suite = CU_add_suite("suite1", InitSuite, EndSuite);
// if(NULL == suite)
// {
// CU_cleanup_registry();
// return CU_get_error();
// }
//
// /* add tests */
// if(NULL == CU_add_test(suite, "test of InsertHeap()", TestInsertHeap)
// || NULL == CU_add_test(suite, "test of DelMinHeap()", TestDelMinHeap))
// {
// CU_cleanup_registry();
// return CU_get_error();
// }
//
// /* run */
// CU_console_run_tests();
//
// /* cleanup */
// CU_cleanup_registry();
CU_TestInfo testcase[] = {
{"TestBuild", TestBuildHeap},
{"TestInsert", TestInsertHeap},
{"TestIncreaseKey", TestIncreaseKey},
{"TestDecreaseKey", TestDecreaseKey},
{"TestDelete", TestDelete},
{"TestDelMin", TestDelMinHeap},
CU_TEST_INFO_NULL
};
CU_SuiteInfo suite[] = {
{"Testing func ", InitSuite, EndSuite, testcase},
CU_SUITE_INFO_NULL
};
/* Init registry */
if(CUE_SUCCESS != CU_initialize_registry())
{
return CU_get_error();
}
/* register suite */
if(CUE_SUCCESS != CU_register_suites(suite))
{
return 1;
}
CU_console_run_tests();
CU_cleanup_registry();
return 0;
}