PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 24/* The following was based on some pthread examples written by Andrae
 25 * Muys.
 26 */
 27
 28/** @defgroup Test Tests
 29 *  This group contains tests and test cases.
 30 *  @{
 31 */
 32
 33/*! \file qtest.c
 34  \brief Test queue.
 35
 36  Test application which tests if the queue used to send messages
 37  between the BT thread and the LIRC thread is working as expected.
 38*/
 39
 40#include <q.h>
 41
 42#include <stdio.h>
 43#include <unistd.h>
 44#include <stdlib.h>
 45#include <string.h>
 46
 47#include <pthread.h>
 48
 49#define LOOP 20
 50
 51void* producer (void *args);
 52void* consumer (void *args);
 53
 54unsigned int globalLogMask = 
 55  MODULEMASK_LIRC_THR | MODULEMASK_LIRC_SOCK | 
 56  MODULEMASK_LIRC_CB | MODULEMASK_BT_IF | MODULEMASK_BT_IMPL | 
 57  MODULEMASK_QUEUE | MODULEMASK_SPARE | MODULEMASK_MAIN;
 58
 59
 60static const unsigned int moduleMask = MODULEMASK_MAIN;
 61
 62int main(int argc, char *argv[])
 63{
 64  queue fifo;
 65  pthread_t pro, con;
 66  int res = -1;
 67  int i   = 0;
 68  
 69  if (argc > 1)
 70    {
 71      printf("Arguments are not supported.\n");
 72
 73      for (i = 1; i < argc; i++)
 74	{
 75	  printf("Unhandled argument: %s.\n", argv[i]);
 76	}
 77      return -1;
 78    }
 79
 80  res = queueInit(&fifo);
 81  if (res ==  Q_ERR)
 82    {
 83      fprintf (stderr, "main: Queue Init failed.\n");
 84      exit(1);
 85    }
 86
 87  pthread_create (&pro, NULL, producer, &fifo);
 88  pthread_create (&con, NULL, consumer, &fifo);
 89  pthread_join (pro, NULL);
 90  pthread_join (con, NULL);
 91  queueDeinit (&fifo);
 92
 93  return 0;
 94}
 95
 96void* producer (void* q)
 97{
 98  queue *fifo = (queue *)q;
 99  int i;
100  char msg[100];
101  queueData* qd = NULL;
102
103  for (i = 0; i < LOOP; i++)
104    {
105      sprintf(msg, "Test 1: %d", i);
106      qd = queueDataInit(msg, strlen(msg)+1);
107      queueAdd (fifo, qd);
108      printf ("producer: added %s.\n", msg);
109      usleep (100000);
110    }
111  for (i = 0; i < LOOP; i++)
112    {
113      sprintf(msg, "Test 2: %d", i);
114      qd = queueDataInit(msg, strlen(msg)+1);
115      queueAdd (fifo, qd);
116      printf ("producer: added %s.\n", msg);
117      usleep (200000);
118    }
119  return (NULL);
120}
121
122void* consumer (void* q)
123{
124  queue* fifo = (queue *)q;
125  int i;
126  queueData* d = 0;
127  
128  for (i = 0; i < LOOP; i++)
129    {
130      queueRemBlock(fifo, &d);
131      printf ("consumer: recieved %s.\n", d->buffer);
132      queueDataDeInit(d);
133      d = NULL;
134      usleep(200000);
135    }
136  for (i = 0; i < LOOP; i++)
137    {
138      queueRemBlock(fifo, &d);
139      printf ("consumer: recieved %s.\n", d->buffer);
140      queueDataDeInit(d);
141      d = NULL;
142      usleep (50000);
143    }
144  return (NULL);
145}
146
147/*\@}*/
148