time.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/time.hpp>
  6. #include <uavcan/Timestamp.hpp>
  7. TEST(Time, Monotonic)
  8. {
  9. using uavcan::MonotonicDuration;
  10. using uavcan::MonotonicTime;
  11. MonotonicTime m1;
  12. MonotonicTime m2 = MonotonicTime::fromMSec(1);
  13. MonotonicDuration md1 = m2 - m1; // 1000
  14. MonotonicDuration md2 = m1 - m2; // -1000
  15. ASSERT_EQ(0, m1.toUSec());
  16. ASSERT_EQ(1000, m2.toUSec());
  17. ASSERT_EQ(1000, md1.toUSec());
  18. ASSERT_EQ(-1000, md2.toUSec());
  19. ASSERT_LT(m1, m2);
  20. ASSERT_LE(m1, m2);
  21. ASSERT_NE(m1, m2);
  22. ASSERT_TRUE(m1.isZero());
  23. ASSERT_FALSE(m2.isZero());
  24. ASSERT_GT(md1, md2);
  25. ASSERT_GE(md1, md2);
  26. ASSERT_NE(md1, md2);
  27. ASSERT_FALSE(md1.isZero());
  28. ASSERT_TRUE(md1.isPositive());
  29. ASSERT_TRUE(md2.isNegative());
  30. ASSERT_EQ(0, (md1 + md2).toUSec());
  31. ASSERT_EQ(2000, (md1 - md2).toUSec());
  32. md1 *= 2; // 2000
  33. ASSERT_EQ(2000, md1.toUSec());
  34. md2 += md1; // md2 = -1000 + 2000
  35. ASSERT_EQ(1000, md2.toUSec());
  36. ASSERT_EQ(-1000, (-md2).toUSec());
  37. /*
  38. * To string
  39. */
  40. ASSERT_EQ("0.000000", m1.toString());
  41. ASSERT_EQ("0.001000", m2.toString());
  42. ASSERT_EQ("0.002000", md1.toString());
  43. ASSERT_EQ("-0.001000", (-md2).toString());
  44. ASSERT_EQ("1001.000001", MonotonicTime::fromUSec(1001000001).toString());
  45. ASSERT_EQ("-1001.000001", MonotonicDuration::fromUSec(-1001000001).toString());
  46. }
  47. TEST(Time, Utc)
  48. {
  49. using uavcan::UtcDuration;
  50. using uavcan::UtcTime;
  51. using uavcan::Timestamp;
  52. Timestamp ts;
  53. ts.usec = 9000;
  54. UtcTime u1(ts);
  55. ASSERT_EQ(9000, u1.toUSec());
  56. ts.usec *= 2;
  57. u1 = ts;
  58. ASSERT_EQ(18000, u1.toUSec());
  59. ts = UtcTime::fromUSec(12345678900);
  60. ASSERT_EQ(12345678900, ts.usec);
  61. /*
  62. * To string
  63. */
  64. ASSERT_EQ("0.018000", u1.toString());
  65. ASSERT_EQ("12345.678900", UtcTime(ts).toString());
  66. }
  67. TEST(Time, Overflow)
  68. {
  69. using uavcan::MonotonicDuration;
  70. using uavcan::MonotonicTime;
  71. MonotonicTime max_4 = MonotonicTime::fromUSec(MonotonicTime::getMax().toUSec() / 4);
  72. MonotonicDuration max_4_duration = max_4 - MonotonicTime();
  73. std::cout << max_4 << std::endl;
  74. ASSERT_EQ(max_4_duration.toUSec(), max_4.toUSec());
  75. MonotonicTime max = (((max_4 + max_4_duration) + max_4_duration) + max_4_duration) + max_4_duration;
  76. ASSERT_EQ(max, MonotonicTime::getMax()); // Must not overflow
  77. MonotonicTime min;
  78. min -= max_4_duration;
  79. ASSERT_EQ(min, MonotonicTime()); // Must not underflow
  80. }