VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstTime-2.cpp@ 5197

Last change on this file since 5197 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/* $Id: tstTime-2.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - Simple RTTime test.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#ifdef RT_OS_WINDOWS
22# include <Windows.h>
23
24#elif defined RT_OS_L4
25
26#else /* posix */
27# include <sys/time.h>
28#endif
29
30#include <iprt/time.h>
31#include <iprt/stream.h>
32#include <iprt/runtime.h>
33#include <iprt/thread.h>
34#include <VBox/sup.h>
35
36DECLINLINE(uint64_t) OSNanoTS(void)
37{
38#ifdef RT_OS_WINDOWS
39 uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
40 GetSystemTimeAsFileTime((LPFILETIME)&u64);
41 return u64 * 100;
42
43#elif defined RT_OS_L4
44 /** @todo fix a different timesource on l4. */
45 return RTTimeNanoTS();
46
47#else /* posix */
48
49 struct timeval tv;
50 gettimeofday(&tv, NULL);
51 return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
52 + (uint64_t)(tv.tv_usec * 1000);
53#endif
54}
55
56
57
58int main()
59{
60 unsigned cErrors = 0;
61 int i;
62 RTR3Init();
63SUPInit();
64 RTPrintf("tstTime-2: TESTING...\n");
65
66 /*
67 * RTNanoTimeTS() shall never return something which
68 * is less or equal to the return of the previous call.
69 */
70
71 OSNanoTS(); RTTimeNanoTS(); RTThreadYield();
72 uint64_t u64RTStartTS = RTTimeNanoTS();
73 uint64_t u64OSStartTS = OSNanoTS();
74#define NUMBER_OF_CALLS (100*_1M)
75
76 for (i = 0; i < NUMBER_OF_CALLS; i++)
77 RTTimeNanoTS();
78
79 uint64_t u64RTElapsedTS = RTTimeNanoTS();
80 uint64_t u64OSElapsedTS = OSNanoTS();
81 u64RTElapsedTS -= u64RTStartTS;
82 u64OSElapsedTS -= u64OSStartTS;
83 int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
84 if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
85 {
86 RTPrintf("tstTime-2: error: total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
87 u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
88 cErrors++;
89 }
90 else
91 RTPrintf("tstTime-2: total time difference: u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
92 u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
93
94#if defined RT_OS_WINDOWS || defined RT_OS_LINUX
95 RTPrintf("tstTime-2: RTTime1nsSteps -> %u out of %u calls\n", RTTime1nsSteps(), NUMBER_OF_CALLS);
96#endif
97 if (!cErrors)
98 RTPrintf("tstTime-2: SUCCESS\n");
99 else
100 RTPrintf("tstTime-2: FAILURE - %d errors\n", cErrors);
101 return !!cErrors;
102}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette