From e5b24a582e1fe8b62032f0fa49dfdba26a449c6d Mon Sep 17 00:00:00 2001 From: ejakowatz Date: Fri, 23 May 2003 23:14:41 +0000 Subject: [PATCH] Many tweaks, mostly to support the freakiness of doing Add/FindData() with entry_refs and BMessages. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3297 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kits/app/bmessage/MessageBPointItemTest.h | 5 +- .../kits/app/bmessage/MessageBRectItemTest.h | 3 + .../kits/app/bmessage/MessageBoolItemTest.h | 18 +- .../kits/app/bmessage/MessageDoubleItemTest.h | 5 +- .../kits/app/bmessage/MessageFloatItemTest.h | 5 +- .../kits/app/bmessage/MessageInt16ItemTest.h | 11 +- .../kits/app/bmessage/MessageInt32ItemTest.h | 11 +- .../kits/app/bmessage/MessageInt64ItemTest.h | 11 +- .../kits/app/bmessage/MessageInt8ItemTest.h | 11 +- src/tests/kits/app/bmessage/MessageItemTest.h | 199 ++++++++++------ .../app/bmessage/MessageMessageItemTest.h | 225 ++++++++++++++++++ .../kits/app/bmessage/MessageRefItemTest.h | 93 +++++++- src/tests/kits/app/bmessage/MessageTest.cpp | 10 +- 13 files changed, 497 insertions(+), 110 deletions(-) create mode 100644 src/tests/kits/app/bmessage/MessageMessageItemTest.h diff --git a/src/tests/kits/app/bmessage/MessageBPointItemTest.h b/src/tests/kits/app/bmessage/MessageBPointItemTest.h index c6cc555c9d..453b76780c 100644 --- a/src/tests/kits/app/bmessage/MessageBPointItemTest.h +++ b/src/tests/kits/app/bmessage/MessageBPointItemTest.h @@ -35,6 +35,7 @@ struct TPointInitPolicy : public ArrayTypeBase inline static BPoint Zero() { return BPoint(0.0, 0.0); } inline static BPoint Test1() { return BPoint(10.0, 10.0); } inline static BPoint Test2() { return BPoint(20.0, 20.0); } + inline static size_t SizeOf(const BPoint&) { return sizeof (BPoint); } inline static ArrayType Array() { ArrayType array; @@ -48,7 +49,9 @@ struct TPointInitPolicy : public ArrayTypeBase struct TPointAssertPolicy { inline static BPoint Zero() { return BPoint(0.0, 0.0); } - inline static BPoint Invalid() { return BPoint(0.0, 0.0);} + inline static BPoint Invalid() { return BPoint(0.0, 0.0); } + inline static bool Size(size_t size, BPoint& p) + { return size == sizeof (p); } }; typedef TMessageItemTest diff --git a/src/tests/kits/app/bmessage/MessageBRectItemTest.h b/src/tests/kits/app/bmessage/MessageBRectItemTest.h index 9ae432e9bd..6a9c032479 100644 --- a/src/tests/kits/app/bmessage/MessageBRectItemTest.h +++ b/src/tests/kits/app/bmessage/MessageBRectItemTest.h @@ -35,6 +35,7 @@ struct TBRectInitPolicy : public ArrayTypeBase inline static BRect Zero() { return BRect(0, 0, 0, 0); } inline static BRect Test1() { return BRect(1, 2, 3, 4); } inline static BRect Test2() { return BRect(5, 6, 7, 8); } + inline static size_t SizeOf(const BRect&) { return sizeof (BRect); } inline static ArrayType Array() { ArrayType array; @@ -49,6 +50,8 @@ struct TBRectAssertPolicy { inline static BRect Zero() { return BRect(0, 0, 0, 0); } inline static BRect Invalid() { return BRect(0, 0, -1, -1); } + inline static bool Size(size_t size, BRect& r) + { return size == sizeof (r); } }; typedef TMessageItemTest diff --git a/src/tests/kits/app/bmessage/MessageBoolItemTest.h b/src/tests/kits/app/bmessage/MessageBoolItemTest.h index 0e915ceee4..d7d55027aa 100644 --- a/src/tests/kits/app/bmessage/MessageBoolItemTest.h +++ b/src/tests/kits/app/bmessage/MessageBoolItemTest.h @@ -80,6 +80,7 @@ struct TBoolInitPolicy : public ArrayTypeBase inline static bool Zero() { return false; } inline static bool Test1() { return true; } inline static bool Test2() { return false; } + inline static size_t SizeOf(const bool&) { return sizeof (bool); } inline static ArrayType Array() { static bool array[] = { true, true, true }; @@ -87,11 +88,18 @@ struct TBoolInitPolicy : public ArrayTypeBase } }; -struct TBoolAssertPolicy -{ - inline static bool Zero() { return false; } - inline static bool Invalid() { return false;} -}; +//struct TBoolAssertPolicy +//{ +// inline static bool Zero() { return false; } +// inline static bool Invalid() { return false;} +//}; +typedef TMessageItemAssertPolicy +< + bool//, +// false, +// false +> +TBoolAssertPolicy; typedef TMessageItemTest < diff --git a/src/tests/kits/app/bmessage/MessageDoubleItemTest.h b/src/tests/kits/app/bmessage/MessageDoubleItemTest.h index e0ef063855..8e6907f7a4 100644 --- a/src/tests/kits/app/bmessage/MessageDoubleItemTest.h +++ b/src/tests/kits/app/bmessage/MessageDoubleItemTest.h @@ -35,6 +35,7 @@ struct TDoubleInitPolicy : public ArrayTypeBase inline static double Zero() { return 0; } inline static double Test1() { return 1.234; } inline static double Test2() { return 5.678; } + inline static size_t SizeOf(const double&) { return sizeof (double); } inline static ArrayType Array() { ArrayType array; @@ -48,7 +49,9 @@ struct TDoubleInitPolicy : public ArrayTypeBase struct TDoubleAssertPolicy { inline static double Zero() { return 0; } - inline static double Invalid() { return 0;} + inline static double Invalid() { return 0; } + inline static bool Size(size_t size, double& d) + { return size == sizeof (d); } }; typedef TMessageItemTest diff --git a/src/tests/kits/app/bmessage/MessageFloatItemTest.h b/src/tests/kits/app/bmessage/MessageFloatItemTest.h index ce107fc5c6..e8df490ca1 100644 --- a/src/tests/kits/app/bmessage/MessageFloatItemTest.h +++ b/src/tests/kits/app/bmessage/MessageFloatItemTest.h @@ -35,6 +35,7 @@ struct TFloatInitPolicy : public ArrayTypeBase inline static float Zero() { return 0; } inline static float Test1() { return 1.234; } inline static float Test2() { return 5.678; } + inline static size_t SizeOf(const float&) { return sizeof (float); } inline static ArrayType Array() { ArrayType array; @@ -48,7 +49,9 @@ struct TFloatInitPolicy : public ArrayTypeBase struct TFloatAssertPolicy { inline static float Zero() { return 0; } - inline static float Invalid() { return 0;} + inline static float Invalid() { return 0; } + inline static bool Size(size_t size, float& f) + { return size == sizeof (f); } }; typedef TMessageItemTest diff --git a/src/tests/kits/app/bmessage/MessageInt16ItemTest.h b/src/tests/kits/app/bmessage/MessageInt16ItemTest.h index 39e66add63..85dab4c7a2 100644 --- a/src/tests/kits/app/bmessage/MessageInt16ItemTest.h +++ b/src/tests/kits/app/bmessage/MessageInt16ItemTest.h @@ -35,6 +35,7 @@ struct TInt16InitPolicy : public ArrayTypeBase inline static int16 Zero() { return 0; } inline static int16 Test1() { return 1234; } inline static int16 Test2() { return 5678; } + inline static size_t SizeOf(const int16&) { return sizeof (int16); } inline static ArrayType Array() { ArrayType array; @@ -45,11 +46,11 @@ struct TInt16InitPolicy : public ArrayTypeBase } }; -struct TInt16AssertPolicy -{ - inline static int16 Zero() { return 0; } - inline static int16 Invalid() { return 0;} -}; +typedef TMessageItemAssertPolicy +< + int16 +> +TInt16AssertPolicy; typedef TMessageItemTest < diff --git a/src/tests/kits/app/bmessage/MessageInt32ItemTest.h b/src/tests/kits/app/bmessage/MessageInt32ItemTest.h index f74afe10cf..2a2b3480e7 100644 --- a/src/tests/kits/app/bmessage/MessageInt32ItemTest.h +++ b/src/tests/kits/app/bmessage/MessageInt32ItemTest.h @@ -38,6 +38,7 @@ struct TInt32InitPolicy : public ArrayTypeBase inline static int32 Zero() { return 0; } inline static int32 Test1() { return 1234; } inline static int32 Test2() { return 5678; } + inline static size_t SizeOf(const int32&) { return sizeof (int32); } inline static ArrayType Array() { ArrayType array; @@ -48,11 +49,11 @@ struct TInt32InitPolicy : public ArrayTypeBase } }; -struct TInt32AssertPolicy -{ - inline static int32 Zero() { return 0; } - inline static int32 Invalid() { return 0;} -}; +typedef TMessageItemAssertPolicy +< + int32 +> +TInt32AssertPolicy; typedef TMessageItemTest < diff --git a/src/tests/kits/app/bmessage/MessageInt64ItemTest.h b/src/tests/kits/app/bmessage/MessageInt64ItemTest.h index d92bb22883..8919173f2f 100644 --- a/src/tests/kits/app/bmessage/MessageInt64ItemTest.h +++ b/src/tests/kits/app/bmessage/MessageInt64ItemTest.h @@ -35,6 +35,7 @@ struct TInt64InitPolicy : public ArrayTypeBase inline static int64 Zero() { return 0; } inline static int64 Test1() { return 1234; } inline static int64 Test2() { return 5678; } + inline static size_t SizeOf(const int64&) { return sizeof (int64); } inline static ArrayType Array() { ArrayType array; @@ -45,11 +46,11 @@ struct TInt64InitPolicy : public ArrayTypeBase } }; -struct TInt64AssertPolicy -{ - inline static int64 Zero() { return 0; } - inline static int64 Invalid() { return 0;} -}; +typedef TMessageItemAssertPolicy +< + int64 +> +TInt64AssertPolicy; typedef TMessageItemTest < diff --git a/src/tests/kits/app/bmessage/MessageInt8ItemTest.h b/src/tests/kits/app/bmessage/MessageInt8ItemTest.h index eae33c29c3..d7da37c67c 100644 --- a/src/tests/kits/app/bmessage/MessageInt8ItemTest.h +++ b/src/tests/kits/app/bmessage/MessageInt8ItemTest.h @@ -35,6 +35,7 @@ struct TInt8InitPolicy : public ArrayTypeBase inline static int8 Zero() { return 0; } inline static int8 Test1() { return 16; } inline static int8 Test2() { return 32; } + inline static size_t SizeOf(const int8&) { return sizeof (int8); } inline static ArrayType Array() { ArrayType array; @@ -45,11 +46,11 @@ struct TInt8InitPolicy : public ArrayTypeBase } }; -struct TInt8AssertPolicy -{ - inline static int8 Zero() { return 0; } - inline static int8 Invalid() { return 0;} -}; +typedef TMessageItemAssertPolicy +< + int8 +> +TInt8AssertPolicy; typedef TMessageItemTest < diff --git a/src/tests/kits/app/bmessage/MessageItemTest.h b/src/tests/kits/app/bmessage/MessageItemTest.h index 64f8180987..35e95757ef 100644 --- a/src/tests/kits/app/bmessage/MessageItemTest.h +++ b/src/tests/kits/app/bmessage/MessageItemTest.h @@ -28,7 +28,7 @@ #include "../common.h" // Local Defines --------------------------------------------------------------- -#define TEMPLATE_TEST_PARAMS +#define TEMPLATE_TEST_PARAMS #define ADD_TEMPLATE_TEST(classbeingtested, suitename, classname, funcname) \ (suitename)->addTest(new TestCaller(((string(#classbeingtested) + "::" + #funcname + "::" + typeid(Type).name()).c_str() ), \ &classname::funcname)); @@ -46,8 +46,12 @@ template class InitPolicy, // Type Zero() // Type Test1() // Type Test2() - // vector Array() - class AssertPolicy // Type Zero() + // size_t SizeOf(const Type&) + // ArrayType Array() + // typedef XXX ArrayType + class AssertPolicy, // Type Zero() + // Type Invalid() + class ComparePolicy // bool Compare(const Type& lhs, const Type& rhs) > class TMessageItemTest; @@ -68,7 +72,11 @@ template status_t (BMessage::*FindFunc)(const char*, int32, Type*) const, Type (BMessage::*QuickFindFunc)(const char*, int32) const, bool (BMessage::*HasFunc)(const char*, int32) const, - status_t (BMessage::*ReplaceFunc)(const char*, int32, Type) + status_t (BMessage::*ReplaceFunc)(const char*, int32, Type), + status_t (BMessage::*AddDataFunc)(const char*, type_code, const void*, + ssize_t, bool, int32) = &BMessage::AddData, + status_t (BMessage::*FindDataFunc)(const char*, type_code, int32, + const void**, ssize_t*) const = &BMessage::FindData > struct TMessageItemFuncPolicy { @@ -76,6 +84,11 @@ struct TMessageItemFuncPolicy { return (msg.*AddFunc)(name, val); } + static status_t AddData(BMessage& msg, const char* name, type_code type, + Type* val, ssize_t size) + { + return (msg.*AddDataFunc)(name, type, (const void*)val, size); + } static status_t Find(BMessage& msg, const char* name, int32 index, Type* val) { return (msg.*FindFunc)(name, index, val); @@ -88,10 +101,31 @@ struct TMessageItemFuncPolicy { return (msg.*HasFunc)(name, index); } - static status_t Replace(BMessage& msg, const char* name, int32 index, Type val) + static status_t Replace(BMessage& msg, const char* name, int32 index, Type& val) { return (msg.*ReplaceFunc)(name, index, val); } + static status_t FindData(BMessage& msg, const char* name, type_code type, + int32 index, const void** data, ssize_t* size) + { + return (msg.*FindDataFunc)(name, type, index, data, size); + } +}; +//------------------------------------------------------------------------------ +template +struct TMessageItemAssertPolicy +{ + inline static T Zero() { return zero; } + inline static T Invalid() { return invalid; } + inline static bool Size(size_t size, T& t) + { return size == sizeof (t); } +}; +//------------------------------------------------------------------------------ +template +struct TMessageItemComparePolicy +{ + inline static bool Compare(const T& lhs, const T& rhs) + { return lhs == rhs; } }; //------------------------------------------------------------------------------ template @@ -103,13 +137,18 @@ template // Type QuickFind(BMessage&, const char*, int32) // bool Has(BMessage&, const char*, int32) // status_t Replace(BMessage&, const char*, int32, Type) + // status_t FindData(BMessage&, const char*, type_code, int32, const void**, ssize_t*) class InitPolicy, // Type Zero() // Type Test1() // Type Test2() + // size_t SizeOf(const Type&) // ArrayType Array() // typedef XXX ArrayType - class AssertPolicy // Type Zero() + class AssertPolicy, // Type Zero() // Type Invalid() + // bool Size(size_t, T&) + class ComparePolicy // bool Compare(const Type& lhs, const Type& rhs) + = TMessageItemComparePolicy > class TMessageItemTest : public TestCase { @@ -139,35 +178,40 @@ template type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest1() { BMessage msg; Type out = InitPolicy::Zero(); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_NAME_NOT_FOUND); - CPPUNIT_ASSERT(out == AssertPolicy::Invalid()); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", 0) == AssertPolicy::Invalid()); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, AssertPolicy::Invalid())); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0), + AssertPolicy::Invalid())); CPPUNIT_ASSERT(!FuncPolicy::Has(msg, "item", 0)); const void* ptr = &out; ssize_t size; - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, &ptr, &size) == + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, 0, &ptr, &size) == B_NAME_NOT_FOUND); CPPUNIT_ASSERT(ptr == NULL); } //------------------------------------------------------------------------------ +// R5 segfaults on the find data portion of this test for entry_refs +// TODO: test R5 for entry_ref & BMessage template < class Type, type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest2() { BMessage msg; @@ -175,27 +219,30 @@ MessageItemTest2() Type out = InitPolicy::Zero(); CPPUNIT_ASSERT(FuncPolicy::Add(msg, "item", in) == B_OK); CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", 0)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", 0) == in); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0), in)); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK); - CPPUNIT_ASSERT(out == in); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in)); Type* pout = NULL; ssize_t size; - status_t err = msg.FindData("item", TypeCode, (const void**)&pout, &size); + status_t err = FuncPolicy::FindData(msg, "item", TypeCode, 0, + (const void**)&pout, &size); CPPUNIT_ASSERT(err == B_OK); - CPPUNIT_ASSERT(*pout == in); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in)); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } //------------------------------------------------------------------------------ +// TODO: test R5 for entry_ref & BMessage template < class Type, type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest3() { BMessage msg; @@ -205,16 +252,16 @@ MessageItemTest3() CPPUNIT_ASSERT(FuncPolicy::Add(msg, "item", in) == B_OK); CPPUNIT_ASSERT(FuncPolicy::Replace(msg, "item", 0, in2) == B_OK); CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", 0)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", 0) == in2); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0), in2)); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK); - CPPUNIT_ASSERT(out == in2); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in2)); out = InitPolicy::Zero(); Type* pout; ssize_t size; - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, (const void**)&pout, - &size) == B_OK); - CPPUNIT_ASSERT(*pout == in2); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, 0, + (const void**)&pout, &size) == B_OK); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in2)); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } //------------------------------------------------------------------------------ template @@ -223,35 +270,39 @@ template type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest4() { BMessage msg; Type out = InitPolicy::Zero(); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 1, &out) == B_NAME_NOT_FOUND); - CPPUNIT_ASSERT(out == AssertPolicy::Invalid()); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", 1) == AssertPolicy::Invalid()); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, AssertPolicy::Invalid())); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 1), + AssertPolicy::Invalid())); CPPUNIT_ASSERT(!FuncPolicy::Has(msg, "item", 1)); const void* ptr = &out; ssize_t size; - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, 1, &ptr, &size) == + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, 1, &ptr, &size) == B_NAME_NOT_FOUND); CPPUNIT_ASSERT(ptr == NULL); } //------------------------------------------------------------------------------ +// TODO: test R5 for entry_ref & BMessage template < class Type, type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest5() { BMessage msg; @@ -268,26 +319,29 @@ MessageItemTest5() for (int32 i = 0; i < InitPolicy::Size(in); ++i) { CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", i)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", i) == in[i]); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", i), + in[i])); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", i, &out) == B_OK); - CPPUNIT_ASSERT(out == in[i]); - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, i, - (const void**)&pout, &size) == B_OK); - CPPUNIT_ASSERT(*pout == in[i]); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in[i])); + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, i, + (const void**)&pout, &size) == B_OK); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in[i])); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } } //------------------------------------------------------------------------------ +// TODO: test R5 for entry_ref & BMessage template < class Type, type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest6() { BMessage msg; @@ -303,45 +357,49 @@ MessageItemTest6() CPPUNIT_ASSERT(FuncPolicy::Replace(msg, "item", rIndex, in2) == B_OK); CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", rIndex)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", rIndex) == in2); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", rIndex), + in2)); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", rIndex, &out) == B_OK); - CPPUNIT_ASSERT(out == in2); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in2)); out = InitPolicy::Zero(); Type* pout; ssize_t size; - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, rIndex, - (const void**)&pout, &size) == B_OK); - CPPUNIT_ASSERT(*pout == in2); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, rIndex, + (const void**)&pout, &size) == B_OK); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in2)); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } //------------------------------------------------------------------------------ +// TODO: test R5 for entry_ref & BMessage template < class Type, type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest7() { BMessage msg; Type in = InitPolicy::Test1(); Type out = InitPolicy::Zero(); - CPPUNIT_ASSERT(msg.AddData("item", TypeCode, (const void*)&in, - sizeof (Type)) == B_OK); + CPPUNIT_ASSERT(FuncPolicy::AddData(msg, "item", TypeCode, &in, + InitPolicy::SizeOf(in)) == B_OK); CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", 0)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", 0) == in); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0), + in)); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK); - CPPUNIT_ASSERT(out == in); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in)); Type* pout = NULL; ssize_t size; - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, (const void**)&pout, - &size) == B_OK); - CPPUNIT_ASSERT(*pout == in); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, 0, + (const void**)&pout, &size) == B_OK); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in)); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } //------------------------------------------------------------------------------ template @@ -350,10 +408,11 @@ template type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > void -TMessageItemTest:: +TMessageItemTest:: MessageItemTest8() { BMessage msg; @@ -364,20 +423,21 @@ MessageItemTest8() for (int32 i = 0; i < InitPolicy::Size(in); ++i) { - CPPUNIT_ASSERT(msg.AddData("item", TypeCode, - (const void*)&in[i], sizeof (Type)) == B_OK); + CPPUNIT_ASSERT(FuncPolicy::AddData(msg, "item", TypeCode, + &in[i], InitPolicy::SizeOf(in[i])) == B_OK); } for (int32 i = 0; i < InitPolicy::Size(in); ++i) { CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", i)); - CPPUNIT_ASSERT(FuncPolicy::QuickFind(msg, "item", i) == in[i]); + CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", i), + in[i])); CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", i, &out) == B_OK); - CPPUNIT_ASSERT(out == in[i]); - CPPUNIT_ASSERT(msg.FindData("item", TypeCode, i, - (const void**)&pout, &size) == B_OK); - CPPUNIT_ASSERT(*pout == in[i]); - CPPUNIT_ASSERT(size == sizeof (Type)); + CPPUNIT_ASSERT(ComparePolicy::Compare(out, in[i])); + CPPUNIT_ASSERT(FuncPolicy::FindData(msg, "item", TypeCode, i, + (const void**)&pout, &size) == B_OK); + CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in[i])); + CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout)); } } //------------------------------------------------------------------------------ @@ -387,10 +447,11 @@ template type_code TypeCode, class FuncPolicy, class InitPolicy, - class AssertPolicy + class AssertPolicy, + class ComparePolicy > TestSuite* -TMessageItemTest:: +TMessageItemTest:: Suite() { TestSuite* suite = new TestSuite("BMessage::Add/Find/Replace/HasRect()"); diff --git a/src/tests/kits/app/bmessage/MessageMessageItemTest.h b/src/tests/kits/app/bmessage/MessageMessageItemTest.h new file mode 100644 index 0000000000..b6a6f598f2 --- /dev/null +++ b/src/tests/kits/app/bmessage/MessageMessageItemTest.h @@ -0,0 +1,225 @@ +//------------------------------------------------------------------------------ +// MessageMessageItemTest.h +// +//------------------------------------------------------------------------------ + +#ifndef MESSAGEMESSAGEITEMTEST_H +#define MESSAGEMESSAGEITEMTEST_H + +// Standard Includes ----------------------------------------------------------- + +// System Includes ------------------------------------------------------------- +#include + +// Project Includes ------------------------------------------------------------ + +// Local Includes -------------------------------------------------------------- +#include "MessageItemTest.h" + +// Local Defines --------------------------------------------------------------- + +// Globals --------------------------------------------------------------------- + +//------------------------------------------------------------------------------ +struct TMessageFuncPolicy +{ + static status_t Add(BMessage& msg, const char* name, BMessage& val); + static status_t AddData(BMessage& msg, const char* name, type_code type, + BMessage* data, ssize_t size); + static status_t Find(BMessage& msg, const char* name, int32 index, + BMessage* val); + static BMessage QuickFind(BMessage& msg, const char* name, int32 index); + static bool Has(BMessage& msg, const char* name, int32 index); + static status_t Replace(BMessage& msg, const char* name, int32 index, + BMessage& val); + static status_t FindData(BMessage& msg, const char* name, type_code type, + int32 index, const void** data, ssize_t* size); + + private: + static BMessage sMsg; +}; +BMessage TMessageFuncPolicy::sMsg; +//------------------------------------------------------------------------------ +status_t TMessageFuncPolicy::Add(BMessage& msg, const char* name, + BMessage& val) +{ + return msg.AddMessage(name, &val); +} +//------------------------------------------------------------------------------ +status_t TMessageFuncPolicy::AddData(BMessage& msg, const char* name, + type_code type, BMessage* data, + ssize_t size) +{ + char* buf = new char[size]; + status_t err = data->Flatten(buf, size); + if (!err) + { + err = msg.AddData(name, type, buf, size, false); + } + delete[] buf; + return err; +} +//------------------------------------------------------------------------------ +inline status_t TMessageFuncPolicy::Find(BMessage& msg, const char* name, + int32 index, BMessage* val) +{ + return msg.FindMessage(name, index, val); +} +//------------------------------------------------------------------------------ +BMessage TMessageFuncPolicy::QuickFind(BMessage& msg, const char* name, + int32 index) +{ + BMessage val; + msg.FindMessage(name, index, &val); + return val; +} +//------------------------------------------------------------------------------ +inline bool TMessageFuncPolicy::Has(BMessage& msg, const char* name, + int32 index) +{ + return msg.HasMessage(name, index); +} +//------------------------------------------------------------------------------ +inline status_t TMessageFuncPolicy::Replace(BMessage& msg, const char* name, + int32 index, BMessage& val) +{ + return msg.ReplaceMessage(name, index, &val); +} +//------------------------------------------------------------------------------ +inline status_t TMessageFuncPolicy::FindData(BMessage& msg, const char* name, + type_code type, int32 index, + const void** data, ssize_t* size) +{ + *data = NULL; + char* ptr; + status_t err = msg.FindData(name, type, index, (const void**)&ptr, size); + if (!err) + { + err = sMsg.Unflatten(ptr); + if (!err) + { + *(BMessage**)data = &sMsg; + } + } + return err; +// return (msg.*FindDataFunc)(name, type, index, data, size); +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +template<> +struct ArrayTypeBase +{ + class ArrayType + { + public: + ArrayType() : array(NULL), size(0) {;} + ArrayType(const ArrayType& rhs) : array(NULL), size(0) + { *this = rhs; } + ArrayType(BMessage* data, uint32 len) : array(NULL), size(0) + { Assign(data, len); } + ~ArrayType() { if (array) delete[] array; } + + ArrayType& operator=(const ArrayType& rhs) + { + if (this != &rhs) + Assign(rhs.array, rhs.size); + return *this; + }; + + uint32 Size() { return size; } + BMessage& operator[](int index) + { + // We're just gonna let a segfault happen + return array[index]; + } + + private: + void Assign(BMessage* data, uint32 len) + { + size = len; + + if (array) + delete[] array; + array = new BMessage[Size()]; + for (uint32 i = 0; i < Size(); ++i) + array[i] = data[i]; + } + BMessage* array; + uint32 size; + }; + + typedef uint32 SizeType; + static SizeType Size(ArrayType& array) { return array.Size(); } +}; +//------------------------------------------------------------------------------ +struct TMessageInitPolicy : public ArrayTypeBase +{ + inline static BMessage Zero() { return BMessage(); } + static BMessage Test1() + { + BMessage msg('1234'); + msg.AddInt32("int32", 1234); + return msg; + } + static BMessage Test2() + { + BMessage msg('5678'); + msg.AddString("string", "5678"); + return msg; + } + static size_t SizeOf(const BMessage& data) + { + return data.FlattenedSize(); + } + inline static ArrayType Array() + { + BMessage array[3]; + array[0] = Zero(); + array[1] = Test1(); + array[2] = Test2(); + return ArrayType(array, 3); + } +}; +//------------------------------------------------------------------------------ +struct TMessageAssertPolicy +{ + inline static BMessage Zero() { return BMessage(); } + inline static BMessage Invalid() { return BMessage(); } + static bool Size(size_t size, BMessage& msg) + ;//{ return size == msg.FlattenedSize(); } +}; + bool TMessageAssertPolicy::Size(size_t size, BMessage& msg) +{ + ssize_t msgSize = msg.FlattenedSize(); + return size == msgSize; +} +//------------------------------------------------------------------------------ +template<> +struct TMessageItemComparePolicy +{ + inline static bool Compare(const BMessage& lhs, const BMessage& rhs) + { return lhs.what == rhs.what; } +}; +//------------------------------------------------------------------------------ +typedef TMessageItemTest +< + BMessage, + B_MESSAGE_TYPE, + TMessageFuncPolicy, + TMessageInitPolicy, + TMessageAssertPolicy +> +TMessageMessageItemTest; +//------------------------------------------------------------------------------ + +#endif // MESSAGEMESSAGEITEMTEST_H + +/* + * $Log $ + * + * $Id $ + * + */ + diff --git a/src/tests/kits/app/bmessage/MessageRefItemTest.h b/src/tests/kits/app/bmessage/MessageRefItemTest.h index 1eb10c951c..ba130d5bed 100644 --- a/src/tests/kits/app/bmessage/MessageRefItemTest.h +++ b/src/tests/kits/app/bmessage/MessageRefItemTest.h @@ -10,6 +10,7 @@ // System Includes ------------------------------------------------------------- #include +#include // Project Includes ------------------------------------------------------------ @@ -25,31 +26,91 @@ struct TRefFuncPolicy { return msg.AddRef(name, &val); } + static status_t AddData(BMessage& msg, const char* name, type_code type, + entry_ref* data, ssize_t size); static status_t Find(BMessage& msg, const char* name, int32 index, entry_ref* val) { return msg.FindRef(name, index, val); } - static entry_ref QuickFind(BMessage& msg, const char* name, int32 index) - { - // TODO: make cooler - // There must be some 1337 template voodoo way of making the inclusion - // of this function conditional. In the meantime, we offer this - // cheesy hack - entry_ref ref; - msg.FindRef(name, index, &ref); - return ref; - } + static entry_ref QuickFind(BMessage& msg, const char* name, int32 index); static bool Has(BMessage& msg, const char* name, int32 index) { return msg.HasRef(name, index); } static status_t Replace(BMessage& msg, const char* name, int32 index, - entry_ref val) + entry_ref& val) { return msg.ReplaceRef(name, index, &val); } + static status_t FindData(BMessage& msg, const char* name, type_code type, + int32 index, const void** data, ssize_t* size); + + private: + static entry_ref sRef; }; +status_t TRefFuncPolicy::AddData(BMessage& msg, const char* name, type_code type, + entry_ref* data, ssize_t size) +{ + BPath Path(data); + status_t err = Path.InitCheck(); + char* buf = NULL; +// if (!err) + { + buf = new char[size]; + err = Path.Flatten(buf, size); + } + + if (!err) + { + err = msg.AddData(name, type, buf, size, false); + } + delete[] buf; + return err; +} + +entry_ref TRefFuncPolicy::QuickFind(BMessage &msg, const char *name, int32 index) +{ + // TODO: make cooler + // There must be some 1337 template voodoo way of making the inclusion + // of this function conditional. In the meantime, we offer this + // cheesy hack + entry_ref ref; + msg.FindRef(name, index, &ref); + return ref; +} + +status_t TRefFuncPolicy::FindData(BMessage& msg, const char* name, + type_code type, int32 index, + const void** data, ssize_t* size) +{ + *data = NULL; + char* ptr; + status_t err = msg.FindData(name, type, index, (const void**)&ptr, size); + if (!err) + { + BPath Path; + err = Path.Unflatten(type, ptr, *size); + if (!err) + { + if (Path.Path()) + { + err = get_ref_for_path(Path.Path(), &sRef); + if (!err) + { + *(entry_ref**)data = &sRef; + } + } + else + { + sRef = entry_ref(); + *(entry_ref**)data = &sRef; + } + } + } + return err; +} +entry_ref TRefFuncPolicy::sRef; //------------------------------------------------------------------------------ struct TRefInitPolicy : public ArrayTypeBase { @@ -66,6 +127,11 @@ struct TRefInitPolicy : public ArrayTypeBase get_ref_for_path("/boot/develop/headers/be/Be.h", &ref); return ref; } + static size_t SizeOf(const entry_ref& data) + { + BPath Path(&data); + return Path.FlattenedSize(); + } inline static ArrayType Array() { ArrayType array; @@ -80,6 +146,11 @@ struct TRefAssertPolicy { inline static entry_ref Zero() { return TRefInitPolicy::Zero(); } inline static entry_ref Invalid() { return TRefInitPolicy::Zero();} +// TODO: fix + static bool Size(size_t size, entry_ref& ref) + { + return size == TRefInitPolicy::SizeOf(ref); + } }; //------------------------------------------------------------------------------ typedef TMessageItemTest diff --git a/src/tests/kits/app/bmessage/MessageTest.cpp b/src/tests/kits/app/bmessage/MessageTest.cpp index b800f6f12e..5eac8cb209 100644 --- a/src/tests/kits/app/bmessage/MessageTest.cpp +++ b/src/tests/kits/app/bmessage/MessageTest.cpp @@ -3,6 +3,7 @@ #include "../common.h" #include "MessageConstructTest.h" #include "MessageOpAssignTest.h" +#include "MessageEasyFindTest.h" #include "MessageBoolItemTest.h" #include "MessageInt8ItemTest.h" #include "MessageInt16ItemTest.h" @@ -10,10 +11,12 @@ #include "MessageInt64ItemTest.h" #include "MessageBRectItemTest.h" #include "MessageBPointItemTest.h" -#include "MessageEasyFindTest.h" #include "MessageFloatItemTest.h" #include "MessageDoubleItemTest.h" +#include "MessageMessageItemTest.h" +//#ifdef TEST_OBOS #include "MessageRefItemTest.h" +//#endif Test* MessageTestSuite() { @@ -21,6 +24,7 @@ Test* MessageTestSuite() tests->addTest(TMessageConstructTest::Suite()); tests->addTest(TMessageOpAssignTest::Suite()); + tests->addTest(TMessageEasyFindTest::Suite()); tests->addTest(TMessageBoolItemTest::Suite()); tests->addTest(TMessageInt8ItemTest::Suite()); tests->addTest(TMessageInt16ItemTest::Suite()); @@ -28,10 +32,12 @@ Test* MessageTestSuite() tests->addTest(TMessageInt64ItemTest::Suite()); tests->addTest(TMessageBRectItemTest::Suite()); tests->addTest(TMessageBPointItemTest::Suite()); - tests->addTest(TMessageEasyFindTest::Suite()); tests->addTest(TMessageFloatItemTest::Suite()); tests->addTest(TMessageDoubleItemTest::Suite()); + tests->addTest(TMessageMessageItemTest::Suite()); +//#ifdef TEST_OBOS tests->addTest(TMessageRefItemTest::Suite()); +//#endif return tests; }