michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/TimeStamp.h" michael@0: michael@0: #include "TestHarness.h" michael@0: michael@0: #include "prinrval.h" michael@0: #include "prthread.h" michael@0: michael@0: using mozilla::TimeStamp; michael@0: using mozilla::TimeDuration; michael@0: michael@0: static void Assert(bool aCond, const char* aMsg) michael@0: { michael@0: if (aCond) { michael@0: passed(aMsg); michael@0: } else { michael@0: fail("%s", aMsg); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: ScopedXPCOM xpcom("nsTimeStamp"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: TimeDuration td; michael@0: Assert(td.ToSeconds() == 0.0, "TimeDuration default value 0"); michael@0: Assert(TimeDuration::FromSeconds(5).ToSeconds() == 5.0, michael@0: "TimeDuration FromSeconds/ToSeconds round-trip"); michael@0: Assert(TimeDuration::FromMilliseconds(5000).ToSeconds() == 5.0, michael@0: "TimeDuration FromMilliseconds/ToSeconds round-trip"); michael@0: Assert(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(2), michael@0: "TimeDuration < operator works"); michael@0: Assert(!(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(1)), michael@0: "TimeDuration < operator works"); michael@0: Assert(TimeDuration::FromSeconds(2) > TimeDuration::FromSeconds(1), michael@0: "TimeDuration > operator works"); michael@0: Assert(!(TimeDuration::FromSeconds(1) > TimeDuration::FromSeconds(1)), michael@0: "TimeDuration > operator works"); michael@0: Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(2), michael@0: "TimeDuration <= operator works"); michael@0: Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(1), michael@0: "TimeDuration <= operator works"); michael@0: Assert(!(TimeDuration::FromSeconds(2) <= TimeDuration::FromSeconds(1)), michael@0: "TimeDuration <= operator works"); michael@0: Assert(TimeDuration::FromSeconds(2) >= TimeDuration::FromSeconds(1), michael@0: "TimeDuration >= operator works"); michael@0: Assert(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(1), michael@0: "TimeDuration >= operator works"); michael@0: Assert(!(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(2)), michael@0: "TimeDuration >= operator works"); michael@0: michael@0: TimeStamp ts; michael@0: Assert(ts.IsNull(), "Default TimeStamp value null"); michael@0: michael@0: ts = TimeStamp::Now(); michael@0: Assert(!ts.IsNull(), "TimeStamp time value non-null"); michael@0: Assert((ts - ts).ToSeconds() == 0.0, "TimeStamp zero-length duration"); michael@0: michael@0: PR_Sleep(PR_SecondsToInterval(2)); michael@0: michael@0: TimeStamp ts2(TimeStamp::Now()); michael@0: Assert(ts2 > ts, "TimeStamp > comparison"); michael@0: Assert(!(ts > ts), "TimeStamp > comparison"); michael@0: Assert(ts < ts2, "TimeStamp < comparison"); michael@0: Assert(!(ts < ts), "TimeStamp < comparison"); michael@0: Assert(ts <= ts2, "TimeStamp <= comparison"); michael@0: Assert(ts <= ts, "TimeStamp <= comparison"); michael@0: Assert(!(ts2 <= ts), "TimeStamp <= comparison"); michael@0: Assert(ts2 >= ts, "TimeStamp >= comparison"); michael@0: Assert(ts2 >= ts, "TimeStamp >= comparison"); michael@0: Assert(!(ts >= ts2), "TimeStamp >= comparison"); michael@0: michael@0: // We can't be sure exactly how long PR_Sleep slept for. It should have michael@0: // slept for at least one second. We might have slept a lot longer due michael@0: // to process scheduling, but hopefully not more than 10 seconds. michael@0: td = ts2 - ts; michael@0: Assert(td.ToSeconds() > 1.0, "TimeStamp difference lower bound"); michael@0: Assert(td.ToSeconds() < 20.0, "TimeStamp difference upper bound"); michael@0: td = ts - ts2; michael@0: Assert(td.ToSeconds() < -1.0, "TimeStamp negative difference lower bound"); michael@0: Assert(td.ToSeconds() > -20.0, "TimeStamp negative difference upper bound"); michael@0: michael@0: double resolution = TimeDuration::Resolution().ToSecondsSigDigits(); michael@0: printf(" (platform timer resolution is ~%g s)\n", resolution); michael@0: Assert(0.000000001 < resolution, "Time resolution is sane"); michael@0: // Don't upper-bound sanity check ... although NSPR reports 1ms michael@0: // resolution, it might be lying, so we shouldn't compare with it michael@0: michael@0: return gFailCount > 0; michael@0: }