crt: tests: add call to mingw_test_init() from test programs

Signed-off-by: Kirill Makurin <maiddaisuki@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 2e0d7b2..cdf3c1a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -633,7 +633,7 @@
             ${{matrix.arch}}-w64-mingw32-objdump -d t_intrinc.o | grep -iE 'cmpxchg|xadd'
             exit 1
           fi
-          ${{matrix.arch}}-w64-mingw32-clang -std=c99 -march=i386 -o t_intrinc.exe mingw-w64-crt/testcases/t_intrinc.c
+          ${{matrix.arch}}-w64-mingw32-clang -std=c99 -march=i386 -o t_intrinc.exe mingw-w64-crt/testcases/t_intrinc.c mingw-w64-crt/testcases/libtest.c
           ./t_intrinc.exe
       - name: Run mingw-w64-crt testcases
         if: matrix.arch != 'armv7'
@@ -716,7 +716,7 @@
         if: matrix.arch == 'i686'
         run: |
           export PATH=/gcc-mingw/bin:$PATH
-          ${{matrix.arch}}-w64-mingw32-gcc -std=c99 -march=i386 -o t_intrinc.exe mingw-w64-crt/testcases/t_intrinc.c
+          ${{matrix.arch}}-w64-mingw32-gcc -std=c99 -march=i386 -o t_intrinc.exe mingw-w64-crt/testcases/t_intrinc.c mingw-w64-crt/testcases/libtest.c
           ./t_intrinc.exe
       - name: Build and test winpthreads
         run: |
diff --git a/mingw-w64-crt/testcases/t__fstat_all.c b/mingw-w64-crt/testcases/t__fstat_all.c
index 9338ade..3893faa 100644
--- a/mingw-w64-crt/testcases/t__fstat_all.c
+++ b/mingw-w64-crt/testcases/t__fstat_all.c
@@ -3,6 +3,9 @@
 #include <string.h>
 #include <sys/stat.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
 	int ret;
 	struct _stat st = {};
@@ -10,6 +13,9 @@
 	struct _stat32i64 st32i64 = {};
 	struct _stat64 st64 = {};
 	struct _stat64i32 st64i32 = {};
+
+	mingw_test_init ();
+
 	errno = 0;
 	ret = _fstat(0, &st);
 	printf("_fstat:\n");
diff --git a/mingw-w64-crt/testcases/t__stat_all.c b/mingw-w64-crt/testcases/t__stat_all.c
index 0d46312..8c9e5f4 100644
--- a/mingw-w64-crt/testcases/t__stat_all.c
+++ b/mingw-w64-crt/testcases/t__stat_all.c
@@ -3,6 +3,9 @@
 #include <string.h>
 #include <sys/stat.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char *argv[]) {
 	int ret;
 	struct _stat st = {};
@@ -10,6 +13,9 @@
 	struct _stat32i64 st32i64 = {};
 	struct _stat64 st64 = {};
 	struct _stat64i32 st64i32 = {};
+
+	mingw_test_init ();
+
 	errno = 0;
 	ret = _stat(argc >= 2 ? argv[1] : argv[0], &st);
 	printf("_stat:\n");
diff --git a/mingw-w64-crt/testcases/t__sys_errlist.c b/mingw-w64-crt/testcases/t__sys_errlist.c
index bff2d7f..8f441c5 100644
--- a/mingw-w64-crt/testcases/t__sys_errlist.c
+++ b/mingw-w64-crt/testcases/t__sys_errlist.c
@@ -6,9 +6,16 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
-  char **s = (char **) _sys_errlist;
+  char **s;
+
+  mingw_test_init ();
+
+  s = _sys_errlist;
 
   if (s) {
     printf ("Msg 0: %s\n", s[0]);
diff --git a/mingw-w64-crt/testcases/t__wstat_all.c b/mingw-w64-crt/testcases/t__wstat_all.c
index 5fd8580..747bf4e 100644
--- a/mingw-w64-crt/testcases/t__wstat_all.c
+++ b/mingw-w64-crt/testcases/t__wstat_all.c
@@ -4,6 +4,9 @@
 #include <sys/stat.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int wmain(int argc, wchar_t *wargv[]) {
 	int ret;
 	struct _stat st = {};
@@ -11,6 +14,9 @@
 	struct _stat32i64 st32i64 = {};
 	struct _stat64 st64 = {};
 	struct _stat64i32 st64i32 = {};
+
+	mingw_test_init ();
+
 	errno = 0;
 	ret = _wstat(argc >= 2 ? wargv[1] : wargv[0], &st);
 	printf("_wstat:\n");
diff --git a/mingw-w64-crt/testcases/t_aligned_alloc.c b/mingw-w64-crt/testcases/t_aligned_alloc.c
index c29b920..7061ae7 100644
--- a/mingw-w64-crt/testcases/t_aligned_alloc.c
+++ b/mingw-w64-crt/testcases/t_aligned_alloc.c
@@ -4,6 +4,9 @@
 #include <stdio.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define TEST(xfail, a, b, msg, ...) do { \
   if ((a) != (b)) { \
     fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \
@@ -49,6 +52,7 @@
   void *ptr;
   size_t size;
 
+  mingw_test_init ();
 
   /*
    * Surprisingly this malloc/_msize test is failing on msvcrt10.dll and msvcrt20.dll
diff --git a/mingw-w64-crt/testcases/t_ansi_io.c b/mingw-w64-crt/testcases/t_ansi_io.c
index 611b34f..bb2b1d3 100644
--- a/mingw-w64-crt/testcases/t_ansi_io.c
+++ b/mingw-w64-crt/testcases/t_ansi_io.c
@@ -3,8 +3,13 @@
 #include <stdint.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main (int argc, char **argv)
 {
+  mingw_test_init ();
+
   printf ("%zd %p\n", (intptr_t) argc, argv[0]);
   fprintf (stdout, "%zd %p %" PRIuPTR "\n", (intptr_t) argc, argv[0], (intptr_t) argv[0]);
 
diff --git a/mingw-w64-crt/testcases/t_assert.c b/mingw-w64-crt/testcases/t_assert.c
index 876a6fd..e66c406 100644
--- a/mingw-w64-crt/testcases/t_assert.c
+++ b/mingw-w64-crt/testcases/t_assert.c
@@ -6,7 +6,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char *argv[]) {
+    mingw_test_init ();
+
     if (argc != 2 || strcmp(argv[1], "assert_test") != 0) {
         int exit_code;
         int pipefd[2];
@@ -46,9 +51,6 @@
     _setmode(fileno(stderr), _O_U8TEXT);
 
     /* call assert, it prints to stderr, parent process will check that our stderr is not empty */
-#if __MSVCRT_VERSION__ > 0x800
-    _set_abort_behavior(0, _CALL_REPORTFAULT); /* assert() will not call Dr. Watson */
-#endif
     assert(0);
 
     /* assert(0) does not return, this process pass when returns non-zero */
diff --git a/mingw-w64-crt/testcases/t_btowc.c b/mingw-w64-crt/testcases/t_btowc.c
index a6916f0..f81145b 100644
--- a/mingw-w64-crt/testcases/t_btowc.c
+++ b/mingw-w64-crt/testcases/t_btowc.c
@@ -13,7 +13,12 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main (void) {
+  mingw_test_init ();
+
   /**
    * Test "C" locale
    */
diff --git a/mingw-w64-crt/testcases/t_char16_t.c b/mingw-w64-crt/testcases/t_char16_t.c
index a3159eb..315b737 100644
--- a/mingw-w64-crt/testcases/t_char16_t.c
+++ b/mingw-w64-crt/testcases/t_char16_t.c
@@ -1,8 +1,13 @@
 #include <stdlib.h>
 #include <uchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main (void)
 {
+	mingw_test_init ();
+
 	/* wchar_t should be compatible with char16_t on Windows */
 	if (sizeof(wchar_t) != sizeof(char16_t))
 		abort ();
diff --git a/mingw-w64-crt/testcases/t_ctype.c b/mingw-w64-crt/testcases/t_ctype.c
index 29048c8..8de6c83 100644
--- a/mingw-w64-crt/testcases/t_ctype.c
+++ b/mingw-w64-crt/testcases/t_ctype.c
@@ -10,7 +10,9 @@
 #include <locale.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
 #include "ctype-test.h"
+#include "libtest.h"
 
 /**
  * Test conformance of ctype.h functions to POSIX specification.
@@ -19,6 +21,8 @@
  */
 
 int main (void) {
+  mingw_test_init ();
+
   assert (setlocale (LC_ALL, "C") != NULL);
 
   for (int i = 0; i < (int) _countof (CharTypeTable); ++i) {
diff --git a/mingw-w64-crt/testcases/t_excpt_gcc_cpp.c b/mingw-w64-crt/testcases/t_excpt_gcc_cpp.c
index ffc0410..d2713b5 100644
--- a/mingw-w64-crt/testcases/t_excpt_gcc_cpp.c
+++ b/mingw-w64-crt/testcases/t_excpt_gcc_cpp.c
@@ -5,6 +5,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define STATUS_USER_DEFINED     (1U << 29)
 #define GCC_MAGIC               (('G' << 16) | ('C' << 8) | 'C')
 #define GCC_EXCEPTION(TYPE)     (STATUS_USER_DEFINED | ((TYPE) << 24) | GCC_MAGIC)
@@ -75,6 +78,8 @@
   pthread_t pthread;
   HANDLE wthread;
 
+  mingw_test_init ();
+
   {
     int tret = test();
     if (tret)
diff --git a/mingw-w64-crt/testcases/t_excpt_int_divzero.c b/mingw-w64-crt/testcases/t_excpt_int_divzero.c
index 70ba2ea..940eb13 100644
--- a/mingw-w64-crt/testcases/t_excpt_int_divzero.c
+++ b/mingw-w64-crt/testcases/t_excpt_int_divzero.c
@@ -5,6 +5,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /* This test is run as XFAIL, so it has to return non-zero on success (as the crash is expected) and zero on error */
 #define FAILURE 0
 
@@ -28,7 +31,7 @@
   puts("Division by zero is ignored on ARM");
   return 77;
 #endif
-  SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
+  mingw_test_init ();
 #ifdef RUN_TEST_IN_THREAD
   HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, test, NULL, 0, &(unsigned){0} /*out: ThreadId*/);
   if (!thread)
diff --git a/mingw-w64-crt/testcases/t_excpt_int_overflow.c b/mingw-w64-crt/testcases/t_excpt_int_overflow.c
index 0e2594a..9ce2480 100644
--- a/mingw-w64-crt/testcases/t_excpt_int_overflow.c
+++ b/mingw-w64-crt/testcases/t_excpt_int_overflow.c
@@ -5,6 +5,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /* This test is run as XFAIL, so it has to return non-zero on success (as the crash is expected) and zero on error */
 #define FAILURE 0
 
@@ -28,7 +31,7 @@
   puts("Division overflow is ignored on ARM");
   return 77;
 #endif
-  SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
+  mingw_test_init ();
 #ifdef RUN_TEST_IN_THREAD
   HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, test, NULL, 0, &(unsigned){0} /*out: ThreadId*/);
   if (!thread)
diff --git a/mingw-w64-crt/testcases/t_exp_denormal.c b/mingw-w64-crt/testcases/t_exp_denormal.c
index 91402ba..1077246 100644
--- a/mingw-w64-crt/testcases/t_exp_denormal.c
+++ b/mingw-w64-crt/testcases/t_exp_denormal.c
@@ -1,8 +1,12 @@
 #include <math.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main()
 {
+  mingw_test_init ();
   printf ("%g\n", exp (-709.));
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_expm1.c b/mingw-w64-crt/testcases/t_expm1.c
index da634a2..84c9730 100644
--- a/mingw-w64-crt/testcases/t_expm1.c
+++ b/mingw-w64-crt/testcases/t_expm1.c
@@ -7,6 +7,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define STR_VALUE(arg) #arg
 #define MINUS_ZERO -0.
 #define N_TESTS 5
@@ -15,6 +18,8 @@
 {
   int res = 0;
 
+  mingw_test_init ();
+
 #define TEST(func, dtype, fmt) do { \
     int i; \
     char fmt_str[20]; \
diff --git a/mingw-w64-crt/testcases/t_fctptr.c b/mingw-w64-crt/testcases/t_fctptr.c
index 2e97c3a..45e145d 100644
--- a/mingw-w64-crt/testcases/t_fctptr.c
+++ b/mingw-w64-crt/testcases/t_fctptr.c
@@ -1,10 +1,14 @@
 #include <assert.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 void (__cdecl *exit_ptr)(int) = exit;
 
 int main()
 {
+  mingw_test_init ();
   assert (exit_ptr != NULL);
   exit_ptr (EXIT_SUCCESS);
   _exit (EXIT_FAILURE);
diff --git a/mingw-w64-crt/testcases/t_findfirst.c b/mingw-w64-crt/testcases/t_findfirst.c
index 998c3a7..0aff53a 100644
--- a/mingw-w64-crt/testcases/t_findfirst.c
+++ b/mingw-w64-crt/testcases/t_findfirst.c
@@ -4,6 +4,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define A(x) x
 #define W(x) L##x
 
@@ -49,6 +52,7 @@
 int main(int argc, char **argv)
 {
   int ret = 0;
+  mingw_test_init ();
   ret |= test ();
   ret |= testi64 ();
   ret |= test32 ();
diff --git a/mingw-w64-crt/testcases/t_float.c b/mingw-w64-crt/testcases/t_float.c
index fc06599..0ea3d99 100644
--- a/mingw-w64-crt/testcases/t_float.c
+++ b/mingw-w64-crt/testcases/t_float.c
@@ -3,6 +3,9 @@
 #include <math.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 volatile long double in1 = -1.0L / 0.0L;
 volatile long double in2 = 1.0L / 0.0L;
 
@@ -53,6 +56,8 @@
 
 int main()
 {
+  mingw_test_init ();
+
   printf ("sizeof(float)=%"printfmt", sizeof(double)=%"printfmt", sizeof(long double)=%"printfmt"\n", sizeof (float), sizeof(double), sizeof(long double));
   printf ("%g %g %g\n", (double) ld1, (double) ld2, (double) ld3);
 
diff --git a/mingw-w64-crt/testcases/t_fmod.c b/mingw-w64-crt/testcases/t_fmod.c
index 88f548b..7e94f9b 100644
--- a/mingw-w64-crt/testcases/t_fmod.c
+++ b/mingw-w64-crt/testcases/t_fmod.c
@@ -2,11 +2,16 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main()
 {
  int res = 0;
  int i;
 
+ mingw_test_init ();
+
  for (i = 0; i < 5; i++)
    {
      double m;
diff --git a/mingw-w64-crt/testcases/t_format_Z_tmpl.h b/mingw-w64-crt/testcases/t_format_Z_tmpl.h
index 76b16e5..1fe7754 100644
--- a/mingw-w64-crt/testcases/t_format_Z_tmpl.h
+++ b/mingw-w64-crt/testcases/t_format_Z_tmpl.h
@@ -3,6 +3,9 @@
 #include <wchar.h>
 #include <ntdef.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define ARRAY_COUNT(a) (sizeof(a)/sizeof(a[0]))
 #define DEF_STRING(str) { sizeof(str)-sizeof(str[0]), sizeof(str)-sizeof(str[0]), str }
 
@@ -38,6 +41,8 @@
 
 	(void)ws1; /* avoid "warning: unused variable" on some builds */
 
+	mingw_test_init ();
+
 	/*
 	 * These tests do not work with msvcrt20.dll, msvcrt40.dll and msvcr40d.dll
 	 * which do not support %Z format. All older and new CRT libraries support it.
diff --git a/mingw-w64-crt/testcases/t_fseeki64.c b/mingw-w64-crt/testcases/t_fseeki64.c
index d45b92a..ab997d5 100644
--- a/mingw-w64-crt/testcases/t_fseeki64.c
+++ b/mingw-w64-crt/testcases/t_fseeki64.c
@@ -7,6 +7,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define assert_winapi_seek(handle, offset, method) do { \
     LARGE_INTEGER li = { .QuadPart = (offset) }; \
     li.LowPart = SetFilePointer((handle), li.LowPart, &li.HighPart, (method)); \
@@ -31,6 +34,8 @@
     char buf[5];
     DWORD len;
 
+    mingw_test_init ();
+
     /* create temporary file which is automatically deleted when process terminates */
     file = tmpfile();
     assert(file);
diff --git a/mingw-w64-crt/testcases/t_fseeki64_ftelli64.c b/mingw-w64-crt/testcases/t_fseeki64_ftelli64.c
index f1bf096..f983298 100644
--- a/mingw-w64-crt/testcases/t_fseeki64_ftelli64.c
+++ b/mingw-w64-crt/testcases/t_fseeki64_ftelli64.c
@@ -1,9 +1,14 @@
 #include <assert.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
     FILE *file;
 
+    mingw_test_init ();
+
     /* create temporary file which is automatically deleted when process terminates */
     file = tmpfile();
     assert(file);
diff --git a/mingw-w64-crt/testcases/t_fseeko64.c b/mingw-w64-crt/testcases/t_fseeko64.c
index 71a1ece..4243ecb 100644
--- a/mingw-w64-crt/testcases/t_fseeko64.c
+++ b/mingw-w64-crt/testcases/t_fseeko64.c
@@ -7,6 +7,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 static const char *writebuf = "TESTVECTORSTRING";
 #define TMPTMPL "mingw-w64-fseeko64-XXXXXX"
 static char szPath[MAX_PATH + sizeof(TMPTMPL)];
@@ -46,6 +49,8 @@
   char *readbuff;
   int ret;
 
+  mingw_test_init ();
+
   readbuff = calloc(strlen(writebuf) + 1, sizeof(char));
   if (!readbuff) return 1;
 
diff --git a/mingw-w64-crt/testcases/t_fstat.c b/mingw-w64-crt/testcases/t_fstat.c
index 8f5890d..9500009 100644
--- a/mingw-w64-crt/testcases/t_fstat.c
+++ b/mingw-w64-crt/testcases/t_fstat.c
@@ -8,6 +8,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define TMPTMPL "mingw-w64-fstat-XXXXXX"
 static char dirpath[MAX_PATH + sizeof(TMPTMPL)];
 
@@ -20,6 +23,8 @@
     HANDLE handle;
     int dirfd;
 
+    mingw_test_init ();
+
     assert(fstat(0, &st) == 0);
     printf("fstat(0): mode = %08o\n", st.st_mode);
     assert(!S_ISDIR(st.st_mode));
diff --git a/mingw-w64-crt/testcases/t_imagebase.c b/mingw-w64-crt/testcases/t_imagebase.c
index ac333cf..d4c7113 100644
--- a/mingw-w64-crt/testcases/t_imagebase.c
+++ b/mingw-w64-crt/testcases/t_imagebase.c
@@ -1,9 +1,13 @@
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 extern unsigned int __ImageBase;
 
 int main()
 {
+  mingw_test_init ();
   printf ("%x\n", __ImageBase);
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_intrinc.c b/mingw-w64-crt/testcases/t_intrinc.c
index 3f9894d..f572870 100644
--- a/mingw-w64-crt/testcases/t_intrinc.c
+++ b/mingw-w64-crt/testcases/t_intrinc.c
@@ -3,6 +3,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define TEST(cond, msg, ...) do { \
   if (!(cond)) { \
     fprintf(stderr, "%s:%d: " msg "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
@@ -16,6 +19,8 @@
   void *ptr;
   void *old;
 
+  mingw_test_init ();
+
   /* InterlockedIncrement */
   val = 0;
   TEST(InterlockedIncrement(&val) == 1, "InterlockedIncrement: expected return 1, got %ld", val);
diff --git a/mingw-w64-crt/testcases/t_iostream.cpp b/mingw-w64-crt/testcases/t_iostream.cpp
index 3537937..1a2b79e 100644
--- a/mingw-w64-crt/testcases/t_iostream.cpp
+++ b/mingw-w64-crt/testcases/t_iostream.cpp
@@ -1,5 +1,9 @@
 #include <iostream>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 int main() {
+  mingw_test_init ();
   std::cout << "Hello!" << std::endl; return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_isfine.cpp b/mingw-w64-crt/testcases/t_isfine.cpp
index b76cf38..19c1292 100644
--- a/mingw-w64-crt/testcases/t_isfine.cpp
+++ b/mingw-w64-crt/testcases/t_isfine.cpp
@@ -2,6 +2,9 @@
 #include <cstdlib>
 #include <math.h>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 class f {
 public:
   f(float a) { x = a; }
@@ -14,6 +17,8 @@
 
 int main(int argc, char **argv)
 {
+  mingw_test_init ();
+
   f m((float) argc);
 
   if (!m.is_finite ())
diff --git a/mingw-w64-crt/testcases/t_iswctype.c b/mingw-w64-crt/testcases/t_iswctype.c
index a25aecc..7f483c4 100644
--- a/mingw-w64-crt/testcases/t_iswctype.c
+++ b/mingw-w64-crt/testcases/t_iswctype.c
@@ -9,6 +9,9 @@
 #include <locale.h>
 #include <wctype.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * Verify that `iswctype` is consistent with class-specific wctype.h functions.
  *
@@ -20,6 +23,8 @@
  */
 
 int main (void) {
+  mingw_test_init ();
+
   if (setlocale (LC_ALL, "English_United States") == NULL) {
     return 77;
   }
diff --git a/mingw-w64-crt/testcases/t_lfs.c b/mingw-w64-crt/testcases/t_lfs.c
index 114d0e3..4679485 100644
--- a/mingw-w64-crt/testcases/t_lfs.c
+++ b/mingw-w64-crt/testcases/t_lfs.c
@@ -4,10 +4,14 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char *argv[]) {
     int fd;
     FILE *file;
     fpos_t pos;
+    mingw_test_init ();
     ftello64(stdin);
     _ftelli64(stdin);
     fgetpos64(stdin, &pos);
diff --git a/mingw-w64-crt/testcases/t_lseeki64.c b/mingw-w64-crt/testcases/t_lseeki64.c
index 7cf085e..608d06e 100644
--- a/mingw-w64-crt/testcases/t_lseeki64.c
+++ b/mingw-w64-crt/testcases/t_lseeki64.c
@@ -2,10 +2,15 @@
 #include <io.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
     FILE *file;
     int fd;
 
+    mingw_test_init ();
+
     /* create temporary file which is automatically deleted when process terminates */
     file = tmpfile();
     assert(file);
diff --git a/mingw-w64-crt/testcases/t_main.c b/mingw-w64-crt/testcases/t_main.c
index a92efaa..f9e8844 100644
--- a/mingw-w64-crt/testcases/t_main.c
+++ b/mingw-w64-crt/testcases/t_main.c
@@ -6,8 +6,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
+  mingw_test_init ();
   printf ("Hallo world!\n");
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_main_assert.c b/mingw-w64-crt/testcases/t_main_assert.c
index 0c0a3ff..3ab7172 100644
--- a/mingw-w64-crt/testcases/t_main_assert.c
+++ b/mingw-w64-crt/testcases/t_main_assert.c
@@ -7,8 +7,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
+  mingw_test_init ();
   assert (argc >= 1);
   assert (argv != NULL);
   assert (argv[0] != NULL);
diff --git a/mingw-w64-crt/testcases/t_main_cpp.cpp b/mingw-w64-crt/testcases/t_main_cpp.cpp
index a92efaa..cedeb77 100644
--- a/mingw-w64-crt/testcases/t_main_cpp.cpp
+++ b/mingw-w64-crt/testcases/t_main_cpp.cpp
@@ -6,8 +6,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
+  mingw_test_init ();
   printf ("Hallo world!\n");
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_matherr.c b/mingw-w64-crt/testcases/t_matherr.c
index 947b23e..e371287 100644
--- a/mingw-w64-crt/testcases/t_matherr.c
+++ b/mingw-w64-crt/testcases/t_matherr.c
@@ -1,10 +1,14 @@
 #include <math.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int _MINGW_INSTALL_DEBUG_MATHERR = 1;
 
 int main()
 {
+  mingw_test_init ();
   printf ("sqrt(-1)=%g\n", sqrt (-1.0));
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_mbrlen.c b/mingw-w64-crt/testcases/t_mbrlen.c
index bd7b196..f43fd6c 100644
--- a/mingw-w64-crt/testcases/t_mbrlen.c
+++ b/mingw-w64-crt/testcases/t_mbrlen.c
@@ -9,6 +9,9 @@
 #include <stdlib.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * This test is for mingw-w64's implementation of mbrlen function.
  *
@@ -33,9 +36,12 @@
 char InvalidMultibyte[] = {(char) 0x81, 0};
 
 int main (void) {
+  mingw_test_init ();
+
 #ifdef _UCRT
   return 77;
 #endif
+
   mbstate_t state = {0};
 
   /**
diff --git a/mingw-w64-crt/testcases/t_mbrtowc.c b/mingw-w64-crt/testcases/t_mbrtowc.c
index d13a194..a8e28db 100644
--- a/mingw-w64-crt/testcases/t_mbrtowc.c
+++ b/mingw-w64-crt/testcases/t_mbrtowc.c
@@ -9,6 +9,9 @@
 #include <stdlib.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * This test is for mingw-w64's implementation of mbrtowc function.
  *
@@ -33,9 +36,12 @@
 char InvalidMultibyte[] = {(char) 0x81, 0};
 
 int main (void) {
+  mingw_test_init ();
+
 #ifdef _UCRT
   return 77;
 #endif
+
   mbstate_t state = {0};
   wchar_t   wc = WEOF;
 
diff --git a/mingw-w64-crt/testcases/t_mbsrtowcs.c b/mingw-w64-crt/testcases/t_mbsrtowcs.c
index e9f60ec..b7b5a30 100644
--- a/mingw-w64-crt/testcases/t_mbsrtowcs.c
+++ b/mingw-w64-crt/testcases/t_mbsrtowcs.c
@@ -9,6 +9,9 @@
 #include <stdlib.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * This test is for mingw-w64's implementation of mbsrtowcs function.
  *
@@ -31,9 +34,12 @@
 unsigned char BadText[] = {0x93, 0xFA, 0x96, 0x7B, 0x8C, 0x0};
 
 int main (void) {
+  mingw_test_init ();
+
 #ifdef _UCRT
   return 77;
 #endif
+
   mbstate_t state = {0};
   wchar_t   buffer[BUFSIZ];
 
diff --git a/mingw-w64-crt/testcases/t_nullptrexception.c b/mingw-w64-crt/testcases/t_nullptrexception.c
index efd5ba8..cfa8409 100644
--- a/mingw-w64-crt/testcases/t_nullptrexception.c
+++ b/mingw-w64-crt/testcases/t_nullptrexception.c
@@ -3,11 +3,14 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 char *p = NULL;
 
 int main()
 {
-  SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
+  mingw_test_init ();
 
   printf ("Raise uncaught NULL pointer exception...\n");
   *p = 0;
diff --git a/mingw-w64-crt/testcases/t_pow.c b/mingw-w64-crt/testcases/t_pow.c
index 1bf7aee..5606af1 100644
--- a/mingw-w64-crt/testcases/t_pow.c
+++ b/mingw-w64-crt/testcases/t_pow.c
@@ -5,6 +5,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #ifndef __MSVCRT_VERSION__
 #define __MSVCRT_VERSION__ 0x0000
 #endif
@@ -92,7 +95,11 @@
 
 int main (int argc, char **argv)
 {
-  int e = (argc > 1 ? atoi(argv[1]) : 20000000);
+  int e;
+
+  mingw_test_init ();
+
+  e = (argc > 1 ? atoi(argv[1]) : 20000000);
 
   /**
    * Test `pow` function in libmingwex.
diff --git a/mingw-w64-crt/testcases/t_powi.c b/mingw-w64-crt/testcases/t_powi.c
index 636da4f..7c349ba 100644
--- a/mingw-w64-crt/testcases/t_powi.c
+++ b/mingw-w64-crt/testcases/t_powi.c
@@ -1,10 +1,17 @@
 #include <math.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
     int i;
-    double x, x2, z = pow(10.0, 15);
+    double x, x2, z;
+
+    mingw_test_init ();
+
+    z = pow(10.0, 15);
 
     for(i = -300; i < -290; i++) {
         x = pow(10.0, i);
diff --git a/mingw-w64-crt/testcases/t_printf.c b/mingw-w64-crt/testcases/t_printf.c
index 23516db..8baa115 100644
--- a/mingw-w64-crt/testcases/t_printf.c
+++ b/mingw-w64-crt/testcases/t_printf.c
@@ -1,7 +1,11 @@
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
+  mingw_test_init ();
   printf ("%I32d\n", argc);
   printf ("%I32d\n", (long) argc);
   printf ("%I64d\n", (long long) argc);
diff --git a/mingw-w64-crt/testcases/t_printf_g_width.c b/mingw-w64-crt/testcases/t_printf_g_width.c
index b056464..c407bf1 100644
--- a/mingw-w64-crt/testcases/t_printf_g_width.c
+++ b/mingw-w64-crt/testcases/t_printf_g_width.c
@@ -2,9 +2,14 @@
 #include <stdio.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(void) {
     char buffer[64];
 
+    mingw_test_init ();
+
     // Test the %g width specifier
     // This should output "(    100000)" with 4 leading spaces
     snprintf(buffer, sizeof(buffer), "(%10g)", 100000.0f);
diff --git a/mingw-w64-crt/testcases/t_readdir.c b/mingw-w64-crt/testcases/t_readdir.c
index 3182b9e..108c985 100644
--- a/mingw-w64-crt/testcases/t_readdir.c
+++ b/mingw-w64-crt/testcases/t_readdir.c
@@ -6,10 +6,17 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
   struct dirent *di;
-  DIR *h = opendir (".");
+  DIR *h;
+
+  mingw_test_init ();
+
+  h = opendir (".");
 
   if (!h)
     return 1;
diff --git a/mingw-w64-crt/testcases/t_safe_flush.c b/mingw-w64-crt/testcases/t_safe_flush.c
index a830e68..79f4068 100644
--- a/mingw-w64-crt/testcases/t_safe_flush.c
+++ b/mingw-w64-crt/testcases/t_safe_flush.c
@@ -10,6 +10,9 @@
 #include <windows.h>
 #include <versionhelpers.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define STRING "hello world!"
 
 /* child process */
@@ -137,6 +140,8 @@
 /* common */
 
 int main(int argc, char *argv[]) {
+    mingw_test_init ();
+
     if (!IsWindowsVistaOrGreater()) {
         /* This test requires at least Windows Vista (where
          * CRITICAL_SECTIONs were made shutdown-aware)
diff --git a/mingw-w64-crt/testcases/t_setjmp.c b/mingw-w64-crt/testcases/t_setjmp.c
index fe9c0a4..0a45577 100644
--- a/mingw-w64-crt/testcases/t_setjmp.c
+++ b/mingw-w64-crt/testcases/t_setjmp.c
@@ -1,6 +1,9 @@
 #include <setjmp.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 jmp_buf buf;
 
 void __attribute__((noreturn)) foo (void)
@@ -11,6 +14,8 @@
 
 int main ()
 {
+  mingw_test_init ();
+
   if (setjmp (buf))
     {
       printf ("Caught\n");
@@ -21,5 +26,6 @@
       printf ("Should not be reached.\n");
       return 1;
     }
+
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_sigfpe.c b/mingw-w64-crt/testcases/t_sigfpe.c
index 2e6225b..a77b5ee 100644
--- a/mingw-w64-crt/testcases/t_sigfpe.c
+++ b/mingw-w64-crt/testcases/t_sigfpe.c
@@ -15,6 +15,9 @@
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+
+/* mingw-w64 headers */
+#include "libtest.h"
 #endif
 
 #if defined(__i386__)
@@ -1209,6 +1212,10 @@
   HANDLE wthread;
 #endif
 
+#ifdef _WIN32
+  mingw_test_init ();
+#endif
+
 #if defined(_WIN32)
   main_threadid = GetCurrentThreadId();
 #endif
diff --git a/mingw-w64-crt/testcases/t_sigv.c b/mingw-w64-crt/testcases/t_sigv.c
index 08a069d..c504c89 100644
--- a/mingw-w64-crt/testcases/t_sigv.c
+++ b/mingw-w64-crt/testcases/t_sigv.c
@@ -3,6 +3,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 jmp_buf buf;
 
 #if defined(__i386__)
@@ -20,6 +23,8 @@
 {
  volatile int v;
 
+ mingw_test_init ();
+
  signal(SIGSEGV, catchSigSegV);
 
  if (!setjmp(buf))
diff --git a/mingw-w64-crt/testcases/t_snprintf_tmpl.h b/mingw-w64-crt/testcases/t_snprintf_tmpl.h
index 5e755b9..22d9ec6 100644
--- a/mingw-w64-crt/testcases/t_snprintf_tmpl.h
+++ b/mingw-w64-crt/testcases/t_snprintf_tmpl.h
@@ -1,10 +1,15 @@
 #include <stdio.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
   int i;
   char buffer[11] = "XXXXXXXXXX";
-  int ret = snprintf(buffer, 3, "%s", "AAA");
+  int ret;
+  mingw_test_init ();
+  ret = snprintf(buffer, 3, "%s", "AAA");
   if (ret != 3 || memcmp(buffer, "AA\0XXXXXXX", 10) != 0) {
     fprintf(stderr, "ret: expected=3 got=%d\n", ret);
     fprintf(stderr, "buffer:");
diff --git a/mingw-w64-crt/testcases/t_snwprintf_tmpl.h b/mingw-w64-crt/testcases/t_snwprintf_tmpl.h
index 15aff9d..c2babd6 100644
--- a/mingw-w64-crt/testcases/t_snwprintf_tmpl.h
+++ b/mingw-w64-crt/testcases/t_snwprintf_tmpl.h
@@ -1,10 +1,15 @@
 #include <stdio.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
   int i;
   wchar_t buffer[11] = L"XXXXXXXXXX";
-  int ret = snwprintf(buffer, 3, L"%ls", L"AAA");
+  int ret;
+  mingw_test_init ();
+  ret = snwprintf(buffer, 3, L"%ls", L"AAA");
   if (ret != 3 || wmemcmp(buffer, L"AA\0XXXXXXX", 10) != 0) {
     fprintf(stderr, "ret: expected=3 got=%d\n", ret);
     fprintf(stderr, "buffer:");
diff --git a/mingw-w64-crt/testcases/t_speed_powl.c b/mingw-w64-crt/testcases/t_speed_powl.c
index bd2dfd4..6ccc786 100644
--- a/mingw-w64-crt/testcases/t_speed_powl.c
+++ b/mingw-w64-crt/testcases/t_speed_powl.c
@@ -5,8 +5,13 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main()
 {
+  mingw_test_init ();
+
   long double ld = 1.0L;
   long double sum = 0.0L;
 
diff --git a/mingw-w64-crt/testcases/t_stat.c b/mingw-w64-crt/testcases/t_stat.c
index b6d6a55..3fa4edd 100644
--- a/mingw-w64-crt/testcases/t_stat.c
+++ b/mingw-w64-crt/testcases/t_stat.c
@@ -6,6 +6,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define _WIN32_WINNT_NT35 MAKEWORD(50, 3) /* really decimal, not hex */
 
 #define type(mode) (S_ISDIR(mode) ? "DIR" : S_ISFIFO(mode) ? "FIFO" : S_ISCHR(mode) ? "CHR" : S_ISBLK(mode) ? "BLK" : S_ISREG(mode) ? "REG" : "UNKN")
@@ -20,6 +23,8 @@
     unsigned ver;
     unsigned type;
 
+    mingw_test_init ();
+
     if (0 == stat(argv[0], &st))
         printf("mode = %x\n", st.st_mode);
     if (0 == stat64(argv[0], &st64))
diff --git a/mingw-w64-crt/testcases/t_stat_slash.c b/mingw-w64-crt/testcases/t_stat_slash.c
index 61b2b99..c43bb3d 100644
--- a/mingw-w64-crt/testcases/t_stat_slash.c
+++ b/mingw-w64-crt/testcases/t_stat_slash.c
@@ -6,6 +6,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 void
 test (char *path)
 {
@@ -36,7 +39,7 @@
   wchar_t windir_wbuf[MAX_PATH + 1];
   char windir_abuf[MAX_PATH + 1];
 
-  setvbuf (stdout, NULL, _IONBF, 0);
+  mingw_test_init ();
 
   if (GetWindowsDirectoryA(&windir_abuf[0], MAX_PATH))
   {
diff --git a/mingw-w64-crt/testcases/t_staticconstmember.cpp b/mingw-w64-crt/testcases/t_staticconstmember.cpp
index 5bebb43..479148a 100644
--- a/mingw-w64-crt/testcases/t_staticconstmember.cpp
+++ b/mingw-w64-crt/testcases/t_staticconstmember.cpp
@@ -1,5 +1,8 @@
 #include <stdio.h>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 struct Foobar {
   static const double var;
 };
@@ -8,6 +11,8 @@
 
 int main()
 {
+  mingw_test_init ();
+
   const char *ptr = (const char *) (&Foobar::var);
 
   for (int i = 0; i < 8; ++i) {
diff --git a/mingw-w64-crt/testcases/t_stderr_buffering.c b/mingw-w64-crt/testcases/t_stderr_buffering.c
index c7d8b23..a2498c7 100644
--- a/mingw-w64-crt/testcases/t_stderr_buffering.c
+++ b/mingw-w64-crt/testcases/t_stderr_buffering.c
@@ -9,9 +9,14 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define STRING "hello world!\n"
 
 int main(int argc, char *argv[]) {
+    mingw_test_init ();
+
     if (argc != 2 || strcmp(argv[1], "stderr_buffering_test") != 0) {
         int exit_code;
         int pipefd[2];
diff --git a/mingw-w64-crt/testcases/t_stdint.c b/mingw-w64-crt/testcases/t_stdint.c
index c2e39d4..4829ce8 100644
--- a/mingw-w64-crt/testcases/t_stdint.c
+++ b/mingw-w64-crt/testcases/t_stdint.c
@@ -2,8 +2,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main()
 {
+  mingw_test_init ();
+
   int check_UINT8_C  = (-1 < UINT8_C (0)) == (-1 < (uint_least8_t) 0) ? 1 : -1;
   int check_UINT16_C = (-1 < UINT16_C (0)) == (-1 < (uint_least16_t) 0) ? 1 : -1;
 
diff --git a/mingw-w64-crt/testcases/t_stprintf_tmpl.h b/mingw-w64-crt/testcases/t_stprintf_tmpl.h
index 88912e5..3082b91 100644
--- a/mingw-w64-crt/testcases/t_stprintf_tmpl.h
+++ b/mingw-w64-crt/testcases/t_stprintf_tmpl.h
@@ -3,6 +3,9 @@
 #include <tchar.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
   int i;
   TCHAR buffer[10] = { _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X'), _T('X') };
@@ -16,7 +19,9 @@
    * %hs takes char* string
    * %ls takes wchar_t* string
    */
-  int ret = _stprintf(buffer, _T("%sBB%hs%ls"), _T("AA"), "CC", L"DD");
+  int ret;
+  mingw_test_init ();
+  ret = _stprintf(buffer, _T("%sBB%hs%ls"), _T("AA"), "CC", L"DD");
   if (sizeof(buffer) != exp_sizeof_buffer) {
     fprintf(stderr, "sizeof(buffer): expected=%d got=%d\n", exp_sizeof_buffer, (int)sizeof(buffer));
     return 1;
diff --git a/mingw-w64-crt/testcases/t_swprintf_tmpl.h b/mingw-w64-crt/testcases/t_swprintf_tmpl.h
index e7c6fa1..a12cba9 100644
--- a/mingw-w64-crt/testcases/t_swprintf_tmpl.h
+++ b/mingw-w64-crt/testcases/t_swprintf_tmpl.h
@@ -1,10 +1,15 @@
 #include <stdio.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
   int i;
   wchar_t buffer[11] = L"XXXXXXXXXX";
-  int ret = swprintf(buffer, 3, L"%ls", L"AAA");
+  int ret;
+  mingw_test_init ();
+  ret = swprintf(buffer, 3, L"%ls", L"AAA");
   if (ret >= 0 || wmemcmp(buffer, L"AA\0XXXXXXX", 10) != 0) {
     fprintf(stderr, "ret: expected=<0 got=%d\n", ret);
     fprintf(stderr, "buffer:");
diff --git a/mingw-w64-crt/testcases/t_time.c b/mingw-w64-crt/testcases/t_time.c
index e17f9e8..ce663e5 100644
--- a/mingw-w64-crt/testcases/t_time.c
+++ b/mingw-w64-crt/testcases/t_time.c
@@ -5,6 +5,9 @@
 #include <sys/timeb.h>
 #include <time.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int __cdecl ftime32(struct __timeb32 *tb32);
 int __cdecl ftime64(struct __timeb64 *tb64);
 
@@ -25,6 +28,8 @@
   const char *str;
   const wchar_t *wstr;
 
+  mingw_test_init ();
+
   time (&t);
   _time32 (&t32);
   _time64 (&t64);
diff --git a/mingw-w64-crt/testcases/t_tls_c.c b/mingw-w64-crt/testcases/t_tls_c.c
index c9f4c69..8084630 100644
--- a/mingw-w64-crt/testcases/t_tls_c.c
+++ b/mingw-w64-crt/testcases/t_tls_c.c
@@ -2,6 +2,9 @@
 #include <stdlib.h>
 #include <time.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #if __STDC_VERSION__ >= 201112L
 #define __threadlocal__ _Thread_local
 #else
@@ -17,6 +20,8 @@
    */
   int x;
 
+  mingw_test_init ();
+
   srand (time (NULL));
   x = rand ();
   tvar += x;
diff --git a/mingw-w64-crt/testcases/t_tls_cpp.cpp b/mingw-w64-crt/testcases/t_tls_cpp.cpp
index 40e3403..59f4d5f 100644
--- a/mingw-w64-crt/testcases/t_tls_cpp.cpp
+++ b/mingw-w64-crt/testcases/t_tls_cpp.cpp
@@ -7,6 +7,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 int __thread tls_value = 0;
 
 unsigned __stdcall thread_fn(void*)
@@ -20,6 +23,8 @@
 // some value rather than tying it to the actual # of cores...
 int main()
 {
+    mingw_test_init ();
+
     // Get the number of processors available to use
     SYSTEM_INFO si;
     GetSystemInfo(&si);
diff --git a/mingw-w64-crt/testcases/t_tmpfile.c b/mingw-w64-crt/testcases/t_tmpfile.c
index 6b753ea..7edd8f0 100644
--- a/mingw-w64-crt/testcases/t_tmpfile.c
+++ b/mingw-w64-crt/testcases/t_tmpfile.c
@@ -2,9 +2,14 @@
 #include <direct.h>
 #include <stdio.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
     FILE *file;
 
+    mingw_test_init ();
+
     /* ensure that tmpfile() works also when cwd is in C:\Windows or C:\ but do not fail if caller has no access to C:\ */
     if (chdir("C:\\Windows\\") != 0)
         chdir("C:\\");
diff --git a/mingw-w64-crt/testcases/t_towctrans.c b/mingw-w64-crt/testcases/t_towctrans.c
index 978a105..d2e2b4a 100644
--- a/mingw-w64-crt/testcases/t_towctrans.c
+++ b/mingw-w64-crt/testcases/t_towctrans.c
@@ -8,6 +8,9 @@
 #include <locale.h>
 #include <wctype.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * Test Summary:
  *
@@ -22,6 +25,8 @@
  */
 
 int main (void) {
+  mingw_test_init ();
+
   if (setlocale (LC_ALL, "English_United States") == NULL) {
     return 77;
   }
diff --git a/mingw-w64-crt/testcases/t_trycatch.cpp b/mingw-w64-crt/testcases/t_trycatch.cpp
index 367178d..560d533 100644
--- a/mingw-w64-crt/testcases/t_trycatch.cpp
+++ b/mingw-w64-crt/testcases/t_trycatch.cpp
@@ -1,6 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 void foo (int d)
 {
   throw (d);
@@ -8,6 +11,8 @@
 
 int main()
 {
+  mingw_test_init ();
+
   try {
     foo (10);
     fwprintf (stderr, L"Failed to throw an exception?\n");
diff --git a/mingw-w64-crt/testcases/t_tsearch.c b/mingw-w64-crt/testcases/t_tsearch.c
index 2e97b67..4965259 100644
--- a/mingw-w64-crt/testcases/t_tsearch.c
+++ b/mingw-w64-crt/testcases/t_tsearch.c
@@ -4,6 +4,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * FIXME: calling `twalk` after `tdestroy` walks the tree as if tree was not
  *  destroyed. That is, it dereferences freed nodes.
@@ -45,6 +48,8 @@
  void *root = NULL;
  void *nodep;
 
+ mingw_test_init ();
+
  tsearch("aaa", &root, node_cmp);
  tsearch("bbb", &root, node_cmp);
  tsearch("ccc", &root, node_cmp);
diff --git a/mingw-w64-crt/testcases/t_utime.c b/mingw-w64-crt/testcases/t_utime.c
index 236c6d4..0b9e1a9 100644
--- a/mingw-w64-crt/testcases/t_utime.c
+++ b/mingw-w64-crt/testcases/t_utime.c
@@ -5,6 +5,9 @@
 #include <time.h>
 #include <utime.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main() {
     FILE *file;
     int fd;
@@ -15,6 +18,7 @@
     struct _stat32 st32;
     struct _stat64 st64;
 
+    mingw_test_init ();
 
     /* mingw-w64 64-bit _futime64() and _fstat64() functions are not compatible
      * with msvcrt10-40 32-bit futime() and fstat() functions and test is failing.
diff --git a/mingw-w64-crt/testcases/t_vsscanf.c b/mingw-w64-crt/testcases/t_vsscanf.c
index d6f3510..dae95cd 100644
--- a/mingw-w64-crt/testcases/t_vsscanf.c
+++ b/mingw-w64-crt/testcases/t_vsscanf.c
@@ -3,6 +3,9 @@
 #include <stdio.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 __attribute__((__format__(scanf, 2, 3)))
 static int call_vsscanf(const char *str, const char *format, ...)
 {
@@ -17,6 +20,9 @@
 int main()
 {
   char b[51];
+
+  mingw_test_init ();
+
   call_vsscanf(
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY",
     "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
@@ -28,5 +34,6 @@
     &b[41],&b[42],&b[43],&b[44],&b[45],&b[46],&b[47],&b[48],&b[49],&b[50]
   );
   printf("b=%.51s\n", b);
+
   return memcmp(b, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY", 51);
 }
diff --git a/mingw-w64-crt/testcases/t_wcrtomb.c b/mingw-w64-crt/testcases/t_wcrtomb.c
index f9d1a8d..bd49df4 100644
--- a/mingw-w64-crt/testcases/t_wcrtomb.c
+++ b/mingw-w64-crt/testcases/t_wcrtomb.c
@@ -9,6 +9,9 @@
 #include <stdlib.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * This test is for mingw-w64's implementation of wcrtomb function.
  *
@@ -31,9 +34,12 @@
 }
 
 int main (void) {
+  mingw_test_init ();
+
 #ifdef _UCRT
   return 77;
 #endif
+
   mbstate_t state = {0};
 
   /**
diff --git a/mingw-w64-crt/testcases/t_wcsrtombs.c b/mingw-w64-crt/testcases/t_wcsrtombs.c
index 705cdc2..6f8827d 100644
--- a/mingw-w64-crt/testcases/t_wcsrtombs.c
+++ b/mingw-w64-crt/testcases/t_wcsrtombs.c
@@ -10,6 +10,9 @@
 #include <string.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 /**
  * This test is for mingw-w64's implementation of wcsrtombs function.
  *
@@ -27,9 +30,12 @@
 wchar_t BadText[] = {L'テ', L'く', WEOF, L'ト'};
 
 int main (void) {
+  mingw_test_init ();
+
 #ifdef _UCRT
   return 77;
 #endif
+
   const wchar_t *original_text = NULL;
   const wchar_t *text = NULL;
   size_t         text_length = 0;
diff --git a/mingw-w64-crt/testcases/t_wcstok_s.c b/mingw-w64-crt/testcases/t_wcstok_s.c
index e4c82c1..5f7d6d8 100644
--- a/mingw-w64-crt/testcases/t_wcstok_s.c
+++ b/mingw-w64-crt/testcases/t_wcstok_s.c
@@ -4,6 +4,9 @@
 #include <stdlib.h>
 #include <wchar.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 static int handler_called = 0;
 static int handler_errno = -1;
 static void handler(const wchar_t *expression __attribute__((unused)), const wchar_t *function __attribute__((unused)), const wchar_t *file __attribute__((unused)), unsigned int line __attribute__((unused)), uintptr_t reserved __attribute__((unused))) {
@@ -17,6 +20,7 @@
     wchar_t *state;
     wchar_t *token;
 
+    mingw_test_init ();
     _set_invalid_parameter_handler(handler);
 
     /* wcstok normal usage without errors */
diff --git a/mingw-w64-crt/testcases/t_wctob.c b/mingw-w64-crt/testcases/t_wctob.c
index 00f10ab..953b1f0 100644
--- a/mingw-w64-crt/testcases/t_wctob.c
+++ b/mingw-w64-crt/testcases/t_wctob.c
@@ -12,7 +12,12 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main (void) {
+  mingw_test_init ();
+
   /**
    * Test "C" locale
    */
diff --git a/mingw-w64-crt/testcases/t_wctype.c b/mingw-w64-crt/testcases/t_wctype.c
index 25579cf..f965557 100644
--- a/mingw-w64-crt/testcases/t_wctype.c
+++ b/mingw-w64-crt/testcases/t_wctype.c
@@ -10,7 +10,9 @@
 #include <stdlib.h>
 #include <wctype.h>
 
+/* mingw-w64 headers */
 #include "ctype-test.h"
+#include "libtest.h"
 
 /**
  * Test conformance of wctype.h functions to POSIX specification.
@@ -19,6 +21,8 @@
  */
 
 int main (void) {
+  mingw_test_init ();
+
   assert (setlocale (LC_ALL, "C") != NULL);
 
   for (int i = 0; i < (int) _countof (CharTypeTable); ++i) {
diff --git a/mingw-w64-crt/testcases/t_winmain.c b/mingw-w64-crt/testcases/t_winmain.c
index cd3a278..b73368f 100644
--- a/mingw-w64-crt/testcases/t_winmain.c
+++ b/mingw-w64-crt/testcases/t_winmain.c
@@ -6,6 +6,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 #define TITLE "WinMain"
 #define WIDTH 250
 #define HEIGHT 250
@@ -39,6 +42,8 @@
   MSG messages;
   WNDCLASSEX wincl;
 
+  mingw_test_init ();
+
   wincl.hInstance = hThisInstance;
   wincl.lpszClassName = szClassName;
   wincl.lpfnWndProc = WindowProc;
diff --git a/mingw-w64-crt/testcases/t_winmain_assert.c b/mingw-w64-crt/testcases/t_winmain_assert.c
index 6b9f10a..10bbea7 100644
--- a/mingw-w64-crt/testcases/t_winmain_assert.c
+++ b/mingw-w64-crt/testcases/t_winmain_assert.c
@@ -8,8 +8,12 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 {
+  mingw_test_init ();
   assert (lpCmdLine != NULL);
   return 0;
 }
diff --git a/mingw-w64-crt/testcases/t_wreaddir.c b/mingw-w64-crt/testcases/t_wreaddir.c
index cb46a08..89b58e2 100644
--- a/mingw-w64-crt/testcases/t_wreaddir.c
+++ b/mingw-w64-crt/testcases/t_wreaddir.c
@@ -6,10 +6,17 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* mingw-w64 headers */
+#include "libtest.h"
+
 int main(int argc, char **argv)
 {
   struct _wdirent *di;
-  _WDIR *h = _wopendir (L".");
+  _WDIR *h;
+
+  mingw_test_init ();
+
+  h = _wopendir (L".");
 
   if (!h)
     return 1;
diff --git a/mingw-w64-crt/testcases/t_wrongret.cpp b/mingw-w64-crt/testcases/t_wrongret.cpp
index 4393452..0902626 100644
--- a/mingw-w64-crt/testcases/t_wrongret.cpp
+++ b/mingw-w64-crt/testcases/t_wrongret.cpp
@@ -1,6 +1,9 @@
 #include <iostream>
 #include <vector>
 
+// mingw-w64 headers
+#include "libtest.h"
+
 int F()
 {
 
@@ -12,5 +15,6 @@
 
 int main()
 {
+        mingw_test_init ();
         std::cout << F() << std::endl; // prints something other than 12345
 }