summaryrefslogtreecommitdiff
path: root/eel/eel-self-checks.c
blob: eca9cc2f5b360888e290bf5407244a18834e1de3 (plain)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   eel-self-checks.c: The self-check framework.

   Copyright (C) 1999 Eazel, Inc.

   This program 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 program 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 program; if not, write to the
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Author: Darin Adler <darin@eazel.com>
*/

#include <config.h>

#if ! defined (EEL_OMIT_SELF_CHECK)

#include "eel-self-checks.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static gboolean failed;

static const char *current_expression;
static const char *current_file_name;
static int current_line_number;

void
eel_exit_if_self_checks_failed (void)
{
    if (!failed)
    {
        return;
    }

    printf ("\n");

    exit (EXIT_FAILURE);
}

void
eel_report_check_failure (char *result, char *expected)
{
    if (!failed)
    {
        fprintf (stderr, "\n");
    }

    fprintf (stderr, "FAIL: check failed in %s, line %d\n", current_file_name, current_line_number);
    fprintf (stderr, "      evaluated: %s\n", current_expression);
    fprintf (stderr, "       expected: %s\n", expected == NULL ? "NULL" : expected);
    fprintf (stderr, "            got: %s\n", result == NULL ? "NULL" : result);

    failed = TRUE;

    g_free (result);
    g_free (expected);
}

static char *
eel_strdup_boolean (gboolean boolean)
{
    if (boolean == FALSE)
    {
        return g_strdup ("FALSE");
    }
    if (boolean == TRUE)
    {
        return g_strdup ("TRUE");
    }
    return g_strdup_printf ("gboolean(%d)", boolean);
}

void
eel_before_check (const char *expression,
                  const char *file_name,
                  int line_number)
{
    current_expression = expression;
    current_file_name = file_name;
    current_line_number = line_number;
}

void
eel_after_check (void)
{
    /* It would be good to check here if there was a memory leak. */
}

void
eel_check_boolean_result (gboolean result, gboolean expected)
{
    if (result != expected)
    {
        eel_report_check_failure (eel_strdup_boolean (result),
                                  eel_strdup_boolean (expected));
    }
    eel_after_check ();
}

void
eel_check_rectangle_result (EelIRect result,
                            int expected_x0,
                            int expected_y0,
                            int expected_x1,
                            int expected_y1)
{
    if (result.x0 != expected_x0
            || result.y0 != expected_y0
            || result.x1 != expected_x1
            || result.y1 != expected_y1)
    {
        eel_report_check_failure (g_strdup_printf ("x0=%d, y0=%d, x1=%d, y1=%d",
                                  result.x0,
                                  result.y0,
                                  result.x1,
                                  result.y1),
                                  g_strdup_printf ("x0=%d, y0=%d, x1=%d, y1=%d",
                                          expected_x0,
                                          expected_y0,
                                          expected_x1,
                                          expected_y1));
    }
    eel_after_check ();
}

void
eel_check_dimensions_result (EelDimensions result,
                             int expected_width,
                             int expected_height)
{
    if (result.width != expected_width
            || result.height != expected_height)
    {
        eel_report_check_failure (g_strdup_printf ("width=%d, height=%d",
                                  result.width,
                                  result.height),
                                  g_strdup_printf ("width=%d, height=%d",
                                          expected_width,
                                          expected_height));
    }
    eel_after_check ();
}

void
eel_check_point_result (EelIPoint result,
                        int expected_x,
                        int expected_y)
{
    if (result.x != expected_x
            || result.y != expected_y)
    {
        eel_report_check_failure (g_strdup_printf ("x=%d, y=%d",
                                  result.x,
                                  result.y),
                                  g_strdup_printf ("x=%d, y=%d",
                                          expected_x,
                                          expected_y));
    }
    eel_after_check ();
}

void
eel_check_integer_result (long result, long expected)
{
    if (result != expected)
    {
        eel_report_check_failure (g_strdup_printf ("%ld", result),
                                  g_strdup_printf ("%ld", expected));
    }
    eel_after_check ();
}

void
eel_check_double_result (double result, double expected)
{
    if (result != expected)
    {
        eel_report_check_failure (g_strdup_printf ("%f", result),
                                  g_strdup_printf ("%f", expected));
    }
    eel_after_check ();
}

void
eel_check_string_result (char *result, const char *expected)
{
    gboolean match;

    /* Stricter than eel_strcmp.
     * NULL does not match "" in this test.
     */
    if (expected == NULL)
    {
        match = result == NULL;
    }
    else
    {
        match = result != NULL && strcmp (result, expected) == 0;
    }

    if (!match)
    {
        eel_report_check_failure (result, g_strdup (expected));
    }
    else
    {
        g_free (result);
    }
    eel_after_check ();
}

void
eel_before_check_function (const char *name)
{
    fprintf (stderr, "running %s\n", name);
}

void
eel_after_check_function (void)
{
}

#endif /* ! EEL_OMIT_SELF_CHECK */