wslay_queue_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Wslay - The WebSocket Library
  3. *
  4. * Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "wslay_queue_test.h"
  26. #include <CUnit/CUnit.h>
  27. #include "wslay_queue.h"
  28. #include "wslay_macro.h"
  29. struct queue_entry {
  30. struct wslay_queue_entry qe;
  31. int value;
  32. };
  33. void test_wslay_queue(void) {
  34. struct queue_entry ents[] = {
  35. {{0}, 1}, {{0}, 2}, {{0}, 3}, {{0}, 4}, {{0}, 5},
  36. };
  37. int i;
  38. struct wslay_queue queue;
  39. wslay_queue_init(&queue);
  40. CU_ASSERT(wslay_queue_empty(&queue));
  41. for (i = 0; i < 5; ++i) {
  42. wslay_queue_push(&queue, &ents[i].qe);
  43. CU_ASSERT_EQUAL(ents[0].value, wslay_struct_of(wslay_queue_top(&queue),
  44. struct queue_entry, qe)
  45. ->value);
  46. CU_ASSERT(!wslay_queue_empty(&queue));
  47. }
  48. for (i = 0; i < 5; ++i) {
  49. CU_ASSERT_EQUAL(ents[i].value, wslay_struct_of(wslay_queue_top(&queue),
  50. struct queue_entry, qe)
  51. ->value);
  52. wslay_queue_pop(&queue);
  53. }
  54. CU_ASSERT(wslay_queue_empty(&queue));
  55. for (i = 0; i < 5; ++i) {
  56. wslay_queue_push_front(&queue, &ents[i].qe);
  57. CU_ASSERT_EQUAL(ents[i].value, wslay_struct_of(wslay_queue_top(&queue),
  58. struct queue_entry, qe)
  59. ->value);
  60. CU_ASSERT(!wslay_queue_empty(&queue));
  61. }
  62. for (i = 4; i >= 0; --i) {
  63. CU_ASSERT_EQUAL(ents[i].value, wslay_struct_of(wslay_queue_top(&queue),
  64. struct queue_entry, qe)
  65. ->value);
  66. wslay_queue_pop(&queue);
  67. }
  68. CU_ASSERT(wslay_queue_empty(&queue));
  69. wslay_queue_deinit(&queue);
  70. }