xref: /haiku/src/tests/kits/app/bmessagequeue/MessageQueueTestCase.cpp (revision 5ac9b506412b11afb993bb52d161efe7666958a5)
1 /*
2 	$Id: MessageQueueTestCase.cpp 383 2002-07-22 09:28:00Z tylerdauwalder $
3 
4 	This file implements a base class for testing BMessageQueue functionality.
5 
6 	*/
7 
8 
9 #include "MessageQueueTestCase.h"
10 #include <MessageQueue.h>
11 #include "MessageQueue.h"
12 
13 
14 int testMessageClass::messageDestructorCount = 0;
15 
16 
17 
18 	MessageQueueTestCase::MessageQueueTestCase(
19 		std::string name) : BThreadedTestCase(name),
20 							theMessageQueue(new BMessageQueue())
21 {
22 	}
23 
24 
25 
26 	MessageQueueTestCase::~MessageQueueTestCase()
27 {
28 	delete theMessageQueue;
29 	theMessageQueue = NULL;
30 	}
31 
32 
33 
34 	void MessageQueueTestCase::CheckQueueAgainstList(void)
35 {
36 	SafetyLock theSafetyLock(theMessageQueue);
37 
38 	if (theMessageQueue->Lock()) {
39 		CPPUNIT_ASSERT(theMessageQueue->CountMessages() == messageList.CountItems());
40 		int i;
41 		for (i = 0; i < theMessageQueue->CountMessages(); i++) {
42 			CPPUNIT_ASSERT(theMessageQueue->FindMessage((int32)i) ==
43 					messageList.ItemAt(i));
44 		}
45 		theMessageQueue->Unlock();
46 	}
47 }
48 
49 
50 
51 	void MessageQueueTestCase::AddMessage(BMessage *message)
52 {
53 	if (theMessageQueue->Lock()) {
54 		theMessageQueue->AddMessage(message);
55 		messageList.AddItem(message);
56 		theMessageQueue->Unlock();
57 	}
58 }
59 
60 
61 	void MessageQueueTestCase::RemoveMessage(BMessage *message)
62 {
63 	if (theMessageQueue->Lock()) {
64 		theMessageQueue->RemoveMessage(message);
65 		messageList.RemoveItem((void *)message);
66 		theMessageQueue->Unlock();
67 	}
68 }
69 
70 
71 	BMessage *MessageQueueTestCase::NextMessage(void)
72 {
73 	SafetyLock theSafetyLock(theMessageQueue);
74 
75 	BMessage *result = NULL;
76 	if (theMessageQueue->Lock()) {
77 		result = theMessageQueue->NextMessage();
78 		CPPUNIT_ASSERT(result == messageList.RemoveItem((int32)0));
79 		theMessageQueue->Unlock();
80 	}
81 	return(result);
82 }
83 
84 
85 
86 	BMessage *MessageQueueTestCase::FindMessage(uint32 what, int index)
87 {
88 	int listCount = messageList.CountItems();
89 	int i;
90 
91 	for (i = 0; i < listCount; i++) {
92 		BMessage *theMessage = (BMessage *)messageList.ItemAt(i);
93 		if (theMessage->what == what) {
94 			if (index == 0) {
95 				return(theMessage);
96 			}
97 			index--;
98 		}
99 	}
100 
101 	return(NULL);
102 }
103 
104 
105 
106