From 99aef872d02dbc4483c921891729b41bae64930f Mon Sep 17 00:00:00 2001
From: Andrew Wesie <awesie@gmail.com>
Date: Thu, 30 May 2019 11:06:49 -0500
Subject: [PATCH] kernel32/tests: Compare GetTickCount to ntdll functions.

Signed-off-by: Andrew Wesie <awesie@gmail.com>
---
 dlls/kernel32/tests/time.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/dlls/kernel32/tests/time.c b/dlls/kernel32/tests/time.c
index 536f233..fc9aaca 100644
--- a/dlls/kernel32/tests/time.c
+++ b/dlls/kernel32/tests/time.c
@@ -32,6 +32,11 @@ static int (WINAPI *pGetCalendarInfoW)(LCID,CALID,CALTYPE,LPWSTR,int,LPDWORD);
 static DWORD (WINAPI *pGetDynamicTimeZoneInformation)(DYNAMIC_TIME_ZONE_INFORMATION*);
 static void (WINAPI *pGetSystemTimePreciseAsFileTime)(LPFILETIME);
 static BOOL (WINAPI *pGetTimeZoneInformationForYear)(USHORT, PDYNAMIC_TIME_ZONE_INFORMATION, LPTIME_ZONE_INFORMATION);
+static DWORD (WINAPI *pGetTickCount)(void);
+static ULONGLONG (WINAPI *pGetTickCount64)(void);
+static BOOL (WINAPI *pQueryPerformanceCounter)(LARGE_INTEGER *);
+static BOOL (WINAPI *pQueryPerformanceFrequency)(LARGE_INTEGER *);
+static ULONG (WINAPI *pNtGetTickCount)(void);
 
 #define SECSPERMIN         60
 #define SECSPERDAY        86400
@@ -1004,9 +1009,74 @@ static void test_GetTimeZoneInformationForYear(void)
        "GetTimeZoneInformationForYear err %u\n", GetLastError());
 }
 
+static void test_GetTickCount(void)
+{
+    DWORD t1, t2;
+    LARGE_INTEGER ctr1, ctr2, freq;
+
+    if (pNtGetTickCount)
+    {
+        t1 = pGetTickCount();
+        t2 = pNtGetTickCount();
+        ok((DWORD)(t2 - t1) < 20, "NtGetTickCount - GetTickCount too large, %u\n", t2 - t1);
+    }
+    else
+    {
+        win_skip("NtGetTickCount is not available.\n");
+    }
+
+    ok(pQueryPerformanceFrequency(&freq), "QueryPerformanceFrequency failed\n");
+    ok(pQueryPerformanceCounter(&ctr1), "QueryPerformanceCounter failed\n");
+    t1 = pGetTickCount();
+
+    Sleep(500);
+
+    ok(pQueryPerformanceCounter(&ctr2), "QueryPerformanceCounter failed\n");
+    t1 = pGetTickCount() - t1;
+    t2 = (ctr2.QuadPart - ctr1.QuadPart) / (freq.QuadPart / 1000);
+
+    ok(abs(t2 - t1) < 20, "QueryPerformanceCounter - GetTickCount too large, %d\n", t2 - t1);
+}
+
+static void test_GetTickCount64(void)
+{
+    ULONGLONG t1, t2;
+    LARGE_INTEGER ctr1, ctr2, freq;
+
+    if (!pGetTickCount64)
+    {
+        win_skip("GetTickCount64 is not available.\n");
+        return;
+    }
+
+    if (pNtGetTickCount)
+    {
+        t1 = pGetTickCount64();
+        t2 = pNtGetTickCount();
+        ok((DWORD)(t2 - t1) < 20, "NtGetTickCount - GetTickCount64 too large, %u\n", (DWORD)(t2 - t1));
+    }
+    else
+    {
+        win_skip("NtGetTickCount is not available.\n");
+    }
+
+    ok(pQueryPerformanceFrequency(&freq), "QueryPerformanceFrequency failed\n");
+    ok(pQueryPerformanceCounter(&ctr1), "QueryPerformanceCounter failed\n");
+    t1 = pGetTickCount64();
+
+    Sleep(500);
+
+    ok(pQueryPerformanceCounter(&ctr2), "QueryPerformanceCounter failed\n");
+    t1 = pGetTickCount64() - t1;
+    t2 = (ctr2.QuadPart - ctr1.QuadPart) / (freq.QuadPart / 1000);
+
+    ok(abs(t2 - t1) < 20, "QueryPerformanceCounter - GetTickCount64 too large, %I64d\n", t2 - t1);
+}
+
 START_TEST(time)
 {
     HMODULE hKernel = GetModuleHandleA("kernel32");
+    HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
     pTzSpecificLocalTimeToSystemTime = (void *)GetProcAddress(hKernel, "TzSpecificLocalTimeToSystemTime");
     pSystemTimeToTzSpecificLocalTime = (void *)GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
     pGetSystemTimes = (void *)GetProcAddress( hKernel, "GetSystemTimes");
@@ -1015,6 +1085,11 @@ START_TEST(time)
     pGetDynamicTimeZoneInformation = (void *)GetProcAddress(hKernel, "GetDynamicTimeZoneInformation");
     pGetSystemTimePreciseAsFileTime = (void *)GetProcAddress(hKernel, "GetSystemTimePreciseAsFileTime");
     pGetTimeZoneInformationForYear = (void *)GetProcAddress(hKernel, "GetTimeZoneInformationForYear");
+    pGetTickCount = (void *)GetProcAddress(hKernel, "GetTickCount");
+    pGetTickCount64 = (void *)GetProcAddress(hKernel, "GetTickCount64");
+    pQueryPerformanceCounter = (void *)GetProcAddress(hKernel, "QueryPerformanceCounter");
+    pQueryPerformanceFrequency = (void *)GetProcAddress(hKernel, "QueryPerformanceFrequency");
+    pNtGetTickCount = (void *)GetProcAddress(hNtdll, "NtGetTickCount");
 
     test_conversions();
     test_invalid_arg();
@@ -1029,4 +1104,6 @@ START_TEST(time)
     test_GetSystemTimeAsFileTime();
     test_GetSystemTimePreciseAsFileTime();
     test_GetTimeZoneInformationForYear();
+    test_GetTickCount();
+    test_GetTickCount64();
 }
-- 
2.1.4

