tst-difftime.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Test for difftime
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <time.h>
  16. #include <support/check.h>
  17. #if __GNUC_PREREQ (5, 0)
  18. static void
  19. test_difftime_helper (time_t t1, time_t t0, double exp_val)
  20. {
  21. double sub = difftime (t1, t0);
  22. if (sub != exp_val)
  23. FAIL_EXIT1 ("*** Difftime returned %f (expected %f)\n", sub, exp_val);
  24. }
  25. static int
  26. do_test (void)
  27. {
  28. time_t t = 1383791700; /* Provide reproducible start value. */
  29. /* Check if difftime works with current time. */
  30. test_difftime_helper (t + 1800, t - 1800, 3600.0);
  31. test_difftime_helper (t - 1800, t + 1800, -3600.0);
  32. t = 0x7FFFFFFF;
  33. /* Check if we run on port with 32 bit time_t size */
  34. time_t tov;
  35. if (__builtin_add_overflow (t, 1, &tov))
  36. return 0;
  37. /* Check if the time is converted after 32 bit time_t overflow. */
  38. test_difftime_helper (t + 1800, t - 1800, 3600.0);
  39. test_difftime_helper (t - 1800, t + 1800, -3600.0);
  40. t = tov;
  41. test_difftime_helper (t + 1800, t - 1800, 3600.0);
  42. test_difftime_helper (t - 1800, t + 1800, -3600.0);
  43. return 0;
  44. }
  45. #include <support/test-driver.c>
  46. #else
  47. #include <support/test-driver.h>
  48. int
  49. main (void)
  50. {
  51. return EXIT_UNSUPPORTED;
  52. }
  53. #endif