Skip to content

Commit 8d17673

Browse files
committed
Fix #981, rework "unit-tests" to use macros
Update most of the conditions in the "unit tests" to use the support macros whenever possible. The macros display the function being called as well as the return value being checked for, and therefore this makes it so the test log file can be cross referenced back to the documentation to ensure the documented return codes are being tested. As a nice side effect this also removes quite a bit of repetition in the test programs.
1 parent 706f0de commit 8d17673

17 files changed

+1491
-4561
lines changed

src/unit-tests/inc/ut_os_support.h

+33-16
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,59 @@
6161
*/
6262
#define UT_OS_IO_BUFF_SIZE 128
6363

64-
static inline bool UtOsalRetVal(int32 Fn, int32 Exp, UtAssert_CaseType_t casetype, const char *File, uint32 Line,
65-
const char *FnTxt, const char *ExpTxt, const char *Msg)
64+
static inline bool UtOsalRetVal(int32 Fn, int32 Exp, bool NotImplAllowed, UtAssert_CaseType_t casetype,
65+
const char *File, uint32 Line, const char *FnTxt, const char *ExpTxt)
6666
{
67-
return UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d): %s", FnTxt, (int)Fn, ExpTxt, (int)Exp, Msg);
67+
/* If "not implemented" is acceptable, override the casetype to be N/A */
68+
if (NotImplAllowed && (Fn == OS_ERR_NOT_IMPLEMENTED || Fn == OS_ERR_OPERATION_NOT_SUPPORTED))
69+
{
70+
casetype = UTASSERT_CASETYPE_NA;
71+
}
72+
73+
return UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d)", FnTxt, (int)Fn, ExpTxt, (int)Exp);
74+
}
75+
76+
static inline bool UtManualInspectionWithStatus(int32 Fn, const char *File, uint32 Line, const char *FnTxt)
77+
{
78+
UtAssertEx(false, UTASSERT_CASETYPE_MIR, File, Line, "%s value=%d", FnTxt, (int)Fn);
79+
return (Fn >= 0);
6880
}
6981

7082
/* Only report errors */
71-
static inline bool UtOsalCheck(int32 Fn, int32 Exp, UtAssert_CaseType_t casetype, const char *File, uint32 Line,
72-
const char *FnTxt, const char *ExpTxt)
83+
static inline bool UtOsalCheck(int32 Fn, UtAssert_CaseType_t casetype, const char *File, uint32 Line, const char *FnTxt)
7384
{
74-
return Fn == Exp
75-
? true
76-
: UtAssertEx(Fn == Exp, casetype, File, Line, "%s (%d) == %s (%d)", FnTxt, (int)Fn, ExpTxt, (int)Exp);
85+
/* Check non-negative to support APIs that return nonzero on success (e.g. read/write) */
86+
if (Fn >= 0)
87+
{
88+
return true;
89+
}
90+
91+
return UtAssertEx(false, casetype, File, Line, "%s (%d) >= %s", FnTxt, (int)Fn, "OS_SUCCESS");
7792
}
7893

7994
static inline bool UtOsalImplemented(int32 Fn, const char *File, uint32 Line)
8095
{
8196
if (Fn == OS_ERR_NOT_IMPLEMENTED)
8297
{
83-
UtAssertEx(false, UTASSERT_CASETYPE_NA, File, Line, "API not implemented");
98+
UtAssertEx(false, UTASSERT_CASETYPE_NA, File, Line, "API not implemented (%d)", (int)Fn);
8499
return false;
85100
}
86101

87102
return true;
88103
}
89104

105+
#define UT_RETVAL(Fn, Exp) UtOsalRetVal(Fn, Exp, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, #Exp)
90106
#define UT_NOMINAL(Fn) \
91-
UtOsalRetVal(Fn, OS_SUCCESS, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS", "Nominal")
92-
#define UT_RETVAL(Fn, Exp, Msg) UtOsalRetVal(Fn, Exp, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, #Exp, Msg)
93-
#define UT_SETUP(Fn) UtOsalCheck(Fn, OS_SUCCESS, UTASSERT_CASETYPE_TSF, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
94-
#define UT_TEARDOWN(Fn) UtOsalCheck(Fn, OS_SUCCESS, UTASSERT_CASETYPE_TTF, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
95-
#define UT_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__)
107+
UtOsalRetVal(Fn, OS_SUCCESS, false, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
108+
#define UT_NOMINAL_OR_NOTIMPL(Fn) \
109+
UtOsalRetVal(Fn, OS_SUCCESS, true, UTASSERT_CASETYPE_FAILURE, __FILE__, __LINE__, #Fn, "OS_SUCCESS")
96110

97-
/*--------------------------------------------------------------------------------*/
111+
#define UT_MIR_STATUS(Fn) UtManualInspectionWithStatus(Fn, __FILE__, __LINE__, #Fn)
112+
#define UT_MIR_VOID(Fn) Fn, UtAssertEx(false, UTASSERT_CASETYPE_MIR, __FILE__, __LINE__, "%s", #Fn)
98113

99-
#define UT_OS_TEST_RESULT(descStr, caseType) UtAssertEx(false, caseType, __FILE__, __LINE__, "%s", descStr)
114+
#define UT_SETUP(Fn) UtOsalCheck(Fn, UTASSERT_CASETYPE_TSF, __FILE__, __LINE__, #Fn)
115+
#define UT_TEARDOWN(Fn) UtOsalCheck(Fn, UTASSERT_CASETYPE_TTF, __FILE__, __LINE__, #Fn)
116+
#define UT_IMPL(Fn) UtOsalImplemented(Fn, __FILE__, __LINE__)
100117

101118
/*--------------------------------------------------------------------------------*/
102119

src/unit-tests/oscore-test/ut_oscore_binsem_test.c

+17-51
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,15 @@ void UT_os_bin_sem_create_test()
7373
osal_id_t sem_ids[OS_MAX_BIN_SEMAPHORES + 1];
7474

7575
/*-----------------------------------------------------*/
76-
if (!UT_IMPL(OS_BinSemCreate(&sem_ids[0], "Good", 1, 0)))
77-
return;
78-
UT_TEARDOWN(OS_BinSemDelete(sem_ids[0]));
79-
80-
/*-----------------------------------------------------*/
81-
UT_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER, "null pointer arg 1");
76+
UT_RETVAL(OS_BinSemCreate(NULL, "BinSem1", 1, 0), OS_INVALID_POINTER);
8277

8378
/*-----------------------------------------------------*/
84-
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER, "null pointer arg 2");
79+
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], NULL, 1, 0), OS_INVALID_POINTER);
8580

8681
/*-----------------------------------------------------*/
8782
memset(long_sem_name, 'X', sizeof(long_sem_name));
8883
long_sem_name[sizeof(long_sem_name) - 1] = '\0';
89-
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG, "name too long");
84+
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], long_sem_name, 1, 0), OS_ERR_NAME_TOO_LONG);
9085

9186
/*-----------------------------------------------------*/
9287
/* Setup */
@@ -103,8 +98,7 @@ void UT_os_bin_sem_create_test()
10398

10499
if (i == OS_MAX_BIN_SEMAPHORES) /* setup was successful */
105100
{
106-
UT_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS,
107-
"no free ids");
101+
UT_RETVAL(OS_BinSemCreate(&sem_ids[OS_MAX_BIN_SEMAPHORES], "OneTooMany", 1, 0), OS_ERR_NO_FREE_IDS);
108102
}
109103

110104
/* Reset test environment */
@@ -113,7 +107,7 @@ void UT_os_bin_sem_create_test()
113107
/*-----------------------------------------------------*/
114108
if (UT_SETUP(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0)))
115109
{
116-
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN, "duplicate name");
110+
UT_RETVAL(OS_BinSemCreate(&sem_ids[0], "DUPLICATE", 1, 0), OS_ERR_NAME_TAKEN);
117111

118112
/* Reset test environment */
119113
UT_TEARDOWN(OS_BinSemDelete(sem_ids[0]));
@@ -139,11 +133,7 @@ void UT_os_bin_sem_delete_test()
139133
osal_id_t bin_sem_id;
140134

141135
/*-----------------------------------------------------*/
142-
if (!UT_IMPL(OS_BinSemDelete(OS_OBJECT_ID_UNDEFINED)))
143-
return;
144-
145-
/*-----------------------------------------------------*/
146-
UT_RETVAL(OS_BinSemDelete(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
136+
UT_RETVAL(OS_BinSemDelete(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);
147137

148138
/*-----------------------------------------------------*/
149139
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "DeleteTest", 1, 0)))
@@ -166,11 +156,7 @@ void UT_os_bin_sem_flush_test()
166156
osal_id_t bin_sem_id;
167157

168158
/*-----------------------------------------------------*/
169-
if (!UT_IMPL(OS_BinSemFlush(OS_OBJECT_ID_UNDEFINED)))
170-
return;
171-
172-
/*-----------------------------------------------------*/
173-
UT_RETVAL(OS_BinSemFlush(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
159+
UT_RETVAL(OS_BinSemFlush(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);
174160

175161
/*----------------------------------------------------*/
176162
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "FlushTest", 1, 0)))
@@ -194,11 +180,7 @@ void UT_os_bin_sem_give_test()
194180
osal_id_t bin_sem_id;
195181

196182
/*-----------------------------------------------------*/
197-
if (!UT_IMPL(OS_BinSemGive(OS_OBJECT_ID_UNDEFINED)))
198-
return;
199-
200-
/*-----------------------------------------------------*/
201-
UT_RETVAL(OS_BinSemGive(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
183+
UT_RETVAL(OS_BinSemGive(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);
202184

203185
/*-----------------------------------------------------*/
204186
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GiveTest", 1, 0)))
@@ -223,11 +205,7 @@ void UT_os_bin_sem_take_test()
223205
osal_id_t bin_sem_id;
224206

225207
/*-----------------------------------------------------*/
226-
if (!UT_IMPL(OS_BinSemTake(OS_OBJECT_ID_UNDEFINED)))
227-
return;
228-
229-
/*-----------------------------------------------------*/
230-
UT_RETVAL(OS_BinSemTake(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID, "invalid id arg");
208+
UT_RETVAL(OS_BinSemTake(UT_OBJID_INCORRECT), OS_ERR_INVALID_ID);
231209

232210
/*-----------------------------------------------------*/
233211
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "TakeTest", 1, 0)))
@@ -251,16 +229,12 @@ void UT_os_bin_sem_timed_wait_test()
251229
osal_id_t bin_sem_id;
252230

253231
/*-----------------------------------------------------*/
254-
if (!UT_IMPL(OS_BinSemTimedWait(OS_OBJECT_ID_UNDEFINED, 1000)))
255-
return;
256-
257-
/*-----------------------------------------------------*/
258-
UT_RETVAL(OS_BinSemTimedWait(UT_OBJID_INCORRECT, 1000), OS_ERR_INVALID_ID, "invalid id arg");
232+
UT_RETVAL(OS_BinSemTimedWait(UT_OBJID_INCORRECT, 1000), OS_ERR_INVALID_ID);
259233

260234
/*-----------------------------------------------------*/
261235
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "TimedWait", 1, 0)) && UT_SETUP(OS_BinSemTake(bin_sem_id)))
262236
{
263-
UT_RETVAL(OS_BinSemTimedWait(bin_sem_id, 1000), OS_SEM_TIMEOUT, "semtake timed out");
237+
UT_RETVAL(OS_BinSemTimedWait(bin_sem_id, 1000), OS_SEM_TIMEOUT);
264238
UT_TEARDOWN(OS_BinSemDelete(bin_sem_id));
265239
}
266240

@@ -288,22 +262,18 @@ void UT_os_bin_sem_get_id_by_name_test()
288262
char long_sem_name[UT_OS_NAME_BUFF_SIZE];
289263

290264
/*-----------------------------------------------------*/
291-
if (!UT_IMPL(OS_BinSemGetIdByName(NULL, "InvalidName")))
292-
return;
265+
UT_RETVAL(OS_BinSemGetIdByName(NULL, "InvalidName"), OS_INVALID_POINTER);
293266

294267
/*-----------------------------------------------------*/
295-
UT_RETVAL(OS_BinSemGetIdByName(NULL, "InvalidName"), OS_INVALID_POINTER, "invalid ptr arg 1");
296-
297-
/*-----------------------------------------------------*/
298-
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, NULL), OS_INVALID_POINTER, "invalid ptr arg 2");
268+
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, NULL), OS_INVALID_POINTER);
299269

300270
/*-----------------------------------------------------*/
301271
memset(long_sem_name, 'Y', sizeof(long_sem_name));
302272
long_sem_name[sizeof(long_sem_name) - 1] = '\0';
303-
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, long_sem_name), OS_ERR_NAME_TOO_LONG, "name too long");
273+
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, long_sem_name), OS_ERR_NAME_TOO_LONG);
304274

305275
/*-----------------------------------------------------*/
306-
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, "NameNotFound"), OS_ERR_NAME_NOT_FOUND, "name not found");
276+
UT_RETVAL(OS_BinSemGetIdByName(&bin_sem_id, "NameNotFound"), OS_ERR_NAME_NOT_FOUND);
307277

308278
/*-----------------------------------------------------*/
309279
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GetIDByName", 1, 0)))
@@ -328,16 +298,12 @@ void UT_os_bin_sem_get_info_test()
328298
OS_bin_sem_prop_t bin_sem_prop;
329299

330300
/*-----------------------------------------------------*/
331-
if (!UT_IMPL(OS_BinSemGetInfo(OS_OBJECT_ID_UNDEFINED, &bin_sem_prop)))
332-
return;
333-
334-
/*-----------------------------------------------------*/
335-
UT_RETVAL(OS_BinSemGetInfo(UT_OBJID_INCORRECT, &bin_sem_prop), OS_ERR_INVALID_ID, "invalid id");
301+
UT_RETVAL(OS_BinSemGetInfo(UT_OBJID_INCORRECT, &bin_sem_prop), OS_ERR_INVALID_ID);
336302

337303
/*-----------------------------------------------------*/
338304
if (UT_SETUP(OS_BinSemCreate(&bin_sem_id, "GetInfo", 1, 0)))
339305
{
340-
UT_RETVAL(OS_BinSemGetInfo(bin_sem_id, NULL), OS_INVALID_POINTER, "invalid ptr");
306+
UT_RETVAL(OS_BinSemGetInfo(bin_sem_id, NULL), OS_INVALID_POINTER);
341307
UT_TEARDOWN(OS_BinSemDelete(bin_sem_id));
342308
}
343309

0 commit comments

Comments
 (0)