nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / src / cplus / tests / interval.cpp
blob1223d67a72efda4a29d3da2d422b296d1d365ee9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* interval.cpp - a test program */
40 #include "rclock.h"
41 #include "rcthread.h"
42 #include "rcinrval.h"
43 #include "rccv.h"
45 #include <prio.h>
46 #include <prlog.h>
47 #include <prprf.h>
49 #define DEFAULT_ITERATIONS 100
51 PRIntn main(PRIntn argc, char **argv)
53 RCLock ml;
54 PRStatus rv;
55 RCCondition cv(&ml);
57 RCInterval now, timeout, epoch, elapsed;
58 PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
59 PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS;
61 /* slow, agonizing waits */
62 for (seconds = 0; seconds < 10; ++seconds)
64 timeout = RCInterval::FromSeconds(seconds);
65 cv.SetTimeout(timeout);
67 RCEnter lock(&ml);
69 epoch.SetToNow();
71 rv = cv.Wait();
72 PR_ASSERT(PR_SUCCESS == rv);
74 now = RCInterval(RCInterval::now);
75 elapsed = now - epoch;
78 PR_fprintf(
79 output, "Waiting %u seconds took %s%u milliseconds\n",
80 seconds, ((elapsed < timeout)? "**" : ""),
81 elapsed.ToMilliseconds());
84 /* more slow, agonizing sleeps */
85 for (seconds = 0; seconds < 10; ++seconds)
87 timeout = RCInterval::FromSeconds(seconds);
89 epoch.SetToNow();
91 rv = RCThread::Sleep(timeout);
92 PR_ASSERT(PR_SUCCESS == rv);
94 now = RCInterval(RCInterval::now);
95 elapsed = now - epoch;
98 PR_fprintf(
99 output, "Sleeping %u seconds took %s%u milliseconds\n",
100 seconds, ((elapsed < timeout)? "**" : ""),
101 elapsed.ToMilliseconds());
104 /* fast, spritely little devils */
105 for (msecs = 10; msecs < 100; msecs += 10)
107 timeout = RCInterval::FromMilliseconds(msecs);
108 cv.SetTimeout(timeout);
110 RCEnter lock(&ml);
112 epoch.SetToNow();
114 for (loops = 0; loops < iterations; ++loops)
116 rv = cv.Wait();
117 PR_ASSERT(PR_SUCCESS == rv);
120 now = RCInterval(RCInterval::now);
121 elapsed = now - epoch;
123 elapsed /= iterations;
125 PR_fprintf(
126 output, "Waiting %u msecs took %s%u milliseconds average\n",
127 msecs, ((elapsed < timeout)? "**" : ""), elapsed.ToMilliseconds());
129 return 0;
130 } /* main */
132 /* interval.cpp */