blob: bedfd5da2d2e88b09417ffda5613621ea4ef8a8b [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14/*************************************************************************
15 * 1. Includes
16 ************************************************************************/
17#include "adbg_int.h"
18
19/*************************************************************************
20 * 2. Definition of external constants and variables
21 ************************************************************************/
22
23/*************************************************************************
24 * 3. File scope types, constants and variables
25 ************************************************************************/
26
27/*************************************************************************
28 * 4. Declaration of file local functions
29 ************************************************************************/
30
31static bool ADBG_AssertHelper(ADBG_Case_t *const Case_p,
32 const char *const FileName_p,
33 const int LineNumber, const bool ExpressionOK);
34
35static const char *ADBG_GetFileBase(const char *const FileName_p);
36
37/*************************************************************************
38 * 5. Definition of external functions
39 ************************************************************************/
40void Do_ADBG_Assert(
41 ADBG_Case_t *const Case_p,
42 const char *const FileName_p,
43 const int LineNumber,
44 const bool ExpressionOK,
45 const char *const Format_p, ...
46 )
47{
48 char Buffer[ADBG_STRING_LENGTH_MAX];
49 va_list List;
50
51 if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, ExpressionOK))
52 return;
53
54 /*lint -save -e718 -e746 -e530 lint doesn't seem to know of va_start */
55 va_start(List, Format_p);
56 /*lint -restore */
57 (void)vsnprintf(Buffer, sizeof(Buffer), Format_p, List);
58 va_end(List);
59
60 Do_ADBG_Log("%s:%d: %s",
61 ADBG_GetFileBase(FileName_p), LineNumber, Buffer);
62}
63
64bool Do_ADBG_Expect(
65 ADBG_Case_t *const Case_p,
66 const char *const FileName_p,
67 const int LineNumber,
68 const int Expected,
69 const int Got,
70 const char *const GotVarName_p,
71 const ADBG_EnumTable_t *const EnumTable_p
72 )
73{
74 if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected == Got))
75 return true;
76
77 if (EnumTable_p != NULL) {
78 Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, "
79 "expected 0x%x = %s",
80 ADBG_GetFileBase(FileName_p), LineNumber,
81 GotVarName_p,
82 Got, Do_ADBG_GetEnumName(Got, EnumTable_p),
83 Expected,
84 Do_ADBG_GetEnumName(Expected, EnumTable_p));
85 } else {
86 Do_ADBG_Log(
87 "%s:%d: %s has an unexpected value: 0x%x, expected 0x%x",
88 ADBG_GetFileBase(FileName_p), LineNumber,
89 GotVarName_p, Got, Expected);
90 }
91
92 return false;
93}
94
95bool Do_ADBG_ExpectNot(
96 ADBG_Case_t *const Case_p,
97 const char *const FileName_p,
98 const int LineNumber,
99 const int NotExpected,
100 const int Got,
101 const char *const GotVarName_p,
102 const ADBG_EnumTable_t *const EnumTable_p
103 )
104{
105 if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, NotExpected !=
106 Got))
107 return true;
108
109 if (EnumTable_p != NULL) {
110 Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, "
111 "expected 0x%x = %s",
112 ADBG_GetFileBase(FileName_p), LineNumber,
113 GotVarName_p,
114 Got, Do_ADBG_GetEnumName(Got, EnumTable_p),
115 NotExpected,
116 Do_ADBG_GetEnumName(NotExpected, EnumTable_p));
117 } else {
118 Do_ADBG_Log(
119 "%s:%d: %s has an unexpected value: 0x%zu, expected 0x%zu",
120 ADBG_GetFileBase(FileName_p), LineNumber,
121 GotVarName_p, (size_t)Got, (size_t)NotExpected);
122 }
123
124 return false;
125}
126
127bool Do_ADBG_ExpectBuffer(
128 ADBG_Case_t *const Case_p,
129 const char *const FileName_p,
130 const int LineNumber,
131 const void *const ExpectedBuffer_p,
132 const size_t ExpectedBufferLength,
133 const char *const GotBufferName_p,
134 const void *const GotBuffer_p,
135 const char *const GotBufferLengthName_p,
136 const size_t GotBufferLength
137 )
138{
139 if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber,
140 ExpectedBufferLength == GotBufferLength)) {
141 Do_ADBG_Log(
142 "%s:%d: %s has an unexpected value: %zu, expected %zu",
143 ADBG_GetFileBase(
144 FileName_p), LineNumber,
145 GotBufferLengthName_p, GotBufferLength,
146 ExpectedBufferLength);
147 return false;
148 }
149
150 if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber,
151 memcmp(ExpectedBuffer_p, GotBuffer_p,
152 ExpectedBufferLength) == 0)) {
153 Do_ADBG_Log("%s:%d: %s has an unexpected content:",
154 ADBG_GetFileBase(
155 FileName_p), LineNumber, GotBufferName_p);
156 Do_ADBG_Log("Got");
157 Do_ADBG_HexLog(GotBuffer_p, GotBufferLength, 16);
158 Do_ADBG_Log("Expected");
159 Do_ADBG_HexLog(ExpectedBuffer_p, ExpectedBufferLength, 16);
160 return false;
161 }
162
163 return true;
164}
165
166bool Do_ADBG_ExpectPointer(
167 ADBG_Case_t *const Case_p,
168 const char *const FileName_p,
169 const int LineNumber,
170 const void *Expected_p,
171 const void *Got_p,
172 const char *const GotVarName_p
173 )
174{
175 if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected_p ==
176 Got_p))
177 return true;
178
179 Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected %p",
180 ADBG_GetFileBase(FileName_p), LineNumber,
181 GotVarName_p, Got_p, Expected_p);
182
183 return false;
184}
185
186
187
188bool Do_ADBG_ExpectPointerNotNULL(
189 ADBG_Case_t *const Case_p,
190 const char *const FileName_p,
191 const int LineNumber,
192 const void *Got_p,
193 const char *const GotVarName_p
194 )
195{
196 if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Got_p != NULL))
197 return true;
198
199 Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected not NULL",
200 ADBG_GetFileBase(FileName_p), LineNumber,
201 GotVarName_p, Got_p);
202
203 return false;
204}
205
206bool Do_ADBG_ExpectCompareSigned(
207 ADBG_Case_t *const Case_p,
208 const char *const FileName_p,
209 const int LineNumber,
210 const long Value1,
211 const long Value2,
212 const bool Result,
213 const char *const Value1Str_p,
214 const char *const ComparStr_p,
215 const char *const Value2Str_p
216 )
217{
218 if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
219 Do_ADBG_Log(
220 "%s:%d: Expression \"%s %s %s\" (%ld %s %ld) is false",
221 ADBG_GetFileBase(FileName_p), LineNumber,
222 Value1Str_p, ComparStr_p, Value2Str_p,
223 Value1, ComparStr_p, Value2);
224 }
225 return Result;
226}
227
228bool Do_ADBG_ExpectCompareUnsigned(
229 ADBG_Case_t *const Case_p,
230 const char *const FileName_p,
231 const int LineNumber,
232 const unsigned long Value1,
233 const unsigned long Value2,
234 const bool Result,
235 const char *const Value1Str_p,
236 const char *const ComparStr_p,
237 const char *const Value2Str_p
238 )
239{
240 if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
241 Do_ADBG_Log(
242 "%s:%d: Expression \"%s %s %s\" (%lu %s %lu) is false",
243 ADBG_GetFileBase(FileName_p), LineNumber,
244 Value1Str_p, ComparStr_p, Value2Str_p,
245 Value1, ComparStr_p, Value2);
246 }
247 return Result;
248}
249
250
251bool Do_ADBG_ExpectComparePointer(
252 ADBG_Case_t *const Case_p,
253 const char *const FileName_p,
254 const int LineNumber,
255 const void *const Value1_p,
256 const void *const Value2_p,
257 const bool Result,
258 const char *const Value1Str_p,
259 const char *const ComparStr_p,
260 const char *const Value2Str_p
261 )
262{
263 if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) {
264 Do_ADBG_Log(
265 "%s:%d: Expression \"%s %s %s\" (%p %s %p) is false",
266 ADBG_GetFileBase(FileName_p), LineNumber,
267 Value1Str_p, ComparStr_p, Value2Str_p,
268 Value1_p, ComparStr_p, Value2_p);
269 }
270 return Result;
271}
272
273
274
275size_t Do_ADBG_GetNumberOfErrors(ADBG_Case_t *const Case_p)
276{
277 return Case_p->CurrentSubCase_p->Result.NumFailedTests;
278}
279
280
281/*************************************************************************
282 * 6. Definitions of internal functions
283 ************************************************************************/
284static bool ADBG_AssertHelper(
285 ADBG_Case_t *const Case_p,
286 const char *const FileName_p,
287 const int LineNumber,
288 const bool ExpressionOK
289 )
290{
291 ADBG_SubCase_t *SubCase_p = Case_p->CurrentSubCase_p;
292
293 IDENTIFIER_NOT_USED(Case_p)
294
295 SubCase_p->Result.NumTests++;
296
297 if (!ExpressionOK) {
298 SubCase_p->Result.NumFailedTests++;
299 if (SubCase_p->Result.FirstFailedRow == 0) {
300 SubCase_p->Result.FirstFailedRow = LineNumber;
301 SubCase_p->Result.FirstFailedFile_p = ADBG_GetFileBase(
302 FileName_p);
303 }
304 }
305
306 return ExpressionOK;
307}
308
309static const char *ADBG_GetFileBase(const char *const FileName_p)
310{
311 const char *Ch_p = FileName_p;
312 const char *Base_p = FileName_p;
313
314 while (*Ch_p != '\0') {
315 if (*Ch_p == '\\')
316 Base_p = Ch_p + 1;
317
318 Ch_p++;
319 }
320
321 return Base_p;
322}