list.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <glib.h>
  4. #include "test.h"
  5. RESULT
  6. test_list_length ()
  7. {
  8. GList *list = g_list_prepend (NULL, "foo");
  9. if (g_list_length (list) != 1)
  10. return FAILED ("length failed. #1");
  11. list = g_list_prepend (list, "bar");
  12. if (g_list_length (list) != 2)
  13. return FAILED ("length failed. #2");
  14. list = g_list_append (list, "bar");
  15. if (g_list_length (list) != 3)
  16. return FAILED ("length failed. #3");
  17. g_list_free (list);
  18. return NULL;
  19. }
  20. RESULT
  21. test_list_nth ()
  22. {
  23. char *foo = "foo";
  24. char *bar = "bar";
  25. char *baz = "baz";
  26. GList *nth, *list;
  27. list = g_list_prepend (NULL, baz);
  28. list = g_list_prepend (list, bar);
  29. list = g_list_prepend (list, foo);
  30. nth = g_list_nth (list, 0);
  31. if (nth->data != foo)
  32. return FAILED ("nth failed. #0");
  33. nth = g_list_nth (list, 1);
  34. if (nth->data != bar)
  35. return FAILED ("nth failed. #1");
  36. nth = g_list_nth (list, 2);
  37. if (nth->data != baz)
  38. return FAILED ("nth failed. #2");
  39. nth = g_list_nth (list, 3);
  40. if (nth)
  41. return FAILED ("nth failed. #3: %s", nth->data);
  42. g_list_free (list);
  43. return OK;
  44. }
  45. RESULT
  46. test_list_index ()
  47. {
  48. int i;
  49. char *foo = "foo";
  50. char *bar = "bar";
  51. char *baz = "baz";
  52. GList *list;
  53. list = g_list_prepend (NULL, baz);
  54. list = g_list_prepend (list, bar);
  55. list = g_list_prepend (list, foo);
  56. i = g_list_index (list, foo);
  57. if (i != 0)
  58. return FAILED ("index failed. #0: %d", i);
  59. i = g_list_index (list, bar);
  60. if (i != 1)
  61. return FAILED ("index failed. #1: %d", i);
  62. i = g_list_index (list, baz);
  63. if (i != 2)
  64. return FAILED ("index failed. #2: %d", i);
  65. g_list_free (list);
  66. return OK;
  67. }
  68. RESULT
  69. test_list_append ()
  70. {
  71. GList *list = g_list_prepend (NULL, "first");
  72. if (g_list_length (list) != 1)
  73. return FAILED ("Prepend failed");
  74. list = g_list_append (list, "second");
  75. if (g_list_length (list) != 2)
  76. return FAILED ("Append failed");
  77. g_list_free (list);
  78. return OK;
  79. }
  80. RESULT
  81. test_list_last ()
  82. {
  83. GList *foo = g_list_prepend (NULL, "foo");
  84. GList *bar = g_list_prepend (NULL, "bar");
  85. GList *last;
  86. foo = g_list_concat (foo, bar);
  87. last = g_list_last (foo);
  88. if (last != bar)
  89. return FAILED ("last failed. #1");
  90. foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
  91. foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
  92. last = g_list_last (foo);
  93. if (strcmp ("quux", last->data))
  94. return FAILED ("last failed. #2");
  95. g_list_free (foo);
  96. return OK;
  97. }
  98. RESULT
  99. test_list_concat ()
  100. {
  101. GList *foo = g_list_prepend (NULL, "foo");
  102. GList *bar = g_list_prepend (NULL, "bar");
  103. GList *list = g_list_concat (foo, bar);
  104. if (g_list_length (list) != 2)
  105. return FAILED ("Concat failed. #1");
  106. if (strcmp (list->data, "foo"))
  107. return FAILED ("Concat failed. #2");
  108. if (strcmp (list->next->data, "bar"))
  109. return FAILED ("Concat failed. #3");
  110. if (g_list_first (list) != foo)
  111. return FAILED ("Concat failed. #4");
  112. if (g_list_last (list) != bar)
  113. return FAILED ("Concat failed. #5");
  114. g_list_free (list);
  115. return OK;
  116. }
  117. static gint
  118. compare (gconstpointer a, gconstpointer b)
  119. {
  120. char *foo = (char *) a;
  121. char *bar = (char *) b;
  122. if (strlen (foo) < strlen (bar))
  123. return -1;
  124. return 1;
  125. }
  126. RESULT
  127. test_list_insert_sorted ()
  128. {
  129. GList *list = g_list_prepend (NULL, "a");
  130. list = g_list_append (list, "aaa");
  131. /* insert at the middle */
  132. list = g_list_insert_sorted (list, "aa", compare);
  133. if (strcmp ("aa", list->next->data))
  134. return FAILED ("insert_sorted failed. #1");
  135. /* insert at the beginning */
  136. list = g_list_insert_sorted (list, "", compare);
  137. if (strcmp ("", list->data))
  138. return FAILED ("insert_sorted failed. #2");
  139. /* insert at the end */
  140. list = g_list_insert_sorted (list, "aaaa", compare);
  141. if (strcmp ("aaaa", g_list_last (list)->data))
  142. return FAILED ("insert_sorted failed. #3");
  143. g_list_free (list);
  144. return OK;
  145. }
  146. RESULT
  147. test_list_copy ()
  148. {
  149. int i, length;
  150. GList *list, *copy;
  151. list = g_list_prepend (NULL, "a");
  152. list = g_list_append (list, "aa");
  153. list = g_list_append (list, "aaa");
  154. list = g_list_append (list, "aaaa");
  155. length = g_list_length (list);
  156. copy = g_list_copy (list);
  157. for (i = 0; i < length; i++)
  158. if (strcmp (g_list_nth (list, i)->data,
  159. g_list_nth (copy, i)->data))
  160. return FAILED ("copy failed.");
  161. g_list_free (list);
  162. g_list_free (copy);
  163. return OK;
  164. }
  165. RESULT
  166. test_list_reverse ()
  167. {
  168. guint i, length;
  169. GList *list, *reverse;
  170. list = g_list_prepend (NULL, "a");
  171. list = g_list_append (list, "aa");
  172. list = g_list_append (list, "aaa");
  173. list = g_list_append (list, "aaaa");
  174. length = g_list_length (list);
  175. reverse = g_list_reverse (g_list_copy (list));
  176. if (g_list_length (reverse) != length)
  177. return FAILED ("reverse failed #1");
  178. for (i = 0; i < length; i++){
  179. guint j = length - i - 1;
  180. if (strcmp (g_list_nth (list, i)->data,
  181. g_list_nth (reverse, j)->data))
  182. return FAILED ("reverse failed. #2");
  183. }
  184. g_list_free (list);
  185. g_list_free (reverse);
  186. return OK;
  187. }
  188. RESULT
  189. test_list_remove ()
  190. {
  191. GList *list = g_list_prepend (NULL, "three");
  192. char *one = "one";
  193. list = g_list_prepend (list, "two");
  194. list = g_list_prepend (list, one);
  195. list = g_list_remove (list, one);
  196. if (g_list_length (list) != 2)
  197. return FAILED ("Remove failed");
  198. if (strcmp ("two", list->data) != 0)
  199. return FAILED ("Remove failed");
  200. g_list_free (list);
  201. return OK;
  202. }
  203. RESULT
  204. test_list_remove_link ()
  205. {
  206. GList *foo = g_list_prepend (NULL, "a");
  207. GList *bar = g_list_prepend (NULL, "b");
  208. GList *baz = g_list_prepend (NULL, "c");
  209. GList *list = foo;
  210. foo = g_list_concat (foo, bar);
  211. foo = g_list_concat (foo, baz);
  212. list = g_list_remove_link (list, bar);
  213. if (g_list_length (list) != 2)
  214. return FAILED ("remove_link failed #1");
  215. if (bar->next != NULL)
  216. return FAILED ("remove_link failed #2");
  217. g_list_free (list);
  218. g_list_free (bar);
  219. return OK;
  220. }
  221. RESULT
  222. test_list_insert_before ()
  223. {
  224. GList *foo, *bar, *baz;
  225. foo = g_list_prepend (NULL, "foo");
  226. foo = g_list_insert_before (foo, NULL, "bar");
  227. bar = g_list_last (foo);
  228. if (strcmp (bar->data, "bar"))
  229. return FAILED ("1");
  230. baz = g_list_insert_before (foo, bar, "baz");
  231. if (foo != baz)
  232. return FAILED ("2");
  233. if (strcmp (g_list_nth_data (foo, 1), "baz"))
  234. return FAILED ("3: %s", g_list_nth_data (foo, 1));
  235. g_list_free (foo);
  236. return OK;
  237. }
  238. #define N_ELEMS 101
  239. static int intcompare (gconstpointer p1, gconstpointer p2)
  240. {
  241. return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
  242. }
  243. static gboolean verify_sort (GList *list, int len)
  244. {
  245. int prev;
  246. if (list->prev)
  247. return FALSE;
  248. prev = GPOINTER_TO_INT (list->data);
  249. len--;
  250. for (list = list->next; list; list = list->next) {
  251. int curr = GPOINTER_TO_INT (list->data);
  252. if (prev > curr)
  253. return FALSE;
  254. prev = curr;
  255. if (!list->prev || list->prev->next != list)
  256. return FALSE;
  257. if (len == 0)
  258. return FALSE;
  259. len--;
  260. }
  261. return len == 0;
  262. }
  263. RESULT
  264. test_list_sort ()
  265. {
  266. int i, j, mul;
  267. GList *list = NULL;
  268. for (i = 0; i < N_ELEMS; ++i)
  269. list = g_list_prepend (list, GINT_TO_POINTER (i));
  270. list = g_list_sort (list, intcompare);
  271. if (!verify_sort (list, N_ELEMS))
  272. return FAILED ("decreasing list");
  273. g_list_free (list);
  274. list = NULL;
  275. for (i = 0; i < N_ELEMS; ++i)
  276. list = g_list_prepend (list, GINT_TO_POINTER (-i));
  277. list = g_list_sort (list, intcompare);
  278. if (!verify_sort (list, N_ELEMS))
  279. return FAILED ("increasing list");
  280. g_list_free (list);
  281. list = g_list_prepend (NULL, GINT_TO_POINTER (0));
  282. for (i = 1; i < N_ELEMS; ++i) {
  283. list = g_list_prepend (list, GINT_TO_POINTER (i));
  284. list = g_list_prepend (list, GINT_TO_POINTER (-i));
  285. }
  286. list = g_list_sort (list, intcompare);
  287. if (!verify_sort (list, 2*N_ELEMS-1))
  288. return FAILED ("alternating list");
  289. g_list_free (list);
  290. list = NULL;
  291. mul = 1;
  292. for (i = 1; i < N_ELEMS; ++i) {
  293. mul = -mul;
  294. for (j = 0; j < i; ++j)
  295. list = g_list_prepend (list, GINT_TO_POINTER (mul * j));
  296. }
  297. list = g_list_sort (list, intcompare);
  298. if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
  299. return FAILED ("wavering list");
  300. g_list_free (list);
  301. return OK;
  302. }
  303. static gint
  304. find_custom (gconstpointer a, gconstpointer b)
  305. {
  306. return(strcmp (a, b));
  307. }
  308. RESULT
  309. test_list_find_custom ()
  310. {
  311. GList *list = NULL, *found;
  312. char *foo = "foo";
  313. char *bar = "bar";
  314. char *baz = "baz";
  315. list = g_list_prepend (list, baz);
  316. list = g_list_prepend (list, bar);
  317. list = g_list_prepend (list, foo);
  318. found = g_list_find_custom (list, baz, find_custom);
  319. if (found == NULL)
  320. return FAILED ("Find failed");
  321. g_list_free (list);
  322. return OK;
  323. }
  324. static Test list_tests [] = {
  325. { "length", test_list_length},
  326. { "nth", test_list_nth},
  327. { "index", test_list_index},
  328. { "last", test_list_last},
  329. { "append", test_list_append},
  330. { "concat", test_list_concat},
  331. {"insert_sorted", test_list_insert_sorted},
  332. {"insert_before", test_list_insert_before},
  333. { "copy", test_list_copy},
  334. { "reverse", test_list_reverse},
  335. { "remove", test_list_remove},
  336. { "remove_link", test_list_remove_link},
  337. { "remove_link", test_list_remove_link},
  338. { "sort", test_list_sort},
  339. { "find_custom", test_list_find_custom},
  340. {NULL, NULL}
  341. };
  342. DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)