test_lists.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_LISTS_H_
  23. #define _TEST_LISTS_H_
  24. // (well-defined object declaration)
  25. typedef struct student_s {
  26. TSK_DECLARE_OBJECT;
  27. char *id;
  28. char *name;
  29. }
  30. student_t;
  31. // (constructor)
  32. static tsk_object_t* student_ctor(tsk_object_t* self, va_list * app)
  33. {
  34. student_t* student = (tsk_object_t*)self;
  35. if(student) {
  36. }
  37. return self;
  38. }
  39. // (destructor)
  40. static tsk_object_t* student_dtor(tsk_object_t* self)
  41. {
  42. student_t* student = (tsk_object_t*)self;
  43. TSK_FREE(student->id);
  44. TSK_FREE(student->name);
  45. return self;
  46. }
  47. // (case insensitive comparator)
  48. static int student_icmp(const tsk_object_t *self, const tsk_object_t *object)
  49. {
  50. const student_t* student1 = (const tsk_object_t*)self;
  51. const student_t* student2 = (const tsk_object_t*)object;
  52. if(!student1 || !student2) {
  53. return 0;// must never happen
  54. }
  55. return tsk_stricmp(student1->id, student2->id);
  56. }
  57. // (well-defined object declaration)
  58. static const tsk_object_def_t student_def_s = {
  59. sizeof(student_t),
  60. student_ctor,
  61. student_dtor,
  62. student_icmp
  63. };
  64. // create a stun object
  65. static student_t* student_create(const char* id, const char* name)
  66. {
  67. student_t* student;
  68. if((student = (student_t*)tsk_object_new(&student_def_s))) {
  69. student->id = tsk_strdup(id);
  70. student->name = tsk_strdup(name);
  71. }
  72. return student;
  73. }
  74. // predicate function to find a student by name
  75. static int pred_find_student_by_name(const tsk_list_item_t *item, const void *name)
  76. {
  77. if(item && item->data) {
  78. student_t *student = item->data;
  79. return tsk_striequals(student->name, name);
  80. }
  81. return -1;
  82. }
  83. // predicate function to find a student by id
  84. static int pred_find_student_by_id(const tsk_list_item_t *item, const void *id)
  85. {
  86. if(item && item->data) {
  87. student_t *student = item->data;
  88. return tsk_striequals(student->id, id);
  89. }
  90. return -1;
  91. }
  92. /* testing basic linked list */
  93. void test_basic_list()
  94. {
  95. tsk_list_t *list = tsk_list_create();
  96. tsk_list_item_t *item = tsk_list_item_create();
  97. /* add items to the list */
  98. item->data = tsk_string_create("First item");
  99. tsk_list_push_front_item(list, &item);
  100. item = tsk_list_item_create();
  101. item->data = tsk_string_create("Second item");
  102. tsk_list_push_back_item(list, &item);
  103. item = tsk_list_item_create();
  104. item->data = tsk_string_create("Third item");
  105. tsk_list_push_front_item(list, &item);
  106. item = tsk_list_item_create();
  107. item->data = tsk_string_create("Fourth item");
  108. tsk_list_push_back_item(list, &item);
  109. /* dump all items */
  110. tsk_list_foreach(item, list) {
  111. tsk_string_t* item_data = ((tsk_string_t*)item->data);
  112. printf("test_basic_list/// --> [%s]\n", item_data->value);
  113. }
  114. tsk_list_remove_item(list, list->tail);
  115. tsk_list_remove_item(list, list->tail);
  116. tsk_list_remove_item(list, list->tail);
  117. tsk_list_remove_item(list, list->tail);
  118. /* delete all items in the list */
  119. TSK_OBJECT_SAFE_FREE(list);
  120. }
  121. void test_filtered_list()
  122. {
  123. tsk_list_t *list = tsk_list_create();
  124. tsk_list_item_t *item = 0;
  125. /* add items to the list */
  126. {
  127. student_t *student2 = student_create("2", "student2");
  128. tsk_list_push_ascending_data(list, ((void**) &student2));
  129. }
  130. {
  131. student_t *student6 = student_create("6", "student6");
  132. tsk_list_push_ascending_data(list, ((void**) &student6));
  133. }
  134. {
  135. student_t *student1 = student_create("1", "student1");
  136. tsk_list_push_ascending_data(list, ((void**) &student1));
  137. }
  138. {
  139. student_t *student6 = student_create("6", "student6");
  140. tsk_list_push_ascending_data(list, ((void**) &student6));
  141. }
  142. {
  143. student_t *student6 = student_create("6", "student6");
  144. tsk_list_push_ascending_data(list, ((void**) &student6));
  145. }
  146. {
  147. student_t *student2 = student_create("2", "student2");
  148. tsk_list_push_ascending_data(list, ((void**) &student2));
  149. }
  150. {
  151. student_t *student2 = student_create("2", "student2");
  152. tsk_list_push_ascending_data(list, ((void**) &student2));
  153. }
  154. {
  155. student_t *student5 = student_create("5", "student5");
  156. tsk_list_push_ascending_data(list, ((void**) &student5));
  157. }
  158. {
  159. student_t *student4 = student_create("4", "student4");
  160. tsk_list_push_ascending_data(list, ((void**) &student4));
  161. }
  162. {
  163. student_t *student1 = student_create("1", "student1");
  164. tsk_list_push_ascending_data(list, ((void**) &student1));
  165. }
  166. {
  167. student_t *student1 = student_create("1", "student1");
  168. tsk_list_push_ascending_data(list, ((void**) &student1));
  169. }
  170. {
  171. student_t *student3 = student_create("3", "student3");
  172. tsk_list_push_ascending_data(list, ((void**) &student3));
  173. }
  174. {
  175. student_t *student6 = student_create("6", "student6");
  176. tsk_list_push_ascending_data(list, ((void**) &student6));
  177. }
  178. {
  179. student_t *student1 = student_create("1", "student1");
  180. tsk_list_push_ascending_data(list, ((void**) &student1));
  181. }
  182. {
  183. student_t *student3 = student_create("3", "student3");
  184. tsk_list_push_ascending_data(list, ((void**) &student3));
  185. }
  186. {
  187. student_t *student6 = student_create("6", "student6");
  188. tsk_list_push_ascending_data(list, ((void**) &student6));
  189. }
  190. /* dump all items */
  191. tsk_list_foreach(item, list) {
  192. student_t* item_data = item->data;
  193. TSK_DEBUG_INFO("test_filtered_list/// --> [id=%s and name=%s]", item_data->id, item_data->name);
  194. }
  195. /* delete all items in the list */
  196. TSK_OBJECT_SAFE_FREE(list);
  197. }
  198. void test_complex_list()
  199. {
  200. tsk_list_t *list = tsk_list_create();
  201. tsk_list_item_t *item = 0;
  202. /* add items to the list */
  203. {
  204. student_t *student1 = student_create("1", "student1");
  205. tsk_list_push_back_data(list, ((void**) &student1));
  206. }
  207. {
  208. student_t *student2 = student_create("2", "student2");
  209. tsk_list_push_front_data(list, ((void**) &student2));
  210. }
  211. {
  212. student_t *student3 = student_create("3", "student3");
  213. tsk_list_push_front_data(list, ((void**) &student3));
  214. }
  215. /* dump all items */
  216. tsk_list_foreach(item, list) {
  217. student_t* item_data = item->data;
  218. TSK_DEBUG_INFO("test_complex_list/// --> [id=%s and name=%s]", item_data->id, item_data->name);
  219. }
  220. /* Find student using tsk_object* */
  221. {
  222. student_t *student_to_find = student_create("1", "student1");
  223. const tsk_list_item_t *item_const = tsk_list_find_item_by_data(list, student_to_find);
  224. {
  225. const student_t* item_data_const = item_const->data;
  226. TSK_DEBUG_INFO("test_complex_list/// using tsk_object --> student with name==\"student1\" and id=\"%s\"", item_data_const->id);
  227. }
  228. TSK_OBJECT_SAFE_FREE(student_to_find);
  229. }
  230. /* Find student named "student2" using predicate */
  231. {
  232. const tsk_list_item_t *item_const = tsk_list_find_item_by_pred(list, pred_find_student_by_name, "student2");
  233. {
  234. const student_t* item_data_const = item_const->data;
  235. TSK_DEBUG_INFO("test_complex_list/// using predicate --> student with name==\"student2\" and id=\"%s\"", item_data_const->id);
  236. }
  237. }
  238. /* delete all items in the list */
  239. TSK_OBJECT_SAFE_FREE(list);
  240. }
  241. #endif /* _TEST_LISTS_H_ */