queue.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <glib.h>
  4. #include "test.h"
  5. RESULT
  6. test_queue_push ()
  7. {
  8. GQueue *queue = g_queue_new ();
  9. g_queue_push_head (queue, "foo");
  10. g_queue_push_head (queue, "bar");
  11. g_queue_push_head (queue, "baz");
  12. if (queue->length != 3)
  13. return FAILED ("push failed");
  14. if (NULL != queue->head->prev)
  15. return FAILED ("HEAD: prev is wrong");
  16. if (strcmp ("baz", queue->head->data))
  17. return FAILED ("HEAD: First element is wrong");
  18. if (strcmp ("bar", queue->head->next->data))
  19. return FAILED ("HEAD: Second element is wrong");
  20. if (strcmp ("foo", queue->head->next->next->data))
  21. return FAILED ("HEAD: Third element is wrong");
  22. if (NULL != queue->head->next->next->next)
  23. return FAILED ("HEAD: End is wrong");
  24. if (NULL != queue->tail->next)
  25. return FAILED ("TAIL: next is wrong");
  26. if (strcmp ("foo", queue->tail->data))
  27. return FAILED ("TAIL: Third element is wrong");
  28. if (strcmp ("bar", queue->tail->prev->data))
  29. return FAILED ("TAIL: Second element is wrong");
  30. if (strcmp ("baz", queue->tail->prev->prev->data))
  31. return FAILED ("TAIL: First element is wrong");
  32. if (NULL != queue->tail->prev->prev->prev)
  33. return FAILED ("TAIL: End is wrong");
  34. g_queue_free (queue);
  35. return OK;
  36. }
  37. RESULT
  38. test_queue_push_tail ()
  39. {
  40. GQueue *queue = g_queue_new ();
  41. g_queue_push_tail (queue, "baz");
  42. g_queue_push_tail (queue, "bar");
  43. g_queue_push_tail (queue, "foo");
  44. if (queue->length != 3)
  45. return FAILED ("push failed");
  46. if (NULL != queue->head->prev)
  47. return FAILED ("HEAD: prev is wrong");
  48. if (strcmp ("baz", queue->head->data))
  49. return FAILED ("HEAD: First element is wrong");
  50. if (strcmp ("bar", queue->head->next->data))
  51. return FAILED ("HEAD: Second element is wrong");
  52. if (strcmp ("foo", queue->head->next->next->data))
  53. return FAILED ("HEAD: Third element is wrong");
  54. if (NULL != queue->head->next->next->next)
  55. return FAILED ("HEAD: End is wrong");
  56. if (NULL != queue->tail->next)
  57. return FAILED ("TAIL: next is wrong");
  58. if (strcmp ("foo", queue->tail->data))
  59. return FAILED ("TAIL: Third element is wrong");
  60. if (strcmp ("bar", queue->tail->prev->data))
  61. return FAILED ("TAIL: Second element is wrong");
  62. if (strcmp ("baz", queue->tail->prev->prev->data))
  63. return FAILED ("TAIL: First element is wrong");
  64. if (NULL != queue->tail->prev->prev->prev)
  65. return FAILED ("TAIL: End is wrong");
  66. g_queue_free (queue);
  67. return OK;
  68. }
  69. RESULT
  70. test_queue_pop ()
  71. {
  72. GQueue *queue = g_queue_new ();
  73. gpointer data;
  74. g_queue_push_head (queue, "foo");
  75. g_queue_push_head (queue, "bar");
  76. g_queue_push_head (queue, "baz");
  77. data = g_queue_pop_head (queue);
  78. if (strcmp ("baz", data))
  79. return FAILED ("expect baz.");
  80. data = g_queue_pop_head (queue);
  81. if (strcmp ("bar", data))
  82. return FAILED ("expect bar.");
  83. data = g_queue_pop_head (queue);
  84. if (strcmp ("foo", data))
  85. return FAILED ("expect foo.");
  86. if (g_queue_is_empty (queue) == FALSE)
  87. return FAILED ("expect is_empty.");
  88. if (queue->length != 0)
  89. return FAILED ("expect 0 length .");
  90. g_queue_push_head (queue, "foo");
  91. g_queue_push_head (queue, "bar");
  92. g_queue_push_head (queue, "baz");
  93. g_queue_pop_head (queue);
  94. if (NULL != queue->head->prev)
  95. return FAILED ("HEAD: prev is wrong");
  96. if (strcmp ("bar", queue->head->data))
  97. return FAILED ("HEAD: Second element is wrong");
  98. if (strcmp ("foo", queue->head->next->data))
  99. return FAILED ("HEAD: Third element is wrong");
  100. if (NULL != queue->head->next->next)
  101. return FAILED ("HEAD: End is wrong");
  102. if (NULL != queue->tail->next)
  103. return FAILED ("TAIL: next is wrong");
  104. if (strcmp ("foo", queue->tail->data))
  105. return FAILED ("TAIL: Second element is wrong");
  106. if (strcmp ("bar", queue->tail->prev->data))
  107. return FAILED ("TAIL: First element is wrong");
  108. if (NULL != queue->tail->prev->prev)
  109. return FAILED ("TAIL: End is wrong");
  110. g_queue_free (queue);
  111. return OK;
  112. }
  113. RESULT
  114. test_queue_new ()
  115. {
  116. GQueue *queue = g_queue_new ();
  117. if (queue->length != 0)
  118. return FAILED ("expect length == 0");
  119. if (queue->head != NULL)
  120. return FAILED ("expect head == NULL");
  121. if (queue->tail != NULL)
  122. return FAILED ("expect tail == NULL");
  123. g_queue_free (queue);
  124. return OK;
  125. }
  126. RESULT
  127. test_queue_is_empty ()
  128. {
  129. GQueue *queue = g_queue_new ();
  130. if (g_queue_is_empty (queue) == FALSE)
  131. return FAILED ("new queue should be empty");
  132. g_queue_push_head (queue, "foo");
  133. if (g_queue_is_empty (queue) == TRUE)
  134. return FAILED ("expected TRUE");
  135. g_queue_free (queue);
  136. return OK;
  137. }
  138. static Test queue_tests [] = {
  139. { "push", test_queue_push},
  140. {"push_tail", test_queue_push_tail},
  141. { "pop", test_queue_pop},
  142. { "new", test_queue_new},
  143. {"is_empty", test_queue_is_empty},
  144. {NULL, NULL}
  145. };
  146. DEFINE_TEST_GROUP_INIT(queue_tests_init, queue_tests)