你好,游客 登录 注册 搜索
背景:
阅读新闻

二叉堆例程

[日期:2013-05-08] 来源:Linux社区  作者:xiahouzuoxin [字体: ]

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;
}

linux
相关资讯       二叉堆 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款