No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 eel-self-checks.c: The self-check framework.
4
5 Copyright (C) 1999 Eazel, Inc.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Author: Darin Adler <darin@eazel.com>
23 */
24
25 #include <config.h>
26
27 #if ! defined (EEL_OMIT_SELF_CHECK)
28
29 #include "eel-self-checks.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 static gboolean failed;
36
37 static const char *current_expression;
38 static const char *current_file_name;
39 static int current_line_number;
40
41 void
42 eel_exit_if_self_checks_failed (void)
43 {
44 if (!failed) {
45 return;
46 }
47
48 printf ("\n");
49
50 exit (EXIT_FAILURE);
51 }
52
53 void
54 eel_report_check_failure (char *result, char *expected)
55 {
56 if (!failed) {
57 fprintf (stderr, "\n");
58 }
59
60 fprintf (stderr, "FAIL: check failed in %s, line %d\n", current_file_name, current_line_number);
61 fprintf (stderr, " evaluated: %s\n", current_expression);
62 fprintf (stderr, " expected: %s\n", expected == NULL ? "NULL" : expected);
63 fprintf (stderr, " got: %s\n", result == NULL ? "NULL" : result);
64
65 failed = TRUE;
66
67 g_free (result);
68 g_free (expected);
69 }
70
71 static char *
72 eel_strdup_boolean (gboolean boolean)
73 {
74 if (boolean == FALSE) {
75 return g_strdup ("FALSE");
76 }
77 if (boolean == TRUE) {
78 return g_strdup ("TRUE");
79 }
80 return g_strdup_printf ("gboolean(%d)", boolean);
81 }
82
83 void
84 eel_before_check (const char *expression,
85 const char *file_name,
86 int line_number)
87 {
88 current_expression = expression;
89 current_file_name = file_name;
90 current_line_number = line_number;
91 }
92
93 void
94 eel_after_check (void)
95 {
96 /* It would be good to check here if there was a memory leak. */
97 }
98
99 void
100 eel_check_boolean_result (gboolean result, gboolean expected)
101 {
102 if (result != expected) {
103 eel_report_check_failure (eel_strdup_boolean (result),
104 eel_strdup_boolean (expected));
105 }
106 eel_after_check ();
107 }
108
109 void
110 eel_check_rectangle_result (EelIRect result,
111 int expected_x0,
112 int expected_y0,
113 int expected_x1,
114 int expected_y1)
115 {
116 if (result.x0 != expected_x0
117 || result.y0 != expected_y0
118 || result.x1 != expected_x1
119 || result.y1 != expected_y1) {
120 eel_report_check_failure (g_strdup_printf ("x0=%d, y0=%d, x1=%d, y1=%d",
121 result.x0,
122 result.y0,
123 result.x1,
124 result.y1),
125 g_strdup_printf ("x0=%d, y0=%d, x1=%d, y1=%d",
126 expected_x0,
127 expected_y0,
128 expected_x1,
129 expected_y1));
130 }
131 eel_after_check ();
132 }
133
134 void
135 eel_check_integer_result (long result, long expected)
136 {
137 if (result != expected) {
138 eel_report_check_failure (g_strdup_printf ("%ld", result),
139 g_strdup_printf ("%ld", expected));
140 }
141 eel_after_check ();
142 }
143
144 void
145 eel_check_double_result (double result, double expected)
146 {
147 if (result != expected) {
148 eel_report_check_failure (g_strdup_printf ("%f", result),
149 g_strdup_printf ("%f", expected));
150 }
151 eel_after_check ();
152 }
153
154 void
155 eel_check_string_result (char *result, const char *expected)
156 {
157 gboolean match;
158
159 /* Stricter than eel_strcmp.
160 * NULL does not match "" in this test.
161 */
162 if (expected == NULL) {
163 match = result == NULL;
164 } else {
165 match = result != NULL && strcmp (result, expected) == 0;
166 }
167
168 if (!match) {
169 eel_report_check_failure (result, g_strdup (expected));
170 } else {
171 g_free (result);
172 }
173 eel_after_check ();
174 }
175
176 void
177 eel_before_check_function (const char *name)
178 {
179 fprintf (stderr, "running %s\n", name);
180 }
181
182 void
183 eel_after_check_function (void)
184 {
185 }
186
187 #endif /* ! EEL_OMIT_SELF_CHECK */