デジタルちんぶろぐ

デジタルな話題

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  268  : 記事  0  : コメント  4375  : トラックバック  79

ニュース


技術以外は
ちんぶろぐ

記事カテゴリ

書庫

日記カテゴリ

こことかここに触発されて3種類ほど書いてみた。

#include <cstdio>
#include <cstdlib>

using namespace std;

int test_val;

bool test1(void) { printf("test1()\n"); return test_val > 1; }
bool test2(void) { printf("test2()\n"); return test_val > 2; }
bool test3(void) { printf("test3()\n"); return test_val > 3; }
bool test4(void) { printf("test4()\n"); return test_val > 4; }
bool test5(void) { printf("test5()\n"); return test_val > 5; }
void test_ok(void) { printf("Test OK\n"); }

bool do_test1(void)
{
    bool result = false;
    printf("Test pattern 1\n");

    do {
        if(!test1()) break;
        if(!test2()) break;
        if(!test3()) break;
        if(!test4()) break;
        if(!test5()) break;
        test_ok();
        result = true;
    } while(0);

    return result;
}

bool do_test2(void)
{
    bool (*tests[])(void) = {
        test1,
        test2,
        test3,
        test4,
        test5,
        NULL,
    };
    int    i;
    bool result = true;

    printf("Test pattern 2\n");

    for(i = 0; tests[i] != NULL && result; i++) result = tests[i]();
    if(result) test_ok();

    return result;
}

bool do_test3(void)
{
    printf("Test pattern 3\n");

    return test1() && test2() && test3() && test4() && test5() && (test_ok(), true);
}

int main(int argc, const char* argv[])
{
    bool result = false;
    if(argc < 3) return 0;

    test_val = atoi(argv[2]);
    switch(atoi(argv[1])) {
    case 1: result = do_test1(); break;
    case 2: result = do_test2(); break;
    case 3: result = do_test3(); break;
    }

    if(result) printf("全テスト正常終了");

    return 0;
}
 

 

コンパイルして実行するときに

[program name] テストパターン エラーを返す位置

で3種類の挙動を見ることが出来ます(どれも同じだけど)

 

ちょっとトリッキーなのはテストパターン3ですかね。1行で済んですっきり。

C++で書いたのはboolを使いたかったから。

 

投稿日時 : 2008年6月5日 21:41

コメント

# ycrMqudAJzIkqBNCXL 2011/12/16 0:20 http://www.circalighting.com/
Sent the first post, but it wasn`t published. I am writing the second. It's me, the African tourist.

Post Feedback

タイトル
名前
Url:
コメント: