From dab526936f8146b02bffae4f40b5536341a55472 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 23 Feb 2018 09:55:57 +0000 Subject: [PATCH] Add missing test files to mongoose repo PUBLISHED_FROM=9a81cc34aa0039a75c851bc3685055f46936252e --- src/common/test_main.c | 71 ++++++++++++++++++++++++++++++++++++++++++ src/common/test_main.h | 22 +++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/common/test_main.c create mode 100644 src/common/test_main.h diff --git a/src/common/test_main.c b/src/common/test_main.c new file mode 100644 index 00000000..53116d92 --- /dev/null +++ b/src/common/test_main.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014-2018 Cesanta Software Limited + * All rights reserved + */ + +#include "common/cs_dbg.h" +#include "common/test_main.h" +#include "common/test_util.h" + +#if defined(_MSC_VER) && _MSC_VER >= 1900 +#include + +int __cdecl CrtDbgHook(int nReportType, char *szMsg, int *pnRet) { + (void) nReportType; + (void) szMsg; + (void) pnRet; + + fprintf(stderr, "CRT debug hook: type: %d, msg: %s\n", nReportType, szMsg); + /* Return true - Abort,Retry,Ignore dialog will *not* be displayed */ + return 1; +} +#endif + +#ifndef __cdecl +#define __cdecl +#endif + +int g_num_tests = 0; +int g_num_checks = 0; +const char *g_argv_0 = NULL; + +int __cdecl main(int argc, char *argv[]) { + const char *fail_msg; + const char *filter = argc > 1 ? argv[1] : ""; + char *seed_str = getenv("TEST_SEED"); + double started; + int seed = 0; + + if (seed_str != NULL) { + seed = atoi(seed_str); + } else { + seed = (int) time(NULL); + } + printf("seed: %d\n", seed); + srand(seed); + + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); +#if defined(_MSC_VER) && _MSC_VER >= 1900 + /* NOTE: not available on wine/vc6 */ + _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, CrtDbgHook); +#endif + + g_argv_0 = argv[0]; + + tests_setup(); + + started = cs_time(); + fail_msg = tests_run(filter); + printf("%s, ran %d tests (%d checks) in %.3lfs\n", fail_msg ? "FAIL" : "PASS", + g_num_tests, g_num_checks, cs_time() - started); + + tests_teardown(); + + if (fail_msg != NULL) { + /* Prevent leak analyzer from running: there will be "leaks" because of + * premature return from the test, and in this case we don't care. */ + _exit(EXIT_FAILURE); + } + return EXIT_SUCCESS; +} diff --git a/src/common/test_main.h b/src/common/test_main.h new file mode 100644 index 00000000..6b6f5f39 --- /dev/null +++ b/src/common/test_main.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2014-2018 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_COMMON_TEST_MAIN_H_ +#define CS_COMMON_TEST_MAIN_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void tests_setup(void); +const char *tests_run(const char *filter); +void tests_teardown(void); +extern const char *g_argv_0; + +#ifdef __cplusplus +} +#endif + +#endif /* CS_COMMON_TEST_MAIN_H_ */