src/server/ucx/test.h

Fri, 21 Jun 2013 12:10:44 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 21 Jun 2013 12:10:44 +0200
changeset 71
069c152f6272
permissions
-rw-r--r--

ucx update

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
/*
 *
 * Usage of this test framework:
 *
 * **** IN HEADER FILE: ****
 *
 * UCX_TEST_DECLARE(function_name)
 *
 * **** IN SOURCE FILE: ****
 *
 * UCX_TEST_IMPLEMENT(function_name) {
 *  <memory allocation and other stuff here>
 *  UCX_TEST_BEGIN
 *  <tests with UCX_TEST_ASSERT here>
 *  UCX_TEST_END
 *  <cleanup of memory here>
 * }
 *
 * PLEASE NOTE: if a test fails, a longjump is performed
 * back to the UCX_TEST_BEGIN macro!
 *
 * You may use multiple BEGIN-END blocks if you are aware of the
 * longjmp behaviour.
 *
 */

#ifndef TEST_H
#define	TEST_H

#include "ucx.h"
#include <stdio.h>
#include <string.h>
#include <setjmp.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifndef __FUNCTION__
#define __FUNCTION__ __func__
#endif

typedef struct UcxTestList UcxTestList;
typedef struct UcxTestSuite UcxTestSuite;
typedef void(*UcxTest)(UcxTestSuite*,FILE*);
    
struct UcxTestList{
    UcxTest test;
    UcxTestList *next;
};

struct UcxTestSuite {
    unsigned int success;
    unsigned int failure;
    UcxTestList *tests;
};

UcxTestSuite* ucx_test_suite_new();
void ucx_test_suite_free(UcxTestSuite*);

int ucx_test_register(UcxTestSuite*, UcxTest);
void ucx_test_run(UcxTestSuite*, FILE*);

#define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)

#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
        fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
        fwrite("... ", 1, 4, _output_);\
        jmp_buf _env_; \
        if (!setjmp(_env_)) {

#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
        fwrite(message".\n", 1, 2+strlen(message), _output_); \
        _suite_->failure++; \
        longjmp(_env_, 1);\
    }

#define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
        FILE *_output_, jmp_buf _env_, __VA_ARGS__)
#define UCX_TEST_CALL_SUBROUTINE(name,...) \
        name(_suite_,_output_,_env_,__VA_ARGS__);

#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}

#ifdef	__cplusplus
}
#endif

#endif	/* TEST_H */

mercurial