blob: 4d8c77ea6ee8a1b0fa57b96f83b76d7054be1441 [file] [log] [blame]
[email protected]87c32922012-04-11 01:44:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]662500242011-07-08 16:35:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <math.h>
6#include <stdlib.h>
7
[email protected]0141ba3c2011-07-12 20:58:588#include "ppapi/shared_impl/time_conversion.h"
[email protected]87c32922012-04-11 01:44:059#include "testing/gtest/include/gtest/gtest.h"
[email protected]662500242011-07-08 16:35:3810
[email protected]662500242011-07-08 16:35:3811namespace ppapi {
12
13// Slop we'll allow in two Time "internal values" to consider them equal.
14// Double conversion can introduce rounding errors. The internal values are in
15// microseconds, so an error here is very small.
16static const int kTimeInternalValueSlop = 2;
17
18// Same as above in double-precision seconds units.
19static const double kTimeSecondsSlop =
20 static_cast<double>(kTimeInternalValueSlop) /
21 base::Time::kMicrosecondsPerSecond;
22
[email protected]9fe54272012-02-21 20:37:0523TEST(TimeConversion, Time) {
[email protected]662500242011-07-08 16:35:3824 // Should be able to round-trip.
25 base::Time now = base::Time::Now();
[email protected]0141ba3c2011-07-12 20:58:5826 base::Time converted = ppapi::PPTimeToTime(TimeToPPTime(now));
[email protected]662500242011-07-08 16:35:3827 EXPECT_GE(kTimeInternalValueSlop,
28 abs(static_cast<int>((converted - now).ToInternalValue())));
29
30 // Units should be in seconds.
31 base::Time one_second_from_now = now + base::TimeDelta::FromSeconds(1);
[email protected]150f3392012-04-20 01:51:1632 double converted_one_second_from_now =
33 ppapi::TimeToPPTime(one_second_from_now) - ppapi::TimeToPPTime(now);
34 EXPECT_GE(kTimeSecondsSlop, fabs(converted_one_second_from_now - 1));
[email protected]662500242011-07-08 16:35:3835}
36
[email protected]a8b96382012-12-17 19:54:5937TEST(TimeConversion, EpochTime) {
38 // Should be able to round-trip from epoch time.
39 base::Time epoch = base::Time::UnixEpoch();
40 base::Time converted = ppapi::PPTimeToTime(TimeToPPTime(epoch));
41 EXPECT_GE(kTimeInternalValueSlop,
42 abs(static_cast<int>((converted - epoch).ToInternalValue())));
43
44 // Units should be in seconds.
45 base::Time one_second_from_epoch = epoch + base::TimeDelta::FromSeconds(1);
46 double converted_one_second_from_epoch =
47 ppapi::TimeToPPTime(one_second_from_epoch) - ppapi::TimeToPPTime(epoch);
48 EXPECT_GE(kTimeSecondsSlop, fabs(converted_one_second_from_epoch - 1));
49
50 // Epoch time should be equal to a PP_Time of 0.0.
51 EXPECT_GE(kTimeSecondsSlop, fabs(ppapi::TimeToPPTime(epoch) - 0.0));
52 EXPECT_GE(kTimeInternalValueSlop,
53 abs(static_cast<int>(
54 (ppapi::PPTimeToTime(0.0) - epoch).ToInternalValue())));
55}
56
[email protected]662500242011-07-08 16:35:3857} // namespace ppapi