| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: test1.c |
| 8 | ** |
| 9 | ** Purpose: Tests that SleepEx correctly sleeps for a given amount of time, |
| 10 | ** regardless of the alertable flag. |
| 11 | ** |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | typedef struct |
| 18 | { |
| 19 | DWORD SleepTime; |
| 20 | BOOL Alertable; |
| 21 | } testCase; |
| 22 | |
| 23 | testCase testCases[] = |
| 24 | { |
| 25 | {0, FALSE}, |
| 26 | {50, FALSE}, |
| 27 | {100, FALSE}, |
| 28 | {500, FALSE}, |
| 29 | {2000, FALSE}, |
| 30 | |
| 31 | {0, TRUE}, |
| 32 | {50, TRUE}, |
| 33 | {100, TRUE}, |
| 34 | {500, TRUE}, |
| 35 | {2000, TRUE}, |
| 36 | }; |
| 37 | |
| 38 | /* Milliseconds of error which are acceptable Function execution time, etc. */ |
| 39 | DWORD AcceptableTimeError = 150; |
| 40 | |
| 41 | int __cdecl main( int argc, char **argv ) |
| 42 | { |
| 43 | UINT64 OldTimeStamp; |
| 44 | UINT64 NewTimeStamp; |
| 45 | DWORD MaxDelta; |
| 46 | DWORD TimeDelta; |
| 47 | DWORD i; |
| 48 | |
| 49 | if (0 != (PAL_Initialize(argc, argv))) |
| 50 | { |
| 51 | return FAIL; |
| 52 | } |
| 53 | |
| 54 | LARGE_INTEGER performanceFrequency; |
| 55 | if (!QueryPerformanceFrequency(&performanceFrequency)) |
| 56 | { |
| 57 | return FAIL; |
| 58 | } |
| 59 | |
| 60 | for (i = 0; i<sizeof(testCases) / sizeof(testCases[0]); i++) |
| 61 | { |
| 62 | OldTimeStamp = GetHighPrecisionTimeStamp(performanceFrequency); |
| 63 | |
| 64 | SleepEx(testCases[i].SleepTime, testCases[i].Alertable); |
| 65 | |
| 66 | NewTimeStamp = GetHighPrecisionTimeStamp(performanceFrequency); |
| 67 | |
| 68 | TimeDelta = NewTimeStamp - OldTimeStamp; |
| 69 | |
| 70 | /* For longer intervals use a 10 percent tolerance */ |
| 71 | if ((testCases[i].SleepTime * 0.1) > AcceptableTimeError) |
| 72 | { |
| 73 | MaxDelta = testCases[i].SleepTime + (DWORD)(testCases[i].SleepTime * 0.1); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | MaxDelta = testCases[i].SleepTime + AcceptableTimeError; |
| 78 | } |
| 79 | |
| 80 | if (TimeDelta < testCases[i].SleepTime || TimeDelta > MaxDelta) |
| 81 | { |
| 82 | Fail("The sleep function slept for %d ms when it should have " |
| 83 | "slept for %d ms\n" , TimeDelta, testCases[i].SleepTime); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | PAL_Terminate(); |
| 88 | return PASS; |
| 89 | } |
| 90 | |