| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/time.h" | 5 #include "base/time.h" |
| 6 | 6 |
| 7 #include <time.h> | 7 #include <time.h> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 // | 457 // |
| 458 // Unfortunately, our InMilliseconds() function truncates | 458 // Unfortunately, our InMilliseconds() function truncates |
| 459 // rather than rounds. We should consider fixing this | 459 // rather than rounds. We should consider fixing this |
| 460 // so that our averages come out better. | 460 // so that our averages come out better. |
| 461 EXPECT_GE(delta.InMilliseconds(), 9); | 461 EXPECT_GE(delta.InMilliseconds(), 9); |
| 462 EXPECT_GE(delta.InMicroseconds(), 9000); | 462 EXPECT_GE(delta.InMicroseconds(), 9000); |
| 463 EXPECT_EQ(delta.InSeconds(), 0); | 463 EXPECT_EQ(delta.InSeconds(), 0); |
| 464 } | 464 } |
| 465 } | 465 } |
| 466 | 466 |
| 467 TEST(TimeTicks, HighResNow) { | 467 static void HighResClockTest(TimeTicks (*GetTicks)()) { |
| 468 #if defined(OS_WIN) | 468 #if defined(OS_WIN) |
| 469 // HighResNow doesn't work on some systems. Since the product still works | 469 // HighResNow doesn't work on some systems. Since the product still works |
| 470 // even if it doesn't work, it makes this entire test questionable. | 470 // even if it doesn't work, it makes this entire test questionable. |
| 471 if (!TimeTicks::IsHighResClockWorking()) | 471 if (!TimeTicks::IsHighResClockWorking()) |
| 472 return; | 472 return; |
| 473 #endif | 473 #endif |
| 474 | 474 |
| 475 // Why do we loop here? | 475 // Why do we loop here? |
| 476 // We're trying to measure that intervals increment in a VERY small amount | 476 // We're trying to measure that intervals increment in a VERY small amount |
| 477 // of time -- less than 15ms. Unfortunately, if we happen to have a | 477 // of time -- less than 15ms. Unfortunately, if we happen to have a |
| 478 // context switch in the middle of our test, the context switch could easily | 478 // context switch in the middle of our test, the context switch could easily |
| 479 // exceed our limit. So, we iterate on this several times. As long as we're | 479 // exceed our limit. So, we iterate on this several times. As long as we're |
| 480 // able to detect the fine-granularity timers at least once, then the test | 480 // able to detect the fine-granularity timers at least once, then the test |
| 481 // has succeeded. | 481 // has succeeded. |
| 482 | 482 |
| 483 const int kTargetGranularityUs = 15000; // 15ms | 483 const int kTargetGranularityUs = 15000; // 15ms |
| 484 | 484 |
| 485 bool success = false; | 485 bool success = false; |
| 486 int retries = 100; // Arbitrary. | 486 int retries = 100; // Arbitrary. |
| 487 TimeDelta delta; | 487 TimeDelta delta; |
| 488 while (!success && retries--) { | 488 while (!success && retries--) { |
| 489 TimeTicks ticks_start = TimeTicks::HighResNow(); | 489 TimeTicks ticks_start = GetTicks(); |
| 490 // Loop until we can detect that the clock has changed. Non-HighRes timers | 490 // Loop until we can detect that the clock has changed. Non-HighRes timers |
| 491 // will increment in chunks, e.g. 15ms. By spinning until we see a clock | 491 // will increment in chunks, e.g. 15ms. By spinning until we see a clock |
| 492 // change, we detect the minimum time between measurements. | 492 // change, we detect the minimum time between measurements. |
| 493 do { | 493 do { |
| 494 delta = TimeTicks::HighResNow() - ticks_start; | 494 delta = GetTicks() - ticks_start; |
| 495 } while (delta.InMilliseconds() == 0); | 495 } while (delta.InMilliseconds() == 0); |
| 496 | 496 |
| 497 if (delta.InMicroseconds() <= kTargetGranularityUs) | 497 if (delta.InMicroseconds() <= kTargetGranularityUs) |
| 498 success = true; | 498 success = true; |
| 499 } | 499 } |
| 500 | 500 |
| 501 // In high resolution mode, we expect to see the clock increment | 501 // In high resolution mode, we expect to see the clock increment |
| 502 // in intervals less than 15ms. | 502 // in intervals less than 15ms. |
| 503 EXPECT_TRUE(success); | 503 EXPECT_TRUE(success); |
| 504 } | 504 } |
| 505 | 505 |
| 506 TEST(TimeTicks, HighResNow) { |
| 507 HighResClockTest(&TimeTicks::HighResNow); |
| 508 } |
| 509 |
| 510 TEST(TimeTicks, NowFromSystemTraceTime) { |
| 511 // Re-use HighResNow test for now since clock properties are identical. |
| 512 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); |
| 513 } |
| 514 |
| 506 TEST(TimeDelta, FromAndIn) { | 515 TEST(TimeDelta, FromAndIn) { |
| 507 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); | 516 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); |
| 508 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); | 517 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); |
| 509 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); | 518 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); |
| 510 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); | 519 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); |
| 511 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == | 520 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == |
| 512 TimeDelta::FromMicroseconds(2000)); | 521 TimeDelta::FromMicroseconds(2000)); |
| 513 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); | 522 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); |
| 514 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); | 523 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); |
| 515 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); | 524 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 exploded.minute = 0; | 563 exploded.minute = 0; |
| 555 exploded.second = 0; | 564 exploded.second = 0; |
| 556 exploded.millisecond = 0; | 565 exploded.millisecond = 0; |
| 557 Time t = Time::FromUTCExploded(exploded); | 566 Time t = Time::FromUTCExploded(exploded); |
| 558 // Unix 1970 epoch. | 567 // Unix 1970 epoch. |
| 559 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); | 568 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); |
| 560 | 569 |
| 561 // We can't test 1601 epoch, since the system time functions on Linux | 570 // We can't test 1601 epoch, since the system time functions on Linux |
| 562 // only compute years starting from 1900. | 571 // only compute years starting from 1900. |
| 563 } | 572 } |
| OLD | NEW |