File: assert.c

package info (click to toggle)
autounit 0.20.1-5
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,700 kB
  • sloc: sh: 8,777; ansic: 2,606; makefile: 119
file content (345 lines) | stat: -rw-r--r-- 9,823 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Autounit test
 * Copyright (C) 2001-2002  Simon Janes
 * Copyright (C) 2002 James Lewismoss
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/wait.h>

#include <glib.h>

#include <autounit.h>
#include <autounit-private.h>

gboolean
au_assert_true(autounit_test_t *t, gboolean result, 
               char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_assert_true_v(t, result, filename, lineno, msg, ap);
    va_end(ap);
    return ret;
}

gboolean
au_assert_true_v(autounit_test_t *t, gboolean result, 
                 char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    autounit_test_set_status_fp fp;
    char *type;
    char *message;
    char *tmpmsg;
    
    if (!result)
    {
        ret = FALSE;
        type = _("FAIL");
        fp = t->fail_fp;
    } 
    else
    {
        ret = TRUE;
        type = _("SUCCEED");
        fp = t->succeed_fp;
    }

    tmpmsg = g_strdup_vprintf(msg, ap);

    message = g_strdup_printf("%s:%d:%s:%s\n", filename, lineno,
                              type, tmpmsg);
    fp(t, message);
    g_free(message);
    g_free(tmpmsg);
    return ret;
}

static gboolean
get_relative_value_guint64(AU_ASSERT_RELATION_TYPE type,
                           guint64 in1, guint64 in2)
{
    switch (type)
    {
    case AU_REL_GT:
        return (in1 > in2);
     case AU_REL_GT_EQ:
        return (in1 >= in2);
     case AU_REL_LT:
        return (in1 < in2);
     case AU_REL_LT_EQ:
        return (in1 <= in2);
     case AU_REL_EQUAL:
        return (in1 == in2);
     case AU_REL_NOTEQUAL:
        return (in1 != in2);
    default:
        return 0;
     }
}

static gboolean
get_relative_value_gint64(AU_ASSERT_RELATION_TYPE type,
                          gint64 in1, gint64 in2)
{
    switch (type)
    {
    case AU_REL_GT:
        return (in1 > in2);
    case AU_REL_GT_EQ:
        return (in1 >= in2);
    case AU_REL_LT:
        return (in1 < in2);
    case AU_REL_LT_EQ:
        return (in1 <= in2);
    case AU_REL_EQUAL:
        return (in1 == in2);
    case AU_REL_NOTEQUAL:
        return (in1 != in2);
    default:
        return 0;
    }
}

static const char *
get_relative_string(AU_ASSERT_RELATION_TYPE type)
{
    switch (type)
    {
    case AU_REL_GT:
        return ">";
    case AU_REL_GT_EQ:
        return ">=";
    case AU_REL_LT:
        return "<";
    case AU_REL_LT_EQ:
        return "<=";
    case AU_REL_EQUAL:
        return "==";
    case AU_REL_NOTEQUAL:
        return "!=";
    default:
        return "";
    }
}

gboolean
au_assert_str_int_v(autounit_test_t *t,
                    AU_ASSERT_RELATION_TYPE type,
                    char *str1, char *str2, 
                    char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    char *message = g_strdup_printf(_("%s: `%s' not %s `%s'"), msg, str1,
                                    get_relative_string(type), str2);
    if (str1 != 0 && str2 != 0)
    {
        ret = au_assert_true_v(t,
                               get_relative_value_gint64(type,
                                                         strcmp(str1, str2),
                                                         0),
                               filename, lineno, message, ap);
    }
    else if ((str1 == 0 && str2 != 0) || (str1 != 0 && str2 == 0))
    {
        ret = au_assert_true_v(t, 0, filename, lineno, message, ap);
    }
    else /* FIXME: Surprising? */
    {
        ret = au_assert_true_v(t, 0, filename, lineno, message, ap);
    }
    g_free(message);
    return ret;
}


gboolean
au_assert_str_int(autounit_test_t *t,
                  AU_ASSERT_RELATION_TYPE type,
                  char *str1, char *str2, 
                  char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_assert_str_int_v(t, type, str1, str2, filename,
                              lineno, msg, ap);
    va_end(ap);
    return ret;
}

gboolean
au_assert_guint64_int_v(autounit_test_t *t,
                        AU_ASSERT_RELATION_TYPE type,
                        guint64 in1, guint64 in2,
                        char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    char *message = g_strdup_printf(_("%s: `%llu' not %s `%llu'"), msg, in1,
                                    get_relative_string(type), in2);
    
    ret = au_assert_true_v(t, get_relative_value_guint64(type, in1, in2),
                           filename, lineno, message, ap);
    g_free(message);
    return ret;
}

gboolean
au_assert_guint64_int(autounit_test_t *t,
                      AU_ASSERT_RELATION_TYPE type,
                      guint64 in1, guint64 in2,
                      char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_assert_guint64_int_v(t, type, in1, in2, filename,
                                    lineno, msg, ap);
    va_end(ap);
    return ret;
}

gboolean
au_assert_gint64_int_v(autounit_test_t *t,
                       AU_ASSERT_RELATION_TYPE type,
                       gint64 in1, gint64 in2,
                       char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    char *message = g_strdup_printf(_("%s: `%lld' not %s `%lld'"), msg, in1,
                                    get_relative_string(type), in2);
    ret = au_assert_true_v(t, get_relative_value_gint64(type, in1, in2),
                           filename, lineno, message, ap);
    g_free(message);
    return ret;
}

gboolean
au_assert_gint64_int(autounit_test_t *t,
                     AU_ASSERT_RELATION_TYPE type,
                     gint64 in1, gint64 in2,
                     char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_assert_gint64_int_v(t, type, in1, in2, filename, lineno, msg, ap);
    va_end(ap);
    return ret;
}

gboolean
au_asserteq_char_int_v(autounit_test_t *t, char in1, char in2,
                       char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    char *message = g_strdup_printf(_("%s: `%c' not == `%c'"), msg, in1, in2);
    ret = au_assert_true_v(t, in1 == in2, filename, lineno, message, ap);
    g_free(message);
    return ret;
}

gboolean
au_asserteq_char_int(autounit_test_t *t, char in1, char in2,
                     char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_asserteq_char_int_v(t, in1, in2, filename, lineno, msg, ap);
    va_end(ap);
    return ret;
}

gboolean
au_assert_obj_int_v(autounit_test_t *t,
                    AU_ASSERT_RELATION_TYPE type,
                    gpointer ob1, gpointer ob2,
                    autounit_compare_func compare_func,
                    autounit_to_string_func string_func1,
                    autounit_to_string_func string_func2,
                    char *filename, int lineno, char *msg, va_list ap)
{
    gboolean ret;
    char *ob1str;
    char *ob2str;
    char *message;

    ob1str = ((string_func1 != 0)
              ? string_func1(ob1)
              : g_strdup(_("(nostrfunc)")));
    ob2str = ((string_func2 != 0)
              ? string_func2(ob1)
              : g_strdup(_("(nostrfunc)")));
    message = g_strdup_printf("%s: `%s' not %s `%s'", msg, ob1str, get_relative_string(type), ob2str);
    
    if (ob1 != 0 && ob2 != 0)
    {
        if (compare_func != 0)
        {
            ret = au_assert_true_v(t,
                                   get_relative_value_gint64(
                                       type, compare_func(ob1, ob2), 0), 
                                   filename, lineno, message, ap);
        }
        else 
        {
            ret = au_assert_true_v(t, get_relative_value_gint64(
                                       type, (gint)ob1, (gint)ob2),
                                   filename, lineno, message, ap);
        }
    }
    else if ((ob1 == 0 && ob2 != 0) || (ob1 != 0 && ob2 == 0))
    {
        ret = au_assert_true_v(t, 0, filename, lineno, message, ap);
    }
    else /* FIXME: both null means they are not equal.  Surprising? */
    {
        ret = au_assert_true_v(t, 0, filename, lineno, message, ap);
    }
    g_free(message);
    g_free(ob1str);
    g_free(ob2str);
    return ret;
}

gboolean
au_assert_obj_int(autounit_test_t *t,
                  AU_ASSERT_RELATION_TYPE type,
                  gpointer ob1, gpointer ob2,
                  autounit_compare_func compare_func,
                  autounit_to_string_func string_func1,
                  autounit_to_string_func string_func2,
                  char *filename, int lineno, char *msg, ...)
{
    gboolean ret;
    va_list ap;
    va_start(ap, msg);
    ret = au_assert_obj_int_v(t, type, ob1, ob2, compare_func,
                              string_func1, string_func1,
                              filename, lineno, msg, ap);
    va_end(ap);
    return ret;
}