Remove support for performance tests since that never worked
[reactos.git] / reactos / regtests / shared / regtests.c
index 1729cae..3513911 100755 (executable)
@@ -6,14 +6,11 @@
  * UPDATE HISTORY:
  *      06-07-2003  CSH  Created
  */
-#include <ctype.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <malloc.h>
+#define WIN32_NO_STATUS
+#include <windows.h>
 #define NTOS_MODE_USER
-#include <ntos.h>
-#include <pseh.h>
+#include <ndk/ntndk.h>
+#include <pseh/pseh.h>
 #include "regtests.h"
 
 #define NDEBUG
 typedef struct _PERFORM_TEST_ARGS
 {
   TestOutputRoutine OutputRoutine;
-  PROS_TEST Test;
+  _PTEST Test;
   LPSTR TestName;
+  DWORD Result;
+  char Buffer[5000];
+  DWORD Time;
 } PERFORM_TEST_ARGS;
 
 int _Result;
@@ -37,44 +37,106 @@ InitializeTests()
   InitializeListHead(&AllTests);
 }
 
+char*
+FormatExecutionTime(char *buffer, ULONG milliseconds)
+{
+  sprintf(buffer,
+         "%ldms",
+         milliseconds);
+  return buffer;
+}
+
 DWORD WINAPI
 PerformTest(PVOID _arg)
 {
   PERFORM_TEST_ARGS *Args = (PERFORM_TEST_ARGS *)_arg;
-  TestOutputRoutine OutputRoutine = Args->OutputRoutine;
-  PROS_TEST Test = Args->Test;
-  LPSTR TestName = Args->TestName;
-  char OutputBuffer[5000];
-  char Buffer[5000];
+  _PTEST Test = Args->Test;
+
+  _SetThreadPriority(_GetCurrentThread(), THREAD_PRIORITY_IDLE);
 
-  memset(Buffer, 0, sizeof(Buffer));
+  memset(Args->Buffer, 0, sizeof(Args->Buffer));
 
   _SEH_TRY {
     _Result = TS_OK;
-    _Buffer = Buffer;
+    _Buffer = Args->Buffer;
     (Test->Routine)(TESTCMD_RUN);
+    Args->Result = _Result;
   } _SEH_HANDLE {
-    _Result = TS_FAILED;
-    sprintf(Buffer, "due to exception 0x%lx", _SEH_GetExceptionCode());
+    Args->Result = TS_FAILED;
+    sprintf(Args->Buffer, "due to exception 0x%lx", _SEH_GetExceptionCode());
   } _SEH_END;
+  return 1;
+}
 
-  if (_Result != TS_OK)
-    {
-      sprintf(OutputBuffer, "[%s] Failed (%s)\n", TestName, Buffer);
-    }
-  else
+VOID
+ControlNormalTest(HANDLE hThread,
+            PERFORM_TEST_ARGS *Args,
+            DWORD TimeOut)
+{
+  _FILETIME time;
+  _FILETIME executionTime;
+  DWORD status;
+
+  status = _WaitForSingleObject(hThread, TimeOut);
+  if (status == WAIT_TIMEOUT)
+  {
+    _TerminateThread(hThread, 0);
+    Args->Result = TS_TIMEDOUT;
+  }
+  status = _GetThreadTimes(hThread,
+                          &time,
+                          &time,
+                          &time,
+                          &executionTime);
+  Args->Time = executionTime.dwLowDateTime / 10000;
+}
+
+VOID
+DisplayResult(PERFORM_TEST_ARGS* Args,
+              LPSTR OutputBuffer)
+{
+  char Format[100];
+
+  if (Args->Result == TS_OK)
     {
-      sprintf(OutputBuffer, "[%s] Success\n", TestName);
+      sprintf(OutputBuffer,
+              "[%s] Success [%s]\n",
+              Args->TestName,
+              FormatExecutionTime(Format,
+                                  Args->Time));
     }
-  if (OutputRoutine != NULL)
+  else if (Args->Result == TS_TIMEDOUT)
     {
-      (*OutputRoutine)(OutputBuffer);
+      sprintf(OutputBuffer,
+              "[%s] Timed out [%s]\n",
+              Args->TestName,
+              FormatExecutionTime(Format,
+                                  Args->Time));
     }
   else
-    {
-      DbgPrint(OutputBuffer);
-    }
-  return 1;
+      sprintf(OutputBuffer, "[%s] Failed (%s)\n", Args->TestName, Args->Buffer);
+
+  if (Args->OutputRoutine != NULL)
+    (*Args->OutputRoutine)(OutputBuffer);
+  else
+    DbgPrint(OutputBuffer);
+}
+
+VOID
+ControlTest(HANDLE hThread,
+            PERFORM_TEST_ARGS *Args,
+            DWORD TestType,
+            DWORD TimeOut)
+{
+  switch (TestType)
+  {
+    case TT_NORMAL:
+      ControlNormalTest(hThread, Args, TimeOut);
+      break;
+    default:
+      printf("Unknown test type %ld\n", TestType);
+      break;
+  }
 }
 
 VOID
@@ -82,21 +144,23 @@ PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName)
 {
   PLIST_ENTRY CurrentEntry;
   PLIST_ENTRY NextEntry;
-  PROS_TEST Current;
+  _PTEST Current;
   PERFORM_TEST_ARGS Args;
   HANDLE hThread;
   char OutputBuffer[1024];
   char Name[200];
+  DWORD TestType;
   DWORD TimeOut;
 
   Args.OutputRoutine = OutputRoutine;
   Args.TestName = Name;
-  
+  Args.Time = 0;
+
   CurrentEntry = AllTests.Flink;
   for (; CurrentEntry != &AllTests; CurrentEntry = NextEntry)
     {
       NextEntry = CurrentEntry->Flink;
-      Current = CONTAINING_RECORD(CurrentEntry, ROS_TEST, ListEntry);
+      Current = CONTAINING_RECORD(CurrentEntry, _TEST, ListEntry);
       Args.Test = Current;
 
       /* Get name of test */
@@ -108,19 +172,19 @@ PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName)
       if (_Result != TS_OK)
         {
           if (TestName != NULL)
-            {
-              continue;
-            }
+            continue;
           strcpy(Name, "Unnamed");
         }
 
-      if (TestName != NULL)
-        {
-          if (_stricmp(Name, TestName) != 0)
-            {
-              continue;
-            }
-        }
+      if ((TestName != NULL) && (_stricmp(Name, TestName) != 0))
+        continue;
+
+      TestType = TT_NORMAL;
+      _Result = TS_OK;
+      _Buffer = (char *)&TestType;
+      (Current->Routine)(TESTCMD_TESTTYPE);
+      if (_Result != TS_OK)
+        TestType = TT_NORMAL;
 
       /* Get timeout for test */
       TimeOut = 0;
@@ -128,53 +192,30 @@ PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName)
       _Buffer = (char *)&TimeOut;
       (Current->Routine)(TESTCMD_TIMEOUT);
       if (_Result != TS_OK || TimeOut == INFINITE)
-        {
-          TimeOut = 5000;
-        }
+        TimeOut = 5000;
 
-      /* Run test in thread */
+      /* Run test in a separate thread */
       hThread = _CreateThread(NULL, 0, PerformTest, (PVOID)&Args, 0, NULL);
       if (hThread == NULL)
         {
-          sprintf(OutputBuffer,
-                  "[%s] Failed (CreateThread failed: 0x%x)\n",
-                  Name, (unsigned int)GetLastError());
-        }
-      else if (_WaitForSingleObject(hThread, TimeOut) == WAIT_TIMEOUT)
-        {
-          if (!_TerminateThread(hThread, 0))
-            {
-              sprintf(OutputBuffer,
-                      "[%s] Failed (Test timed out - %d ms, TerminateThread failed: 0x%x)\n",
-                      Name, (int)TimeOut, (unsigned int)GetLastError());
-            }
-          else
-            {
-              sprintf(OutputBuffer, "[%s] Failed (Test timed out - %d ms)\n", Name, (int)TimeOut);
-            }
+          printf("[%s] Failed (CreateThread() failed: %ld)\n",
+                 Name,
+                 _GetLastError());
+          Args.Result = TS_FAILED;
         }
       else
-        {
-          continue;
-        }
+        ControlTest(hThread, &Args, TestType, TimeOut);
 
-      if (OutputRoutine != NULL)
-        {
-          (*OutputRoutine)(OutputBuffer);
-        }
-      else
-        {
-          DbgPrint(OutputBuffer);
-        }
+      DisplayResult(&Args, OutputBuffer);
     }
 }
 
 VOID
 AddTest(TestRoutine Routine)
 {
-  PROS_TEST Test;
+  _PTEST Test;
 
-  Test = (PROS_TEST) malloc(sizeof(ROS_TEST));
+  Test = (_PTEST) malloc(sizeof(_TEST));
   if (Test == NULL)
     {
       DbgPrint("Out of memory");