xref: /haiku/src/tests/kits/app/bmessagequeue/AddMessageTest1.cpp (revision d374a27286b8a52974a97dba0d5966ea026a665d)
1 /*
2 	$Id: AddMessageTest1.cpp 383 2002-07-22 09:28:00Z tylerdauwalder $
3 
4 	This file implements the first test for the OpenBeOS BMessageQueue code.
5 	It tests the Construction, Destruction, Add Message 1, Count Messages,
6 	Is Empty and Find Message 1 use cases.  It does so by doing the following:
7 		- checks that the queue is empty according to IsEmpty() and CountMessages()
8 		- adds 10000 messages to the queue and a BList
9 		- as each is added, checks that IsEmpty() is false and CountMessages()
10 		  returns what is expected
11 		- the BList contents is compared to the queue contents (uses FindMessage())
12 		- the number of BMessages destructed is checked, no messages should have been
13 		  deleted so far.
14 		- the queue is deleted
15 		- the number of BMessages destructed should now be 10000
16 
17 	*/
18 
19 
20 #include "ThreadedTestCaller.h"
21 #include "AddMessageTest1.h"
22 #include <Message.h>
23 #include <MessageQueue.h>
24 
25 
26 /*
27  *  Method:  AddMessageTest1::AddMessageTest1()
28  *   Descr:  This is the constructor for this class.
29  */
30 
31 
32 	AddMessageTest1::AddMessageTest1(std::string name) :
33 		MessageQueueTestCase(name)
34 {
35 	}
36 
37 
38 /*
39  *  Method:  AddMessageTest1::~AddMessageTest1()
40  *   Descr:  This is the destructor for this class.
41  */
42 
43 
44 	AddMessageTest1::~AddMessageTest1()
45 {
46 	}
47 
48 
49 /*
50  *  Method:  AddMessageTest1::setUp()
51  *   Descr:  This member function is called just prior to running the test.
52  *           It resets the destructor count for testMessageClass.
53  */
54 
55 
56 	void AddMessageTest1::setUp(void)
57 {
58 	testMessageClass::messageDestructorCount = 0;
59 	}
60 
61 
62 /*
63  *  Method:  AddMessageTest1::PerformTest()
64  *   Descr:  This member function performs this test.  It adds
65  *           10000 messages to the message queue and confirms that
66  *           the queue contains the right messages.  Then it confirms
67  *           that all 10000 messages are deleted when the message
68  *           queue is deleted.
69  */
70 
71 
72 	void AddMessageTest1::PerformTest(void)
73 {
74 	assert(theMessageQueue->IsEmpty());
75 	assert(theMessageQueue->CountMessages() == 0);
76 
77 	int i;
78 	for(i = 0; i < 10000; i++) {
79 		if (i % (10000 / 10) == 0)
80 			NextSubTest();
81 		BMessage *theMessage = new testMessageClass(i);
82 		AddMessage(theMessage);
83 		assert(!theMessageQueue->IsEmpty());
84 		assert(theMessageQueue->CountMessages() == i + 1);
85 	}
86 
87 	NextSubTest();
88 	CheckQueueAgainstList();
89 
90 	assert(testMessageClass::messageDestructorCount == 0);
91 	delete theMessageQueue;
92 	theMessageQueue = NULL;
93 	NextSubTest();
94 	assert(testMessageClass::messageDestructorCount == 10000);
95 }
96 
97 
98 /*
99  *  Method:  AddMessageTest1::suite()
100  *   Descr:  This static member function returns a test caller for performing
101  *           all combinations of "AddMessageTest1".  The test
102  *           is created as a ThreadedTestCase (typedef'd as
103  *           AddMessageTest1Caller) with only one thread.
104  */
105 
106  Test *AddMessageTest1::suite(void)
107 {
108 	typedef BThreadedTestCaller<AddMessageTest1>
109 		AddMessageTest1Caller;
110 
111 	AddMessageTest1 *theTest = new AddMessageTest1("");
112 	AddMessageTest1Caller *testCaller = new AddMessageTest1Caller("BMessageQueue::Add Message Test #1", theTest);
113 	testCaller->addThread("A", &AddMessageTest1::PerformTest);
114 
115 	return(testCaller);
116 	}
117 
118 
119