1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/cplus/tests/interval.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* interval.cpp - a test program */ 1.10 + 1.11 +#include "rclock.h" 1.12 +#include "rcthread.h" 1.13 +#include "rcinrval.h" 1.14 +#include "rccv.h" 1.15 + 1.16 +#include <prio.h> 1.17 +#include <prlog.h> 1.18 +#include <prprf.h> 1.19 + 1.20 +#define DEFAULT_ITERATIONS 100 1.21 + 1.22 +PRIntn main(PRIntn argc, char **argv) 1.23 +{ 1.24 + RCLock ml; 1.25 + PRStatus rv; 1.26 + RCCondition cv(&ml); 1.27 + 1.28 + RCInterval now, timeout, epoch, elapsed; 1.29 + PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput); 1.30 + PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS; 1.31 + 1.32 + /* slow, agonizing waits */ 1.33 + for (seconds = 0; seconds < 10; ++seconds) 1.34 + { 1.35 + timeout = RCInterval::FromSeconds(seconds); 1.36 + cv.SetTimeout(timeout); 1.37 + { 1.38 + RCEnter lock(&ml); 1.39 + 1.40 + epoch.SetToNow(); 1.41 + 1.42 + rv = cv.Wait(); 1.43 + PR_ASSERT(PR_SUCCESS == rv); 1.44 + 1.45 + now = RCInterval(RCInterval::now); 1.46 + elapsed = now - epoch; 1.47 + } 1.48 + 1.49 + PR_fprintf( 1.50 + output, "Waiting %u seconds took %s%u milliseconds\n", 1.51 + seconds, ((elapsed < timeout)? "**" : ""), 1.52 + elapsed.ToMilliseconds()); 1.53 + } 1.54 + 1.55 + /* more slow, agonizing sleeps */ 1.56 + for (seconds = 0; seconds < 10; ++seconds) 1.57 + { 1.58 + timeout = RCInterval::FromSeconds(seconds); 1.59 + { 1.60 + epoch.SetToNow(); 1.61 + 1.62 + rv = RCThread::Sleep(timeout); 1.63 + PR_ASSERT(PR_SUCCESS == rv); 1.64 + 1.65 + now = RCInterval(RCInterval::now); 1.66 + elapsed = now - epoch; 1.67 + } 1.68 + 1.69 + PR_fprintf( 1.70 + output, "Sleeping %u seconds took %s%u milliseconds\n", 1.71 + seconds, ((elapsed < timeout)? "**" : ""), 1.72 + elapsed.ToMilliseconds()); 1.73 + } 1.74 + 1.75 + /* fast, spritely little devils */ 1.76 + for (msecs = 10; msecs < 100; msecs += 10) 1.77 + { 1.78 + timeout = RCInterval::FromMilliseconds(msecs); 1.79 + cv.SetTimeout(timeout); 1.80 + { 1.81 + RCEnter lock(&ml); 1.82 + 1.83 + epoch.SetToNow(); 1.84 + 1.85 + for (loops = 0; loops < iterations; ++loops) 1.86 + { 1.87 + rv = cv.Wait(); 1.88 + PR_ASSERT(PR_SUCCESS == rv); 1.89 + } 1.90 + 1.91 + now = RCInterval(RCInterval::now); 1.92 + elapsed = now - epoch; 1.93 + } 1.94 + elapsed /= iterations; 1.95 + 1.96 + PR_fprintf( 1.97 + output, "Waiting %u msecs took %s%u milliseconds average\n", 1.98 + msecs, ((elapsed < timeout)? "**" : ""), elapsed.ToMilliseconds()); 1.99 + } 1.100 + return 0; 1.101 +} /* main */ 1.102 + 1.103 +/* interval.cpp */ 1.104 +