src/server/ucx/test.h

Sun, 30 Jun 2013 15:11:48 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Jun 2013 15:11:48 +0200
changeset 82
740cfd9dd443
parent 71
069c152f6272
permissions
-rw-r--r--

added some nsapi stuff

71
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 /*
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 * Usage of this test framework:
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 * **** IN HEADER FILE: ****
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * UCX_TEST_DECLARE(function_name)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 * **** IN SOURCE FILE: ****
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 * UCX_TEST_IMPLEMENT(function_name) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 * <memory allocation and other stuff here>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 * UCX_TEST_BEGIN
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 * <tests with UCX_TEST_ASSERT here>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 * UCX_TEST_END
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 * <cleanup of memory here>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 * }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 * PLEASE NOTE: if a test fails, a longjump is performed
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 * back to the UCX_TEST_BEGIN macro!
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 * You may use multiple BEGIN-END blocks if you are aware of the
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 * longjmp behaviour.
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 *
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 */
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 #ifndef TEST_H
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 #define TEST_H
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 #include "ucx.h"
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 #include <stdio.h>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 #include <string.h>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 #include <setjmp.h>
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 #ifdef __cplusplus
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 extern "C" {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 #endif
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 #ifndef __FUNCTION__
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 #define __FUNCTION__ __func__
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 #endif
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 typedef struct UcxTestList UcxTestList;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 typedef struct UcxTestSuite UcxTestSuite;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 struct UcxTestList{
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 UcxTest test;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 UcxTestList *next;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 };
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 struct UcxTestSuite {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 unsigned int success;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 unsigned int failure;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 UcxTestList *tests;
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 };
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 UcxTestSuite* ucx_test_suite_new();
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 void ucx_test_suite_free(UcxTestSuite*);
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 int ucx_test_register(UcxTestSuite*, UcxTest);
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 void ucx_test_run(UcxTestSuite*, FILE*);
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 fwrite("... ", 1, 4, _output_);\
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 jmp_buf _env_; \
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if (!setjmp(_env_)) {
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 fwrite(message".\n", 1, 2+strlen(message), _output_); \
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 _suite_->failure++; \
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 longjmp(_env_, 1);\
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 FILE *_output_, jmp_buf _env_, __VA_ARGS__)
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 #define UCX_TEST_CALL_SUBROUTINE(name,...) \
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 name(_suite_,_output_,_env_,__VA_ARGS__);
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 #ifdef __cplusplus
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 }
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 #endif
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 #endif /* TEST_H */
069c152f6272 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119

mercurial