Bug 596580: Fix mozJSSubScriptLoader's version finding. (r=brendan)
[mozilla-central.git] / xpcom / tests / TestTimeStamp.cpp
blob03c1fdcbb42193b72b23ef5c8441a34351725df6
1 /* -*- Mode: C++; tab-width: 2; 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 XPCOM TimeStamp tests.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * robert@ocallahan.org
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 #include "mozilla/TimeStamp.h"
40 #include "TestHarness.h"
42 #include "prinrval.h"
43 #include "prthread.h"
45 using mozilla::TimeStamp;
46 using mozilla::TimeDuration;
48 static void Assert(PRBool aCond, const char* aMsg)
50 if (aCond) {
51 passed(aMsg);
52 } else {
53 fail("%s", aMsg);
57 int main(int argc, char** argv)
59 ScopedXPCOM xpcom("nsTimeStamp");
60 if (xpcom.failed())
61 return 1;
63 TimeDuration td;
64 Assert(td.ToSeconds() == 0.0, "TimeDuration default value 0");
65 Assert(TimeDuration::FromSeconds(5).ToSeconds() == 5.0,
66 "TimeDuration FromSeconds/ToSeconds round-trip");
67 Assert(TimeDuration::FromMilliseconds(5000).ToSeconds() == 5.0,
68 "TimeDuration FromMilliseconds/ToSeconds round-trip");
69 Assert(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(2),
70 "TimeDuration < operator works");
71 Assert(!(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(1)),
72 "TimeDuration < operator works");
73 Assert(TimeDuration::FromSeconds(2) > TimeDuration::FromSeconds(1),
74 "TimeDuration > operator works");
75 Assert(!(TimeDuration::FromSeconds(1) > TimeDuration::FromSeconds(1)),
76 "TimeDuration > operator works");
77 Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(2),
78 "TimeDuration <= operator works");
79 Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(1),
80 "TimeDuration <= operator works");
81 Assert(!(TimeDuration::FromSeconds(2) <= TimeDuration::FromSeconds(1)),
82 "TimeDuration <= operator works");
83 Assert(TimeDuration::FromSeconds(2) >= TimeDuration::FromSeconds(1),
84 "TimeDuration >= operator works");
85 Assert(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(1),
86 "TimeDuration >= operator works");
87 Assert(!(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(2)),
88 "TimeDuration >= operator works");
90 TimeStamp ts;
91 Assert(ts.IsNull(), "Default TimeStamp value null");
93 ts = TimeStamp::Now();
94 Assert(!ts.IsNull(), "TimeStamp time value non-null");
95 Assert((ts - ts).ToSeconds() == 0.0, "TimeStamp zero-length duration");
97 PR_Sleep(PR_SecondsToInterval(2));
99 TimeStamp ts2(TimeStamp::Now());
100 Assert(ts2 > ts, "TimeStamp > comparison");
101 Assert(!(ts > ts), "TimeStamp > comparison");
102 Assert(ts < ts2, "TimeStamp < comparison");
103 Assert(!(ts < ts), "TimeStamp < comparison");
104 Assert(ts <= ts2, "TimeStamp <= comparison");
105 Assert(ts <= ts, "TimeStamp <= comparison");
106 Assert(!(ts2 <= ts), "TimeStamp <= comparison");
107 Assert(ts2 >= ts, "TimeStamp >= comparison");
108 Assert(ts2 >= ts, "TimeStamp >= comparison");
109 Assert(!(ts >= ts2), "TimeStamp >= comparison");
111 // We can't be sure exactly how long PR_Sleep slept for. It should have
112 // slept for at least one second. We might have slept a lot longer due
113 // to process scheduling, but hopefully not more than 10 seconds.
114 td = ts2 - ts;
115 Assert(td.ToSeconds() > 1.0, "TimeStamp difference lower bound");
116 Assert(td.ToSeconds() < 20.0, "TimeStamp difference upper bound");
117 td = ts - ts2;
118 Assert(td.ToSeconds() < -1.0, "TimeStamp negative difference lower bound");
119 Assert(td.ToSeconds() > -20.0, "TimeStamp negative difference upper bound");
121 double resolution = TimeDuration::Resolution().ToSecondsSigDigits();
122 printf(" (platform timer resolution is ~%g s)\n", resolution);
123 Assert(0.000000001 < resolution, "Time resolution is sane");
124 // Don't upper-bound sanity check ... although NSPR reports 1ms
125 // resolution, it might be lying, so we shouldn't compare with it
127 return gFailCount > 0;