Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | static bool ADBG_AssertHelper(ADBG_Case_t *const Case_p, |
| 32 | const char *const FileName_p, |
| 33 | const int LineNumber, const bool ExpressionOK); |
| 34 | |
| 35 | static const char *ADBG_GetFileBase(const char *const FileName_p); |
| 36 | |
| 37 | /************************************************************************* |
| 38 | * 5. Definition of external functions |
| 39 | ************************************************************************/ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 40 | bool Do_ADBG_Expect( |
| 41 | ADBG_Case_t *const Case_p, |
| 42 | const char *const FileName_p, |
| 43 | const int LineNumber, |
| 44 | const int Expected, |
| 45 | const int Got, |
| 46 | const char *const GotVarName_p, |
| 47 | const ADBG_EnumTable_t *const EnumTable_p |
| 48 | ) |
| 49 | { |
| 50 | if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected == Got)) |
| 51 | return true; |
| 52 | |
| 53 | if (EnumTable_p != NULL) { |
| 54 | Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, " |
| 55 | "expected 0x%x = %s", |
| 56 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 57 | GotVarName_p, |
| 58 | Got, Do_ADBG_GetEnumName(Got, EnumTable_p), |
| 59 | Expected, |
| 60 | Do_ADBG_GetEnumName(Expected, EnumTable_p)); |
| 61 | } else { |
| 62 | Do_ADBG_Log( |
| 63 | "%s:%d: %s has an unexpected value: 0x%x, expected 0x%x", |
| 64 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 65 | GotVarName_p, Got, Expected); |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool Do_ADBG_ExpectNot( |
| 72 | ADBG_Case_t *const Case_p, |
| 73 | const char *const FileName_p, |
| 74 | const int LineNumber, |
| 75 | const int NotExpected, |
| 76 | const int Got, |
| 77 | const char *const GotVarName_p, |
| 78 | const ADBG_EnumTable_t *const EnumTable_p |
| 79 | ) |
| 80 | { |
| 81 | if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, NotExpected != |
| 82 | Got)) |
| 83 | return true; |
| 84 | |
| 85 | if (EnumTable_p != NULL) { |
| 86 | Do_ADBG_Log("%s:%d: %s has an unexpected value: 0x%x = %s, " |
| 87 | "expected 0x%x = %s", |
| 88 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 89 | GotVarName_p, |
| 90 | Got, Do_ADBG_GetEnumName(Got, EnumTable_p), |
| 91 | NotExpected, |
| 92 | Do_ADBG_GetEnumName(NotExpected, EnumTable_p)); |
| 93 | } else { |
| 94 | Do_ADBG_Log( |
| 95 | "%s:%d: %s has an unexpected value: 0x%zu, expected 0x%zu", |
| 96 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 97 | GotVarName_p, (size_t)Got, (size_t)NotExpected); |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | bool Do_ADBG_ExpectBuffer( |
| 104 | ADBG_Case_t *const Case_p, |
| 105 | const char *const FileName_p, |
| 106 | const int LineNumber, |
| 107 | const void *const ExpectedBuffer_p, |
| 108 | const size_t ExpectedBufferLength, |
| 109 | const char *const GotBufferName_p, |
| 110 | const void *const GotBuffer_p, |
| 111 | const char *const GotBufferLengthName_p, |
| 112 | const size_t GotBufferLength |
| 113 | ) |
| 114 | { |
| 115 | if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, |
| 116 | ExpectedBufferLength == GotBufferLength)) { |
| 117 | Do_ADBG_Log( |
| 118 | "%s:%d: %s has an unexpected value: %zu, expected %zu", |
| 119 | ADBG_GetFileBase( |
| 120 | FileName_p), LineNumber, |
| 121 | GotBufferLengthName_p, GotBufferLength, |
| 122 | ExpectedBufferLength); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, |
| 127 | memcmp(ExpectedBuffer_p, GotBuffer_p, |
| 128 | ExpectedBufferLength) == 0)) { |
| 129 | Do_ADBG_Log("%s:%d: %s has an unexpected content:", |
| 130 | ADBG_GetFileBase( |
| 131 | FileName_p), LineNumber, GotBufferName_p); |
| 132 | Do_ADBG_Log("Got"); |
| 133 | Do_ADBG_HexLog(GotBuffer_p, GotBufferLength, 16); |
| 134 | Do_ADBG_Log("Expected"); |
| 135 | Do_ADBG_HexLog(ExpectedBuffer_p, ExpectedBufferLength, 16); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool Do_ADBG_ExpectPointer( |
| 143 | ADBG_Case_t *const Case_p, |
| 144 | const char *const FileName_p, |
| 145 | const int LineNumber, |
| 146 | const void *Expected_p, |
| 147 | const void *Got_p, |
| 148 | const char *const GotVarName_p |
| 149 | ) |
| 150 | { |
| 151 | if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Expected_p == |
| 152 | Got_p)) |
| 153 | return true; |
| 154 | |
| 155 | Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected %p", |
| 156 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 157 | GotVarName_p, Got_p, Expected_p); |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | bool Do_ADBG_ExpectPointerNotNULL( |
| 165 | ADBG_Case_t *const Case_p, |
| 166 | const char *const FileName_p, |
| 167 | const int LineNumber, |
| 168 | const void *Got_p, |
| 169 | const char *const GotVarName_p |
| 170 | ) |
| 171 | { |
| 172 | if (ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Got_p != NULL)) |
| 173 | return true; |
| 174 | |
| 175 | Do_ADBG_Log("%s:%d: %s has an unexpected value: %p, expected not NULL", |
| 176 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 177 | GotVarName_p, Got_p); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | bool Do_ADBG_ExpectCompareSigned( |
| 183 | ADBG_Case_t *const Case_p, |
| 184 | const char *const FileName_p, |
| 185 | const int LineNumber, |
| 186 | const long Value1, |
| 187 | const long Value2, |
| 188 | const bool Result, |
| 189 | const char *const Value1Str_p, |
| 190 | const char *const ComparStr_p, |
| 191 | const char *const Value2Str_p |
| 192 | ) |
| 193 | { |
| 194 | if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) { |
| 195 | Do_ADBG_Log( |
| 196 | "%s:%d: Expression \"%s %s %s\" (%ld %s %ld) is false", |
| 197 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 198 | Value1Str_p, ComparStr_p, Value2Str_p, |
| 199 | Value1, ComparStr_p, Value2); |
| 200 | } |
| 201 | return Result; |
| 202 | } |
| 203 | |
| 204 | bool Do_ADBG_ExpectCompareUnsigned( |
| 205 | ADBG_Case_t *const Case_p, |
| 206 | const char *const FileName_p, |
| 207 | const int LineNumber, |
| 208 | const unsigned long Value1, |
| 209 | const unsigned long Value2, |
| 210 | const bool Result, |
| 211 | const char *const Value1Str_p, |
| 212 | const char *const ComparStr_p, |
| 213 | const char *const Value2Str_p |
| 214 | ) |
| 215 | { |
| 216 | if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) { |
| 217 | Do_ADBG_Log( |
| 218 | "%s:%d: Expression \"%s %s %s\" (%lu %s %lu) is false", |
| 219 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 220 | Value1Str_p, ComparStr_p, Value2Str_p, |
| 221 | Value1, ComparStr_p, Value2); |
| 222 | } |
| 223 | return Result; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | bool Do_ADBG_ExpectComparePointer( |
| 228 | ADBG_Case_t *const Case_p, |
| 229 | const char *const FileName_p, |
| 230 | const int LineNumber, |
| 231 | const void *const Value1_p, |
| 232 | const void *const Value2_p, |
| 233 | const bool Result, |
| 234 | const char *const Value1Str_p, |
| 235 | const char *const ComparStr_p, |
| 236 | const char *const Value2Str_p |
| 237 | ) |
| 238 | { |
| 239 | if (!ADBG_AssertHelper(Case_p, FileName_p, LineNumber, Result)) { |
| 240 | Do_ADBG_Log( |
| 241 | "%s:%d: Expression \"%s %s %s\" (%p %s %p) is false", |
| 242 | ADBG_GetFileBase(FileName_p), LineNumber, |
| 243 | Value1Str_p, ComparStr_p, Value2Str_p, |
| 244 | Value1_p, ComparStr_p, Value2_p); |
| 245 | } |
| 246 | return Result; |
| 247 | } |
| 248 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 249 | /************************************************************************* |
| 250 | * 6. Definitions of internal functions |
| 251 | ************************************************************************/ |
| 252 | static bool ADBG_AssertHelper( |
| 253 | ADBG_Case_t *const Case_p, |
| 254 | const char *const FileName_p, |
| 255 | const int LineNumber, |
| 256 | const bool ExpressionOK |
| 257 | ) |
| 258 | { |
| 259 | ADBG_SubCase_t *SubCase_p = Case_p->CurrentSubCase_p; |
| 260 | |
| 261 | IDENTIFIER_NOT_USED(Case_p) |
| 262 | |
| 263 | SubCase_p->Result.NumTests++; |
| 264 | |
| 265 | if (!ExpressionOK) { |
| 266 | SubCase_p->Result.NumFailedTests++; |
| 267 | if (SubCase_p->Result.FirstFailedRow == 0) { |
| 268 | SubCase_p->Result.FirstFailedRow = LineNumber; |
| 269 | SubCase_p->Result.FirstFailedFile_p = ADBG_GetFileBase( |
| 270 | FileName_p); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return ExpressionOK; |
| 275 | } |
| 276 | |
| 277 | static const char *ADBG_GetFileBase(const char *const FileName_p) |
| 278 | { |
| 279 | const char *Ch_p = FileName_p; |
| 280 | const char *Base_p = FileName_p; |
| 281 | |
| 282 | while (*Ch_p != '\0') { |
| 283 | if (*Ch_p == '\\') |
| 284 | Base_p = Ch_p + 1; |
| 285 | |
| 286 | Ch_p++; |
| 287 | } |
| 288 | |
| 289 | return Base_p; |
| 290 | } |