TimeTests.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Math/MathUtils.h>
  9. #include <AzCore/Time/TimeSystem.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. namespace UnitTest
  12. {
  13. class TimeTests : public LeakDetectionFixture
  14. {
  15. public:
  16. void SetUp() override
  17. {
  18. m_timeSystem = AZStd::make_unique<AZ::TimeSystem>();
  19. }
  20. AZStd::unique_ptr<AZ::TimeSystem> m_timeSystem;
  21. };
  22. TEST_F(TimeTests, TestConversionUsToMs)
  23. {
  24. AZ::TimeUs timeUs = AZ::TimeUs{ 1000 };
  25. AZ::TimeMs timeMs = AZ::TimeUsToMs(timeUs);
  26. EXPECT_EQ(timeMs, AZ::TimeMs{ 1 });
  27. }
  28. TEST_F(TimeTests, TestConversionMsToUs)
  29. {
  30. AZ::TimeMs timeMs = AZ::TimeMs{ 1000 };
  31. AZ::TimeUs timeUs = AZ::TimeMsToUs(timeMs);
  32. EXPECT_EQ(timeUs, AZ::TimeUs{ 1000000 });
  33. }
  34. TEST_F(TimeTests, TestConversionTimeMsToSeconds)
  35. {
  36. AZ::TimeMs timeMs = AZ::TimeMs{ 1000 };
  37. float timeSecondsFloat = AZ::TimeMsToSeconds(timeMs);
  38. EXPECT_TRUE(AZ::IsClose(timeSecondsFloat, 1.0f));
  39. double timeSecondsDouble = AZ::TimeMsToSecondsDouble(timeMs);
  40. EXPECT_TRUE(AZ::IsClose(timeSecondsDouble, 1.0));
  41. }
  42. TEST_F(TimeTests, TestConversionSecondsToTimeUs)
  43. {
  44. double seconds = 1.0;
  45. AZ::TimeUs timeUs = AZ::SecondsToTimeUs(seconds);
  46. EXPECT_EQ(timeUs, AZ::TimeUs{ 1000000 });
  47. }
  48. TEST_F(TimeTests, TestConversionSecondsToTimeMs)
  49. {
  50. double seconds = 1.0;
  51. AZ::TimeMs timeMs = AZ::SecondsToTimeMs(seconds);
  52. EXPECT_EQ(timeMs, AZ::TimeMs{ 1000 });
  53. }
  54. TEST_F(TimeTests, TestClocks)
  55. {
  56. AZ::TimeUs timeUs = AZ::GetElapsedTimeUs();
  57. AZ::TimeMs timeMs = AZ::GetElapsedTimeMs();
  58. AZ::TimeMs timeUsToMs = AZ::TimeUsToMs(timeUs);
  59. int64_t delta = static_cast<int64_t>(timeMs) - static_cast<int64_t>(timeUsToMs);
  60. EXPECT_LT(abs(delta), 1);
  61. }
  62. TEST_F(TimeTests, QueryCPUThreadTime_ReturnsNonZero)
  63. {
  64. (void)AZStd::GetCpuThreadTimeNowMicrosecond();
  65. // Windows need at least 50 milliseconds of running time to measure the processer tick scale
  66. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(50));
  67. // The cpu thread time should now return non-zero values
  68. AZStd::chrono::microseconds initialCpuThreadTime = AZStd::GetCpuThreadTimeNowMicrosecond();
  69. // Sleep for 1 millisecond (or 1000 microseconds) and check the CPU time
  70. // less than 1 miilisecond of CPU should have elapsed
  71. // Now due to the fact that thread sleep can last got longer than it's duration
  72. // this test is only checking that the new CPUThreadTime is at least greater than or equal to the initial
  73. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(1));
  74. AZStd::chrono::microseconds newCpuThreadTime = AZStd::GetCpuThreadTimeNowMicrosecond();
  75. EXPECT_GT((newCpuThreadTime - initialCpuThreadTime).count(), 0);
  76. }
  77. } // namespace UnitTest