test_entrypoints.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2019 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #define SG_EXTERN
  27. #include "sg_assert.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include "sg_macros.h"
  32. #include "sg_entrypoint.h"
  33. #include "sg_entrypoints.c"
  34. #include <sagui.h>
  35. static int
  36. entrypoints_iter_empty(__SG_UNUSED void *cls,
  37. __SG_UNUSED struct sg_entrypoint *entrypoint) {
  38. return 0;
  39. }
  40. static int entrypoints_iter_123(void *cls, struct sg_entrypoint *entrypoint) {
  41. ASSERT(strcmp(cls, "foo") == 0);
  42. ASSERT(entrypoint);
  43. return 123;
  44. }
  45. static int entrypoints_iter_concat(void *cls,
  46. struct sg_entrypoint *entrypoint) {
  47. strcat(cls, sg_entrypoint_name(entrypoint));
  48. return 0;
  49. }
  50. static void test__entrypoints_add(struct sg_entrypoints *entrypoints) {
  51. struct sg_entrypoint entrypoint, *item;
  52. ASSERT(!entrypoints->list);
  53. ASSERT(entrypoints->count == 0);
  54. entrypoint.name = strdup("ghi");
  55. entrypoint.user_data = NULL;
  56. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, "foo") == 0);
  57. ASSERT(entrypoints->list);
  58. ASSERT(entrypoints->count == 1);
  59. item = entrypoints->list;
  60. ASSERT(strcmp(item->name, "ghi") == 0);
  61. ASSERT(strcmp(item->user_data, "foo") == 0);
  62. entrypoint.name = strdup("def");
  63. entrypoint.user_data = NULL;
  64. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, "bar") == 0);
  65. ASSERT(entrypoints->count == 2);
  66. item = entrypoints->list;
  67. ASSERT(strcmp(item->name, "def") == 0);
  68. ASSERT(strcmp(item->user_data, "bar") == 0);
  69. item = entrypoints->list + 1;
  70. ASSERT(strcmp(item->name, "ghi") == 0);
  71. ASSERT(strcmp(item->user_data, "foo") == 0);
  72. entrypoint.name = strdup("abc");
  73. entrypoint.user_data = NULL;
  74. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, "foobar") == 0);
  75. ASSERT(entrypoints->count == 3);
  76. item = entrypoints->list;
  77. ASSERT(strcmp(item->name, "abc") == 0);
  78. ASSERT(strcmp(item->user_data, "foobar") == 0);
  79. item = entrypoints->list + 1;
  80. ASSERT(strcmp(item->name, "def") == 0);
  81. ASSERT(strcmp(item->user_data, "bar") == 0);
  82. item = entrypoints->list + 2;
  83. ASSERT(strcmp(item->name, "ghi") == 0);
  84. ASSERT(strcmp(item->user_data, "foo") == 0);
  85. entrypoint.name = strdup("abc");
  86. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == EALREADY);
  87. sg_free(entrypoint.name);
  88. entrypoint.name = strdup("def");
  89. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == EALREADY);
  90. sg_free(entrypoint.name);
  91. }
  92. static void test__entrypoints_rm(struct sg_entrypoints *entrypoints) {
  93. struct sg_entrypoint entrypoint, *item;
  94. sg_entrypoints_clear(entrypoints);
  95. ASSERT(sg__entrypoints_rm(entrypoints, "") == ENOENT);
  96. entrypoint.name = strdup("/foo");
  97. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == 0);
  98. entrypoint.name = strdup("/bar");
  99. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == 0);
  100. entrypoint.name = strdup("/foobar");
  101. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == 0);
  102. ASSERT(entrypoints->count == 3);
  103. ASSERT(sg__entrypoints_rm(entrypoints, "/foo") == 0);
  104. ASSERT(entrypoints->count == 2);
  105. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foo") == ENOENT);
  106. ASSERT(sg_entrypoints_find(entrypoints, &item, "/bar") == 0);
  107. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foobar") == 0);
  108. ASSERT(sg__entrypoints_rm(entrypoints, "/bar") == 0);
  109. ASSERT(entrypoints->count == 1);
  110. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foo") == ENOENT);
  111. ASSERT(sg_entrypoints_find(entrypoints, &item, "/bar") == ENOENT);
  112. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foobar") == 0);
  113. ASSERT(sg__entrypoints_rm(entrypoints, "/foobar") == 0);
  114. ASSERT(entrypoints->count == 0);
  115. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foo") == ENOENT);
  116. ASSERT(sg_entrypoints_find(entrypoints, &item, "/bar") == ENOENT);
  117. ASSERT(sg_entrypoints_find(entrypoints, &item, "/foobar") == ENOENT);
  118. ASSERT(sg__entrypoints_rm(entrypoints, "/foobar") == ENOENT);
  119. }
  120. static void test__entrypoints_find(struct sg_entrypoints *entrypoints) {
  121. struct sg_entrypoint entrypoint, *item;
  122. sg_entrypoints_clear(entrypoints);
  123. entrypoint.name = strdup("foo");
  124. ASSERT(sg__entrypoints_find(entrypoints, &entrypoint, &item) == ENOENT);
  125. sg_free(entrypoint.name);
  126. entrypoint.name = strdup("bar");
  127. ASSERT(sg__entrypoints_find(entrypoints, &entrypoint, &item) == ENOENT);
  128. sg_free(entrypoint.name);
  129. entrypoint.name = strdup("foo");
  130. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == 0);
  131. entrypoint.name = strdup("bar");
  132. ASSERT(sg__entrypoints_add(entrypoints, &entrypoint, NULL) == 0);
  133. entrypoint.name = strdup("foo");
  134. ASSERT(sg__entrypoints_find(entrypoints, &entrypoint, &item) == 0);
  135. sg_free(entrypoint.name);
  136. entrypoint.name = strdup("bar");
  137. ASSERT(sg__entrypoints_find(entrypoints, &entrypoint, &item) == 0);
  138. sg_free(entrypoint.name);
  139. }
  140. static void test_entrypoints_add(struct sg_entrypoints *entrypoints) {
  141. struct sg_entrypoint *item;
  142. sg_entrypoints_clear(entrypoints);
  143. ASSERT(sg_entrypoints_add(NULL, "foo", "bar") == EINVAL);
  144. ASSERT(sg_entrypoints_add(entrypoints, NULL, "bar") == EINVAL);
  145. ASSERT(entrypoints->count == 0);
  146. ASSERT(sg_entrypoints_add(entrypoints, "foo", NULL) == 0);
  147. ASSERT(sg_entrypoints_add(entrypoints, "bar", NULL) == 0);
  148. ASSERT(sg_entrypoints_add(entrypoints, "foobar", NULL) == 0);
  149. ASSERT(entrypoints->count == 3);
  150. item = entrypoints->list;
  151. ASSERT(strcmp(item->name, "/bar") == 0);
  152. item = entrypoints->list + 1;
  153. ASSERT(strcmp(item->name, "/foo") == 0);
  154. item = entrypoints->list + 2;
  155. ASSERT(strcmp(item->name, "/foobar") == 0);
  156. ASSERT(sg_entrypoints_find(entrypoints, &item, "foo") == 0);
  157. ASSERT(sg_entrypoints_find(entrypoints, &item, "bar") == 0);
  158. ASSERT(sg_entrypoints_find(entrypoints, &item, "foobar") == 0);
  159. }
  160. static void test_entrypoints_rm(struct sg_entrypoints *entrypoints) {
  161. struct sg_entrypoint *item;
  162. ASSERT(sg_entrypoints_rm(NULL, "") == EINVAL);
  163. ASSERT(sg_entrypoints_rm(entrypoints, NULL) == EINVAL);
  164. ASSERT(sg_entrypoints_clear(entrypoints) == 0);
  165. ASSERT(entrypoints->count == 0);
  166. ASSERT(sg_entrypoints_add(entrypoints, "foo", NULL) == 0);
  167. ASSERT(sg_entrypoints_add(entrypoints, "bar", NULL) == 0);
  168. ASSERT(sg_entrypoints_add(entrypoints, "foobar", NULL) == 0);
  169. ASSERT(entrypoints->count == 3);
  170. ASSERT(sg_entrypoints_rm(entrypoints, "foo") == 0);
  171. ASSERT(sg_entrypoints_find(entrypoints, &item, "foo") == ENOENT);
  172. ASSERT(sg_entrypoints_rm(entrypoints, "bar") == 0);
  173. ASSERT(sg_entrypoints_find(entrypoints, &item, "bar") == ENOENT);
  174. ASSERT(sg_entrypoints_rm(entrypoints, "foobar") == 0);
  175. ASSERT(sg_entrypoints_find(entrypoints, &item, "foobar") == ENOENT);
  176. ASSERT(sg_entrypoints_rm(entrypoints, "foobar") == ENOENT);
  177. }
  178. static void test_entrypoints_iter(struct sg_entrypoints *entrypoints) {
  179. char str[100];
  180. ASSERT(sg_entrypoints_iter(NULL, entrypoints_iter_empty, "foo") == EINVAL);
  181. ASSERT(sg_entrypoints_iter(entrypoints, NULL, "foo") == EINVAL);
  182. sg_entrypoints_clear(entrypoints);
  183. ASSERT(sg_entrypoints_iter(entrypoints, entrypoints_iter_empty, "foo") == 0);
  184. ASSERT(sg_entrypoints_iter(entrypoints, entrypoints_iter_123, "foo") == 0);
  185. ASSERT(sg_entrypoints_add(entrypoints, "foo", NULL) == 0);
  186. ASSERT(sg_entrypoints_add(entrypoints, "bar", NULL) == 0);
  187. ASSERT(sg_entrypoints_add(entrypoints, "foobar", NULL) == 0);
  188. ASSERT(entrypoints->count == 3);
  189. ASSERT(sg_entrypoints_iter(entrypoints, entrypoints_iter_empty, "foo") == 0);
  190. ASSERT(sg_entrypoints_iter(entrypoints, entrypoints_iter_123, "foo") == 123);
  191. memset(str, 0, sizeof(str));
  192. ASSERT(strcmp(str, "") == 0);
  193. ASSERT(sg_entrypoints_iter(entrypoints, entrypoints_iter_concat, str) == 0);
  194. ASSERT(strcmp(str, "/bar/foo/foobar") == 0);
  195. }
  196. static void test_entrypoints_clear(struct sg_entrypoints *entrypoints) {
  197. struct sg_entrypoint *item;
  198. ASSERT(sg_entrypoints_clear(NULL) == EINVAL);
  199. ASSERT(sg_entrypoints_clear(entrypoints) == 0);
  200. ASSERT(entrypoints->count == 0);
  201. ASSERT(sg_entrypoints_add(entrypoints, "foo", NULL) == 0);
  202. ASSERT(sg_entrypoints_add(entrypoints, "bar", NULL) == 0);
  203. ASSERT(sg_entrypoints_add(entrypoints, "foobar", NULL) == 0);
  204. ASSERT(entrypoints->count == 3);
  205. ASSERT(sg_entrypoints_clear(entrypoints) == 0);
  206. ASSERT(entrypoints->count == 0);
  207. ASSERT(sg_entrypoints_find(entrypoints, &item, "foo") == ENOENT);
  208. ASSERT(sg_entrypoints_find(entrypoints, &item, "bar") == ENOENT);
  209. ASSERT(sg_entrypoints_find(entrypoints, &item, "foobar") == ENOENT);
  210. }
  211. static void test_entrypoints_find(struct sg_entrypoints *entrypoints) {
  212. struct sg_entrypoint *item;
  213. ASSERT(sg_entrypoints_find(NULL, &item, "") == EINVAL);
  214. ASSERT(sg_entrypoints_find(entrypoints, NULL, "") == EINVAL);
  215. ASSERT(sg_entrypoints_find(entrypoints, &item, NULL) == EINVAL);
  216. ASSERT(sg_entrypoints_clear(entrypoints) == 0);
  217. ASSERT(sg_entrypoints_add(entrypoints, "foo", NULL) == 0);
  218. ASSERT(sg_entrypoints_add(entrypoints, "bar", NULL) == 0);
  219. ASSERT(sg_entrypoints_add(entrypoints, "foobar", NULL) == 0);
  220. ASSERT(entrypoints->count == 3);
  221. ASSERT(sg_entrypoints_find(entrypoints, &item, "foo") == 0);
  222. ASSERT(item);
  223. ASSERT(sg_entrypoints_find(entrypoints, &item, "bar") == 0);
  224. ASSERT(item);
  225. ASSERT(sg_entrypoints_find(entrypoints, &item, "foobar") == 0);
  226. ASSERT(item);
  227. ASSERT(sg_entrypoints_clear(entrypoints) == 0);
  228. ASSERT(entrypoints->count == 0);
  229. ASSERT(sg_entrypoints_find(entrypoints, &item, "foo") == ENOENT);
  230. ASSERT(!item);
  231. ASSERT(sg_entrypoints_find(entrypoints, &item, "bar") == ENOENT);
  232. ASSERT(!item);
  233. ASSERT(sg_entrypoints_find(entrypoints, &item, "foobar") == ENOENT);
  234. ASSERT(!item);
  235. }
  236. int main(void) {
  237. struct sg_entrypoints *entrypoints = sg_entrypoints_new();
  238. ASSERT(entrypoints != NULL);
  239. test__entrypoints_add(entrypoints);
  240. test__entrypoints_rm(entrypoints);
  241. test__entrypoints_find(entrypoints);
  242. test_entrypoints_add(entrypoints);
  243. test_entrypoints_rm(entrypoints);
  244. test_entrypoints_iter(entrypoints);
  245. test_entrypoints_clear(entrypoints);
  246. test_entrypoints_find(entrypoints);
  247. sg_entrypoints_free(entrypoints);
  248. return EXIT_SUCCESS;
  249. }