CUnit for Mr.Ando.
『CUnit for Mr.Ando』は
CppUnit-x
をベースにしたC言語なんちゃってテスティングフレームワークです。
これは、C言語のソースコードを単体テストのために供給します。
"CUnit for Mr.Ando" is
CppUnit-x
based C langage testing framework for Mr.Ando.
It provide the C source code for unit testing.
イントロダクション
Introduction.
JUnit
(Javaテスティングフレームワーク)は単体テストのために『オーバライト』を使いますが、
しかし『CUnit for Mr.Ando』は単体テストのために『スタブ』を使います。
JUnit
(Java testing framework) is used for "override" for unit testing,
but "CUnit for Mr.Ando" is "stub" for unit testing.

図1 -- CUnit for Mr.Ando テスティングイメージ
Fig.1 -- CUnit for Mr.Ando testing image.
ダウンロード
Download.
構成
Composition.
これは以下のファイルとフォルダで構成されます。
It is forrowing files and folders.
- ./CUnitForAndo/CUnitForAndo -- 本体.
- ./CUnitForAndo/Sample1/real -- サンプル1.
- ./CUnitForAndo/Sample1/test
- ./CUnitForAndo/Sample2/real -- サンプル2.
- ./CUnitForAndo/Sample2/test
- ./CUnitForAndo/Sample3/real -- サンプル3.
- ./CUnitForAndo/Sample3/test
- ./Makefile -- 圧縮とテスト用.
- ./README.txt -- ライセンス.
- ./CUnitForAndo/html -- このファイル
テストサンプル
Test Sample.
テストサンプル1(順序)
Test Sample 1 (Sequence)
これはテストされるファイルです。
This is a file for a test.
#include "plus.h"
/** Sums are done. A result is returned to ans.
* However, he is stupid.
* The numerical value to which he exceeds 100
* writes except zero to a return value.
* @param data1 imput paramater 1
* @param data2 imput paramater 2
* @param ans return paramater
* @return 0:normal, Other 0:abnormal
*/
unsigned int plus(
unsigned int data1,
unsigned int data2,
unsigned int *ans) {
unsigned int max = 100;
*ans = data1 + data2;
if ((max <= data1) || (max <= data2) || (max <= *ans)) {
return 1;
}
return 0;
}
これはテストファイルです。
This is test file.
#include <stdio.h>
#include <testRunner.h>
#include "plus.h"
static unsigned int testPlus(void);
/** Main function. */
int main() {
return (int) testRunner(testPlus);
}
static unsigned int testPlus(void) {
unsigned int err;
unsigned int ans;
unsigned int data1;
unsigned int data2;
data1 = 1;
data2 = 1;
err = plus(data1,data2,&ans); // do Test
TEST_ASSERT_EQUALS((data1 + data2),(int)ans);
TEST_ASSERT_EQUALS(err,0);
data1 = 100;
data2 = 1;
err = plus(data1,data2,&ans); // do Test
TEST_ASSERT(err != 0);
data1 = 0;
data2 = 0xffffffffu; // -1
err = plus(data1,data2,&ans); // do Test
TEST_ASSERT(err != 0);
data1 = 1;
data2 = 99;
err = plus(data1,data2,&ans); // do Test
TEST_ASSERT(err != 0);
return 0;
}
テストサンプル2(選択)
Test Sample 2 (Selection)
これはテストされるファイルです。
This is a file for a test.
#include "select.h"
/** @see select.h */
unsigned int hardAccsess(
unsigned int address,
unsigned int data,
HARD_TYPE_t select) {
unsigned int err = 0;
switch (select) {
case HARD_TYPE_A:
err = setDataA(address,data);
break;
case HARD_TYPE_B:
default:
if ((data & 0xffff0000u) != 0) {
err = 1;
} else {
writeAccessB(address,(unsigned short)data);
}
break;
}
return err;
}
これはテストファイルです。
This is test file.
#include <stdio.h>
#include <testRunner.h>
#include "select.h"
/* A test is divided. */
static unsigned int testAll(void);
static unsigned int testHardA(void);
static unsigned int testHardB(void);
/* A domain is secured. */
static unsigned int inSetDataA_address;
static unsigned int inSetDataA_data;
static unsigned int returnSetDataA;
static unsigned int inWriteAccessB_address;
static unsigned short inWriteAccessB_data;
int main(int argc,char **argv) {
return (int) testRunner(testAll);
}
static unsigned int testAll(void) {
TEST_ASSERT(! testHardA());
TEST_ASSERT(! testHardB());
return 0;
}
static unsigned int testHardA(void) {
unsigned int err;
unsigned int address;
unsigned int data;
HARD_TYPE_t select;
/////////////////////////////////////////////////
// setDataA normal
address = 0x85000038u; // 0x0-ffffffffu
data = 456; // 0x0-ffffffffu
select = HARD_TYPE_A; // HARD_TYPE_A,HARD_TYPE_B
inSetDataA_address = 0xffffffffu;// imput(dirty)
inSetDataA_data = 0xffffffffu;// imput(dirty)
returnSetDataA = 0; // 0:normal,1-0xffffffffu:abnormal
inWriteAccessB_address = 0xffffffffu; // imput(dirty)
inWriteAccessB_data = 0xffffu; // imput(dirty)
err = hardAccsess(address,data,select); // do test
TEST_ASSERT_EQUALS(err,0); // normal
TEST_ASSERT_EQUALS(inSetDataA_address,(int)address); // Check!
TEST_ASSERT_EQUALS(inSetDataA_data,(int)data); // Check!
TEST_ASSERT_EQUALS(inWriteAccessB_address,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inWriteAccessB_data,0xffffu); // no access
/////////////////////////////////////////////////
// setDataA abnormal
address = 0x86540004u; // 0x0-ffffffffu
data = 22; // 0x0-ffffffffu
select = HARD_TYPE_A; // HARD_TYPE_A,HARD_TYPE_B
inSetDataA_address = 0xffffffffu;// imput(dirty)
inSetDataA_data = 0xffffffffu;// imput(dirty)
returnSetDataA = 0x31u; // 0:normal,1-0xffffffffu:abnormal
inWriteAccessB_address = 0xffffffffu; // imput(dirty)
inWriteAccessB_data = 0xffffu; // imput(dirty)
err = hardAccsess(address,data,select); // do test
TEST_ASSERT(err != 0); // abnormal
TEST_ASSERT_EQUALS(inSetDataA_address,(int)address); // Check!
TEST_ASSERT_EQUALS(inSetDataA_data,(int)data); // Check!
TEST_ASSERT_EQUALS(inWriteAccessB_address,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inWriteAccessB_data,0xffffu); // no access
return 0;
}
static unsigned int testHardB(void) {
unsigned int err;
unsigned int address;
unsigned int data;
HARD_TYPE_t select;
/////////////////////////////////////////////////
// writeAccessB normal
address = 0x36540004u; // 0x0-ffffffffu
data = 22; // 0x0-0000ffffu:normal,0x00010000-0xffff0000:abnormal
select = HARD_TYPE_B; // HARD_TYPE_A,HARD_TYPE_B
inSetDataA_address = 0xffffffffu;// imput(dirty)
inSetDataA_data = 0xffffffffu;// imput(dirty)
returnSetDataA = 0; // 0:normal,1-0xffffffffu:abnormal
inWriteAccessB_address = 0xffffffffu; // imput(dirty)
inWriteAccessB_data = 0xffffu; // imput(dirty)
err = hardAccsess(address,data,select); // do test
TEST_ASSERT_EQUALS(err , 0); // normal
TEST_ASSERT_EQUALS(inSetDataA_address,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inSetDataA_data,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inWriteAccessB_address,(int)address); // Check!
TEST_ASSERT_EQUALS(inWriteAccessB_data,data); // Check!
/////////////////////////////////////////////////
// writeAccessB abnormal
address = 0x36500027u; // 0x0-ffffffffu
data = 0x04000003u; // 0x0-0000ffffu:normal,0x00010000-0xffff0000:abnormal
select = HARD_TYPE_B; // HARD_TYPE_A,HARD_TYPE_B
inSetDataA_address = 0xffffffffu;// imput(dirty)
inSetDataA_data = 0xffffffffu;// imput(dirty)
returnSetDataA = 0; // 0:normal,1-0xffffffffu:abnormal
inWriteAccessB_address = 0xffffffffu; // imput(dirty)
inWriteAccessB_data = 0xffffu; // imput(dirty)
err = hardAccsess(address,data,select);
TEST_ASSERT(err != 0); // abnormal
TEST_ASSERT_EQUALS(inSetDataA_address,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inSetDataA_data,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inWriteAccessB_address,(int)0xffffffffu); // no access
TEST_ASSERT_EQUALS(inWriteAccessB_data,0xffffu); // no access
return 0;
}
/** Unit A Access driver. */
unsigned int setDataA(
unsigned int address,unsigned int data) {
inSetDataA_address = address;
inSetDataA_data = data;
return returnSetDataA;
}
/** Unit B Access driver. */
void writeAccessB(
unsigned int address,unsigned short data) {
inWriteAccessB_address = address;
inWriteAccessB_data = data;
}
テストサンプル3(繰り返し)
Test Sample 3 (foreach)
これはテストされるファイルです。
This is a file for a test.
#include "select.h"
#include "loop.h"
unsigned int loop(void) {
unsigned int err;
unsigned int loopErr = 0;
unsigned int i = 0;
for (i = 0 ; i < 10 ; i++) {
unsigned int address = 0x85000000u + ( i + 8);
err = setDataA(address,i);
if (err && (! loopErr)) {
loopErr = err;
}
}
return loopErr;
}
これはテストファイルです。
This is test file.
#include <stdio.h>
#include <string.h>
#include <testRunner.h>
#include "select.h"
#include "loop.h"
/* A domain is secured. */
#define DATA_ARRAY 100
static unsigned int inSetDataA_address[DATA_ARRAY];
static unsigned int inSetDataA_data[DATA_ARRAY];
static unsigned int returnSetDataA[DATA_ARRAY];
static unsigned int countSetDataA = 0;
static unsigned int testLoop(void);
/** Main function. */
int main() {
return (int) testRunner(testLoop);
}
static unsigned int testLoop(void) {
unsigned int err;
unsigned int i;
/////////////////////////////////////////////////
// normal
memset(inSetDataA_address,0xffffffffu,sizeof(unsigned int)*DATA_ARRAY);
memset(inSetDataA_data,0xffffffffu,sizeof(unsigned int)*DATA_ARRAY);
memset(returnSetDataA,0,sizeof(unsigned int)*DATA_ARRAY);
countSetDataA = 0;
err = loop(); // do test
TEST_ASSERT_EQUALS(err,0); // normal
TEST_ASSERT_EQUALS(countSetDataA,10); //
for (i = 0 ; i < 10 ; i++) {
TEST_ASSERT_EQUALS(inSetDataA_address[i] ,(int) (0x85000000u + (i + 8)));
TEST_ASSERT_EQUALS(inSetDataA_data[i] ,(int) i);
}
/////////////////////////////////////////////////
// abnormal
memset(inSetDataA_address,0xffffffffu,sizeof(unsigned int)*DATA_ARRAY);
memset(inSetDataA_data,0xffffffffu,sizeof(unsigned int)*DATA_ARRAY);
memset(returnSetDataA,0,sizeof(unsigned int)*DATA_ARRAY);
returnSetDataA[3] = 5; // A mistake is mixed.
countSetDataA = 0;
err = loop(); // do test
TEST_ASSERT(err != 0); // abnormal
// Even if there is an error, a hard setup has run
TEST_ASSERT_EQUALS(countSetDataA,10);
for (i = 0 ; i < 10 ; i++) {
TEST_ASSERT_EQUALS(inSetDataA_address[i] ,(int) (0x85000000u + (i + 8)));
TEST_ASSERT_EQUALS(inSetDataA_data[i] ,(int) i);
}
return 0;
}
/** Unit A Access driver. */
unsigned int setDataA(
unsigned int address,unsigned int data) {
inSetDataA_address[countSetDataA] = address;
inSetDataA_data[countSetDataA] = data;
countSetDataA++;
return returnSetDataA[countSetDataA];
}
ライセンス
Licence
GNU Lesser General Public License
参考文献
Bibliography.
作者
Auther.
安藤利和
Toshikazu Ando.
P.S.
このファイルは日本人が書いてます。
もし異常な英語があったら、私に流暢な英訳を教えてください。
Japanese are writing this file.
If abnormalities are looked at by English,
please gime me fluenet English translation!!