Exemple #1
0
void TestMessageQueue1()
{
    MessageQueue q;

    Uint32 sum = 0;

    for (Uint32 i = 1; i <= 5; i++)
    {
	q.enqueue(new Alarm(i));
	sum += i;
    }

    assert(Sum(q) == sum);

    // Test removing from the middle:
    Message* m = q.findByKey(3);
    assert(m != 0);
    q.remove(m);
    assert(Sum(q) == sum - 3);
    assert(q.getCount() == 4);

    // Test removing from the front:
    q.remove(q.front());
    assert(Sum(q) == sum - 3 - 1);
    assert(q.getCount() == 3);

    // Test removing from the front:
    q.remove(q.back());
    assert(Sum(q) == sum - 3 - 1 - 5);
    assert(q.getCount() == 2);

    // Test dequeue:
    m = q.dequeue();
    assert(m->getKey() == 2);
    assert(Sum(q) == sum - 3 - 1 - 5 - 2);
    assert(q.getCount() == 1);

    // Test dequeue:
    m = q.dequeue();
    assert(m->getKey() == 4);
    assert(Sum(q) == sum - 3 - 1 - 5 - 2 - 4);
    assert(q.getCount() == 0);
}
Exemple #2
0
void TestMessageQueue3()
{
    MessageQueue q;

    Uint32 sum = 0;

    for (Uint32 i = 1; i <= 5; i++)
    {
	q.enqueue(new Alarm(i));
	sum += i;
    }
    assert(sum == 15);

    while (!q.isEmpty())
	q.remove(q.back());

    assert(q.getCount() == 0);
}
int
main(int argc, char **argv)
{

  unsigned int id;
  MessageQueue *mq = new MessageQueue();

  cout << "Message queue initialized, now appending three test messages" << endl;

  TestInterface::SetTestIntMessage *m1 = new TestInterface::SetTestIntMessage(1);
  TestInterface::SetTestIntMessage *m2 = new TestInterface::SetTestIntMessage(2);
  TestInterface::SetTestIntMessage *m3 = new TestInterface::SetTestIntMessage(3);
  TestInterface::SetTestStringMessage *m4 = new TestInterface::SetTestStringMessage();
  id = mq->append(m1);
  cout << "m1 added with id " << id << endl;
  id = mq->append(m1);
  cout << "m1 added with id " << id << endl;
  id = mq->append(m2);
  cout << "m2 added with id " << id << endl;
  id = mq->append(m3);
  cout << "m3 added with id " << id << endl;
  id = mq->append(m4);
  cout << "m4 (of different type!) added with id " << id << endl;

  cout << "Size is now " << mq->size() << endl;

  cout << "Unreferencing messages" << endl;
  m1->unref();
  m2->unref();
  m3->unref();
  m4->unref();

  cout << "Appending m1 again" << endl;
  id = mq->append(m1);
  cout << "m1 added with id " << id << endl;
  cout << "Size is now " << mq->size() << endl;
  cout << "m1 refcount is now " << m1->refcount() << endl;

  printMessages(mq);

  cout << "Now removing message m1 once" << endl;
  mq->remove(m1);
  printMessages(mq);


  cout << "m1 has refcount " << m1->refcount() << endl;

  cout << "Now removing message m2" << endl;
  mq->remove(m2);
  printMessages(mq);

  cout << "Now removing message m4" << endl;
  mq->remove(m4);
  printMessages(mq);

  cout << "Size is now " << mq->size() << endl;

  printMessages(mq);

  delete mq;
  // messages will be erased when enqueued in mq!

}