1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2 This file is part of the CCUnit Library.
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.
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.
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.
25 * Assert function implementations.
29 #include <ccunit/CCUnitAssert.h>
30 #include <ccunit/CCUnitTestFailure.h>
36 * @addtogroup CCUnitAssert
40 extern jmp_buf _ccunit_runTest_env;
41 extern CCUnitTestFailure* _ccunit_testFailure;
43 void ccunit_assert (const char* file, unsigned int line,
44 bool cond, const char* condstr)
48 _ccunit_testFailure = ccunit_newTestFailure (file, line, condstr, NULL, NULL);
49 longjmp (_ccunit_runTest_env, 1);
53 * @defgroup CCUnitValueToString Convert value to string
54 * Make value to string.
59 * @name ccunit_assert_format_TYPE
60 * snprintf format string for each types.
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";
78 #if CCUNIT_HAVE_U_QUAD_T
79 const char* _ccunit_assert_format_u_quad_t = "%llu";
83 /** @name Value to string definitions
84 * Make value to string.
85 * @param TYP type of value.
86 * @param CAPACITY string size.
89 #define DEF_VALUE_TO_STRING(TYP, CAPACITY) \
90 static char* TYP ## _to_string (TYP value) \
94 int capacity = CAPACITY; \
95 for (str = malloc (capacity); \
97 str = realloc (str, capacity)) \
99 require = snprintf (str, capacity, \
100 _ccunit_assert_format_ ## TYP, \
104 else if (require < capacity) \
107 capacity = require + 1; \
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);
125 #if CCUNIT_HAVE_U_QUAD_T
126 DEF_VALUE_TO_STRING(u_quad_t, 24);
128 DEF_VALUE_TO_STRING(_ccunit_str_t, 32);
129 DEF_VALUE_TO_STRING(_ccunit_ptr_t, 24);
132 * end of DEF_VALUE_TO_STRING
136 * end of CCUnitValueToString
140 * @name Assert test type
142 * Assert test function.
143 * @param TYP type of test value.
145 #define DEF_CCUNIT_ASSERT_TEST_TYPE(TYP) \
146 void ccunit_assert_test_ ## TYP (const char* file, \
149 const char* condstr, \
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); \
162 assert (_ccunit_testFailure != NULL); \
163 longjmp (_ccunit_runTest_env, 1); \
167 void ccunit_assert_test__ccunit_obj_t (const char* file,
171 _ccunit_obj_t expect,
172 _ccunit_obj_t actual,
173 char* (*to_string)(_ccunit_obj_t))
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);
186 assert (_ccunit_testFailure != NULL);
187 longjmp (_ccunit_runTest_env, 1);
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);
203 #if CCUNIT_HAVE_U_QUAD_T
204 DEF_CCUNIT_ASSERT_TEST_TYPE(u_quad_t);
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);