The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

375 lines
8.7 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup st
  4. @brief Simple test program of the st library.
  5. @copyright@parblock
  6. Copyright (c) 2015, Regents of the University of Colorado
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. Neither the name of the University of Colorado nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. @endparblock
  32. */
  33. #include "util.h"
  34. #include "st.h"
  35. /**
  36. @brief Just some struct type.
  37. */
  38. typedef struct mys {
  39. double a;
  40. int b;
  41. int c;
  42. } mys_t;
  43. /** \cond */
  44. static int testString(void);
  45. static int testStruct(void);
  46. static int testUintPtr(void);
  47. static int testInt(void);
  48. static int testArg(void);
  49. static int mys_cmp(void const * key1, void const * key2);
  50. static int mys_hash(void const * key, int size);
  51. static enum st_retval mys_accm(void * key, void * value, void * arg);
  52. static int array_hash(void const * key, int modulus, void const * arg);
  53. static int array_cmp(void const * key1, void const * key2, void const *arg);
  54. /** \endcond */
  55. /**
  56. @brief Main program.
  57. @return the number of failed tests.
  58. @details Uses TAP (Test Anything Protocol) to report results.
  59. */
  60. int
  61. main(void)
  62. {
  63. int ret = 0;
  64. printf("TAP version 13\n1..5\n");
  65. if (testString() != 0) {
  66. ret++;
  67. printf("not ");
  68. }
  69. printf("ok 1 string table\n");
  70. if (testStruct() != 0) {
  71. ret++;
  72. printf("not ");
  73. }
  74. printf("ok 2 struct-to-uintptr_t map\n");
  75. if (testUintPtr() != 0) {
  76. ret++;
  77. printf("not ");
  78. }
  79. printf("ok 3 uintptr_t-to-string map\n");
  80. if (testInt() != 0) {
  81. ret++;
  82. printf("not ");
  83. }
  84. printf("ok 4 int-to-int map\n");
  85. if (testArg() != 0) {
  86. ret++;
  87. printf("not ");
  88. }
  89. printf("ok 5 table with arg\n");
  90. return ret;
  91. }
  92. /**
  93. @brief Tests a table that stores C strings.
  94. @return 0 if successful; the number of errors otherwise.
  95. */
  96. static int
  97. testString(void)
  98. {
  99. int error = 0;
  100. char foo[] = "foo";
  101. char *cp = foo;
  102. char bar[] = "bar";
  103. char foobar[] = "foobar";
  104. st_table * tbl = st_init_table((st_compare_t) strcmp, st_strhash);
  105. if (!tbl)
  106. error++;
  107. if (st_insert(tbl, foo, NULL) != 0)
  108. error++;
  109. if (st_insert(tbl, bar, NULL) != 0)
  110. error++;
  111. if (st_insert(tbl, foobar, NULL) != 0)
  112. error++;
  113. if (!st_is_member(tbl, "foo"))
  114. error++;
  115. if (!st_delete(tbl, (void **) &cp, NULL))
  116. error++;
  117. if (st_count(tbl) != 2)
  118. error++;
  119. if (st_insert(tbl, bar, NULL) != 1)
  120. error++;
  121. st_free_table(tbl);
  122. return error;
  123. }
  124. /**
  125. @brief Tests a table that maps user-defined structs to uintptr_t.
  126. @return 0 if successful; the number of errors otherwise.
  127. */
  128. static int
  129. testStruct(void)
  130. {
  131. int error = 0;
  132. mys_t m1 = {3.5, 4, 11};
  133. mys_t m2 = {6.7, 5, -2};
  134. uintptr_t u;
  135. st_table * tbl = st_init_table(mys_cmp, mys_hash);
  136. if (!tbl)
  137. error++;
  138. if (st_insert(tbl, &m1, (void *)(uintptr_t) 2) != 0)
  139. error++;
  140. if (st_insert(tbl, &m2, (void *)(uintptr_t) 5) != 0)
  141. error++;
  142. if (st_lookup(tbl, &m1, (void **) &u) != 1)
  143. error++;
  144. if (u != 2)
  145. error++;
  146. u = 0;
  147. if (st_foreach(tbl, mys_accm, &u) != 1)
  148. error++;
  149. if (u != 7)
  150. error++;
  151. st_free_table(tbl);
  152. return error;
  153. }
  154. /**
  155. @brief Tests a table that maps values of type uintptr_t to strings.
  156. @return 0 if successful; the number of errors otherwise.
  157. */
  158. static int
  159. testUintPtr(void)
  160. {
  161. int error = 0;
  162. char foo[] = "foo";
  163. char * cp;
  164. st_table * tbl = st_init_table(st_numcmp, st_numhash);
  165. if (!tbl)
  166. error++;
  167. if (st_insert(tbl, (void *)(uintptr_t) 2, foo) != 0)
  168. error++;
  169. if (st_lookup(tbl, (void *)(uintptr_t) 2, (void **) &cp) != 1)
  170. error++;
  171. if (strcmp(cp, "foo") != 0)
  172. error++;
  173. if (st_is_member(tbl, (void *)(uintptr_t) 76))
  174. error++;
  175. st_free_table(tbl);
  176. return error;
  177. }
  178. /**
  179. @brief Tests a table that maps ints to ints.
  180. @return 0 if successful; the number of errors otherwise.
  181. */
  182. static int
  183. testInt(void)
  184. {
  185. int error = 0;
  186. int n1 = -2;
  187. int n2;
  188. void * e;
  189. int i;
  190. st_generator * gen;
  191. st_table * tbl = st_init_table(st_numcmp, st_numhash);
  192. if (!tbl)
  193. error++;
  194. if (st_insert(tbl, (void *)(intptr_t) n1, (void *)(intptr_t) 3) != 0)
  195. error++;
  196. if (st_lookup_int(tbl, (void *)(intptr_t) n1, &n2) != 1)
  197. error++;
  198. if (n2 != 3)
  199. error++;
  200. e = (void *)(intptr_t) n1;
  201. if (st_delete_int(tbl, &e, &n2) != 1)
  202. error++;
  203. if ((int)(intptr_t) e != n1 || n2 != 3)
  204. error++;
  205. if (st_count(tbl) != 0)
  206. error++;
  207. for (i = 0; i < 100000; i++) {
  208. if (st_insert(tbl, (void *)(intptr_t) i, (void *)(intptr_t) i) != 0)
  209. error++;
  210. }
  211. st_foreach_item_int(tbl, gen, &e, &n1) {
  212. if ((int)(intptr_t) e != n1)
  213. error++;
  214. }
  215. st_free_table(tbl);
  216. return error;
  217. }
  218. /**
  219. @brief Tests a table of arrays of ints.
  220. @return 0 if successful; 1 otherwise.
  221. */
  222. static int
  223. testArg(void)
  224. {
  225. size_t const n = 5;
  226. int error = 0;
  227. int a1[] = {0,1,2,3,4};
  228. int a2[] = {4,3,2,1,0};
  229. int *a3 = a1;
  230. intptr_t val = 0;
  231. st_table *tbl = st_init_table_with_arg(array_cmp, array_hash, (void *) n);
  232. if (!tbl)
  233. error++;
  234. if (st_insert(tbl, a1, (void *)(intptr_t) 1) != 0)
  235. error++;
  236. if (st_insert(tbl, a2, (void *)(intptr_t) 2) != 0)
  237. error++;
  238. if (!st_is_member(tbl, a1))
  239. error++;
  240. if (!st_delete(tbl, (void **) &a3, (void **) &val))
  241. error++;
  242. if (a3[0] != a1[0] || val != 1)
  243. error++;
  244. if (st_is_member(tbl, a1))
  245. error++;
  246. if (!st_is_member(tbl, a2))
  247. error++;
  248. st_free_table(tbl);
  249. return error;
  250. }
  251. /**
  252. @brief Compares two items of type mys_t.
  253. @return 0 if they compare equal and 1 otherwise.
  254. */
  255. static int
  256. mys_cmp(void const * key1, void const * key2)
  257. {
  258. mys_t const *m1 = (mys_t const *) key1;
  259. mys_t const *m2 = (mys_t const *) key2;
  260. return m1->b != m2->b || m1->c != m2->c;
  261. }
  262. /**
  263. @brief Hashes one item of type mys_t.
  264. @return the hash value.
  265. */
  266. static int
  267. mys_hash(void const * key, int size)
  268. {
  269. mys_t const *m = (mys_t const *) key;
  270. return (int)((((unsigned) m->b >> 4) ^ ((unsigned) m->c >> 5)) % size);
  271. }
  272. /**
  273. @brief Accumulates the values associated to items of type mys_t.
  274. @return ST_CONTINUE
  275. */
  276. static enum st_retval
  277. mys_accm(void * key, void * value, void * arg)
  278. {
  279. (void) key; /* avoid warning */
  280. uintptr_t v = (uintptr_t) value;
  281. uintptr_t * accum = (uintptr_t *) arg;
  282. *accum += v;
  283. return ST_CONTINUE;
  284. }
  285. /**
  286. @brief Compares two arrays of ints.
  287. @details The length of the two arrays is in `arg`.
  288. @return 0 if they compare equal and 1 otherwise.
  289. */
  290. static int
  291. array_cmp(void const * key1, void const * key2, void const *arg)
  292. {
  293. int const *a1 = (int const *) key1;
  294. int const *a2 = (int const *) key2;
  295. size_t const size = (size_t const) arg;
  296. size_t i;
  297. for (i = 0; i < size; i++) {
  298. if (a1[i] != a2[i])
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. /**
  304. @brief Hashes one array of ints.
  305. @return the hash value.
  306. */
  307. static int
  308. array_hash(void const * key, int modulus, void const * arg)
  309. {
  310. int const *a = (int const *) key;
  311. size_t const size = (size_t const) arg;
  312. int val = 0;
  313. size_t i;
  314. for (i = 0; i < size; i++) {
  315. val = val * 997 + a[i];
  316. }
  317. return ((val < 0) ? -val : val) % modulus;
  318. }