check_std_c_time.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "torture.h"
  22. #include "csync_time.h"
  23. #include "std/c_time.h"
  24. static void check_c_tspecdiff(void **state)
  25. {
  26. struct timespec start, finish, diff;
  27. (void) state; /* unused */
  28. csync_gettime(&start);
  29. csync_gettime(&finish);
  30. diff = c_tspecdiff(finish, start);
  31. assert_int_equal(diff.tv_sec, 0);
  32. assert_true(diff.tv_nsec >= 0);
  33. }
  34. static void check_c_tspecdiff_five(void **state)
  35. {
  36. struct timespec start, finish, diff;
  37. (void) state; /* unused */
  38. csync_gettime(&start);
  39. sleep(5);
  40. csync_gettime(&finish);
  41. diff = c_tspecdiff(finish, start);
  42. assert_int_equal(diff.tv_sec, 5);
  43. assert_true(diff.tv_nsec > 0);
  44. }
  45. static void check_c_secdiff(void **state)
  46. {
  47. struct timespec start, finish;
  48. double diff;
  49. (void) state; /* unused */
  50. csync_gettime(&start);
  51. csync_gettime(&finish);
  52. diff = c_secdiff(finish, start);
  53. assert_true(diff >= 0.00 && diff < 1.00);
  54. }
  55. static void check_c_secdiff_three(void **state)
  56. {
  57. struct timespec start, finish;
  58. double diff;
  59. (void) state; /* unused */
  60. csync_gettime(&start);
  61. sleep(3);
  62. csync_gettime(&finish);
  63. diff = c_secdiff(finish, start);
  64. assert_true(diff > 3.00 && diff < 4.00);
  65. }
  66. int torture_run_tests(void)
  67. {
  68. const UnitTest tests[] = {
  69. unit_test(check_c_tspecdiff),
  70. unit_test(check_c_tspecdiff_five),
  71. unit_test(check_c_secdiff),
  72. unit_test(check_c_secdiff_three),
  73. };
  74. return run_tests(tests);
  75. }