PCUnitã¯Cèšèª/C++èšèªã§ãã¹ãé§åéçº(Test-Driven Development; TDD)ãåäœãã¹ããããããã® ããŒã¿ãã«ã§ã³ã³ãã¯ããªãŠããããã¹ããã¬ãŒã ã¯ãŒã¯ã§ãã
PCUnitã¯ä»¥äžã®ãããªç¹é·ããããŸãã
-
æ©èœ
- CUnitã©ã€ã¯ãªã¢ãµãŒããã¯ãããµããŒãããŸãã
- ã¢ãµãŒããã¯ãã§å€±æããå 容ã衚瀺ã§ããŸããããã«printf圢åŒã§è¿œå ã¡ãã»ãŒãžã衚瀺ã§ããŸãã
- ãã¹ããã£ã¯ã¹ãã£(setup, teardown, initialize, cleanup)ããµããŒãããŸãã
- 1ã€ã®ãã¹ããç¹°ãè¿ãè¡ãRepeatedTestããµããŒãããŸãããããå©çšããŠãããŒã¿é§åãã¹ããè¡ãããšãã§ããŸãã
- ãã£ã¹ãã¬ã€ãããŒããŒãããªãã¿ãŒã²ããäžã§ãã¹ãããããã«ããŠãŒã¶ãŒå®çŸ©ã®å ¥åºåé¢æ°ãèšå®ã§ããŸãã
- å®è¡ãããã¹ããã€ã³ã¿ã©ã¯ãã£ãã«éžæã§ããã³ã³ãœãŒã«ã¢ãŒãããµããŒãããŸãã
- ã¿ãŒããã«ã察å¿ããŠããã°ãã¹ãçµæã®ã«ã©ãŒè¡šç€ºãã§ããŸãã
- ãã¹ãé¢æ°ã®é圢çæãèªåç»é²ãã§ããŸãã
- ãã¹ãçµæãXMLãã¡ã€ã«ã§åºåããããšãã§ããŸãã
- ã¢ãã¯ãªããžã§ã¯ãã䜿çšããããšãã§ããŸãã
-
移æ€æ§
- ãã©ãããã©ãŒã éäŸåãªå®è£ ãªã®ã§ã倧æµã®PCåããŸãã¯çµã¿èŸŒã¿åãã®C/C++ã³ã³ãã€ã©ã§ãã«ãããããšãã§ããŸãã ã€ãŸãããã¹ãç°å¢ãšã¿ãŒã²ããç°å¢ã®äž¡æ¹ã§åäœããå ±éã®ãã¹ãã³ãŒããå®è£ ããããšãã§ããŸãã
- ããšãæšæºCã©ã€ãã©ãªã䜿ããªãç°å¢ã§ã䜿çšå¯èœã§ãã
GNUéçºç°å¢ã®å Žåã¯ãPCUnitãã£ã¬ã¯ããªã§makeãå®è¡ããŠãã ãããmakeãå®è¡ãããšlibpcunit.aãçæãããã®ã§ ããããã¹ããããžã§ã¯ãã«éçãªã³ã¯ããŠãã ãããããã©ã«ãã®ã³ã³ãã€ã©ã¯gccã§ããã é©å®Makefileãä¿®æ£ããŠã¿ãŒã²ããçšã®ã¯ãã¹ã³ã³ãã€ã©ãã³ã³ãã€ã©ãªãã·ã§ã³ãæå®ããŠãã ããã ãã¹ãç°å¢ã«ã€ã³ã¹ããŒã«ãããå Žåã¯ãmake installãå®è¡ããŠãã ããã make installãå®è¡ãããš/usr/local/libã«libpcunit.aãã/usr/local/include/PCUnitã«ããããã¡ã€ã«ãã /usr/local/binã«ãŠãŒãã£ãªãã£ãã€ã³ã¹ããŒã«ããŸãã make uninstallãå®è¡ãããšã¢ã³ã€ã³ã¹ããŒã«ããŸãã ã€ã³ã¹ããŒã«å ãã£ã¬ã¯ããªã/usr/localããå€æŽããŠã€ã³ã¹ããŒã«ããå Žåã¯ã make install INSTALLDIR=/foo/barã®ããã«INSTALLDIRã«ä»»æã®ã€ã³ã¹ããŒã«å ãã£ã¬ã¯ããªãæå®ããŠãã ããã åæ§ã«ã¢ã³ã€ã³ã¹ããŒã«ããå Žåã¯ãmake uninstall INSTALLDIR=/foo/barãå®è¡ããŠãã ããã
GNUéçºç°å¢ã§ãªãå Žåã¯ãPCUnitãã£ã¬ã¯ããªä»¥äžã®ãœãŒã¹ãã¡ã€ã«ãšããããã¡ã€ã«ããã¹ããããžã§ã¯ãã«è¿œå ã㊠ãã¹ãã³ãŒããšå ±ã«ãã«ãããŠãã ããããããã¯PCUnitã®éçã©ã€ãã©ãªããã«ããããããžã§ã¯ããäœæãã ãã«ãããã©ã€ãã©ãªããã¹ããããžã§ã¯ãã«éçãªã³ã¯ããŠãã ããã
PCUnitã¯ã³ã³ãã€ã©ãªãã·ã§ã³ã®ãã¯ãå®çŸ©ã§ä»¥äžã®èšå®ãããããšãã§ããŸãã
PCUnitã®ãã«ãã倱æããå Žåã¯ãé©å®PCU_NO_*
ãã¯ããå®çŸ©ããããšã§ãã«ãå¯èœã«ãªããŸãã
ãã ããPCUnitããã«ãããããã«ã¯æäœé<stddef.h>
ãš<limits.h>
ãå¿
èŠã§ãã
- vsprintfã䜿çšã§ããªãããã«ãã«ãã倱æããå Žåã¯ã
PCU_NO_VSPRINTF
ãã¯ããå®çŸ©ããŠãã ããã - setjmp/longjmpã䜿çšã§ããªãããã«ãã«ãã倱æããå Žåã¯ã
PCU_NO_SETJMP
ãã¯ããå®çŸ©ããŠãã ããã <string.h>
ãš<stdlib.h>
ã®é¢æ°ã䜿çšã§ããªãããã«ãã«ãã倱æããå Žåã¯ãPCU_NO_LIBC
ãã¯ããå®çŸ©ããŠãã ããã ãã®ãã¯ããå®çŸ©ããå Žåã¯PCU_NO_VSPRINTF
ãPCU_NO_SETJMP
ãå®çŸ©ããå¿ èŠã¯ãããŸããã<stdarg.h>
ããªãããã«ãã«ãã倱æããå Žåã¯ãPCU_NO_STDARG
ãã¯ããå®çŸ©ããŠãã ããã- ããã»ããµã«FPUããªãããœãããŠã§ã¢æµ®åå°æ°ç¹ã©ã€ãã©ãªã䜿çšã§ããªãããã«ãã«ãã倱æããå Žåã¯ã
PCU_NO_FLOATINGPOINT
ãã¯ããå®çŸ©ããŠãã ããã - 32ãããã®é€ç®ãã§ããªãããã«ãã«ãã倱æãã(äŸãã°gccã§
___divsi3
ãªã©ã®ãªã³ã¯ãšã©ãŒãçºçãã)å Žåã¯ãPCU_NO_DIV32
ãã¯ããå®çŸ©ããŠãã ããã - ã³ã³ãœãŒã«ã¢ãŒããäžèŠãªå Žåã¯ã
PCU_NO_CONSOLE_RUN
ãã¯ããå®çŸ©ããŠãã ãããPCU_console_run
ã䜿çšã§ããªããªããŸãããã³ãŒããµã€ãºãå°ãå°ãããªããŸãã - ã¯ã€ãæååããã§ãã¯ããã¢ãµãŒããã¯ãã䜿çšãããå Žåã¯ã
PCU_USE_WCHAR
ãã¯ããå®çŸ©ããŠãã ããã_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©æžã¿ãªãã°ãPCU_USE_WCHAR
ãã¯ãã¯èªåçã«å®çŸ©æžã¿ã«ãªããŸãã PCU_format
/PCU_formatW
ã§äœ¿çšããéçé åã®ãµã€ãºãå€æŽãããå Žåã¯ãPCU_FORMAT_BUFSIZE
ãã¯ãã®å€ãå®çŸ©ããŠãã ããã
PCUnitã®ãã¹ãã¯ãã®ãããªæ§æã«ãªã£ãŠããŸãã
- ãã¹ãå®è¡é¢æ°
- ã¹ã€ãŒãã¡ãœãã 1
- ãã¹ãã¹ã€ãŒã 1
- ãã¹ãã±ãŒã¹ 1-1
- ãã¹ãã±ãŒã¹ 1-2
- ã»ã»ã»
- ãã¹ãã¹ã€ãŒã 1
- ã¹ã€ãŒãã¡ãœãã 2
- ãã¹ãã¹ã€ãŒã 2
- ãã¹ãã±ãŒã¹ 2-1
- ãã¹ãã±ãŒã¹ 2-2
- ã»ã»ã»
- ãã¹ãã¹ã€ãŒã 2
- ã¹ã€ãŒãã¡ãœãã 3
- ãã¹ãã¹ã€ãŒã 3
- ãã¹ãã±ãŒã¹ 3-1
- ãã¹ãã±ãŒã¹ 3-2
- ã»ã»ã»
- ãã¹ãã¹ã€ãŒã 3
- ã»ã»ã»
- ã¹ã€ãŒãã¡ãœãã 1
ãã¹ãã±ãŒã¹ ã¯1ã€ã®ãã¹ãé¢æ°ãå®è¡ããããã®ããŒã¿æ§é ã§ãã
ãã¹ãã±ãŒã¹ã¯PCU_Test
ãšããæ§é äœã§æ¬¡ã®ããã«åå®çŸ©ãããŠããŸãã
typedef struct {
const char *name; /* ãã¹ãã±ãŒã¹å */
void (*test)(void); /* ãã¹ãé¢æ° */
int ntimes; /* ãã¹ãé¢æ°ãå®è¡ããåæ° */
... /* 以äžãéå
¬éã¡ã³ã */
} PCU_Test;
ntimesã«ããç¹°ãè¿ãã¯setup, teardownã®å®è¡ãå«ã¿ãŸãã ntimesã0ã®å Žåã¯1ãæå®ãããšèŠãªããŸãããŸããè² ã®æ°ãªãã°ãã®ãã¹ãã±ãŒã¹ã¯ã¹ããããããŸãã
ãã¹ãã¹ã€ãŒã ã¯è€æ°ã®ãã¹ãã±ãŒã¹ã管çããããã®ããŒã¿æ§é ã§ãã
ãã¹ãã¹ã€ãŒãã¯PCU_Suite
ãšããæ§é äœã§æ¬¡ã®ããã«åå®çŸ©ãããŠããŸãã
typedef struct {
const char *name; /* ãã¹ãã¹ã€ãŒãå */
PCU_Test *tests; /* ãã¹ãã±ãŒã¹ã®é
å */
int ntests; /* testsã®èŠçŽ æ° */
int (*setup)(void); /* testsã®åãã¹ãé¢æ°ã®å®è¡åã«åŒã°ããé¢æ° */
int (*teardown)(void); /* testsã®åãã¹ãé¢æ°ã®å®è¡åŸã«åŒã°ããé¢æ° */
int (*initialize)(void); /* testsã®æåã®ãã¹ãã®å®è¡åã«åŒã°ããé¢æ° */
int (*cleanup)(void); /* testsã®æåŸã®ãã¹ãã®å®è¡åŸã«åŒã°ããé¢æ° */
... /* 以äžãéå
¬éã¡ã³ã */
} PCU_Suite;
setupãšteardownã®æ»ãå€ã¯ã0ãªãã°æ£åžžãé0ãªãã°ç°åžžã§ãã setupãç°åžžã®å Žåããã¹ãé¢æ°ãšteardownã¯å®è¡ãããŸããã
initializeãšcleanupã®æ»ãå€ã¯ã0ãªãã°æ£åžžãé0ãªãã°ç°åžžã§ãã initializeãç°åžžã®å Žåãtestsã®å šãŠã®ãã¹ããšcleanupã¯å®è¡ãããŸããã
setup, teardown, initialize, cleanupã¯ã0ãæå®ãããå Žåã¯åŒã°ããŸããã
ã¹ã€ãŒãã¡ãœãã ã¯ãã¹ãå®è¡é¢æ°ãããã¹ãã¹ã€ãŒãã«ã¢ã¯ã»ã¹ããããã®ã€ã³ã¿ãŒãã§ã€ã¹ã§ãã ã¹ã€ãŒãã¡ãœããã®è©³çŽ°ã«ã€ããŠã¯åŸè¿°ããŸãã
ç°¡åãªäŸãšããŠãaddé¢æ°ãšsubé¢æ°ããã¹ã察象é¢æ°ãšããŸãã
addé¢æ°ã¯åŒæ°aãšåŒæ°bã足ããå€ãè¿ããsubé¢æ°ã¯åŒæ°aããåŒæ°bãåŒããå€ãè¿ãé¢æ°ã§ãã ãããã¿ã€ã宣èšãããããããã¡ã€ã«AddSub.hãäœæããŸãã
[AddSub.h]
1 #ifndef ADDSUB_H_INCLUDED
2 #define ADDSUB_H_INCLUDED
3 int add(int a, int b);
4 int sub(int a, int b);
5 #endif
addé¢æ°ãšsubé¢æ°ãå®è£ ããåã«ããã¹ãé¢æ°ãå ã«å®è£ ããŸãã ãã¹ãé¢æ°ãå®è£ ãããœãŒã¹ãã¡ã€ã«åã¯ä»»æã§ããããã¹ã察象ã®ãœãŒã¹ãã¡ã€ã«åã«Testãä»ããååã«ãããšããã§ãããã 次ã®ããã«å®è£ ããŸãã
[AddSubTest.c]
1 #include "PCUnit/PCUnit.h"
2 #include "AddSub.h"
3
4 static void test_add(void)
5 {
6 PCU_ASSERT_EQUAL(3, add(2, 1));
7 PCU_ASSERT_EQUAL(0, add(0, 0));
8 PCU_ASSERT_EQUAL(1, add(-1, 2));
9 }
10
11 static void test_sub(void)
12 {
13 PCU_ASSERT_EQUAL(1, sub(2, 1));
14 PCU_ASSERT_EQUAL(0, sub(0, 0));
15 PCU_ASSERT_EQUAL(-3, sub(-1, 2));
16 }
17
PCUnitã䜿ãããã«ã¯PCUnit.h
ãã€ã³ã¯ã«ãŒãããå¿
èŠããããŸãã
PCUnit.h
ã®ã€ã³ã¯ã«ãŒããã¹ã¯é©å®ãã¹ããããžã§ã¯ãã§æå®ããŠãã ããã
ãã¹ãé¢æ°ã¯åŒæ°ã»æ»ãå€ãå ±ã«voidã®é¢æ°ã§ãããã¹ãé¢æ°åã¯ä»»æã§ãããtestãä»ããååã«ãããšããã§ãããã ä»ã®ãœãŒã¹ãã¡ã€ã«ããåŒã³åºããªããªãã°staticé¢æ°ã«ããŠãã ããã
ããã§ã¯PCU_ASSERT_EQUAL
ãã¯ãã䜿çšããŠãã¹ã察象é¢æ°ã®æ»ãå€ãä»æ§ã©ããããã§ãã¯ããŠããŸãã
PCU_ASSERT_EQUAL
ãã¯ãã®2ã€ã®åŒæ°ãçãããã°OKã§ãç°ãªã£ãŠããããã¹ã倱æã§ãã
æåã®åŒæ°ã«äºæž¬ãããå€ãæå®ãã2çªç®ã®åŒæ°ã«å®éã®å€ãæå®ããŸãã
次ã«ãã¹ã€ãŒãã¡ãœãããå®çŸ©ããŸãã
ã¹ã€ãŒãã¡ãœãã㯠static ãªPCU_Suite
ã®å€æ°ãžã®ãã€ã³ã¿ãè¿ãã°ããŒãã«é¢æ°ã§ãã
ã¹ã€ãŒãã¡ãœããã¯PCU_Suite
ã®ã¡ã³ãã®testsãsetupé¢æ°çã®ã¹ã³ãŒããå°ãããã圹å²ãæãããŸãã
é¢æ°åã¯ä»»æã§ããããã¹ãã¹ã€ãŒãå_suite()
ãšä»ãããšããã§ãããã
次ã®ããã«å®çŸ©ããŸãã
[AddSubTest.c]
18 PCU_Suite *AddSubTest_suite(void)
19 {
20 static PCU_Test tests[] = {
21 { "test_add", test_add },
22 { "test_sub", test_sub },
23 };
24 static PCU_Suite suite = { "AddSubTest", tests, sizeof tests / sizeof tests[0] };
25 return &suite;
26 }
ã¹ã€ãŒãã¡ãœããã¯åŒæ°ãvoidãæ»ãå€ãPCU_Suite *
ã®é¢æ°ã§ãã
ã¹ã€ãŒãã¡ãœããã§ã¯ãŸããPCU_Test
æ§é äœã®é
åtestsã static ã§å®çŸ©ãšåæã«åæåããŸãã
PCU_Test
æ§é äœã®æåã®ã¡ã³ãnameã«ã¯ä»»æã®ãã¹ãã±ãŒã¹åãæå®ããŸãã
2çªç®ã®ã¡ã³ãtestã«ã¯å
ã«å®çŸ©ãããã¹ãé¢æ°ãæå®ããŸãã
3çªç®ã®ã¡ã³ãã¯ntimesã§ããããã®äŸã§ã¯çç¥ãããŠããã®ã§0ã§åæåããããã¹ãé¢æ°ã¯1åå®è¡ãããŸãã
ç¹°ãè¿ããå¿
èŠãªå Žåã¯ntimesã«åæ°ãæå®ããŠãã ããã
åŸã§æ°ãããã¹ãã±ãŒã¹ãè¿œå ãããå Žåã¯ãæ°ãããã¹ãé¢æ°ãå®çŸ©ããŠãã®é
åã®åæåã«è¿œå ããŠãã ããã
(pcunit_register.rb
ãŠãŒãã£ãªãã£ã䜿çšããã°ãã¹ãã±ãŒã¹ç»é²ãèªååã§ããŸãã)
次ã«ãPCU_Suite
æ§é äœã®å€æ°suiteã static ã§å®çŸ©ãšåæã«åæåããŸãã
PCU_Suite
æ§é äœã®æåã®ã¡ã³ãnameã«ã¯ä»»æã®ãã¹ãã¹ã€ãŒãåãæå®ããŸãã
2çªç®ã®ã¡ã³ãtestsã«ã¯PCU_Test
ã®é
åtestsãæå®ããŸãã
3çªç®ã®ã¡ã³ãntestsã«ã¯testsã®èŠçŽ æ°ãæå®ããŸãã
4çªç®ä»¥äžã®ã¡ã³ãsetup, teardown, initialize, cleanupã¯ãã®äŸã§ã¯çç¥ãããŠããã®ã§ãããã0ã§åæåãããŸãã
setup, teardown, initialize, cleanupãå¿
èŠãªå Žåã¯ããããã®é¢æ°ãå®çŸ©ãæå®ããŠãã ããã
ãã®é¢æ°ã®æåŸã§suiteã®ã¢ãã¬ã¹ãè¿ããŸãã
mainé¢æ°ã¯æ¬¡ã®ããã«å®çŸ©ããŸãã
[main.c]
1 #include "PCUnit/PCUnit.h"
2 #include <stdio.h> /* for putchar */
3
4 PCU_Suite *AddSubTest_suite(void);
5
6 int main(void)
7 {
8 const PCU_SuiteMethod suites[] = {
9 AddSubTest_suite,
10 };
11 PCU_set_putchar(putchar);
12 return PCU_run(suites, sizeof suites / sizeof suites[0]);
13 }
mainé¢æ°ããã¹ããå®è£ ãããœãŒã¹ãšå¥ãã¡ã€ã«ã®å Žåã¯ã¹ã€ãŒãã¡ãœããã®ãããã¿ã€ã宣èšãããŠãã ããã
mainé¢æ°ã§ã¯ãŸããPCU_SuiteMethod
åã®é
åã宣èšããŸãã
PCU_SuiteMethod
åã¯ã¹ã€ãŒãã¡ãœãããžã®ãã€ã³ã¿åã§ã次ã®ããã«åå®çŸ©ãããŠããŸãã
typedef PCU_Suite *(*PCU_SuiteMethod)(void);
ããã§ã¯AddSubTest_suite
ã ãã§åæåããŠããŸãã
åŸã§æ°ãããã¹ãã¹ã€ãŒããè¿œå ãããå Žåã¯ãæ°ãããã¹ãã¹ã€ãŒãã«å¯Ÿå¿ããã¹ã€ãŒãã¡ãœããããã®é
åã®åæåã«è¿œå ããŠãã ããã
(pcunit_register.rb
ãŠãŒãã£ãªãã£ã䜿çšããã°ã¹ã€ãŒãã¡ãœããç»é²ãèªååã§ããŸãã)
次ã«ãPCU_set_putchar
é¢æ°ã§ãã¹ãçµæã®åºåé¢æ°ãputcharã«èšå®ããŠããŸãã
ãã®é¢æ°ã§ã¯ãŠãŒã¶ãŒå®çŸ©ã®putcharçžåœã®é¢æ°ãçšæããããšã§ãã¹ãçµæã®åºåå
ãå€æŽããããšãã§ããŸãã
äŸãã°ã¿ãŒã²ããäžã§åäœãããæã¯ã·ãªã¢ã«åºåé¢æ°ãªã©ãæå®ãããšããã§ãããã
æåŸã«ããã¹ãå®è¡é¢æ°ã§ããPCU_run
é¢æ°ãåŒã³åºããŸãã
PCU_run
ã®æåã®åŒæ°ã¯PCU_SuiteMethod
åã®é
åã§ã2çªç®ã®åŒæ°ã¯ãã®é
åã®èŠçŽ æ°ã§ãã
PCU_run
ã®æ»ãå€ã¯ã倱æããªããã°0ã1ã€ã§ã倱æãããã°é0ã§ãã
ããã§ãã¹ãã³ãŒãã®æºåã¯æŽããŸããã
ãã«ããããšaddé¢æ°ãšsubé¢æ°ã®å®è£ ããªãããããªã³ã¯ãšã©ãŒã«ãªããŸãã
ããã§ãAddSub.cãäœæããaddé¢æ°ãšsubé¢æ°ã®å®è£ ãããŸãã
[AddSub.c]
1 #include "AddSub.h"
2 int add(int a, int b) { return 0; }
3 int sub(int a, int b) { return 0; }
åçŽãªé¢æ°ã§ããããããšãã¹ãã«å€±æãããããã«0ãè¿ãããã«ããŠãããŸãã
å床ãã«ãããŠå®è¡ãããšããã¹ãã«å€±æããŸãã ãã®ããã«åºåãããŸãã
Suite: AddSubTest
Test: test_add
AddSubTest.c:6: PCU_ASSERT_EQUAL(3, add(2, 1))
expected : 0x00000003 (3)
actual : 0x00000000 (0)
Test: test_sub
AddSubTest.c:13: PCU_ASSERT_EQUAL(1, sub(2, 1))
expected : 0x00000001 (1)
actual : 0x00000000 (0)
2 Tests, 2 Failures, 0 Skipped
倱æãããã¹ãã¹ã€ãŒãåããã¹ãã±ãŒã¹åããã¡ã€ã«åãè¡çªå·ãã¢ãµãŒããã¯ããã¢ãµãŒããã¯ãã®åŒæ°ã®å€ã衚瀺ãããŸãã ãã®åŸããã®ãã¹ãã¹ã€ãŒãã«ç»é²ãããŠããå šãã¹ãã±ãŒã¹æ°ãå®è¡ããŠå€±æãããã¹ãã±ãŒã¹æ°ã ã¹ãããããããã¹ãã±ãŒã¹æ°ã衚瀺ãããŸãã
ãã¹ãã«ãã¹ããããã«ãaddé¢æ°ãšsubé¢æ°ãä¿®æ£ããŸãã
[AddSub.c]
- int add(int a, int b) { return 0; }
- int sub(int a, int b) { return 0; }
+ int add(int a, int b) { return a + b; }
+ int sub(int a, int b) { return a - b; }
ä¿®æ£ããããã«ãããŠå®è¡ããŸãã ãã®ããã«åºåãããŸãã
Suite: AddSubTest
2 Tests, 0 Failures, 0 Skipped
OK
å
šãŠã®ãã¹ãã«ãã¹ãããšãOKã®ã¡ãã»ãŒãžã衚瀺ãããŸãã
PCU_set_verbose
ã§åºåãåé·ã¢ãŒãã«ãªã£ãŠããªããã°ããã¹ãããã¹ãã±ãŒã¹ã®æ
å ±ã¯è¡šç€ºãããŸããã
OKã®ã¡ãã»ãŒãžã¯1ã€ã®ãã¹ãã¹ã€ãŒãã«ã€ã1ã€è¡šç€ºãããŸãã ãã®äŸã§ã¯ãã¹ãã¹ã€ãŒãã¯1ã€ã§ãããããã¹ãã¹ã€ãŒããè€æ°ã®å Žåã å šãŠã®ãã¹ãã¹ã€ãŒãã«OKã衚瀺ããããŸã§ä¿®æ£ãç¹°ãè¿ããŠãã ããã
ã¢ãµãŒããã¯ãã¯ãã¹ãé¢æ°å ã§äœ¿çšå¯èœã§ãã åã¢ãµãŒããã¯ãã®åŒæ°ã¯2å以äžè©äŸ¡ãããªãã®ã§ããã¯ãå±éã«ããå¯äœçšãæ°ã«ããå¿ èŠã¯ãããŸããã
ã¢ãµãŒã·ã§ã³å€±ææã¯longjmpã«ãã£ãŠãã¹ãé¢æ°ããæããŸãã
ãã ããPCUnitãPCU_NO_SETJMP
ãã¯ããŸãã¯PCU_NO_LIBC
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯ã
longjmpã®ä»£ããã«returnã«ãã£ãŠãã¹ãé¢æ°ããæããŸãã
ãã®å Žåãã¹ã¿ãçã®ãã¹ãé¢æ°å
ã§åŒã³åºãããé¢æ°ããäžæ°ã«æããããšã¯ã§ããªããªããŸãã
以äžã«PCUnitãæäŸããã¢ãµãŒããã¯ãã瀺ããŸãã
-
PCU_ASSERT(expr)
ä»»æã®åŒexprãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_TRUE(expr)
ä»»æã®åŒexprãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_FALSE(expr)
ä»»æã®åŒexprãåœãã©ãããã§ãã¯ããŸãã çãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_EQUAL(expected, actual)
expectedãšactualãæŽæ°ã§ããåæã§ãexpectedãšactualãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_NOT_EQUAL(value1, value2)
value1ãšvalue2ãæŽæ°ã§ããåæã§ãvalue1ãšvalue2ãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_PTR_EQUAL(expected, actual)
expectedãšactualããã€ã³ã¿ã§ããåæã§ãexpectedãšactualã®ãã€ã³ã¿ã®å€ãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_PTR_NOT_EQUAL(value1, value2)
value1ãšvalue2ããã€ã³ã¿ã§ããåæã§ãvalue1ãšvalue2ã®ãã€ã³ã¿ã®å€ãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_PTR_NULL(value)
valueããã€ã³ã¿ã§ããåæã§ãvalueãNULLãã©ãããã§ãã¯ããŸãã NULLã§ãªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_PTR_NOT_NULL(value)
valueããã€ã³ã¿ã§ããåæã§ãvalueãNULLã§ãªããã©ãããã§ãã¯ããŸãã NULLãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_STRING_EQUAL(expected, actual)
expectedãšactualãæåå(const char *)ã§ããåæã§ãexpectedãšactualã®æååãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_STRING_NOT_EQUAL(value1, value2)
value1ãšvalue2ãæåå(const char *)ã§ããåæã§ãvalue1ãšvalue2ã®æååãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_NSTRING_EQUAL(expected, actual, n)
expectedãšactualãæåå(const char *)ã§ããåæã§ãexpectedãšactualã®æååã®å é ããnæåãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_NSTRING_NOT_EQUAL(value1, value2, n)
value1ãšvalue2ãæåå(const char *)ã§ããåæã§ãvalue1ãšvalue2ã®æååã®å é ããnæåãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_STRINGW_EQUAL(expected, actual)
expectedãšactualãã¯ã€ãæåå(const wchar_t *)ã§ããåæã§ãexpectedãšactualã®ã¯ã€ãæååãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã 倱ææã®åŒæ°ã®æåå衚瀺ã¯ãçŸåšã®ãã±ãŒã«ã®
LC_CTYPE
ã«ããŽãªã«äŸåããŸãã ãªãããã®ãã¯ãã䜿çšããããã«ã¯PCUnitãPCU_USE_WCHAR
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå¿ èŠããããŸãã -
PCU_ASSERT_STRINGW_NOT_EQUAL(value1, value2)
value1ãšvalue2ãã¯ã€ãæåå(const wchar_t *)ã§ããåæã§ãvalue1ãšvalue2ã®ã¯ã€ãæååãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã 倱ææã®åŒæ°ã®æåå衚瀺ã¯ãçŸåšã®ãã±ãŒã«ã®
LC_CTYPE
ã«ããŽãªã«äŸåããŸãã ãªãããã®ãã¯ãã䜿çšããããã«ã¯PCUnitãPCU_USE_WCHAR
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå¿ èŠããããŸãã -
PCU_ASSERT_NSTRINGW_EQUAL(expected, actual, n)
expectedãšactualãã¯ã€ãæåå(const wchar_t *)ã§ããåæã§ãexpectedãšactualã®ã¯ã€ãæååã®å é ããnæåãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã 倱ææã®åŒæ°ã®æåå衚瀺ã¯ãçŸåšã®ãã±ãŒã«ã®
LC_CTYPE
ã«ããŽãªã«äŸåããŸãã ãªãããã®ãã¯ãã䜿çšããããã«ã¯PCUnitãPCU_USE_WCHAR
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå¿ èŠããããŸãã -
PCU_ASSERT_NSTRINGW_NOT_EQUAL(value1, value2, n)
value1ãšvalue2ãã¯ã€ãæåå(const wchar_t *)ã§ããåæã§ãvalue1ãšvalue2ã®ã¯ã€ãæååã®å é ããnæåãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã 倱ææã®åŒæ°ã®æåå衚瀺ã¯ãçŸåšã®ãã±ãŒã«ã®
LC_CTYPE
ã«ããŽãªã«äŸåããŸãã ãªãããã®ãã¯ãã䜿çšããããã«ã¯PCUnitãPCU_USE_WCHAR
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå¿ èŠããããŸãã -
PCU_ASSERT_STRINGT_EQUAL(expected, actual)
_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©ãããŠããå Žåã¯PCU_ASSERT_STRINGW_EQUAL
ã«å±éããã ããã§ãªãå Žåã¯PCU_ASSERT_STRING_EQUAL
ã«å±éãããŸãã -
PCU_ASSERT_STRINGT_NOT_EQUAL(value1, value2)
_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©ãããŠããå Žåã¯PCU_ASSERT_STRINGW_NOT_EQUAL
ã«å±éããã ããã§ãªãå Žåã¯PCU_ASSERT_STRING_NOT_EQUAL
ã«å±éãããŸãã -
PCU_ASSERT_NSTRINGT_EQUAL(expected, actual, n)
_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©ãããŠããå Žåã¯PCU_ASSERT_NSTRINGW_EQUAL
ã«å±éããã ããã§ãªãå Žåã¯PCU_ASSERT_NSTRING_EQUAL
ã«å±éãããŸãã -
PCU_ASSERT_NSTRINGT_NOT_EQUAL(value1, value2, n)
_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©ãããŠããå Žåã¯PCU_ASSERT_NSTRINGW_NOT_EQUAL
ã«å±éããã ããã§ãªãå Žåã¯PCU_ASSERT_NSTRING_NOT_EQUAL
ã«å±éãããŸãã -
PCU_ASSERT_MEMORY_EQUAL(expected, actual, size, n)
expectedãšactualãsizeãã€ãã®åã®ãã€ã³ã¿ã§ããåæã§ãexpectedãšactualã®å é ããnåãçãããã©ãããã§ãã¯ããŸãã çãããªããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_MEMORY_NOT_EQUAL(value1, value2, size, n)
value1ãšvalue2ãsizeãã€ãã®åã®ãã€ã³ã¿ã§ããåæã§ãvalue1ãšvalue2ã®å é ããnåãçãããªããã©ãããã§ãã¯ããŸãã çãããªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
-
PCU_ASSERT_DOUBLE_EQUAL(value1, value2, delta)
value1ãšvalue2ãšdeltaãæµ®åå°æ°ç¹æ°ã§ããåæã§ã|value1 - value2| <= |delta|ãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã ãªããPCUnitã
PCU_NO_FLOATINGPOINT
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯äœ¿çšã§ããŸããã -
PCU_ASSERT_DOUBLE_NOT_EQUAL(value1, value2, delta)
value1ãšvalue2ãšdeltaãæµ®åå°æ°ç¹æ°ã§ããåæã§ã|value1 - value2| > |delta|ãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã ãªããPCUnitã
PCU_NO_FLOATINGPOINT
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯äœ¿çšã§ããŸããã -
PCU_ASSERT_OPERATOR(value1, op, value2)
value1ãšvalue2ã笊å·ç¡ãæŽæ°ãè¿ãä»»æã®åŒã§opãä»£å ¥ä»¥å€ã®ä»»æã®äºé æŒç®åã§ããåæã§ã((value1) op (value2)) ãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
äŸ:
PCU_ASSERT_OPERATOR(x < 0, ||, 100 <= x);
xã0æªæºãŸãã¯100以äžããã§ãã¯PCU_ASSERT_OPERATOR(x, &, 0x01);
xã®æäžäœããããç«ã£ãŠããããã§ãã¯
-
PCU_ASSERT_OPERATOR_INT(value1, op, value2)
value1ãšvalue2ã笊å·ä»ãæŽæ°ãè¿ãä»»æã®åŒã§opãä»£å ¥ä»¥å€ã®ä»»æã®äºé æŒç®åã§ããåæã§ã((value1) op (value2)) ãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã
äŸ:
PCU_ASSERT_OPERATOR_INT(x, <, -1);
xã-1ããå°ããå€ããã§ãã¯
-
PCU_ASSERT_OPERATOR_DOUBLE(value1, op, value2)
value1ãšvalue2ãæµ®åå°æ°ç¹æ°ãè¿ãä»»æã®åŒã§opãæ¯èŒæŒç®åã§ããåæã§ã((value1) op (value2)) ãçãã©ãããã§ãã¯ããŸãã åœãªãã°å€±æãåºåãããã¹ãé¢æ°ããæããŸãã ãªããPCUnitã
PCU_NO_FLOATINGPOINT
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯äœ¿çšã§ããŸãããäŸ:
PCU_ASSERT_OPERATOR_DOUBLE(x, >=, 1.0);
xã1.0以äžã®å€ããã§ãã¯
-
PCU_ASSERT*_MESSAGE(*, msg)
äžèšã®åã¢ãµãŒããã¯ãã«ã¯ãæåŸã®åŒæ°ã«msgãè¿œå ããããã¯ãåã®æ«å°Ÿã«
_MESSAGE
ãä»ããããŒãžã§ã³ããããŸãã ã¢ãµãŒã·ã§ã³å€±ææã«msgã§æå®ããã¡ãã»ãŒãžãè¿œå 衚瀺ã§ããããšä»¥å€ã¯åã¢ãµãŒããã¯ããšåãã§ãã msgã®åã¯const char *
ã§ãã ãªããPCU_format
/PCU_formatW
ã䜿çšããã°printf/wprintfãšåã圢åŒã®æååãæå®ããããšãã§ããŸããäŸ:
PCU_ASSERT_MESSAGE(x, "x is false");
PCU_ASSERT_EQUAL_MESSAGE(x, y, PCU_format("a:%s, b:%x, c:%d", a, b, c));
-
PCU_FAIL(msg)
ç¡æ¡ä»¶ã«å€±æãåºåãããã¹ãé¢æ°ããæããŸãã 倱æ衚瀺ã®éã«msgã§æå®ããã¡ãã»ãŒãžã衚瀺ããŸãã msgã®åã¯
const char *
ã§ãã ãªããPCU_format
/PCU_formatW
ã䜿çšããã°printf/wprintfãšåã圢åŒã®æååãæå®ããããšãã§ããŸãã -
PCU_MESSAGE(msg)
ååŠãã§ãã¯ãè¡ãããmsgã§æå®ããã¡ãã»ãŒãžã衚瀺ããŸãã ãã®ãã¯ãã¯ãã¹ãé¢æ°ããæããŸããã msgã®åã¯
const char *
ã§ãã ãªããPCU_format
/PCU_formatW
ã䜿çšããã°printf/wprintfãšåã圢åŒã®æååãæå®ããããšãã§ããŸãã
-
int PCU_run(const PCU_SuiteMethod *suite_methods, int num)
PCU_SuiteMethod
ã®é åsuite_methods
ããå°ãããå šãã¹ããå®è¡ããŸããnum
ã«ã¯suite_methods
ã®èŠçŽ æ°ãæå®ããŸãã å šãã¹ããå®è¡ããçµæã倱æããªããã°0ãã1ã€ã§ã倱æãããã°é0ãè¿ããŸãã -
int PCU_console_run(const PCU_SuiteMethod *suite_methods, int num)
ã³ã³ãœãŒã«ã¢ãŒãã§ãã¹ããå®è¡ããŸãã å®è¡ãããã¹ãã察話çã«éžæãããå Žåã
PCU_run
ã®ä»£ããã«ãã¡ãã䜿ããŸãã åŒæ°ã¯PCU_run
ãšåãã§ãã ãã¹ããå®è¡ããçµæã倱æããªããã°0ãã1ã€ã§ã倱æãããã°é0ãè¿ããŸãã ãªããPCUnitãPCU_NO_CONSOLE_RUN
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯äœ¿çšã§ããŸããã -
void PCU_set_putchar(PCU_Putchar func)
1æååºåããé¢æ°ãèšå®ããŸãã
PCU_Putchar
åã¯putcharãšåãåã®é¢æ°ãžã®ãã€ã³ã¿åã§ãtypedef int (*PCU_Putchar)(int);
ãšåå®çŸ©ãããŠããŸããPCU_run
ãŸãã¯PCU_console_run
ã®åã«å¿ ãèšå®ããŠãã ããã -
void PCU_set_getchar(PCU_Getchar func)
å ¥åãã1æåãååŸããé¢æ°ãèšå®ããŸãã
PCU_Getchar
åã¯getcharãšåãåã®é¢æ°ãžã®ãã€ã³ã¿åã§ãtypedef int (*PCU_Getchar)(void);
ãšåå®çŸ©ãããŠããŸããPCU_console_run
ã®åã«å¿ ãèšå®ããŠãã ããã -
void PCU_set_verbose(int verbose_flag)
verbose_flag
ãé0ãªãããã¹ãããã¹ããã¹ããããããã¹ãããã¹ãçµæã«åºåããåé·ã¢ãŒãã«ç§»è¡ããŸããverbose_flag
ã0ãªããåºåãéåžžã¢ãŒãã«æ»ããŸãã -
void PCU_enable_color(void)
ã¿ãŒããã«ãã«ã©ãŒè¡šç€ºã«å¯Ÿå¿ããŠããã°ããã¹ãçµæã®ã«ã©ãŒè¡šç€ºãæå¹ã«ããŸãã
-
void PCU_disable_color(void)
ãã¹ãçµæã®ã«ã©ãŒè¡šç€ºãç¡å¹ã«ããŸãã
-
const char *PCU_suite_name(void)
çŸåšå®è¡äžã®ãã¹ãã¹ã€ãŒãåãè¿ããŸãã ãã¹ãé¢æ°ãsetupé¢æ°ãteardowné¢æ°ãinitializeé¢æ°ãcleanupé¢æ°å ã§äœ¿çšå¯èœã§ãã
-
const char *PCU_test_name(void)
çŸåšå®è¡äžã®ãã¹ãã±ãŒã¹åãè¿ããŸãã ãã¹ãé¢æ°ãsetupé¢æ°ãteardowné¢æ°å ã§äœ¿çšå¯èœã§ãã
-
int PCU_repeat_counter(void)
ãã¹ãã±ãŒã¹ã®å®è¡ããçŸåšäœåç®(0ãªãªãžã³)ã§ããããè¿ããŸãã
PCU_Test
ã®ntimesã0ã®å Žåã¯åžžã«0ãè¿ããŸãã- ntimesã0ãã倧ããå Žåã¯0 ïœ ntimes - 1ã®å€ãè¿ããŸãã
ãã¹ãé¢æ°ãsetupé¢æ°ãteardowné¢æ°å ã§äœ¿çšå¯èœã§ãã
äŸãã°ãããŒã¿é§åãã¹ãã«ããããã¹ãããŒã¿ã®ããŒãã«ããèŠçŽ ãååŸããããã®ã€ã³ããã¯ã¹ã«å©çšã§ããŸãã
PCU_Test
ã®ntimesã«ã¯ãã¹ãããŒã¿ã®èŠçŽ æ°ãæå®ããŠãã ããã -
const char *PCU_format(const char *format, ...)
åŒæ°ã«æå®ããprintf圢åŒã®æååãéçé åã«å±éãããã®é åã®ãã€ã³ã¿ãè¿ããŸãã
PCU_ASSERT*_MESSAGE
ãPCU_FAIL
ãPCU_MESSAGE
ã®ã¡ãã»ãŒãžãprintf圢åŒã«ãããå Žåã«å©çšã§ããŸãã éçé åã®ãµã€ãºã¯PCUnitã®ãã«ãæã«PCU_FORMAT_BUFSIZE
ãã¯ãã®å€ã§å®çŸ©ã§ããŸããå®çŸ©ããªãå Žåã®ããã©ã«ãå€ã¯512ã§ãã å éšã§vsprintfã䜿çšããŠããã®ã§æååå±éåŸã®ãµã€ãºãPCU_FORMAT_BUFSIZE
ãè¶ ããªãããã«æ³šæããŠãã ãããäŸ:
PCU_ASSERT_EQUAL_MESSAGE(x, y, PCU_format("a:%s, b:%x, c:%d", a, b, c));
ãªããPCUnitã
PCU_NO_STDARG
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå Žåã¯äœ¿çšã§ããŸãããPCU_NO_STDARG
ãã¯ããå®çŸ©æžã¿ã®å Žåã¯ã代ããã«PCU_format0
ïœPCU_format9
(0ïœ9ã¯åŒæ°format
ããåŸã®åŒæ°ã®åæ°)ã䜿çšããŠãã ãããäŸ:
PCU_ASSERT_EQUAL_MESSAGE(x, y, PCU_format3("a:%s, b:%x, c:%d", a, b, c));
-
const char *PCU_formatW(const wchar_t *format, ...)
åŒæ°ã«æå®ããwprintf圢åŒã®ã¯ã€ãæååããã«ããã€ãæååã«å€æããŠéçé åã«å±éãããã®é åã®ãã€ã³ã¿ãè¿ããŸãã
PCU_ASSERT*_MESSAGE
ãPCU_FAIL
ãPCU_MESSAGE
ã®ã¡ãã»ãŒãžãwprintf圢åŒã«ãããå Žåã«å©çšã§ããŸãã éçé åã®ãµã€ãºã¯PCUnitã®ãã«ãæã«PCU_FORMAT_BUFSIZE
ãã¯ãã®å€ã§å®çŸ©ã§ããŸããå®çŸ©ããªãå Žåã®ããã©ã«ãå€ã¯512ã§ãã å éšã§vsprintfã䜿çšããŠããã®ã§æååå±éåŸã®ãµã€ãºãPCU_FORMAT_BUFSIZE
ãè¶ ããªãããã«æ³šæããŠãã ããã ãªãããã®é¢æ°ã䜿çšããããã«ã¯PCUnitãPCU_USE_WCHAR
ãã¯ããå®çŸ©æžã¿ã§ãã«ããããŠããå¿ èŠããããŸããäŸ:
PCU_ASSERT_EQUAL_MESSAGE(x, y, PCU_formatW(L"a:%s, b:%x, c:%d", a, b, c));
-
const char *PCU_formatT(const TCHAR *format, ...)
PCU_formatT
ã¯ãã¯ãã§ãã_UNICODE
ãã¯ããŸãã¯UNICODE
ãã¯ããå®çŸ©ãããŠããå Žåã¯PCU_formatW
ã«å±éããã ããã§ãªãå Žåã¯PCU_format
ã«å±éãããŸãã
PCU_Test
ã®é
ååæåã®éã«ããã¹ãã±ãŒã¹åãšãã¹ãé¢æ°ãåãååã«ããå Žåããã«ããŒãã¯ããå©çšã§ããŸãã
ãã«ããŒãã¯ãã¯æ¬¡ã®ããã«å®çŸ©ãããŠããŸãã
#define PCU_TEST(func) { #func, func }
#define PCU_TEST_REPEATED(func, ntimes) { #func, func, ntimes }
#define PCU_TEST_SKIPPED(func) { #func, func, -1 }
static PCU_Test tests[] = {
PCU_TEST(test_hoge), /* test_hogeé¢æ°ãç»é² */
PCU_TEST(test_piyo), /* test_piyoé¢æ°ãç»é² */
PCU_TEST_REPEATED(test_foo, 5), /* test_fooé¢æ°ãç»é²(5åå®è¡) */
PCU_TEST_SKIPPED(test_bar), /* test_baré¢æ°ãç»é²(ã¹ããã) */
};
pcunit_template.rb
ã¯PCUnitçšã®ãœãŒã¹ãã¡ã€ã«ã®é圢ãçæããRubyã¹ã¯ãªããã§ãã
æžåŒã¯æ¬¡ã®éãã§ãã
pcunit_template.rb [suite_name ...] [-d DIR] [-p [EXT]] [-m [FILE]] [-M [FILE] [-i]] [-o]
suite_name
ã«1ã€ä»¥äžã®ãã¹ãã¹ã€ãŒãåãæå®ãããšããã¹ãã¹ã€ãŒãå.c
ãšãããã¡ã€ã«ãçæããŸãã
çæããããã¡ã€ã«ã«ã¯ããããããã¹ãã¹ã€ãŒãå_suite()
ãšããååã®ã¹ã€ãŒãã¡ãœãããã
setupé¢æ°ã»ãã¹ãé¢æ°çã®é圢ãå®çŸ©ãããŠããŸãã
é©å®ãã®ãã¡ã€ã«ãç·šéããŠäœ¿çšããŠãã ããã
ãŸããmainé¢æ°ãMakefileã®é圢ãçæããããšãã§ããŸãã
-
-d DIR
DIR
ã«ãã¡ã€ã«ãçæãããã£ã¬ã¯ããªãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ãçç¥ããå Žåãã«ã¬ã³ããã£ã¬ã¯ããªãæå®ãããšèŠãªããŸãã -
-p [EXT]
çæãããã¡ã€ã«ãC++ã«ããŸãã
EXT
ã«C++ã®æ¡åŒµå(cpp, cc, cxxç)ãæå®ããŠãã ãããEXT
ãçç¥ããå Žåãæ¡åŒµåã¯cppã«ãªããŸãã -
-m [FILE]
PCU_run
ãåŒã³åºãmainé¢æ°ãå®çŸ©ãããã¡ã€ã«ãçæããŸããFILE
ã«ãã¡ã€ã«åãæå®ããŠãã ãããFILE
ãçç¥ããå Žåããã¡ã€ã«åã¯main.c
(-p
æå®ã®å Žåã¯main.cpp
)ã«ãªããŸãã -
-M [FILE] [-i]
ãã¹ããããžã§ã¯ãã®Makefileã®éåãçæããŸãã
FILE
ã«ãã¡ã€ã«åãæå®ããŠãã ãããFILE
ãçç¥ããå Žåããã¡ã€ã«åã¯Makefile
ã«ãªããŸãã-i
ãæå®ããå Žåãlibpcunit.aãæ¢ã«ã€ã³ã¹ããŒã«ãããŠããåæã®MakefileãçæããŸãã-i
ãçç¥ããå Žåããã¹ãã³ãŒããšPCUnitãå ±ã«ãã«ãããMakefileãçæããŸãã -
-o
çæãããã¡ã€ã«ãšåãååã®ãã¡ã€ã«ãæ¢ã«ååšããŠããå Žåã§ãäžæžãããŸãã
pcunit_register.rb
ã¯ãã¹ãé¢æ°ãèªåç»é²ããRubyã¹ã¯ãªããã§ãã
æžåŒã¯æ¬¡ã®éãã§ãã
pcunit_register.rb [-d DIR] [-e PATTERN] [-n]
pcunit_register.rb
ã¯ããããã£ã¬ã¯ããªä»¥äžã®å
šãŠã®ãœãŒã¹ãã¡ã€ã«ãååž°çã«æ€çŽ¢ãã
test
ã§å§ãŸããã¹ãé¢æ°ã®äžã§ãPCU_Test
ã®staticé
åã®åæåã«ç»é²ãããŠããªãé¢æ°ãèªåçã«ç»é²ããŸãã
ãŸããPCU_run
ã®åŒæ°ã«ãªãPCU_SuiteMethod
ã®é
åã®åæåã®ç»é²ãè¡ããŸãã
ãœãŒã¹ãã¡ã€ã«ãæŽæ°ããã®ã§ãæŽæ°åã®ãã¡ã€ã«ã¯ãã¡ã€ã«åã«.bak
ãä»ããŠããã¯ã¢ããããŸãã
æŽæ°ããå¿
èŠããªããã¡ã€ã«ã«ã¯äœãããŸããã
PCU_Test
ã®é
ååæåã以äžã®ããã«ããŠãããšããã¹ãé¢æ°ã®ç»é²ã.pcutestsãã¡ã€ã«ã«è¿œãåºãããšãã§ããŸãã.pcutestsãã¡ã€ã«ã¯èªåçã«çæãããã®ã§ãæåã§äœæããå¿
èŠã¯ãããŸããã
static PCU_Test tests[] = {
#include "FooTest.pcutests"
};
䜿çšãããã«ãããŒã«ã«ãŠããã¹ããããžã§ã¯ãã®ãã«ãæã«ãã®ã¹ã¯ãªãããå®è¡ããããã«èšå®ããŠããã°ã ãã¹ãé¢æ°ãæåã§ç»é²ãããå¿ èŠããªããªããŸãã äŸãã°Makefileãªãã°allã¿ãŒã²ããã次ã®ããã«ä¿®æ£ãããšããã§ãããã
- all: $(TARGET)
+ all: pcunit_register $(TARGET)
+
+ pcunit_register:
+ pcunit_register.rb
-
-d DIR
DIR
ã«ãã¹ããããžã§ã¯ãã®ãœãŒã¹ãã¡ã€ã«ã®ãã£ã¬ã¯ããªãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ã¯è€æ°æå®ã§ããŸãã ãã®ãªãã·ã§ã³ãçç¥ããå Žåãã«ã¬ã³ããã£ã¬ã¯ããªãæå®ãããšèŠãªããŸãã -
-e PATTERN
PATTERN
ã«æ€çŽ¢å¯Ÿè±¡ããé€å€ãããã£ã¬ã¯ããªå/ãã¡ã€ã«åã®æ£èŠè¡šçŸãã¿ãŒã³ãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ã¯è€æ°æå®ã§ããŸãã -
-n
æŽæ°åãã¡ã€ã«ã®ããã¯ã¢ãããããªããªããŸãã
pcunit_xml_output.rb
ã¯ãã¹ãçµæãXMLãã¡ã€ã«ã§åºåããRubyã¹ã¯ãªããã§ãã
æžåŒã¯æ¬¡ã®éãã§ãã
pcunit_xml_output.rb [output_file] [-e] [-n]
pcunit_xml_output.rb
ã¯ããã¹ãããã°ã©ã ãæšæºåºåãžåºåãããã¹ãçµæãæšæºå
¥åããèªã¿åããXMLãã¡ã€ã«ã«å€æããŠåºåããŸãã
ãŸããæšæºå
¥åããã®ãŸãŸæšæºåºåã«ãåºåããŸãã
output_file
ã«åºåããXMLãã¡ã€ã«åãæå®ããŸãã
output_file
ãçç¥ããå ŽåãXMLãã¡ã€ã«åã¯test_results.xml
ã«ãªããŸãã
å
šãŠã®ãã¹ãçµæãéèšãããå Žåã¯ããã¹ãããã°ã©ã ã®åºåãPCU_set_verbose
ã§åé·ã¢ãŒãã«ããŠãã ããã
次ã®ããã«ãã€ããå©çšããŠããã¹ãããã°ã©ã ã®æšæºåºåãå ¥åãšããŠäœ¿çšããŸãã
$ ./alltests | pcunit_xml_output.rb
æ¢ã«ããã¹ããã¡ã€ã«ã«ä¿åãããã¹ãçµæããªãã€ã¬ã¯ããå©çšããŠå€æããããšãã§ããŸãã
$ pcunit_xml_output.rb < test_results.txt
-
-e
1ã€ã§ããã¹ãçµæã«å€±æãããã°é0ãçµäºã³ãŒããšããŠè¿ãããã«ãªããŸãã
-
-n
ãã¹ãçµæãæšæºåºåã«åºåããªããªããŸãã
pcunit_mock.rb
ã¯ã¢ãã¯ã®ãœãŒã¹ãã¡ã€ã«ãçæããRubyã¹ã¯ãªããã§ãã
æžåŒã¯æ¬¡ã®éãã§ãã
pcunit_mock.rb header_file ... [OPTIONS]
header_file
ã«æå®ããããããã¡ã€ã«ã§ãããã¿ã€ã宣èšãããŠããã°ããŒãã«é¢æ°ã®ã¢ãã¯ã®ãœãŒã¹ãã¡ã€ã«ãçæããŸãã
header_file
ã¯è€æ°æå®ãŸãã¯ã¯ã€ã«ãã«ãŒãã䜿çšã§ããŸãã
-
-d DIR
DIR
ã«ãã¡ã€ã«ãçæãããã£ã¬ã¯ããªãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ãçç¥ããå Žåãã«ã¬ã³ããã£ã¬ã¯ããªãæå®ãããšèŠãªããŸãã -
-e PATTERN
header_file
ã«ã¯ã€ã«ãã«ãŒãã§æå®ããå ŽåãPATTERN
ã«é€å€ãããã£ã¬ã¯ããªå/ãã¡ã€ã«åã®æ£èŠè¡šçŸãã¿ãŒã³ãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ã¯è€æ°æå®ã§ããŸãã -
-s [SRC_DIR]
header_file
ã§å®£èšãããŠãããªãªãžãã«ã®é¢æ°ããã¹ãã§äœ¿çšãããå Žåãé¢æ°ã®å®çŸ©ã®ãããœãŒã¹ãã¡ã€ã«ã®ãã£ã¬ã¯ããªãSRC_DIR
ã«æå®ããŠãã ãããSRC_DIR
ãçç¥ããå Žåãheader_file
ãšåããã£ã¬ã¯ããªãæå®ãããšèŠãªããŸãã -
-p PREFIX
PREFIX
ã«ãã¡ã€ã«ãçæããã¢ãã¯ã®ãœãŒã¹ãã¡ã€ã«ã®ãã¬ãã£ãã¯ã¹ãæå®ããŠãã ããã ãã®ãªãã·ã§ã³ãçç¥ããå Žåãmock_
ãæå®ãããšèŠãªããŸãã -
--include FILE
çæããã¢ãã¯ã«ã
FILE
ã§æå®ãããã¡ã€ã«ã®ã€ã³ã¯ã«ãŒããè¿œå ããŸãã ãã®ãªãã·ã§ã³ã¯è€æ°æå®ã§ããŸãã -
--type-int TYPE
/--type-float TYPE
/--type-string TYPE
/--type-wstring TYPE
/--type-tstring TYPE
/--type-ptr TYPE
ãããã®ãªãã·ã§ã³ã¯ããããã
TYPE
ã«ãŠãŒã¶ãŒå®çŸ©ã®æŽæ°åãæµ®åå°æ°ç¹æ°åãæåååãã¯ã€ãæåååãTCHARã®æåååããã€ã³ã¿åãæå®ããŸãã ãããã¿ã€ã宣èšã®åŒæ°ã®åããæŽæ°åçã®typedefã®å Žåã«äœ¿çšããŠãã ããã ãããã®ãªãã·ã§ã³ã¯è€æ°æå®ã§ããŸãã -
--others
çæããã¢ãã¯ã®ãœãŒã¹ãã¡ã€ã«ããPCUnit以å€ã®ãŠããããã¹ããã¬ãŒã ã¯ãŒã¯ã§äœ¿çšã§ããããã«ãªããŸãã ãã®ãªãã·ã§ã³ã䜿ãå Žåã¯ããã¹ãã³ãŒãã®ã©ãã1ç®æã§
void PCU_mock_fail(const char *msg)
é¢æ°ãå®çŸ©ããå¿ èŠããããŸããPCU_mock_fail()
é¢æ°ã§ã¯ãã¡ãã»ãŒãžã衚瀺ããŠãã¹ãã倱æãããã¯ã(PCU_FAIL
ã«çžåœ)ãåŒãã§ãã ããã ãŸããC++ã®ãã¹ãã³ãŒãã§ã¢ãã¯ã䜿ãå Žåã¯ãã¢ãã¯ã®ããããã¡ã€ã«ã®ã€ã³ã¯ã«ãŒããšPCU_mock_fail()
ã®å®çŸ©ãextern "C" {}
ã§å²ãã§ãã ãããäŸ: CppUTestã®å Žå
extern "C" { #include "mock_Foo.h" void PCU_mock_fail(const char *msg) { FAIL(msg); } }
äŸ: GoogleTestã®å Žå
extern "C" { #include "mock_Foo.h" void PCU_mock_fail(const char *msg) { FAIL() << msg; } }
äŸ: Unityã®å Žå
void PCU_mock_fail(const char *msg) { TEST_FAIL_MESSAGE(msg); }
äŸãšããŠãFoo.h
ãšããããããã¡ã€ã«ã«
int func(int a, const char *str);
ãšãããããã¿ã€ã宣èšããããšããŸãã
ãã®ããããã¡ã€ã«ã«å¯ŸããŠpcunit_mock.rb
ãå®è¡ãããšãmock_Foo.h
ãš mock_Foo.c
ãçæãããŸãã
mock_Foo.c
ã«ã¯ãã¹ãããã«é¢æ°func()
ãå®çŸ©ãããŸãã
çæãããã¢ãã¯ã®APIã以äžã«ç€ºããŸãã
泚:
é©å®ãFoo
ãš func
ãå®éã«äœ¿çšããããããã¡ã€ã«åãšé¢æ°åã«èªã¿æ¿ããŠãã ããã
ãããã¿ã€ã宣èšãè€æ°ããå Žåã¯ããã®æ°ã ã*_expect()
, *_set_callback()
, *_num_calls()
ãçæãããŸãã
-
void mock_Foo_init(void)
ã¢ãã¯ãåæåããŸãã ã¢ãã¯ã䜿çšãããã¹ãã³ãŒãã®setup()ã§åŒãã§ãã ããã
-
void mock_Foo_verify(void)
*_expect()
ãŸãã¯*_set_callback()
ã䜿ã£ãŠèšå®ãããå šãŠã®ãšã¯ã¹ãã¯ããŒã·ã§ã³ãæºããããã©ãããã§ãã¯ããŸãã 1ã€ã§ãæºãããªããšã¯ã¹ãã¯ããŒã·ã§ã³ããã£ãå Žåã¯ãã¹ã倱æãåºåããŸãã ã¢ãã¯ã䜿çšãããã¹ãã³ãŒãã®teardown()ã§åŒãã§ãã ããã -
void func_expect(const func_Expectation *expectations, int num)
ãã¹ãããã«é¢æ°
func()
ã®ãšã¯ã¹ãã¯ããŒã·ã§ã³(æåŸ ãããåŒæ°ã®å€ãæ»ãå€ãåŒã³åºãåæ°)ãèšå®ããŸããexpectations
ã«func_Expectation
åã®é åãæå®ããnum
ã«ã¯expectations
ã®èŠçŽ æ°ãæå®ããŸããnum
ã¯æåŸ ãããfunc()
ã®åŒã³åºãåæ°ã«ãªããŸããfunc_Expectation
åã¯æ¬¡ã®ããã«åå®çŸ©ãããŠããŸããtypedef struct { int retval; /* ãã¹ãããã«é¢æ°ã®æ»ãå€ãèšå® */ struct { int a; /* æåŸ ãããåŒæ°aã®å€ãèšå® */ const char *str; /* æåŸ ãããåŒæ°strã®å€ãèšå® */ } expected; struct { unsigned int a:1; /* åŒæ°aã®å€ãç¡èŠãããå Žåã«1ãèšå® */ unsigned int str:1; /* åŒæ°strã®å€ãç¡èŠãããå Žåã«1ãèšå® */ } ignored; } func_Expectation;
äŸ:
以äžã®ãšã¯ã¹ãã¯ããŒã·ã§ã³ãèšå®ããŸãã - func()ã2ååŒã³åºãããããš - 1åç®ã®func()ã®åŒæ°aã¯100ã§ããããš - 1åç®ã®func()ã®åŒæ°strã¯"foo"ã§ããããš - 1åç®ã®func()ã¯200ãè¿ã - 2åç®ã®func()ã®åŒæ°aã¯200ã§ããããš - 2åç®ã®func()ã®åŒæ°strã¯äœã§ããã - 2åç®ã®func()ã¯300ãè¿ã /* ãã¹ãé¢æ° */ static void test_func_expect(void) { /* func_Expectationã®é åãããŒã«ã«å€æ°ãŸãã¯staticå€æ°ã§ç¢ºä¿ã * é åã®èŠçŽ æ°ããæåŸ ãããfunc()ã®åŒã³åºãåæ°ãšãªãã * äžå®å€ã§ignoredãæå¹ã«ãªããªãããã«ãé åã¯å¿ ã0ã¯ãªã¢ããããšã */ func_Expectation e[2] = {{0}}; /* ãšã¯ã¹ãã¯ããŒã·ã§ã³ã®èšå® */ e[0].expected.a = 100; /* 1åç®ã®åŒã³åºãæã«æåŸ ãããåŒæ°aã®å€ */ e[0].expected.str = "foo"; /* 1åç®ã®åŒã³åºãæã«æåŸ ãããåŒæ°strã®å€ */ e[0].retval = 200; /* 1åç®ã®åŒã³åºãæã®æ»ãå€ */ e[1].expected.a = 200; /* 2åç®ã®åŒã³åºãæã«æåŸ ãããåŒæ°aã®å€ */ e[1].ignored.str = 1; /* 2åç®ã®åŒã³åºãæã®åŒæ°strã¯ç¡èŠ */ e[1].retval = 300; /* 2åç®ã®åŒã³åºãæã®æ»ãå€ */ func_expect(e, sizeof e / sizeof e[0]); /* 1åç®ã®åŒã³åºããåŒæ°aã100ãã€åŒæ°strã"foo" ã§ãªãå Žåã¯ãã¹ã倱æãåºåãã */ PCU_ASSERT_EQUAL(200, func(100, "foo")); /* 2åç®ã®åŒã³åºããåŒæ°aã¯200ã§ãªããã°ãªããªãããåŒæ°strã¯ã©ããªå€ã§ããã */ PCU_ASSERT_EQUAL(300, func(200, "bar")); }
-
void func_set_callback(func_Callback callback, int expected_num_calls)
ãã¹ãããã«é¢æ°ã«ãŠãŒã¶ãŒå®çŸ©ã®ã³ãŒã«ããã¯é¢æ°ãèšå®ããŸãã ãã¹ãããã«é¢æ°
func()
ãåŒã°ãããšãèšå®ããcallback()
ãåŒã°ããããã«ãªããŸããfunc_Callback
åã¯func()
ãšåãåã®é¢æ°ãžã®ãã€ã³ã¿åã§ãtypedef int (*func_Callback)(int a, const char *str);
ãšåå®çŸ©ãããŠããŸããexpected_num_calls
ã«æåŸ ãããfunc()
ã®åŒã³åºãåæ°ãæå®ããŸãã åŒã³åºãåæ°ãç¡å¶éã«ãããå Žåã¯ãexpected_num_calls
ã«è² ã®æ°ãæå®ããŸããã³ãŒã«ããã¯é¢æ°ã䜿ãã°ã
*_expect()
ããè€éãªãšã¯ã¹ãã¯ããŒã·ã§ã³ãèšå®ããããšãã§ããŸãã äŸãã°ãæåŸ ãããåŒæ°ãç¯å²ã§æå®ãããå Žåãããã€ã³ã¿åŒæ°ãæãå€ãå€æŽããé¢æ°ã䜿ãããå Žåãªã©ã*_expect()
ã§ã¯æåŸ ããèšå®ãã§ããªãæã«äœ¿çšããŠãã ãããäŸ:
以äžã®ãšã¯ã¹ãã¯ããŒã·ã§ã³ãèšå®ããŸãã - func()ã2ååŒã³åºãããããš - 1åç®ã®func()ã®åŒæ°aã®ç¯å²ã¯100 <= a < 200ã§ããããš - 1åç®ã®func()ã®åŒæ°strã¯"foo"ã§ããããš - 1åç®ã®func()ã¯200ãè¿ã - 2åç®ã®func()ã®åŒæ°aã¯äœã§ããã - 2åç®ã®func()ã®åŒæ°strã¯äœã§ããã - 2åç®ã®func()ã¯300ãè¿ã /* func()ãšåãåã®ã³ãŒã«ããã¯é¢æ° */ static int func_callback(int a, const char *str) { /* func_num_calls()ã¯æ¢ã«åŒã³åºãããåæ°ãè¿ããããã䜿ã£ãŠåå²ã§ãã */ if (func_num_calls() == 0) { /* 1åç®ã®åŒã³åºãæã®åŒæ°ããã§ã㯠*/ PCU_ASSERT_OPERATOR(100 <= a, &&, a < 200); PCU_ASSERT_STRING_EQUAL("foo", str); return 200; } else { /* 2åç®ã®åŒã³åºãã¯åŒæ°ããã§ãã¯ããªã */ return 300; } } /* ãã¹ãé¢æ° */ static void test_func_set_callback(void) { /* ã³ãŒã«ããã¯é¢æ°ãšæåŸ ãããåŒã³åºãåæ°ã®èšå® */ func_set_callback(func_callback, 2); /* 1åç®ã®åŒã³åºããåŒæ°aã100 <= a < 200ãã€åŒæ°strã"foo" ã§ãªãå Žåã¯ãã¹ã倱æãåºåãã */ PCU_ASSERT_EQUAL(200, func(150, "foo")); /* 2åç®ã®åŒã³åºãã2ã€ã®åŒæ°ã¯ã©ããªå€ã§ããã */ PCU_ASSERT_EQUAL(300, func(200, "bar")); }
-
int func_num_calls(void)
ãã¹ãããã«é¢æ°
func()
ãæ¢ã«åŒã³åºãããåæ°ãè¿ããŸããfunc_set_callback
ã䜿çšããå Žåãã³ãŒã«ããã¯é¢æ°å ã§åŒã³åºãããåæ°ã«ãã£ãŠåŠçãå€ããæãªã©ã«äœ¿çšã§ããŸãã
PCUnitã¯zlibã©ã€ã»ã³ã¹ã«åŸã£ãŠé åžãããŸãã ã©ã€ã»ã³ã¹ã®è©³çŽ°ã¯ãCOPYINGãã¡ã€ã«(æ¥æ¬èªåèèš³ã¯COPYING.jaãã¡ã€ã«)ãã芧ãã ããã