testc_time.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <sys/time.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include "faux/time.h"
  7. #define TNUM1 3
  8. int testc_faux_nsec_timespec_conversion(void)
  9. {
  10. uint64_t n[TNUM1] = {
  11. 123456789l,
  12. 880123456789l,
  13. 789000000000l
  14. };
  15. struct timespec e[TNUM1] = {
  16. {.tv_sec = 0, .tv_nsec = 123456789l},
  17. {.tv_sec = 880, .tv_nsec = 123456789l},
  18. {.tv_sec = 789, .tv_nsec = 0l}
  19. };
  20. unsigned int i = 0;
  21. int ret = 0;
  22. // From nsec to struct timespec
  23. for (i = 0; i < TNUM1; i++) {
  24. struct timespec res = {};
  25. faux_nsec_to_timespec(&res, n[i]);
  26. if (faux_timespec_cmp(&res, &e[i]) != 0) {
  27. printf("nsec_to_timespec: Test %u failed\n", i);
  28. ret = -1;
  29. }
  30. }
  31. // From struct timespec to nsec
  32. for (i = 0; i < TNUM1; i++) {
  33. uint64_t res = 0;
  34. res = faux_timespec_to_nsec(&e[i]);
  35. if (res != n[i]) {
  36. printf("timespec_to_nsec: Test %u failed\n", i);
  37. ret = -1;
  38. }
  39. }
  40. return ret;
  41. }
  42. #define TNUM2 2
  43. int testc_faux_timespec_diff(void)
  44. {
  45. struct timespec val1[TNUM2] = {
  46. {.tv_sec = 0, .tv_nsec = 123456789l},
  47. {.tv_sec = 880, .tv_nsec = 2l},
  48. };
  49. struct timespec val2[TNUM2] = {
  50. {.tv_sec = 1, .tv_nsec = 123456789l},
  51. {.tv_sec = 770, .tv_nsec = 3l},
  52. };
  53. struct timespec e[TNUM2] = {
  54. {.tv_sec = 0, .tv_nsec = 0l},
  55. {.tv_sec = 109, .tv_nsec = 999999999l},
  56. };
  57. bool_t eretval[TNUM2] = {
  58. BOOL_FALSE,
  59. BOOL_TRUE
  60. };
  61. int eerrno[TNUM2] = {
  62. EOVERFLOW,
  63. 0
  64. };
  65. int ret = 0;
  66. int i = 0;
  67. // Diff
  68. for (i = 0; i < TNUM2; i++) {
  69. struct timespec res = {};
  70. bool_t retval = 0;
  71. int err = 0;
  72. printf("Test %u:\n", i);
  73. printf("val1=%ld:%ld, val2=%ld:%ld\n",
  74. val1[i].tv_sec, val1[i].tv_nsec,
  75. val2[i].tv_sec, val2[i].tv_nsec);
  76. retval = faux_timespec_diff(&res, &val1[i], &val2[i]);
  77. err = errno;
  78. printf("diff=%ld:%ld, etalon=%ld:%ld\n",
  79. res.tv_sec, res.tv_nsec,
  80. e[i].tv_sec, e[i].tv_nsec);
  81. if (faux_timespec_cmp(&res, &e[i]) != 0) {
  82. printf("Test %u timespec cmp failed\n", i);
  83. ret = -1;
  84. }
  85. if (retval != eretval[i]) {
  86. printf("Test %u retval failed. Actual: %d. Need: %d.\n",
  87. i, retval, eretval[i]);
  88. ret = -1;
  89. }
  90. if (!retval && (err != eerrno[i])) {
  91. printf("Test %u errno failed\n", i);
  92. ret = -1;
  93. }
  94. }
  95. return ret;
  96. }
  97. #define TNUM3 2
  98. int testc_faux_timespec_sum(void)
  99. {
  100. struct timespec val1[TNUM3] = {
  101. {.tv_sec = 0, .tv_nsec = 123456789l},
  102. {.tv_sec = 880, .tv_nsec = 2l},
  103. };
  104. struct timespec val2[TNUM3] = {
  105. {.tv_sec = 1, .tv_nsec = 910000000l},
  106. {.tv_sec = 710, .tv_nsec = 8l},
  107. };
  108. struct timespec e[TNUM3] = {
  109. {.tv_sec = 2, .tv_nsec = 33456789l},
  110. {.tv_sec = 1590, .tv_nsec = 10l},
  111. };
  112. int ret = 0;
  113. int i = 0;
  114. // Sum
  115. for (i = 0; i < TNUM3; i++) {
  116. struct timespec res = {};
  117. printf("Test %u:\n", i);
  118. printf("val1=%ld:%ld, val2=%ld:%ld\n",
  119. val1[i].tv_sec, val1[i].tv_nsec,
  120. val2[i].tv_sec, val2[i].tv_nsec);
  121. if (!faux_timespec_sum(&res, &val1[i], &val2[i])) {
  122. printf("Test %u retval failed\n", i);
  123. ret = -1;
  124. }
  125. printf("sum=%ld:%ld, etalon=%ld:%ld\n",
  126. res.tv_sec, res.tv_nsec,
  127. e[i].tv_sec, e[i].tv_nsec);
  128. if (faux_timespec_cmp(&res, &e[i]) != 0) {
  129. printf("Test %u timespec cmp failed\n", i);
  130. ret = -1;
  131. }
  132. }
  133. return ret;
  134. }
  135. int testc_faux_timespec_now(void)
  136. {
  137. int ret = 0;
  138. struct timespec before = {};
  139. struct timespec now = {};
  140. struct timespec after = {};
  141. struct timespec interval = {};
  142. faux_nsec_to_timespec(&interval, 1000000l);
  143. faux_timespec_now(&now);
  144. faux_timespec_diff(&before, &now, &interval);
  145. faux_timespec_sum(&after, &now, &interval);
  146. if (!faux_timespec_before_now(&before))
  147. ret = -1;
  148. if (!faux_timespec_before_now(&now)) // Formally now is before now
  149. ret = -1;
  150. if (faux_timespec_before_now(&after))
  151. ret = -1;
  152. return ret;
  153. }