28 #if !defined(_WINDOWS) 30 #include <sys/socket.h> 38 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 64 if (strcmp(argv[count],
"--test_no") == 0)
71 else if (strcmp(argv[count],
"--iterations") == 0)
78 else if (strcmp(argv[count],
"--verbose") == 0)
88 #include <sys/timeb.h> 93 #if defined(_WIN32) || defined(_WINDOWS) 103 #if defined(_WIN32) || defined(_WINDOWS) 107 gettimeofday(&ts, NULL);
110 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", timeinfo);
112 #if defined(_WIN32) || defined(_WINDOWS) 113 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
115 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
118 va_start(args, format);
119 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
122 printf(
"%s\n", msg_buf);
127 #if defined(_WIN32) || defined(_WINDOWS) 128 #define mysleep(A) Sleep(1000*A) 129 #define START_TIME_TYPE DWORD 130 static DWORD start_time = 0;
133 return GetTickCount();
136 #define mysleep sleep 137 #define START_TIME_TYPE struct timespec 140 static struct timespec start;
141 clock_gettime(CLOCK_REALTIME, &start);
145 #define mysleep sleep 146 #define START_TIME_TYPE struct timeval 150 struct timeval start_time;
151 gettimeofday(&start_time, NULL);
160 return GetTickCount() - start_time;
164 long elapsed(
struct timespec start)
166 struct timespec now, res;
168 clock_gettime(CLOCK_REALTIME, &now);
169 ntimersub(now, start, res);
170 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
175 struct timeval now, res;
177 gettimeofday(&now, NULL);
178 timersub(&now, &start_time, &res);
179 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
183 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 184 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 197 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
203 fprintf(
xml,
"</testcase>\n");
214 printf(
"Assertion failed, file %s, line %d, description: %s, ", filename, lineno, description);
216 va_start(args, format);
217 vprintf(format, args);
223 description, filename, lineno);
226 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
239 assert(
"rc 0 from check_sem", rc == 0,
"rc was %d", rc);
245 assert(
"rc 0 from lock mutex", rc == 0,
"rc was %d", rc);
247 assert(
"duration is 2s", duration >= 2000L,
"duration was %ld", duration);
256 char* testname =
"test_sem";
264 fprintf(
xml,
"<testcase classname=\"test\" name=\"%s\"", testname);
271 assert(
"rc 0 from check_sem", rc == 0,
"rc was %d\n", rc);
275 assert(
"rc 0 from post_sem", rc == 0,
"rc was %d\n", rc);
279 assert(
"rc 1 from check_sem", rc == 1,
"rc was %d", rc);
283 assert(
"rc 0 from check_sem", rc == 0,
"rc was %d", rc);
286 for (i = 0; i < 10; ++i)
289 assert(
"rc 0 from post_sem", rc == 0,
"rc was %d\n", rc);
292 for (i = 0; i < 10; ++i)
295 assert(
"rc 1 from check_sem", rc == 1,
"rc was %d", rc);
298 assert(
"rc 0 from check_sem", rc == 0,
"rc was %d", rc);
304 assert(
"rc ETIMEDOUT from lock mutex", rc == ETIMEDOUT,
"rc was %d", rc);
306 assert(
"duration is 2s", duration >= 1500L,
"duration was %ld", duration);
314 assert(
"rc 1 from post_sem", rc == 1,
"rc was %d", rc);
327 #if !defined(_WIN32) && !defined(_WIN64) 340 assert(
"duration is about 1s", duration >= 1000L && duration <= 1050L,
"duration was %ld", duration);
341 assert(
"rc non 0 from wait_cond", rc == ETIMEDOUT,
"rc was %d", rc);
348 assert(
"duration is around 1s", duration >= 990L && duration <= 1010L,
"duration was %ld", duration);
349 assert(
"rc 9 from wait_cond", rc == 0,
"rc was %d", rc);
358 char* testname =
"test_cond";
366 fprintf(
xml,
"<testcase classname=\"cond\" name=\"%s\"", testname);
371 assert(
"rc 0 from wait_cond", rc == ETIMEDOUT,
"rc was %d", rc);
377 assert(
"rc ETIMEDOUT from lock mutex", rc == ETIMEDOUT,
"rc was %d", rc);
379 assert(
"duration is 2s", duration >= 2000L,
"duration was %ld", duration);
382 for (i = 0; i < 10; ++i)
385 assert(
"rc 0 from signal cond", rc == 0,
"rc was %d\n", rc);
389 for (i = 0; i < 10; ++i)
392 assert(
"rc non-zero from wait_cond", rc == ETIMEDOUT,
"rc was %d", rc);
395 assert(
"rc non-zero from wait_cond", rc == ETIMEDOUT,
"rc was %d", rc);
399 assert(
"rc 0 from signal cond", rc == 0,
"rc was %d", rc);
409 assert(
"rc 0 from signal cond", rc == 0,
"rc was %d", rc);
435 assert(
"rc 0 from lock mutex", rc == 0,
"rc was %d", rc);
437 assert(
"duration is 2s", duration >= 1000L,
"duration was %ld", duration);
440 assert(
"rc 0 from unlock mutex", rc == 0,
"rc was %d", rc);
448 char* testname =
"test_mutex";
456 fprintf(
xml,
"<testcase classname=\"test\" name=\"%s\"", testname);
463 assert(
"rc 0 from lock mutex", rc == 0,
"rc was %d", rc);
465 assert(
"duration is very low", duration < 5L,
"duration was %ld", duration);
472 assert(
"rc 0 from unlock mutex", rc == 0,
"rc was %d", rc);
477 assert(
"rc 0 from lock mutex", rc == 0,
"rc was %d", rc);
479 assert(
"duration is very low", duration < 5L,
"duration was %ld", duration);
494 int main(
int argc,
char** argv)
497 int (*
tests[])() = {NULL,
500 #if !defined(_WIN32) && !defined(_WIN64) 506 xml = fopen(
"TEST-thread.xml",
"w");
507 fprintf(
xml,
"<testsuite name=\"thread\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests)) - 1);
537 fprintf(
xml,
"</testsuite>\n");
int Thread_post_sem(sem_type sem)
int Thread_wait_cond(cond_type condvar, int timeout)
enum MQTTPropertyCodes value
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
sem_type Thread_create_sem(int *rc)
int main(int argc, char **argv)
int Thread_lock_mutex(mutex_type mutex)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
long elapsed(START_TIME_TYPE start_time)
std::tm localtime(std::time_t time)
void write_test_result(void)
static int cond(LexState *ls)
mutex_type Thread_create_mutex(int *rc)
basic_thread< reference > thread
static thread_return_type WINAPI mutex_secondary(void *n)
thread_return_type cond_secondary(void *n)
void MyLog(int LOGA_level, char *format,...)
static thread_return_type WINAPI sem_secondary(void *n)
int Thread_unlock_mutex(mutex_type mutex)
#define assert(a, b, c, d)
START_TIME_TYPE start_clock(void)
int test_cond(struct Options options)
int Thread_wait_sem(sem_type sem, int timeout)
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
cond_type Thread_create_cond(int *rc)
#define thread_return_type
START_TIME_TYPE global_start_time
int Thread_check_sem(sem_type sem)
int test_sem(struct Options options)
int Thread_signal_cond(cond_type condvar)
void getopts(int argc, char **argv)
thread_type Thread_start(thread_fn fn, void *parameter)
int test_mutex(struct Options options)
int Thread_destroy_mutex(mutex_type mutex)