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: Sleep.c |
8 | ** |
9 | ** Purpose: Test to establish whether the Sleep function stops the thread from |
10 | ** executing for the specified times. |
11 | ** |
12 | ** Dependencies: GetSystemTime |
13 | ** Fail |
14 | ** Trace |
15 | ** |
16 | |
17 | ** |
18 | **=========================================================*/ |
19 | |
20 | #include <palsuite.h> |
21 | |
22 | DWORD SleepTimes[] = |
23 | { |
24 | 0, |
25 | 50, |
26 | 100, |
27 | 500, |
28 | 2000 |
29 | }; |
30 | |
31 | /* Milliseconds of error which are acceptable Function execution time, etc. */ |
32 | DWORD AcceptableTimeError = 150; |
33 | |
34 | int __cdecl main( int argc, char **argv ) |
35 | { |
36 | UINT64 OldTimeStamp; |
37 | UINT64 NewTimeStamp; |
38 | DWORD MaxDelta; |
39 | DWORD TimeDelta; |
40 | DWORD i; |
41 | |
42 | if(0 != (PAL_Initialize(argc, argv))) |
43 | { |
44 | return ( FAIL ); |
45 | } |
46 | |
47 | LARGE_INTEGER performanceFrequency; |
48 | if (!QueryPerformanceFrequency(&performanceFrequency)) |
49 | { |
50 | return FAIL; |
51 | } |
52 | |
53 | for( i = 0; i < sizeof(SleepTimes) / sizeof(DWORD); i++) |
54 | { |
55 | OldTimeStamp = GetHighPrecisionTimeStamp(performanceFrequency); |
56 | Sleep(SleepTimes[i]); |
57 | NewTimeStamp = GetHighPrecisionTimeStamp(performanceFrequency); |
58 | |
59 | TimeDelta = NewTimeStamp - OldTimeStamp; |
60 | |
61 | /* For longer intervals use a 10 percent tolerance */ |
62 | if ((SleepTimes[i] * 0.1) > AcceptableTimeError) |
63 | { |
64 | MaxDelta = SleepTimes[i] + (DWORD)(SleepTimes[i] * 0.1); |
65 | } |
66 | else |
67 | { |
68 | MaxDelta = SleepTimes[i] + AcceptableTimeError; |
69 | } |
70 | |
71 | if ( TimeDelta<SleepTimes[i] || TimeDelta>MaxDelta ) |
72 | { |
73 | Fail("The sleep function slept for %d ms when it should have " |
74 | "slept for %d ms\n", TimeDelta, SleepTimes[i]); |
75 | } |
76 | } |
77 | PAL_Terminate(); |
78 | return ( PASS ); |
79 | |
80 | } |
81 |