[email protected] | 87c3292 | 2012-04-11 01:44:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 2 | // 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] | 0141ba3c | 2011-07-12 20:58:58 | [diff] [blame] | 8 | #include "ppapi/shared_impl/time_conversion.h" |
[email protected] | 87c3292 | 2012-04-11 01:44:05 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 10 | |
[email protected] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 11 | namespace 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. |
| 16 | static const int kTimeInternalValueSlop = 2; |
| 17 | |
| 18 | // Same as above in double-precision seconds units. |
| 19 | static const double kTimeSecondsSlop = |
| 20 | static_cast<double>(kTimeInternalValueSlop) / |
| 21 | base::Time::kMicrosecondsPerSecond; |
| 22 | |
[email protected] | 9fe5427 | 2012-02-21 20:37:05 | [diff] [blame] | 23 | TEST(TimeConversion, Time) { |
[email protected] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 24 | // Should be able to round-trip. |
| 25 | base::Time now = base::Time::Now(); |
[email protected] | 0141ba3c | 2011-07-12 20:58:58 | [diff] [blame] | 26 | base::Time converted = ppapi::PPTimeToTime(TimeToPPTime(now)); |
[email protected] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 27 | 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] | 150f339 | 2012-04-20 01:51:16 | [diff] [blame] | 32 | 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] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | a8b9638 | 2012-12-17 19:54:59 | [diff] [blame] | 37 | TEST(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] | 66250024 | 2011-07-08 16:35:38 | [diff] [blame] | 57 | } // namespace ppapi |