/test/qtest.c

http://bdremote-ng.googlecode.com/ · C · 148 lines · 89 code · 23 blank · 36 comment · 8 complexity · c9ae9736c5225accd863432b08d4e7d0 MD5 · raw file

  1. /*
  2. * bdremoteng - helper daemon for Sony(R) BD Remote Control
  3. * Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
  4. *
  5. * Copyright (C) 2009 Michael Wojciechowski <wojci@wojci.dk>
  6. *
  7. *
  8. * This program 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 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program 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 this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /* The following was based on some pthread examples written by Andrae
  24. * Muys.
  25. */
  26. /** @defgroup Test Tests
  27. * This group contains tests and test cases.
  28. * @{
  29. */
  30. /*! \file qtest.c
  31. \brief Test queue.
  32. Test application which tests if the queue used to send messages
  33. between the BT thread and the LIRC thread is working as expected.
  34. */
  35. #include <q.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <pthread.h>
  41. #define LOOP 20
  42. void* producer (void *args);
  43. void* consumer (void *args);
  44. unsigned int globalLogMask =
  45. MODULEMASK_LIRC_THR | MODULEMASK_LIRC_SOCK |
  46. MODULEMASK_LIRC_CB | MODULEMASK_BT_IF | MODULEMASK_BT_IMPL |
  47. MODULEMASK_QUEUE | MODULEMASK_SPARE | MODULEMASK_MAIN;
  48. static const unsigned int moduleMask = MODULEMASK_MAIN;
  49. int main(int argc, char *argv[])
  50. {
  51. queue fifo;
  52. pthread_t pro, con;
  53. int res = -1;
  54. int i = 0;
  55. if (argc > 1)
  56. {
  57. printf("Arguments are not supported.\n");
  58. for (i = 1; i < argc; i++)
  59. {
  60. printf("Unhandled argument: %s.\n", argv[i]);
  61. }
  62. return -1;
  63. }
  64. res = queueInit(&fifo);
  65. if (res == Q_ERR)
  66. {
  67. fprintf (stderr, "main: Queue Init failed.\n");
  68. exit(1);
  69. }
  70. pthread_create (&pro, NULL, producer, &fifo);
  71. pthread_create (&con, NULL, consumer, &fifo);
  72. pthread_join (pro, NULL);
  73. pthread_join (con, NULL);
  74. queueDeinit (&fifo);
  75. return 0;
  76. }
  77. void* producer (void* q)
  78. {
  79. queue *fifo = (queue *)q;
  80. int i;
  81. char msg[100];
  82. queueData* qd = NULL;
  83. for (i = 0; i < LOOP; i++)
  84. {
  85. sprintf(msg, "Test 1: %d", i);
  86. qd = queueDataInit(msg, strlen(msg)+1);
  87. queueAdd (fifo, qd);
  88. printf ("producer: added %s.\n", msg);
  89. usleep (100000);
  90. }
  91. for (i = 0; i < LOOP; i++)
  92. {
  93. sprintf(msg, "Test 2: %d", i);
  94. qd = queueDataInit(msg, strlen(msg)+1);
  95. queueAdd (fifo, qd);
  96. printf ("producer: added %s.\n", msg);
  97. usleep (200000);
  98. }
  99. return (NULL);
  100. }
  101. void* consumer (void* q)
  102. {
  103. queue* fifo = (queue *)q;
  104. int i;
  105. queueData* d = 0;
  106. for (i = 0; i < LOOP; i++)
  107. {
  108. queueRemBlock(fifo, &d);
  109. printf ("consumer: recieved %s.\n", d->buffer);
  110. queueDataDeInit(d);
  111. d = NULL;
  112. usleep(200000);
  113. }
  114. for (i = 0; i < LOOP; i++)
  115. {
  116. queueRemBlock(fifo, &d);
  117. printf ("consumer: recieved %s.\n", d->buffer);
  118. queueDataDeInit(d);
  119. d = NULL;
  120. usleep (50000);
  121. }
  122. return (NULL);
  123. }
  124. /*\@}*/