test-timer-list.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <stdint.h>
  39. #include <poll.h>
  40. #include "timer-list.h"
  41. #define SHORT_TIMEOUT 100
  42. #define LONG_TIMEOUT (60 * 1000)
  43. #define SPEED_TEST_NO_ITEMS 10000
  44. #define HEAP_TEST_NO_ITEMS 20
  45. /*
  46. * Valid heap checking is slow
  47. */
  48. #define HEAP_SPEED_TEST_NO_ITEMS 1000
  49. static int timer_list_fn1_called = 0;
  50. /*
  51. * Reimplementation of timer_list_entry_time_to_expire
  52. */
  53. static uint8_t
  54. time_to_expire(uint8_t expire_time, uint8_t current_time)
  55. {
  56. uint8_t diff, half_interval;
  57. diff = expire_time - current_time;
  58. half_interval = ~0;
  59. half_interval /= 2;
  60. if (diff > half_interval) {
  61. return (0);
  62. }
  63. return (diff);
  64. }
  65. static void
  66. check_time_to_expire(void)
  67. {
  68. unsigned int current_time, delta, expire_time, res;
  69. for (current_time = 0; current_time <= 255 * 2; current_time++) {
  70. for (delta = 0; delta < 255; delta++) {
  71. expire_time = current_time + delta;
  72. res = time_to_expire((uint8_t)expire_time, (uint8_t)current_time);
  73. if (delta < 128) {
  74. assert(res == delta);
  75. } else {
  76. assert(res == 0);
  77. }
  78. }
  79. }
  80. }
  81. static int
  82. timer_list_fn1(void *data1, void *data2)
  83. {
  84. timer_list_fn1_called++;
  85. assert(data1 == &timer_list_fn1_called);
  86. assert(data2 == timer_list_fn1);
  87. return (0);
  88. }
  89. static void
  90. check_timer_list_basics(void)
  91. {
  92. struct timer_list tlist;
  93. struct timer_list_entry *tlist_entry;
  94. struct timer_list_entry *tlist_speed_entry[SPEED_TEST_NO_ITEMS];
  95. int i;
  96. timer_list_init(&tlist);
  97. assert(timer_list_add(&tlist, 0, timer_list_fn1, NULL, NULL) == NULL);
  98. assert(timer_list_add(&tlist, TIMER_LIST_MAX_INTERVAL + 1, timer_list_fn1, NULL, NULL) == NULL);
  99. assert(timer_list_add(&tlist, 1, NULL, NULL, NULL) == NULL);
  100. /*
  101. * callback is called
  102. */
  103. timer_list_fn1_called = 0;
  104. tlist_entry = timer_list_add(&tlist, SHORT_TIMEOUT / 2, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  105. assert(tlist_entry != NULL);
  106. (void)poll(NULL, 0, SHORT_TIMEOUT);
  107. assert(timer_list_time_to_expire(&tlist) == 0);
  108. assert(timer_list_time_to_expire_ms(&tlist) == 0);
  109. timer_list_expire(&tlist);
  110. assert(timer_list_fn1_called == 1);
  111. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  112. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  113. timer_list_expire(&tlist);
  114. assert(timer_list_fn1_called == 1);
  115. /*
  116. * Callback is not called
  117. */
  118. timer_list_fn1_called = 0;
  119. tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  120. assert(tlist_entry != NULL);
  121. (void)poll(NULL, 0, SHORT_TIMEOUT);
  122. assert(timer_list_time_to_expire(&tlist) > 0);
  123. assert(timer_list_time_to_expire_ms(&tlist) > 0);
  124. timer_list_expire(&tlist);
  125. assert(timer_list_fn1_called == 0);
  126. /*
  127. * Delete entry
  128. */
  129. timer_list_delete(&tlist, tlist_entry);
  130. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  131. assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
  132. /*
  133. * Test speed and more entries
  134. */
  135. timer_list_fn1_called = 0;
  136. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  137. tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
  138. timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  139. assert(tlist_speed_entry[i] != NULL);
  140. }
  141. for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
  142. timer_list_reschedule(&tlist, tlist_speed_entry[i]);
  143. }
  144. (void)poll(NULL, 0, SHORT_TIMEOUT);
  145. timer_list_expire(&tlist);
  146. assert(timer_list_fn1_called == SPEED_TEST_NO_ITEMS);
  147. timer_list_free(&tlist);
  148. }
  149. static void
  150. check_timer_heap(void)
  151. {
  152. struct timer_list tlist;
  153. uint32_t u32;
  154. int i;
  155. int j;
  156. struct timer_list_entry *tlist_entry[HEAP_TEST_NO_ITEMS];
  157. struct timer_list_entry *tlist_entry_small;
  158. struct timer_list_entry *tlist_speed_entry[HEAP_SPEED_TEST_NO_ITEMS];
  159. timer_list_init(&tlist);
  160. /*
  161. * Empty tlist
  162. */
  163. assert(tlist.allocated == 0);
  164. assert(tlist.size == 0);
  165. u32 = ~((uint32_t)0);
  166. assert(timer_list_time_to_expire_ms(&tlist) == u32);
  167. assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
  168. /*
  169. * Adding increasing numbers keeps heap property so there should be no reshufling
  170. */
  171. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  172. tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  173. timer_list_fn1, NULL, NULL);
  174. assert(tlist_entry[i] != NULL);
  175. assert(tlist.size == i + 1);
  176. assert(timer_list_debug_is_valid_heap(&tlist));
  177. for (j = 0; j < i + 1; j++) {
  178. assert(tlist.entries[j] == tlist_entry[j]);
  179. }
  180. }
  181. /*
  182. * Add small item which should become first item in tlist entries to keep heap
  183. * property
  184. */
  185. tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
  186. assert(timer_list_debug_is_valid_heap(&tlist));
  187. assert(tlist.size == i + 1);
  188. assert(tlist.entries[0] == tlist_entry_small);
  189. /*
  190. * Remove all items
  191. */
  192. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  193. timer_list_delete(&tlist, tlist_entry[i]);
  194. assert(timer_list_debug_is_valid_heap(&tlist));
  195. assert(tlist.entries[0] == tlist_entry_small);
  196. }
  197. /*
  198. * Remove small item
  199. */
  200. timer_list_delete(&tlist, tlist_entry_small);
  201. assert(timer_list_debug_is_valid_heap(&tlist));
  202. /*
  203. * Add items in reverse order
  204. */
  205. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  206. tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  207. timer_list_fn1, NULL, NULL);
  208. assert(tlist_entry[i] != NULL);
  209. assert(tlist.size == i + 1);
  210. assert(timer_list_debug_is_valid_heap(&tlist));
  211. }
  212. /*
  213. * Remove all items
  214. */
  215. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  216. timer_list_delete(&tlist, tlist_entry[i]);
  217. assert(timer_list_debug_is_valid_heap(&tlist));
  218. }
  219. /*
  220. * Add items in standard and reverse order
  221. */
  222. for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
  223. tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  224. timer_list_fn1, NULL, NULL);
  225. assert(tlist_entry[i] != NULL);
  226. assert(tlist.size == (i * 2) + 1);
  227. tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  228. timer_list_fn1, NULL, NULL);
  229. assert(tlist_entry[i] != NULL);
  230. assert(tlist.size == (i * 2) + 2);
  231. assert(timer_list_debug_is_valid_heap(&tlist));
  232. }
  233. /*
  234. * Remove items
  235. */
  236. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  237. timer_list_delete(&tlist, tlist_entry[i]);
  238. assert(timer_list_debug_is_valid_heap(&tlist));
  239. }
  240. assert(tlist.size == 0);
  241. /*
  242. * Add items again
  243. */
  244. for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
  245. tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
  246. timer_list_fn1, NULL, NULL);
  247. assert(tlist_entry[i] != NULL);
  248. assert(tlist.size == (i * 2) + 1);
  249. tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
  250. timer_list_fn1, NULL, NULL);
  251. assert(tlist_entry[i] != NULL);
  252. assert(tlist.size == (i * 2) + 2);
  253. assert(timer_list_debug_is_valid_heap(&tlist));
  254. }
  255. tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
  256. assert(timer_list_debug_is_valid_heap(&tlist));
  257. assert(tlist.entries[0] == tlist_entry_small);
  258. /*
  259. * And try reschedule
  260. */
  261. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  262. timer_list_reschedule(&tlist, tlist_entry[i]);
  263. assert(timer_list_debug_is_valid_heap(&tlist));
  264. assert(tlist.entries[0] == tlist_entry_small);
  265. }
  266. /*
  267. * Try delete
  268. */
  269. timer_list_delete(&tlist, tlist_entry_small);
  270. assert(timer_list_debug_is_valid_heap(&tlist));
  271. for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
  272. timer_list_delete(&tlist, tlist_entry[i]);
  273. assert(timer_list_debug_is_valid_heap(&tlist));
  274. }
  275. assert(tlist.size == 0);
  276. /*
  277. * Speed test
  278. */
  279. for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
  280. tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
  281. timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
  282. assert(tlist_speed_entry[i] != NULL);
  283. assert(timer_list_debug_is_valid_heap(&tlist));
  284. }
  285. for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
  286. timer_list_reschedule(&tlist, tlist_speed_entry[i]);
  287. assert(timer_list_debug_is_valid_heap(&tlist));
  288. }
  289. /*
  290. * Free list
  291. */
  292. timer_list_free(&tlist);
  293. }
  294. int
  295. main(void)
  296. {
  297. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  298. check_time_to_expire();
  299. check_timer_heap();
  300. check_timer_list_basics();
  301. assert(PR_Cleanup() == PR_SUCCESS);
  302. return (0);
  303. }