tst-time.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Test time function.
  2. Copyright (C) 2024-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 <unistd.h>
  17. #include <support/check.h>
  18. #include <support/test-driver.h>
  19. int
  20. do_test (void)
  21. {
  22. time_t t1, t2, t3, t4, t5, t6;
  23. /* Verify that the calls to time succeed, that the value returned
  24. directly equals that returned through the pointer passed, and
  25. that the time does not decrease. */
  26. t1 = time (&t2);
  27. TEST_VERIFY_EXIT (t1 != (time_t) -1);
  28. TEST_VERIFY (t1 == t2);
  29. t3 = time (NULL);
  30. TEST_VERIFY_EXIT (t3 != (time_t) -1);
  31. TEST_VERIFY (t3 >= t1);
  32. /* Also verify that after sleeping, the time returned has
  33. increased. */
  34. sleep (2);
  35. t4 = time (&t5);
  36. TEST_VERIFY_EXIT (t4 != (time_t) -1);
  37. TEST_VERIFY (t4 == t5);
  38. TEST_VERIFY (t4 > t3);
  39. t6 = time (NULL);
  40. TEST_VERIFY_EXIT (t6 != (time_t) -1);
  41. TEST_VERIFY (t6 >= t4);
  42. return 0;
  43. }
  44. #include <support/test-driver.c>