OSDN Git Service

Changed to use a global variable though delivered a pointer to the second argument...
[ccunit/ccunit.git] / src / ccunit / CCUnitAssert.c
1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2    This file is part of the CCUnit Library.
3
4    The CCUnit Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public License
6    as published by the Free Software Foundation; either version 2.1 of
7    the License, or (at your option) any later version.
8
9    The CCUnit Library is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the CCUnit Library; see the file COPYING.LESSER.
16    If not, write to the Free Software Foundation, Inc., 59 Temple
17    Place - Suite 330, Boston, MA 02111-1307, USA.  
18 */
19
20 /*
21  * $Id$
22  */
23
24 /** @file
25  * Assert function implementations.
26  */
27
28 #include <stdio.h>
29 #include <ccunit/CCUnitAssert.h>
30 #include <ccunit/CCUnitTestFailure.h>
31 #include <setjmp.h>
32 #include <stdarg.h>
33 #include <assert.h>
34
35 /**
36  * @addtogroup CCUnitAssert
37  * @{
38  */
39
40 extern jmp_buf _ccunit_runTest_env;
41 extern CCUnitTestFailure* _ccunit_testFailure;
42
43 void ccunit_assert (const char* file, unsigned int line,
44                     bool cond, const char* condstr)
45 {
46   if (cond)
47     return;
48   _ccunit_testFailure = ccunit_newTestFailure (file, line, condstr, NULL, NULL);
49   longjmp (_ccunit_runTest_env, 1);
50 }
51
52 /**
53  * @defgroup CCUnitValueToString Convert value to string
54  * Make value to string.
55  * @{
56  */
57
58 /**
59  * @name ccunit_assert_format_TYPE
60  * snprintf format string for each types.
61  * @{
62  */
63 const char* _ccunit_assert_format_char = "%c";
64 const char* _ccunit_assert_format_u_char = "%c";
65 const char* _ccunit_assert_format_int = "%d";
66 const char* _ccunit_assert_format_u_int = "%u";
67 const char* _ccunit_assert_format_short = "%hd";
68 const char* _ccunit_assert_format_u_short = "%hu";
69 const char* _ccunit_assert_format_long = "%ld";
70 const char* _ccunit_assert_format_u_long = "%lu";
71 const char* _ccunit_assert_format_float = "%f";
72 const char* _ccunit_assert_format_double = "%f";
73 const char* _ccunit_assert_format__ccunit_str_t = "%s";
74 const char* _ccunit_assert_format__ccunit_ptr_t = "%p";
75 #if CCUNIT_HAVE_QUAD_T
76 const char* _ccunit_assert_format_quad_t = "%lld";
77 #endif
78 #if CCUNIT_HAVE_U_QUAD_T
79 const char* _ccunit_assert_format_u_quad_t = "%llu";
80 #endif
81 /** @} */
82
83 /** @name Value to string definitions
84  * Make value to string.
85  * @param TYP type of value.
86  * @param CAPACITY string size.
87  * @{
88  */
89 #define DEF_VALUE_TO_STRING(TYP, CAPACITY)                      \
90   static char* TYP ## _to_string (TYP value)                    \
91   {                                                             \
92     char* str = NULL;                                           \
93     int require = 0;                                            \
94     int capacity = CAPACITY;                                    \
95     for (str = malloc (capacity);                               \
96          str != NULL;                                           \
97          str = realloc (str, capacity))                         \
98       {                                                         \
99         require = snprintf (str, capacity,                      \
100                             _ccunit_assert_format_ ## TYP,      \
101                             value);                             \
102         if (require < 0)                                        \
103           capacity *= 2;                                        \
104         else if (require < capacity)                            \
105           break;                                                \
106         else                                                    \
107           capacity = require + 1;                               \
108       }                                                         \
109     return str;                                                 \
110   }
111
112 DEF_VALUE_TO_STRING(char, 6);
113 DEF_VALUE_TO_STRING(u_char, 6);
114 DEF_VALUE_TO_STRING(int, 12);
115 DEF_VALUE_TO_STRING(u_int, 12);
116 DEF_VALUE_TO_STRING(short, 8);
117 DEF_VALUE_TO_STRING(u_short, 8);
118 DEF_VALUE_TO_STRING(long, 12);
119 DEF_VALUE_TO_STRING(u_long, 12);
120 DEF_VALUE_TO_STRING(float, 24);
121 DEF_VALUE_TO_STRING(double, 24);
122 #if CCUNIT_HAVE_QUAD_T
123 DEF_VALUE_TO_STRING(quad_t, 24);
124 #endif
125 #if CCUNIT_HAVE_U_QUAD_T
126 DEF_VALUE_TO_STRING(u_quad_t, 24);
127 #endif
128 DEF_VALUE_TO_STRING(_ccunit_str_t, 32);
129 DEF_VALUE_TO_STRING(_ccunit_ptr_t, 24);
130
131 /** @}
132  * end of DEF_VALUE_TO_STRING
133  */
134
135 /** @}
136  * end of CCUnitValueToString
137  */
138
139 /**
140  * @name Assert test type
141  * @{
142  * Assert test function.
143  * @param TYP type of test value.
144  */
145 #define DEF_CCUNIT_ASSERT_TEST_TYPE(TYP)                                \
146   void ccunit_assert_test_ ## TYP (const char* file,                    \
147                                    unsigned int line,                   \
148                                    bool cond,                           \
149                                    const char* condstr,                 \
150                                    TYP expect,                          \
151                                    TYP actual)                          \
152   {                                                                     \
153     if (cond)                                                           \
154       return;                                                           \
155     else                                                                \
156       {                                                                 \
157         const char* ex = TYP ## _to_string (expect);                    \
158         const char* ac = TYP ## _to_string (actual);                    \
159         _ccunit_testFailure = ccunit_newTestFailure (file, line, condstr, ex, ac); \
160         safe_free (ex);                                                 \
161         safe_free (ac);                                                 \
162         assert (_ccunit_testFailure != NULL);                           \
163         longjmp (_ccunit_runTest_env, 1);                               \
164       }                                                                 \
165   }
166
167 void ccunit_assert_test__ccunit_obj_t (const char* file,
168                                        unsigned int line,
169                                        bool cond,
170                                        const char* condstr,
171                                        _ccunit_obj_t expect,
172                                        _ccunit_obj_t actual,
173                                        char* (*to_string)(_ccunit_obj_t))
174 {
175   if (cond)
176     return;
177   else
178     {
179       char* ex = (!to_string ? _ccunit_ptr_t_to_string (expect)
180                   : to_string (expect));
181       char* ac = (!to_string ? _ccunit_ptr_t_to_string (actual)
182                   : to_string (actual));
183       _ccunit_testFailure = ccunit_newTestFailure (file, line, condstr, ex, ac);
184       safe_free (ex);
185       safe_free (ac);
186       assert (_ccunit_testFailure != NULL);
187       longjmp (_ccunit_runTest_env, 1);
188     }
189 }
190
191
192 DEF_CCUNIT_ASSERT_TEST_TYPE(char);
193 DEF_CCUNIT_ASSERT_TEST_TYPE(u_char);
194 DEF_CCUNIT_ASSERT_TEST_TYPE(int);
195 DEF_CCUNIT_ASSERT_TEST_TYPE(u_int);
196 DEF_CCUNIT_ASSERT_TEST_TYPE(short);
197 DEF_CCUNIT_ASSERT_TEST_TYPE(u_short);
198 DEF_CCUNIT_ASSERT_TEST_TYPE(long);
199 DEF_CCUNIT_ASSERT_TEST_TYPE(u_long);
200 #if CCUNIT_HAVE_QUAD_T
201 DEF_CCUNIT_ASSERT_TEST_TYPE(quad_t);
202 #endif
203 #if CCUNIT_HAVE_U_QUAD_T
204 DEF_CCUNIT_ASSERT_TEST_TYPE(u_quad_t);
205 #endif
206 DEF_CCUNIT_ASSERT_TEST_TYPE(float);
207 DEF_CCUNIT_ASSERT_TEST_TYPE(double);
208 DEF_CCUNIT_ASSERT_TEST_TYPE(_ccunit_str_t);
209 DEF_CCUNIT_ASSERT_TEST_TYPE(_ccunit_ptr_t);
210
211 /** @} */
212
213 /** @} */