1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 6 #ifndef ADBG_H 7 #define ADBG_H 8 #include <stdarg.h> 9 #include <stdbool.h> 10 #include <stddef.h> 11 #include <stdint.h> 12 #include <sys/queue.h> 13 14 #define ADBG_STRING_LENGTH_MAX (1024) 15 16 /* 17 * Case definitions 18 */ 19 20 /** 21 * Defines a test case 22 * 23 * Used in the follwing way for readability: 24 */ 25 #if 0 /* #if 0 to avoid nested comments */ 26 ADBG_CASE_DEFINE(TEST_1001, TEST_Test_1001, 27 /* Title */ 28 "My test case title", 29 ); 30 #endif 31 32 typedef struct ADBG_Case ADBG_Case_t; 33 34 typedef struct adbg_case_def { 35 const char *TestID_p; 36 const char *Title_p; 37 void (*Run_fp)(ADBG_Case_t *ADBG_Case_pp); 38 TAILQ_ENTRY(adbg_case_def) link; 39 } ADBG_Case_Definition_t; 40 41 TAILQ_HEAD(adbg_case_def_head, adbg_case_def); 42 43 typedef struct adbg_suite_def { 44 const char *SuiteID_p; 45 struct adbg_case_def_head cases; 46 } ADBG_Suite_Definition_t; 47 48 #define ADBG_CASE_DEFINE(Suite, TestID, Run, Title) \ 49 __attribute__((constructor)) static void \ 50 __adbg_test_case_ ## TestID(void) \ 51 { \ 52 static ADBG_Case_Definition_t case_def = { \ 53 .TestID_p = #Suite "_" #TestID, \ 54 .Title_p = Title, \ 55 .Run_fp = Run, \ 56 }; \ 57 struct adbg_case_def_head *ch = &(ADBG_Suite_ ## Suite).cases; \ 58 struct adbg_case_def *cd = NULL; \ 59 \ 60 TAILQ_FOREACH(cd, ch, link) \ 61 if (strcmp(case_def.TestID_p, cd->TestID_p) < 0) \ 62 break; \ 63 if (cd) \ 64 TAILQ_INSERT_BEFORE(cd, &case_def, link); \ 65 else \ 66 TAILQ_INSERT_TAIL(ch, &case_def, link); \ 67 } 68 69 /* 70 * Suite definitions 71 */ 72 73 /** 74 * Declares a suite defined in a C-file. 75 */ 76 #define ADBG_SUITE_DECLARE(Name) \ 77 extern ADBG_Suite_Definition_t ADBG_Suite_ ## Name 78 79 #define ADBG_SUITE_DEFINE(Name) \ 80 ADBG_Suite_Definition_t ADBG_Suite_ ## Name = { \ 81 .SuiteID_p = #Name, \ 82 .cases = TAILQ_HEAD_INITIALIZER(ADBG_Suite_ ## Name.cases), \ 83 } 84 85 /* 86 * Enum table definitions 87 */ 88 89 #define ADBG_ENUM_TABLE_DECLARE(Name) \ 90 extern const ADBG_EnumTable_t ADBG_EnumTable_ ## Name[] 91 92 #define ADBG_ENUM_TABLE_DEFINE_BEGIN(Name) \ 93 const ADBG_EnumTable_t ADBG_EnumTable_ ## Name[] = { 94 #define ADBG_ENUM_TABLE_ENTRY(Value) { Value, #Value } 95 96 #define ADBG_ENUM_TABLE_DEFINE_END(Name) , { 0, NULL } } 97 98 typedef struct { 99 int Value; 100 const char *const Name_p; 101 } ADBG_EnumEntry_t; 102 103 typedef ADBG_EnumEntry_t ADBG_EnumTable_t; 104 105 ADBG_ENUM_TABLE_DECLARE(Boolean); 106 107 /* 108 * Expect functions/macros 109 */ 110 111 #define ADBG_EXPECT(Case_p, Expected, Got) \ 112 ADBG_EXPECT_ENUM(Case_p, Expected, Got, NULL) 113 114 #define ADBG_EXPECT_NOT(Case_p, Expected, Got) \ 115 ADBG_EXPECT_NOT_ENUM(Case_p, Expected, Got, NULL) 116 117 #define ADBG_EXPECT_ENUM(Case_p, Expected, Got, EnumTable_p) \ 118 Do_ADBG_Expect(Case_p, __FILE__, __LINE__, Expected, Got, #Got, \ 119 EnumTable_p) 120 121 #define ADBG_EXPECT_NOT_ENUM(Case_p, NotExpected, Got, EnumTable_p) \ 122 Do_ADBG_ExpectNot(Case_p, __FILE__, __LINE__, \ 123 NotExpected, Got, #Got, EnumTable_p) 124 125 #define ADBG_EXPECT_BOOLEAN(Case_p, Expected, Got) \ 126 ADBG_EXPECT_ENUM(Case_p, Expected, Got, ADBG_EnumTable_Boolean) 127 128 #define ADBG_EXPECT_TRUE(Case_p, Got) \ 129 ADBG_EXPECT_ENUM(Case_p, true, Got, ADBG_EnumTable_Boolean) 130 131 #define ADBG_EXPECT_EQUAL(Case_p, Buf1_p, Buf2_p, Length) \ 132 ADBG_EXPECT(Case_p, 0, memcmp(Buf1_p, Buf2_p, Length)) 133 134 #define ADBG_EXPECT_BUFFER(Case_p, ExpBuf_p, ExpBufLen, GotBuf_p, GotBufLen) \ 135 Do_ADBG_ExpectBuffer(Case_p, __FILE__, __LINE__, \ 136 ExpBuf_p, ExpBufLen, #GotBuf_p, GotBuf_p, \ 137 #GotBufLen, GotBufLen) 138 139 #define ADBG_EXPECT_POINTER(Case_p, Expected, Got) \ 140 Do_ADBG_ExpectPointer(Case_p, __FILE__, __LINE__, Expected, Got, #Got) 141 142 #define ADBG_EXPECT_NOT_NULL(Case_p, Got) \ 143 Do_ADBG_ExpectPointerNotNULL(Case_p, __FILE__, __LINE__, Got, #Got) 144 145 #define ADBG_EXPECT_COMPARE_SIGNED(Case_p, Val1, Compar, Val2) \ 146 Do_ADBG_ExpectCompareSigned(Case_p, __FILE__, __LINE__, \ 147 Val1, Val2, (Val1)Compar( \ 148 Val2), #Val1, #Compar, #Val2) 149 150 #define ADBG_EXPECT_COMPARE_UNSIGNED(Case_p, Val1, Compar, Val2) \ 151 Do_ADBG_ExpectCompareUnsigned(Case_p, __FILE__, __LINE__, \ 152 Val1, Val2, (Val1)Compar( \ 153 Val2), #Val1, #Compar, #Val2) 154 155 #define ADBG_EXPECT_COMPARE_POINTER(Case_p, Val1, Compar, Val2) \ 156 Do_ADBG_ExpectComparePointer(Case_p, __FILE__, __LINE__, \ 157 Val1, Val2, (Val1)Compar( \ 158 Val2), #Val1, #Compar, #Val2) 159 160 bool Do_ADBG_Expect(ADBG_Case_t *const Case_p, const char *const FileName_p, 161 const int LineNumber, const int Expected, const int Got, 162 const char *const GotVarName_p, 163 const ADBG_EnumTable_t *const EnumTable_p); 164 165 bool Do_ADBG_ExpectNot(ADBG_Case_t *const Case_p, const char *const FileName_p, 166 const int LineNumber, const int NotExpected, 167 const int Got, const char *const GotVarName_p, 168 const ADBG_EnumTable_t *const EnumTable_p); 169 170 bool Do_ADBG_ExpectBuffer(ADBG_Case_t *const Case_p, 171 const char *const FileName_p, const int LineNumber, 172 const void *const ExpectedBuffer_p, 173 const size_t ExpectedBufferLength, 174 const char *const GotBufferName_p, 175 const void *const GotBuffer_p, 176 const char *const GotBufferLengthName_p, 177 const size_t GotBufferLength); 178 179 bool Do_ADBG_ExpectPointer(ADBG_Case_t *const Case_p, 180 const char *const FileName_p, const int LineNumber, 181 const void *Expected_p, const void *Got_p, 182 const char *const GotVarName_p); 183 184 bool Do_ADBG_ExpectPointerNotNULL(ADBG_Case_t *const Case_p, 185 const char *const FileName_p, 186 const int LineNumber, const void *Got_p, 187 const char *const GotVarName_p); 188 189 bool Do_ADBG_ExpectCompareSigned(ADBG_Case_t *const Case_p, 190 const char *const FileName_p, 191 const int LineNumber, const long Value1, 192 const long Value2, const bool Result, 193 const char *const Value1Str_p, 194 const char *const ComparStr_p, 195 const char *const Value2Str_p); 196 197 bool Do_ADBG_ExpectCompareUnsigned(ADBG_Case_t *const Case_p, 198 const char *const FileName_p, 199 const int LineNumber, 200 const unsigned long Value1, 201 const unsigned long Value2, 202 const bool Result, 203 const char *const Value1Str_p, 204 const char *const ComparStr_p, 205 const char *const Value2Str_p); 206 207 bool Do_ADBG_ExpectComparePointer(ADBG_Case_t *const Case_p, 208 const char *const FileName_p, 209 const int LineNumber, 210 const void *const Value1_p, 211 const void *const Value2_p, const bool Result, 212 const char *const Value1Str_p, 213 const char *const ComparStr_p, 214 const char *const Value2Str_p); 215 216 const char *Do_ADBG_GetEnumName(const int Value, 217 const ADBG_EnumTable_t *const EnumTable_p); 218 219 /** 220 * Writes a string to output. 221 * String length max is defined by ADBG_STRING_LENGTH_MAX 222 * 223 * @param Format_p The formatting string as in printf 224 */ 225 void Do_ADBG_Log(const char *const Format_p, ...) 226 __attribute__((__format__(__printf__, 1, 2))); 227 228 /** 229 * Writes out the contents of buf_p formatted so that each line will 230 * have cols number of columns. 231 * 232 * @param[in] Buf_p Buffer to print 233 * @param[in] Size Size of buffer (in bytes) 234 * @param[in] Cols Number of columns. 235 */ 236 void Do_ADBG_HexLog(const void *const Buf_p, const size_t Size, 237 const size_t Cols); 238 239 /* 240 * Suite functions 241 */ 242 243 int Do_ADBG_RunSuite(const ADBG_Suite_Definition_t *Suite_p, int argc, 244 char *argv[]); 245 int Do_ADBG_AppendToSuite(ADBG_Suite_Definition_t *Dest_p, 246 ADBG_Suite_Definition_t *Source_p); 247 248 /* 249 * SubCase functions 250 */ 251 void Do_ADBG_BeginSubCase(ADBG_Case_t *const Case_p, 252 const char *const FormatTitle_p, 253 ...) __attribute__((__format__(__printf__, 2, 3))); 254 255 void Do_ADBG_EndSubCase(ADBG_Case_t *const Case_p, 256 const char *const FormatTitle_p, 257 ...) __attribute__((__format__(__printf__, 2, 3))); 258 259 #endif /* ADBG_H */ 260