xref: /haiku/src/tests/kits/app/bmessagequeue/FindMessageTest1.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2 	$Id: FindMessageTest1.cpp 383 2002-07-22 09:28:00Z tylerdauwalder $
3 
4 	This file implements the second test for the Haiku BMessageQueue code.
5 	It tests the Add Message 1 and Find Message 2 use cases.  It does so by
6 	doing the following:
7 		- creates numPerWhatCode * numWhatCodes messages and puts them on
8 		  the queue and the list
9 		- searches for the xth instance of a what code using FindMessage()
10 		  and by searching the list
11 		- compares the result of the two
12 		- there are many cases where FindMessage() returns NULL and those are
13 		  checked also
14 		- it does all searches for all the what codes used plus two not
15 		  actually used
16 
17 	*/
18 
19 
20 #include "ThreadedTestCaller.h"
21 #include "FindMessageTest1.h"
22 #include <Message.h>
23 #include <MessageQueue.h>
24 #include "MessageQueue.h"
25 
26 
27 // This constant indicates the number of different what codes to use
28 // in messages added to the queue.
29 const int numWhatCodes = 20;
30 
31 // This constant indicates the number of times a what code should be
32 // added to the list.
33 const int numPerWhatCode = 50;
34 
35 
36 /*
37  *  Method:  FindMessageTest1::FindMessageTest1()
38  *   Descr:  This is the constructor for this test class.
39  */
40 
41 
42 	FindMessageTest1::FindMessageTest1(std::string name) :
43 		MessageQueueTestCase(name)
44 {
45 	}
46 
47 
48 /*
49  *  Method:  FindMessageTest1::~FindMessageTest1()
50  *   Descr:  This is the destructor for this test class.
51  */
52 
53 
54 	FindMessageTest1::~FindMessageTest1()
55 {
56 	}
57 
58 
59 /*
60  *  Method:  FindMessageTest1::PerformTest()
61  *   Descr:  This is the member function for executing the test.
62  *           It adds numPerWhatCode*numWhatCodes messages to the
63  *           queue.  Then, it uses FindMessage() to search for
64  *           each message using the queue and the list.  It
65  *           checks that the queue and list return the same result.
66  */
67 
68 
69 	void FindMessageTest1::PerformTest(void)
70 {
71 	int whatCode;
72 	int i;
73 	BMessage *theMessage;
74 	BMessage *listMessage;
75 
76 	for (i = 0; i < numPerWhatCode; i++) {
77 		if (i % (numPerWhatCode / 10) == 0)
78 			NextSubTest();
79 
80 		for (whatCode = 1; whatCode <= numWhatCodes; whatCode++) {
81 			AddMessage(new testMessageClass(whatCode));
82 		}
83 	}
84 
85 	for (whatCode = 0; whatCode <= numWhatCodes + 1; whatCode++) {
86 		if (i % (numWhatCodes / 10) == 0)
87 			NextSubTest();
88 
89 		int index = 0;
90 		while ((theMessage = theMessageQueue->FindMessage(whatCode,
91 														   index)) != NULL) {
92 			listMessage = FindMessage(whatCode, index);
93 			CPPUNIT_ASSERT(listMessage == theMessage);
94 			index++;
95 		}
96 		listMessage = FindMessage(whatCode, index);
97 		CPPUNIT_ASSERT(listMessage == theMessage);
98 	}
99 }
100 
101 
102 /*
103  *  Method:  FindMessageTest1::suite()
104  *   Descr:  This static member function returns a test caller for performing
105  *           all combinations of "FindMessageTest1".  The test
106  *           is created as a ThreadedTestCase (typedef'd as
107  *           FindMessageTest1Caller) with only one thread.
108  */
109 
110  Test *FindMessageTest1::suite(void)
111 {
112 	typedef BThreadedTestCaller<FindMessageTest1>
113 		FindMessageTest1Caller;
114 
115 	FindMessageTest1 *theTest = new FindMessageTest1("");
116 	FindMessageTest1Caller *testCaller = new FindMessageTest1Caller("BMessageQueue::Find Message Test", theTest);
117 	testCaller->addThread("A", &FindMessageTest1::PerformTest);
118 
119 	return(testCaller);
120 	}
121 
122 
123