存档

2009年7月 的存档

CUnit的精简测试框架

2009年7月24日

CUnit使用起来需要去写一些和CUnit相关的代码才能跑起来,没有JUnit4.0那种POJO的方式来的这么清爽,于是我定义了一些宏,将和业务无关的CUnit代码屏蔽起来,使用的时候,针对每个.C文件写一个测试suit文件,让它们一起包含Test.h即可。

200907241927

Test.h出了包含一些头文件以外,还有一串宏,定义如下(下面的中文是举例):

  1. #include "include/Basic.h"
  2.  
  3. #define INITIALIZE_CUNIT() \
  4.  if (CUE_SUCCESS != CU_initialize_registry()) \
  5.   return CU_get_error();
  6.  
  7. #define FINISH_CUNIT() \
  8.  CU_basic_set_mode(CU_BRM_VERBOSE); \
  9.  CU_basic_run_tests(); \
  10.  CU_cleanup_registry(); \
  11.  return CU_get_error();
  12.  
  13. #define NewTestSuit(SuitName) \
  14.  CU_pSuite pSuite = NULL; \
  15.  pSuite = CU_add_suite("Suite_Of_"#SuitName, before, after);
  16.  
  17. #define AddTest(FunctionName) CU_add_test(pSuite, #FunctionName, FunctionName);
  18.  
  19. #define GoTests() \
  20.  test模块1(); \
  21.  test模块2(); \
  22.  test模块3();
  23.  
  24. void test模块1(void);
  25. void test模块2(void);
  26. void test模块3(void);

下面是TestMain.c,以后可以不用动。

  1. #include "Test.h"
  2. int main(){
  3.  INITIALIZE_CUNIT();
  4.  GoTests();
  5.  FINISH_CUNIT();
  6. }

下面给出 Test模块1.c 的例子

  1. #include "Test.h"
  2. static int before(void){
  3.  // 该模块的方法运行前的准备工作写在这里
  4.  return 0;
  5. }
  6. static int after(void){
  7.  // 运行完的销毁工作也在这里
  8.  return 0;
  9. }
  10. void test_方法1(void)
  11. {
  12.  CU_ASSERT_EQUAL(1, 1);
  13. }
  14. void test_方法2(void)
  15. {
  16.  CU_ASSERT_EQUAL(1, 1);
  17. }
  18. void test_方法3(void)
  19. {
  20.  CU_ASSERT_EQUAL(1, 1);
  21. }
  22.  
  23. void test模块1()
  24. {
  25.  NewTestSuit(模块1);
  26.  AddTest(test_方法1);
  27.  AddTest(test_方法2);
  28.  AddTest(test_方法3);
  29. }

需要注意的是,和JUnit4.0有点不同:
JUnit4.0运行流程为:before->方法1->after->before->方法2->after->before->方法3->after
CUnit的运行流程为: before->方法1->方法2->方法3->after

C/C++

转载:中美印日四国程序员比较(自己也亲身体会)

2009年7月16日

最近以裁判的身份参加了公司举办的编程大赛,发现高手云集,对公司内部的程序员能力也有了更深入的了解。我觉得编程能力对程序员而言,虽然很重要,但并不是全部。那么作为一个程序员,到底应该具备什么样的能力呢?这个话题显然太大。不过我觉得可以看看其它国家的程序员,也许可以得到一些借鉴。我有幸和中国,美国,印度和日本四国程序员有比较深入的合作过。虽然他们不一定有代表性,但我觉得他们的共性还是比较明显的。以下的比较纯属个人见解,欢迎指正。

阅读全文…

大杂烩