LoginSignup
3
3

More than 5 years have passed since last update.

CUnitについての備忘録-2

Last updated at Posted at 2017-02-11

CUnit

CUnitについて使用するのだが、CUnitのAssert関数について不明な点が多いので、実際に使用し、該当関数の動作を確認する。

動作確認を行った関数及びCUnitの実行結果は以下の通り。

CUnit Assert関数

関数名 概要
CU_ASSERT(int expression) expressionがTRUE(真)の場合,Pass
CU_ASSERT_FATAL(int expression) 同上
CU_TEST(int expression) 同上
CU_TEST_FATAL(int expression) 同上
CU_ASSERT_TRUE(value) valueがTRUE(真)の場合,Pass
CU_ASSERT_TRUE_FATAL(value) 同上
CU_ASSERT_FALSE(value) valueがFALSE(偽)の場合,Pass
CU_ASSERT_FALSE_FATAL(value) 同上
CU_ASSERT_EQUAL(actual, expected) actual, expessionが同値の場合,Pass
CU_ASSERT_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_NOT_EQUAL(actual, expected)) actual, expessionが同値でない場合,Pass
CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_PTR_EQUAL(actual, expected) actual, expessionが同値の場合,Pass
CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_PTR_NOT_EQUAL(actual, expected) actual, expessionが同値でない場合,Pass
CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_PTR_NULL(value) valueがNULLの場合,Pass
CU_ASSERT_PTR_NULL_FATAL(value) 同上
CU_ASSERT_PTR_NOT_NULL(value) valueがNULLでない場合,Pass
CU_ASSERT_PTR_NOT_NULL_FATAL(value) 同上
CU_ASSERT_STRING_EQUAL(actual, expected) actual, expessionが同値の場合,Pass
CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_STRING_NOT_EQUAL(actual, expected) actual, expessionが同値でない場合,Pass
CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) 同上
CU_ASSERT_NSTRING_EQUAL(actual, expected, count) actural, expectedがcount番目まで同値の場合,Pass
CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) 同上
CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) actural, expectedがcount番目まで同値でない場合,Pass
CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) 同上
CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) (actual - expected) <= granularityの場合,Pass
CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) 同上
CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) (actual - expected) > granularityの場合,Pass
CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) 同上

テストコード

tsrc.c
include "CUnit/CUnit.h"
#include "CUnit/Console.h"

#include "tsrc.h"

int main() {
  CU_pSuite testSuite;

  /* CUnit初期化 */
  CU_initialize_registry();
  testSuite = CU_add_suite("dubleTestSuite", NULL, NULL);

  /* Test関数 */
  CU_add_test(testSuite, "test_01 test", test_01);
  CU_add_test(testSuite, "test_02 test", test_02);
  CU_add_test(testSuite, "test_03 test", test_03);
  CU_add_test(testSuite, "test_04 test", test_04);
  CU_add_test(testSuite, "test_05 test", test_05);
  CU_add_test(testSuite, "test_06 test", test_06);
  CU_add_test(testSuite, "test_07 test", test_07);
  CU_add_test(testSuite, "test_08 test", test_08);
  CU_add_test(testSuite, "test_09 test", test_09);
  CU_add_test(testSuite, "test_10 test", test_10);
  CU_add_test(testSuite, "test_11 test", test_11);
  CU_add_test(testSuite, "test_12 test", test_12);
  CU_add_test(testSuite, "test_13 test", test_13);
  CU_add_test(testSuite, "test_14 test", test_14);
  CU_add_test(testSuite, "test_15 test", test_15);

  /* CUnit 実行 */
  CU_console_run_tests();
  /* CUnit 終了処理 */
  CU_cleanup_registry();

  return 0;
}

void test_01() {

  CU_ASSERT( 1 == 0 );
  CU_ASSERT( 1 == 1 );

  CU_ASSERT_FATAL( 1 == 0 );
  CU_ASSERT_FATAL( 1 == 1 );

  CU_TEST( 1 == 0 );
  CU_TEST( 1 == 1 );

  CU_TEST_FATAL( 1 == 0 );
  CU_TEST_FATAL( 1 == 1 );

  return;
}

void test_02() {

  CU_ASSERT_TRUE( 1 == 0 );
  CU_ASSERT_TRUE( 1 == 1 );

  CU_ASSERT_TRUE_FATAL( 1 == 0 );
  CU_ASSERT_TRUE_FATAL( 1 == 1 );

  return;
}

void test_03() {

  CU_ASSERT_FALSE( 1 == 0 );
  CU_ASSERT_FALSE( 1 == 1 );

  CU_ASSERT_FALSE_FATAL( 1 == 0 );
  CU_ASSERT_FALSE_FATAL( 1 == 1 );

  return;
}

void test_04() {

  CU_ASSERT_EQUAL( 1, 0 );
  CU_ASSERT_EQUAL( 1, 1 );

  CU_ASSERT_EQUAL_FATAL( 1, 0 );
  CU_ASSERT_EQUAL_FATAL( 1, 1 );

  return;
}

void test_05() {

  CU_ASSERT_NOT_EQUAL( 1, 0 );
  CU_ASSERT_NOT_EQUAL( 1, 1 );

  CU_ASSERT_NOT_EQUAL_FATAL( 1, 0 );
  CU_ASSERT_NOT_EQUAL_FATAL( 1, 1 );

  return;
}

void test_06() {

  int i, j;

  CU_ASSERT_PTR_EQUAL( &i, &j );
  CU_ASSERT_PTR_EQUAL( &i, &i );

  CU_ASSERT_PTR_EQUAL_FATAL( &i, &j );
  CU_ASSERT_PTR_EQUAL_FATAL( &i, &i );

  return;
}

void test_07() {

  int i, j;

  CU_ASSERT_PTR_NOT_EQUAL( &i, &j );
  CU_ASSERT_PTR_NOT_EQUAL( &i, &i );

  CU_ASSERT_PTR_NOT_EQUAL_FATAL( &i, &j );
  CU_ASSERT_PTR_NOT_EQUAL_FATAL( &i, &i );

  return;
}

void test_08() {

  int *i;
  i = NULL;

  CU_ASSERT_PTR_NULL( i );
  CU_ASSERT_PTR_NULL_FATAL( i );

  return;
}

void test_09() {

  int *i;
  i = NULL;

  CU_ASSERT_PTR_NOT_NULL( i );
  CU_ASSERT_PTR_NOT_NULL_FATAL( i );

  return;
}

void test_10() {

  char x, y;

  x = 'X';
  y = 'Y';

  CU_ASSERT_STRING_EQUAL( &x, &y );
  CU_ASSERT_STRING_EQUAL_FATAL( &x, &y );

  return;
}

void test_11() {

  char x, y;

  x = 'X';
  y = 'Y';

  CU_ASSERT_STRING_NOT_EQUAL( &x, &y );
  CU_ASSERT_STRING_NOT_EQUAL_FATAL( &x, &y );

  return;
}

void test_12() {

  char i[] = "XYZ";
  char x[] = "IJK";

  CU_ASSERT_NSTRING_EQUAL( i, x, 3 );
  CU_ASSERT_NSTRING_EQUAL_FATAL( i, x, 3 );

  return;
}

void test_13() {

  char i[] = "XYZ";
  char x[] = "IJK";

  CU_ASSERT_NSTRING_NOT_EQUAL( i, x, 3 );
  CU_ASSERT_NSTRING_NOT_EQUAL_FATAL( i, x, 3 );

  return;
}

void test_14() {

  double x, y;

  x = 1.5;
  y = 1.4;

  CU_ASSERT_DOUBLE_EQUAL( x, y, 0.01 );
  CU_ASSERT_DOUBLE_EQUAL_FATAL( x, y, 0.01 );

  return;
}

void test_15() {

  double x, y;

  x = 1.5;
  y = 1.4;

  CU_ASSERT_DOUBLE_NOT_EQUAL( x, y, 0.01 );
  CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL( x, y, 0.01 );

  return;
}
tsrc.h
#ifndef _TSRC_H_
#define _TSRC_H_

int main();
void test_01();
void test_02();
void test_03();
void test_04();
void test_05();
void test_06();
void test_07();
void test_08();
void test_09();                                                                               
void test_10();
void test_11();
void test_12();
void test_13();
void test_14();
void test_15();

#endif /* _TSRC_H_ */

CUnit実行結果


$ ./a.out


     CUnit - A Unit testing framework for C - Version 2.1-2
             http://cunit.sourceforge.net/


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: run

Running Suite : dubleTestSuite
     Running Test : test_01 test
     Running Test : test_02 test
     Running Test : test_03 test
     Running Test : test_04 test
     Running Test : test_05 test
     Running Test : test_06 test
     Running Test : test_07 test
     Running Test : test_08 test
     Running Test : test_09 test
     Running Test : test_10 test
     Running Test : test_11 test
     Running Test : test_12 test
     Running Test : test_13 test
     Running Test : test_14 test
     Running Test : test_15 test

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests     15     15      4     11        0
             asserts     40     40     18     22      n/a

Elapsed time =    0.001 seconds


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: f

--------------- Test Run Failures -------------------------
   src_file:line# : (suite:test) : failure_condition

1. ./tsrc.c:40 : (dubleTestSuite : test_01 test) : 1 == 0
2. ./tsrc.c:43 : (dubleTestSuite : test_01 test) : 1 == 0
3. ./tsrc.c:57 : (dubleTestSuite : test_02 test) : CU_ASSERT_TRUE(1 == 0)
4. ./tsrc.c:60 : (dubleTestSuite : test_02 test) : CU_ASSERT_TRUE_FATAL(1 == 0)
5. ./tsrc.c:69 : (dubleTestSuite : test_03 test) : CU_ASSERT_FALSE(1 == 1)
6. ./tsrc.c:72 : (dubleTestSuite : test_03 test) : CU_ASSERT_FALSE_FATAL(1 == 1)
7. ./tsrc.c:79 : (dubleTestSuite : test_04 test) : CU_ASSERT_EQUAL(1,0)
8. ./tsrc.c:82 : (dubleTestSuite : test_04 test) : CU_ASSERT_EQUAL_FATAL(1,0)
9. ./tsrc.c:91 : (dubleTestSuite : test_05 test) : CU_ASSERT_NOT_EQUAL(1,1)
10. ./tsrc.c:94 : (dubleTestSuite : test_05 test) : CU_ASSERT_NOT_EQUAL_FATAL(1,1)
11. ./tsrc.c:103 : (dubleTestSuite : test_06 test) : CU_ASSERT_PTR_EQUAL(&i,&j)
12. ./tsrc.c:106 : (dubleTestSuite : test_06 test) : CU_ASSERT_PTR_EQUAL_FATAL(&i,&j)
13. ./tsrc.c:117 : (dubleTestSuite : test_07 test) : CU_ASSERT_PTR_NOT_EQUAL(&i,&i)
14. ./tsrc.c:120 : (dubleTestSuite : test_07 test) : CU_ASSERT_PTR_NOT_EQUAL_FATAL(&i,&i)
15. ./tsrc.c:141 : (dubleTestSuite : test_09 test) : CU_ASSERT_PTR_NOT_NULL(i)
16. ./tsrc.c:142 : (dubleTestSuite : test_09 test) : CU_ASSERT_PTR_NOT_NULL_FATAL(i)
17. ./tsrc.c:154 : (dubleTestSuite : test_10 test) : CU_ASSERT_STRING_EQUAL(&x,&y)
18. ./tsrc.c:155 : (dubleTestSuite : test_10 test) : CU_ASSERT_STRING_EQUAL_FATAL(&x,&y)
19. ./tsrc.c:178 : (dubleTestSuite : test_12 test) : CU_ASSERT_NSTRING_EQUAL(i,x,3)
20. ./tsrc.c:179 : (dubleTestSuite : test_12 test) : CU_ASSERT_NSTRING_EQUAL_FATAL(i,x,3)
21. ./tsrc.c:202 : (dubleTestSuite : test_14 test) : CU_ASSERT_DOUBLE_EQUAL(x,y,0.01)
22. ./tsrc.c:203 : (dubleTestSuite : test_14 test) : CU_ASSERT_DOUBLE_EQUAL_FATAL(x,y,0.01)
-----------------------------------------------------------
Total Number of Failures : 22


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: q
$ 

本稿が読者の役に立てれば幸いである。
また、本稿に不備などがあればご指摘、ご教示願いたい。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3