1 //------------------------------------------------------------------------------
4 //------------------------------------------------------------------------------
6 #ifndef MESSAGEITEMTEST_H
7 #define MESSAGEITEMTEST_H
9 // A sad attempt to get rid of the horrible and pathetic vector<bool> specialization
10 #define __SGI_STL_INTERNAL_BVECTOR_H
12 // Standard Includes -----------------------------------------------------------
16 #include <posix/string.h>
18 // System Includes -------------------------------------------------------------
21 // Project Includes ------------------------------------------------------------
23 // Local Includes --------------------------------------------------------------
24 #include "../common.h"
26 // Local Defines ---------------------------------------------------------------
27 #define TEMPLATE_TEST_PARAMS <Type, TypeCode, FuncPolicy, InitPolicy, AssertPolicy, ComparePolicy>
28 #define ADD_TEMPLATE_TEST(classbeingtested, suitename, classname, funcname) \
29 (suitename)->addTest(new TestCaller<classname>(((string(#classbeingtested) + "::" + #funcname + "::" + typeid(Type).name()).c_str() ), \
30 &classname::funcname));
32 // Globals ---------------------------------------------------------------------
36 type_code TypeCode
, // B_INT32_TYPE
37 class FuncPolicy
, // status_t Add(BMessage&, const char*, Type&)
38 // status_t Find(BMessage&, const char*, int32, Type*)
39 // Type QuickFind(BMessage&, const char*, int32)
40 // bool Has(BMessage&, const char*, int32)
41 // status_t Replace(BMessage&, const char*, int32, Type)
42 class InitPolicy
, // Type Zero()
45 // size_t SizeOf(const Type&)
47 // typedef XXX ArrayType
48 class AssertPolicy
, // Type Zero()
50 class ComparePolicy
// bool Compare(const Type& lhs, const Type& rhs)
52 class TMessageItemTest
;
55 //------------------------------------------------------------------------------
59 typedef vector
<T
> ArrayType
;
60 typedef typename
ArrayType::size_type SizeType
;
61 static SizeType
Size(ArrayType
& array
) { return array
.size(); }
63 //------------------------------------------------------------------------------
67 enum { FixedSize
= true };
68 inline Type
& Dereference(Type
* p
)
72 inline Type
* AddressOf(Type
& t
) { return &t
; }
74 //------------------------------------------------------------------------------
78 status_t (BMessage::*AddFunc
)(const char*, Type
),
79 status_t (BMessage::*FindFunc
)(const char*, int32
, Type
*) const,
80 status_t (BMessage::*ShortFindFunc
)(const char*, Type
*) const,
81 Type (BMessage::*QuickFindFunc
)(const char*, int32
) const,
82 bool (BMessage::*HasFunc
)(const char*, int32
) const,
83 status_t (BMessage::*ReplaceFunc
)(const char*, int32
, Type
),
84 status_t (BMessage::*AddDataFunc
)(const char*, type_code
, const void*,
85 ssize_t
, bool, int32
) = &BMessage::AddData
,
86 status_t (BMessage::*FindDataFunc
)(const char*, type_code
, int32
,
87 const void**, ssize_t
*) const = &BMessage::FindData
89 struct TMessageItemFuncPolicy
: public TypePolicy
<Type
>
91 static status_t
Add(BMessage
& msg
, const char* name
, Type
& val
)
93 return (msg
.*AddFunc
)(name
, val
);
95 static status_t
AddData(BMessage
& msg
, const char* name
, type_code type
,
96 Type
* val
, ssize_t size
, bool fixedSize
= true)
98 return (msg
.*AddDataFunc
)(name
, type
, (const void*)val
, size
, fixedSize
, 1);
100 static status_t
Find(BMessage
& msg
, const char* name
, int32 index
, Type
* val
)
102 return (msg
.*FindFunc
)(name
, index
, val
);
104 static status_t
ShortFind(BMessage
& msg
, const char* name
, Type
* val
)
106 return (msg
.*ShortFindFunc
)(name
, val
);
108 static Type
QuickFind(BMessage
& msg
, const char* name
, int32 index
)
110 return (msg
.*QuickFindFunc
)(name
, index
);
112 static bool Has(BMessage
& msg
, const char* name
, int32 index
)
114 return (msg
.*HasFunc
)(name
, index
);
116 static status_t
Replace(BMessage
& msg
, const char* name
, int32 index
, Type
& val
)
118 return (msg
.*ReplaceFunc
)(name
, index
, val
);
120 static status_t
FindData(BMessage
& msg
, const char* name
, type_code type
,
121 int32 index
, const void** data
, ssize_t
* size
)
123 return (msg
.*FindDataFunc
)(name
, type
, index
, data
, size
);
126 //------------------------------------------------------------------------------
127 template<class T
, T zero
= T(), T invalid
= T()>
128 struct TMessageItemAssertPolicy
130 inline static T
Zero() { return zero
; }
131 inline static T
Invalid() { return invalid
; }
132 inline static bool Size(size_t size
, T
& t
)
133 { return size
== sizeof (t
); }
135 //------------------------------------------------------------------------------
137 struct TMessageItemComparePolicy
139 inline static bool Compare(const T
& lhs
, const T
& rhs
);
140 // { return lhs == rhs; }
144 TMessageItemComparePolicy
<T
>::Compare(const T
&lhs
, const T
&rhs
)
148 //------------------------------------------------------------------------------
152 type_code TypeCode
, // B_INT32_TYPE
153 class FuncPolicy
, // status_t Add(BMessage&, const char*, Type&)
154 // status_t Find(BMessage&, const char*, int32, Type*)
155 // Type QuickFind(BMessage&, const char*, int32)
156 // bool Has(BMessage&, const char*, int32)
157 // status_t Replace(BMessage&, const char*, int32, Type)
158 // status_t FindData(BMessage&, const char*, type_code, int32, const void**, ssize_t*)
159 class InitPolicy
, // Type Zero()
162 // size_t SizeOf(const Type&)
164 // typedef XXX ArrayType
165 class AssertPolicy
, // Type Zero()
167 // bool Size(size_t, T&)
168 class ComparePolicy
// bool Compare(const Type& lhs, const Type& rhs)
169 = TMessageItemComparePolicy
<Type
>
171 class TMessageItemTest
: public TestCase
, public TypePolicy
<Type
>
174 TMessageItemTest() {;}
175 TMessageItemTest(std::string name
) : TestCase(name
) {;}
177 void MessageItemTest1();
178 void MessageItemTest2();
179 void MessageItemTest3();
180 void MessageItemTest4();
181 void MessageItemTest5();
182 void MessageItemTest6();
183 void MessageItemTest7();
184 void MessageItemTest8();
185 void MessageItemTest9();
186 void MessageItemTest10();
187 void MessageItemTest11();
188 void MessageItemTest12();
190 static TestSuite
* Suite();
192 typedef typename
InitPolicy::ArrayType ArrayType
;
196 //------------------------------------------------------------------------------
207 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
211 Type out
= InitPolicy::Zero();
212 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, "item", &out
) == B_NAME_NOT_FOUND
);
213 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", 0, &out
) == B_NAME_NOT_FOUND
);
214 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, AssertPolicy::Invalid()));
215 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", 0),
216 AssertPolicy::Invalid()));
217 CPPUNIT_ASSERT(!FuncPolicy::Has(msg
, "item", 0));
218 const void* ptr
= &out
;
220 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, 0, &ptr
, &size
) ==
222 CPPUNIT_ASSERT(ptr
== NULL
);
224 //------------------------------------------------------------------------------
235 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
239 Type in
= InitPolicy::Test1();
240 Type out
= InitPolicy::Zero();
241 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
) == B_OK
);
242 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", 0));
243 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", 0), in
));
244 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, "item", &out
) == B_OK
);
245 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", 0, &out
) == B_OK
);
246 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in
));
249 status_t err
= FuncPolicy::FindData(msg
, "item", TypeCode
, 0,
250 (const void**)&pout
, &size
);
251 CPPUNIT_ASSERT(err
== B_OK
);
252 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in
));
253 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
255 //------------------------------------------------------------------------------
266 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
270 Type in
= InitPolicy::Test1();
271 Type in2
= InitPolicy::Test2();
272 Type out
= InitPolicy::Zero();
273 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
) == B_OK
);
274 CPPUNIT_ASSERT(FuncPolicy::Replace(msg
, "item", 0, in2
) == B_OK
);
275 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", 0));
276 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", 0), in2
));
277 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, "item", &out
) == B_OK
);
278 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", 0, &out
) == B_OK
);
279 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in2
));
280 out
= InitPolicy::Zero();
283 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, 0,
284 (const void**)&pout
, &size
) == B_OK
);
285 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in2
));
286 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
288 //------------------------------------------------------------------------------
299 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
303 Type out
= InitPolicy::Zero();
304 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", 1, &out
) == B_NAME_NOT_FOUND
);
305 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, AssertPolicy::Invalid()));
306 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", 1),
307 AssertPolicy::Invalid()));
308 CPPUNIT_ASSERT(!FuncPolicy::Has(msg
, "item", 1));
309 const void* ptr
= &out
;
311 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, 1, &ptr
, &size
) ==
313 CPPUNIT_ASSERT(ptr
== NULL
);
315 //------------------------------------------------------------------------------
326 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
330 ArrayType in
= InitPolicy::Array();
331 Type out
= InitPolicy::Zero();
335 for (uint32 ii
= 0; ii
< InitPolicy::Size(in
); ++ii
)
337 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
[ii
]) == B_OK
);
340 for (uint32 i
= 0; i
< InitPolicy::Size(in
); ++i
)
342 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", i
));
343 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", i
),
345 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", i
, &out
) == B_OK
);
346 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in
[i
]));
347 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, i
,
348 (const void**)&pout
, &size
) == B_OK
);
349 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in
[i
]));
350 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
353 //------------------------------------------------------------------------------
364 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
368 ArrayType in
= InitPolicy::Array();
369 Type in2
= InitPolicy::Test2();
370 Type out
= InitPolicy::Zero();
371 const int rIndex
= 2;
373 for (uint32 i
= 0; i
< InitPolicy::Size(in
); ++i
)
375 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
[i
]) == B_OK
);
378 CPPUNIT_ASSERT(FuncPolicy::Replace(msg
, "item", rIndex
, in2
) == B_OK
);
379 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", rIndex
));
380 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", rIndex
),
382 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", rIndex
, &out
) == B_OK
);
383 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in2
));
384 out
= InitPolicy::Zero();
387 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, rIndex
,
388 (const void**)&pout
, &size
) == B_OK
);
389 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in2
));
390 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
392 //------------------------------------------------------------------------------
403 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
407 Type in
= InitPolicy::Test1();
408 Type out
= InitPolicy::Zero();
409 CPPUNIT_ASSERT(FuncPolicy::AddData(msg
, "item", TypeCode
, AddressOf(in
),
410 InitPolicy::SizeOf(in
),
411 TypePolicy
<Type
>::FixedSize
) == B_OK
);
412 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", 0));
413 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", 0),
415 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, "item", &out
) == B_OK
);
416 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", 0, &out
) == B_OK
);
417 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in
));
420 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, 0,
421 (const void**)&pout
, &size
) == B_OK
);
422 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in
));
423 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
425 //------------------------------------------------------------------------------
437 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
441 ArrayType in
= InitPolicy::Array();
442 Type out
= InitPolicy::Zero();
445 for (uint32 i
= 0; i
< InitPolicy::Size(in
); ++i
)
447 CPPUNIT_ASSERT(FuncPolicy::AddData(msg
, "item", TypeCode
,
448 AddressOf(in
[i
]), InitPolicy::SizeOf(in
[i
]),
449 TypePolicy
<Type
>::FixedSize
) == B_OK
);
452 for (uint32 i
= 0; i
< InitPolicy::Size(in
); ++i
)
454 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", i
));
455 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", i
),
457 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", i
, &out
) == B_OK
);
458 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in
[i
]));
459 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, i
,
460 (const void**)&pout
, &size
) == B_OK
);
461 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in
[i
]));
462 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
465 //------------------------------------------------------------------------------
476 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
480 Type in
= InitPolicy::Test1();
481 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, NULL
, in
) == B_BAD_VALUE
);
483 //------------------------------------------------------------------------------
494 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
498 Type in
= InitPolicy::Test1();
499 Type out
= InitPolicy::Zero();
500 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
) == B_OK
);
501 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, NULL
, 0) == false);
502 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, NULL
, 0),
503 AssertPolicy::Invalid()));
504 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, NULL
, &out
) == B_BAD_VALUE
);
505 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, NULL
, 0, &out
) == B_BAD_VALUE
);
508 status_t err
= FuncPolicy::FindData(msg
, NULL
, TypeCode
, 0,
509 (const void**)&pout
, &size
);
510 CPPUNIT_ASSERT(err
== B_BAD_VALUE
);
512 //------------------------------------------------------------------------------
523 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
527 Type in
= InitPolicy::Test1();
528 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
) == B_OK
);
530 ssize_t flatSize
= msg
.FlattenedSize();
531 char* buf
= new char[flatSize
];
534 CPPUNIT_ASSERT(msg
.Flatten(buf
, flatSize
) == B_OK
);
537 Type out
= InitPolicy::Zero();
538 CPPUNIT_ASSERT(msg2
.Unflatten(buf
) == B_OK
);
539 CPPUNIT_ASSERT(FuncPolicy::ShortFind(msg
, "item", &out
) == B_OK
);
540 CPPUNIT_ASSERT(ComparePolicy::Compare(in
, out
));
544 //------------------------------------------------------------------------------
555 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
559 ArrayType in
= InitPolicy::Array();
561 for (uint32 ii
= 0; ii
< InitPolicy::Size(in
); ++ii
)
563 CPPUNIT_ASSERT(FuncPolicy::Add(msg
, "item", in
[ii
]) == B_OK
);
566 ssize_t flatSize
= msg
.FlattenedSize();
567 char* buf
= new char[flatSize
];
570 CPPUNIT_ASSERT(msg
.Flatten(buf
, flatSize
) == B_OK
);
573 Type out
= InitPolicy::Zero();
576 CPPUNIT_ASSERT(msg2
.Unflatten(buf
) == B_OK
);
578 for (uint32 i
= 0; i
< InitPolicy::Size(in
); ++i
)
580 CPPUNIT_ASSERT(FuncPolicy::Has(msg
, "item", i
));
581 CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg
, "item", i
),
583 CPPUNIT_ASSERT(FuncPolicy::Find(msg
, "item", i
, &out
) == B_OK
);
584 CPPUNIT_ASSERT(ComparePolicy::Compare(out
, in
[i
]));
585 CPPUNIT_ASSERT(FuncPolicy::FindData(msg
, "item", TypeCode
, i
,
586 (const void**)&pout
, &size
) == B_OK
);
587 CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout
), in
[i
]));
588 CPPUNIT_ASSERT(AssertPolicy::Size(size
, Dereference(pout
)));
593 //------------------------------------------------------------------------------
604 TMessageItemTest
<Type
, TypeCode
, FuncPolicy
, InitPolicy
, AssertPolicy
, ComparePolicy
>::
607 TestSuite
* suite
= new TestSuite("BMessage::Add/Find/Replace/HasRect()");
609 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest1
);
610 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest2
);
611 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest3
);
612 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest4
);
613 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest5
);
614 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest6
);
615 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest7
);
616 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest8
);
618 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest9
);
619 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest10
);
621 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest11
);
622 ADD_TEMPLATE_TEST(BMessage
, suite
, TMessageItemTest TEMPLATE_TEST_PARAMS
, MessageItemTest12
);
626 //------------------------------------------------------------------------------
628 #endif // MESSAGEITEMTEST_H