test_inli.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * This is a test for inli in $package$
  3. */
  4. #include "si_test.h"
  5. #include <stddef.h>
  6. #include "inli.h"
  7. typedef struct Element_ Element;
  8. typedef struct ElementList_ ElementList;
  9. struct Element_ {
  10. int try;
  11. };
  12. struct ElementList_ {
  13. struct Element_ data;
  14. Inli list;
  15. };
  16. TEST_FUNC(inli) {
  17. ElementList l1, l2, l3, l4, l5;
  18. /* Test initialization. */
  19. TEST_NOTNULL(inli_init(&l1.list));
  20. TEST_NOTNULL(inli_init(&l2.list));
  21. TEST_NOTNULL(inli_init(&l3.list));
  22. TEST_NOTNULL(inli_init(&l4.list));
  23. TEST_NOTNULL(inli_init(&l5.list));
  24. /* Test proper initialization. */
  25. TEST_NULL(inli_next(&l1.list));
  26. TEST_NULL(inli_next(&l2.list));
  27. TEST_NULL(inli_prev(&l1.list));
  28. TEST_NULL(inli_prev(&l2.list));
  29. /* Test add to list. */
  30. /* l2 l3 */
  31. TEST_NOTNULL(inli_add(&l2.list, &l3.list));
  32. /* l1 l2 l3 */
  33. TEST_NOTNULL(inli_unshift(&l2.list, &l1.list));
  34. /* l1 l2 l3 l 4 */
  35. TEST_NOTNULL(inli_push(&l1.list, &l4.list));
  36. /* Test next and prev. */
  37. TEST_PTREQ(inli_next(&l1.list), &l2.list);
  38. TEST_PTREQ(inli_prev(&l2.list), &l1.list);
  39. TEST_PTREQ(inli_next(&l2.list), &l3.list);
  40. TEST_PTREQ(inli_prev(&l3.list), &l2.list);
  41. TEST_PTREQ(inli_next(&l3.list), &l4.list);
  42. TEST_PTREQ(inli_prev(&l4.list), &l3.list);
  43. TEST_PTREQ(inli_data(&l1.list, offsetof(ElementList, list)), &l1);
  44. TEST_PTREQ(INLI_DATA(&l1.list, ElementList, list), &l1);
  45. TEST_PTREQ(inli_data(&l2.list, offsetof(ElementList, list)), &l2);
  46. TEST_PTREQ(INLI_DATA(&l2.list, ElementList, list), &l2);
  47. TEST_PTREQ(inli_data(&l3.list, offsetof(ElementList, list)), &l3);
  48. TEST_PTREQ(INLI_DATA(&l3.list, ElementList, list), &l3);
  49. TEST_PTREQ(inli_data(&l4.list, offsetof(ElementList, list)), &l4);
  50. TEST_PTREQ(INLI_DATA(&l4.list, ElementList, list), &l4);
  51. TEST_PTREQ(INLI_LISTDATA(&l4.list, ElementList), &l4);
  52. /* Test first. */
  53. TEST_PTREQ(inli_first(&l4.list), &l1.list);
  54. TEST_PTREQ(inli_first(&l3.list), &l1.list);
  55. TEST_PTREQ(inli_first(&l2.list), &l1.list);
  56. TEST_PTREQ(inli_first(&l1.list), &l1.list);
  57. /* Test last. */
  58. TEST_PTREQ(inli_last(&l1.list), &l4.list);
  59. TEST_PTREQ(inli_last(&l2.list), &l4.list);
  60. TEST_PTREQ(inli_last(&l3.list), &l4.list);
  61. TEST_PTREQ(inli_last(&l4.list), &l4.list);
  62. /* Test remove. */
  63. TEST_PTREQ(inli_remove(&l2.list), &l2.list);
  64. TEST_NULL(inli_next(&l2.list));
  65. TEST_NULL(inli_prev(&l2.list));
  66. TEST_PTREQ(inli_next(&l1.list), &l3.list);
  67. TEST_PTREQ(inli_prev(&l3.list), &l1.list);
  68. TEST_PTREQ(inli_next(&l3.list), &l4.list);
  69. TEST_PTREQ(inli_prev(&l4.list), &l3.list);
  70. /* Test remove at end. */
  71. TEST_PTREQ(inli_pop(&l1.list), &l4.list);
  72. TEST_NULL(inli_next(&l4.list));
  73. TEST_NULL(inli_prev(&l4.list));
  74. TEST_NULL(inli_next(&l3.list));
  75. /* Test remove at beginning. */
  76. TEST_PTREQ(inli_shift(&l1.list), &l1.list);
  77. TEST_NULL(inli_next(&l1.list));
  78. TEST_NULL(inli_prev(&l1.list));
  79. /** l3 since l2 was already deleted from the list. */
  80. TEST_NULL(inli_prev(&l3.list));
  81. /* NULL resistance. */
  82. TEST_NULL(inli_init(NULL));
  83. TEST_NULL(inli_next(NULL));
  84. TEST_NULL(inli_prev(NULL));
  85. TEST_NULL(inli_add(NULL, NULL));
  86. TEST_NULL(inli_unshift(NULL, NULL));
  87. TEST_NULL(inli_push(NULL, NULL));
  88. TEST_NULL(inli_pop(NULL));
  89. TEST_NULL(inli_shift(NULL));
  90. TEST_NULL(inli_data(NULL, 123));
  91. TEST_NULL(INLI_LISTDATA(NULL, ElementList));
  92. TEST_DONE();
  93. }
  94. int main(void) {
  95. TEST_INIT();
  96. TEST_RUN(inli);
  97. TEST_REPORT();
  98. }