xref: /haiku/src/tests/kits/app/blooper/IndexOfTest.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //------------------------------------------------------------------------------
2 //	IndexOfTest.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 
8 // System Includes -------------------------------------------------------------
9 #include <Handler.h>
10 #include <Looper.h>
11 
12 // Project Includes ------------------------------------------------------------
13 
14 // Local Includes --------------------------------------------------------------
15 #include "IndexOfTest.h"
16 
17 // Local Defines ---------------------------------------------------------------
18 
19 // Globals ---------------------------------------------------------------------
20 
21 //------------------------------------------------------------------------------
22 /**
23 	IndexOf(BHandler* handler)
24 	@case		handler is NULL
25 	@param		handler	NULL
26 	@results	IndexOf() returns B_ERROR
27  */
28 void TIndexOfTest::IndexOfTest1()
29 {
30 	BLooper Looper;
31 	CPPUNIT_ASSERT(Looper.IndexOf(NULL) == B_ERROR);
32 }
33 //------------------------------------------------------------------------------
34 /**
35 	IndexOf(BHandler* handler)
36 	@case		handler is valid, doesn't belong to this looper
37 	@param		handler	Valid BHandler pointer, not assigned to this looper
38 	@results	IndexOf() returns B_ERROR
39  */
40 void TIndexOfTest::IndexOfTest2()
41 {
42 	BLooper Looper;
43 	BHandler Handler;
44 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler) == B_ERROR);
45 }
46 //------------------------------------------------------------------------------
47 /**
48 	IndexOf(BHandler* handler)
49 	@case		handler is valid, belongs to looper
50 	@param		handler	Valid BHandler pointer, assigned to this looper
51 	@results	IndexOf returns 1
52  */
53 void TIndexOfTest::IndexOfTest3()
54 {
55 	BLooper Looper;
56 	BHandler Handler;
57 	Looper.AddHandler(&Handler);
58 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler) == 1);
59 }
60 //------------------------------------------------------------------------------
61 /**
62 	IndexOf(BHandler* handler)
63 	@case		handler is valid, one of many added and removed
64 	@param		handler	Valid BHandler pointer, assigned to this looper
65 	@results	IndexOf() returns correct index for each handler
66  */
67 void TIndexOfTest::IndexOfTest4()
68 {
69 	BLooper Looper;
70 	BHandler Handler1;
71 	BHandler Handler2;
72 	BHandler Handler3;
73 	BHandler Handler4;
74 	BHandler Handler5;
75 
76 	Looper.AddHandler(&Handler1);
77 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
78 	Looper.AddHandler(&Handler2);
79 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
80 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
81 	Looper.AddHandler(&Handler3);
82 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
83 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
84 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == 3);
85 	Looper.AddHandler(&Handler4);
86 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
87 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
88 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == 3);
89 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == 4);
90 	Looper.AddHandler(&Handler5);
91 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
92 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
93 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == 3);
94 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == 4);
95 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == 5);
96 
97 	// Now we remove them
98 	Looper.RemoveHandler(&Handler5);
99 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
100 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
101 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == 3);
102 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == 4);
103 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == B_ERROR);
104 	Looper.RemoveHandler(&Handler4);
105 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
106 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
107 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == 3);
108 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == B_ERROR);
109 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == B_ERROR);
110 	Looper.RemoveHandler(&Handler3);
111 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
112 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == 2);
113 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == B_ERROR);
114 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == B_ERROR);
115 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == B_ERROR);
116 	Looper.RemoveHandler(&Handler2);
117 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == 1);
118 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == B_ERROR);
119 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == B_ERROR);
120 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == B_ERROR);
121 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == B_ERROR);
122 	Looper.RemoveHandler(&Handler1);
123 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler1) == B_ERROR);
124 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler2) == B_ERROR);
125 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler3) == B_ERROR);
126 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler4) == B_ERROR);
127 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler5) == B_ERROR);
128 }
129 //------------------------------------------------------------------------------
130 /**
131 	IndexOf(BHandler* handler)
132 	@case		handler is valid, looper is unlocked
133 	@param		handler	Valid BHandler pointer, assigned to this looper
134 	@results	IndexOf returns 1.  Debugger message "Looper must be locked
135 				before calling IndexOf."
136  */
137 void TIndexOfTest::IndexOfTest5()
138 {
139 	DEBUGGER_ESCAPE;
140 
141 	BLooper Looper;
142 	BHandler Handler;
143 	Looper.AddHandler(&Handler);
144 	Looper.Unlock();
145 	CPPUNIT_ASSERT(Looper.IndexOf(&Handler) == 1);
146 }
147 //------------------------------------------------------------------------------
148 /**
149 	IndexOf(BHandler* handler)
150 	@case		handler is "this"
151 	@param		handler	The looper's this pointer
152 	@result		IndexOf() returns 0
153  */
154 void TIndexOfTest::IndexOfTest6()
155 {
156 	BLooper Looper;
157 	CPPUNIT_ASSERT(Looper.IndexOf(&Looper) == 0);
158 }
159 //------------------------------------------------------------------------------
160 TestSuite* TIndexOfTest::Suite()
161 {
162 	TestSuite* suite = new TestSuite("BLooper::IndexOf(BHandler*)");
163 
164 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest1);
165 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest2);
166 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest3);
167 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest4);
168 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest5);
169 	ADD_TEST4(BLooper, suite, TIndexOfTest, IndexOfTest6);
170 
171 	return suite;
172 }
173 //------------------------------------------------------------------------------
174 
175 /*
176  * $Log $
177  *
178  * $Id  $
179  *
180  */
181 
182 
183