/*************************************************************************\ * Copyright (c) 2006 UChicago Argonne LLC, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * SPDX-License-Identifier: EPICS * EPICS BASE is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ /* * Authors: Jeff Hill, Marty Kraimer and Andrew Johnson */ #include #include #include #include #include #include #include #include #include "envDefs.h" #include "epicsTime.h" #include "epicsThread.h" #include "errlog.h" #include "epicsUnitTest.h" #include "testMain.h" using namespace std; /* The functionality of the old invalidFormatTest () and badNanosecTest () * routines is incorporated into epicsTimeTest () below. */ static const unsigned mSecPerSec = 1000u; static const unsigned uSecPerSec = 1000u * mSecPerSec; static const unsigned nSecPerSec = 1000u * uSecPerSec; static const double precisionEPICS = 1.0 / nSecPerSec; static void testAdd(epicsUInt32 lhsSec, epicsUInt32 lhsNS, double rhs, epicsUInt32 expectSec, epicsUInt32 expectNS) { epicsTimeStamp lhs = {lhsSec, lhsNS}; epicsTimeStamp expect = {expectSec, expectNS}; epicsTimeStamp actual = lhs; epicsTimeAddSeconds(&actual, rhs); testOk(epicsTimeEqual(&actual, &expect), "testAdd(%u:%u + %.9f -> %u:%u == %u:%u)", unsigned(lhs.secPastEpoch), unsigned(lhs.nsec), rhs, unsigned(actual.secPastEpoch), unsigned(actual.nsec), unsigned(expect.secPastEpoch), unsigned(expect.nsec)); } static void testDiff(epicsUInt32 lhsSec, epicsUInt32 lhsNS, epicsUInt32 rhsSec, epicsUInt32 rhsNS, double expect) { epicsTimeStamp lhs = {lhsSec, lhsNS}; epicsTimeStamp rhs = {rhsSec, rhsNS}; double actual = epicsTimeDiffInSeconds(&lhs, &rhs); double diff = actual - expect; testOk(fabs(diff) %.9f ~= %.9f (%g)", unsigned(lhs.secPastEpoch), unsigned(lhs.nsec), unsigned(rhs.secPastEpoch), unsigned(rhs.nsec), actual, expect, diff); } static void crossCheck(double delay) { double mindelta = 2*epicsMonotonicResolution()*1e-9, tres = epicsThreadSleepQuantum(); epicsUInt64 A = epicsMonotonicGet(); epicsThreadSleep(delay); epicsUInt64 B = epicsMonotonicGet(); double actual = (B-A)*1e-9, percent; if(mindelta") == 0, "undefined => '%s'", buf); // This is Noon GMT, when all timezones have the same date const epicsTimeStamp tTS = {12*60*60, 98765432}; et = tTS; pFormat = "%Y-%m-%d %S.%09f"; // %H and %M change with timezone et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "1990-01-01 00.098765432") == 0, "'%s' => '%s'", pFormat, buf); pFormat = "%S.%03f"; et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.099") == 0, "'%s' => '%s'", pFormat, buf); pFormat = "%S.%04f"; et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.0988") == 0, "'%s' => '%s'", pFormat, buf); pFormat = "%S.%05f"; et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.09877") == 0, "'%s' => '%s'", pFormat, buf); pFormat = "%S.%05f %S.%05f"; et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.09877 00.09877") == 0, "'%s' => '%s'", pFormat, buf); char smbuf[5]; pFormat = "%S.%05f"; et.strftime(smbuf, sizeof(smbuf), pFormat); testOk(strcmp(smbuf, "00.*") == 0, "'%s' => '%s'", pFormat, smbuf); pFormat = "%S.%03f"; (et + 0.9).strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.999") == 0, "0.998765 => '%s'", buf); pFormat = "%S.%03f"; (et + 0.901).strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "00.999") == 0, "0.999765 => '%s'", buf); pFormat = "%%S.%%05f"; et.strftime(buf, sizeof(buf), pFormat); testOk(strcmp(buf, "%S.%05f") == 0, "'%s' => '%s'", pFormat, buf); char bigBuf [512]; memset(bigBuf, '\a', sizeof(bigBuf)); bigBuf[ sizeof(bigBuf) - 1] = '\0'; et.strftime(buf, sizeof(buf), bigBuf); testOk(strcmp(buf, "") == 0, "bad format => '%s'", buf); } epicsTime now; try { now = epicsTime::getCurrent(); testPass("default time provider"); } catch ( std::exception& ) { testFail("epicsTime::getCurrent() throws"); testAbort("Can't continue, check your time provider"); } testDiag("Running %d loops", nTimes); const epicsTime begin = epicsTime::getCurrent(); for (int loop = 0; loop < nTimes; ++loop) { for (int i = 0; i < wasteTime; ++i) { now = epicsTime::getCurrent(); } const double diff = now - begin; if (loop == 0) { testDiag ("%d calls to epicsTime::getCurrent() " "averaged %6.3f usec each", wasteTime, diff * 1e6 / wasteTime); } epicsTime copy = now; testOk1(copy == now); testOk1(copy <= now); testOk1(copy >= now); testOk1(now > begin); testOk1(now >= begin); testOk1(begin != now); testOk1(begin < now); testOk1(begin <= now); testOk1(now - now == 0); testOk(fabs((now - begin) - diff) < precisionEPICS * 0.01, "now - begin ~= diff"); testOk1(begin + 0 == begin); std::cout<<"# begin + diff ("<<(begin + diff)<<") == now ("<= 0 && delta < 1.0, "OS time_t can represent 10 years hence"); } catch ( ... ) { testFail("OS time_t conversion exception for value 10 years hence"); } testMonotonic(); testTMGames(); return testDone(); }