timedelta_test_suite.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <iostream>
  24. #include <helpers/test_suite.hpp>
  25. #include <xlnt/utils/timedelta.hpp>
  26. class timedelta_test_suite : public test_suite
  27. {
  28. public:
  29. timedelta_test_suite()
  30. {
  31. register_test(test_from_number);
  32. register_test(test_round_trip);
  33. register_test(test_to_number);
  34. register_test(test_carry);
  35. }
  36. void test_from_number()
  37. {
  38. auto td = xlnt::timedelta::from_number(1.0423726852L);
  39. xlnt_assert(td.days == 1);
  40. xlnt_assert(td.hours == 1);
  41. xlnt_assert(td.minutes == 1);
  42. xlnt_assert(td.seconds == 1);
  43. xlnt_assert(td.microseconds == 1);
  44. }
  45. void test_round_trip()
  46. {
  47. double time = 3.14159265359;
  48. auto td = xlnt::timedelta::from_number(time);
  49. auto time_rt = td.to_number();
  50. xlnt_assert_delta(time, time_rt, 1E-9);
  51. }
  52. void test_to_number()
  53. {
  54. xlnt::timedelta td(1, 1, 1, 1, 1);
  55. xlnt_assert_delta(td.to_number(), 1.0423726852L, 1E-9);
  56. }
  57. void test_carry()
  58. {
  59. // We want a time that rolls over to the next second, minute, and hour
  60. // Start off with a time 1 microsecond before the next hour
  61. xlnt::timedelta td(1, 23, 59, 59, 999999);
  62. auto number = td.to_number();
  63. // Add 600 nanoseconds to the raw number which represents time as a fraction of a day
  64. // In other words, 6 tenths of a millionth of a sixtieth of a sixtieth of a twenty-fourth of a day
  65. number += (0.6 / 1000000) / 60 / 60 / 24;
  66. auto rollover = xlnt::timedelta::from_number(number);
  67. xlnt_assert_equals(rollover.days, 2);
  68. xlnt_assert_equals(rollover.hours, 0);
  69. xlnt_assert_equals(rollover.minutes, 0);
  70. xlnt_assert_equals(rollover.seconds, 0);
  71. xlnt_assert_equals(rollover.microseconds, 0);
  72. }
  73. };
  74. static timedelta_test_suite x;