Unit tests for plain C string convenience functions. Tweaks elsewhere to

deal with necessary modifications to test core.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3369 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
ejakowatz 2003-05-28 20:19:35 +00:00
parent e4edcddbc5
commit 159d60f9fb
5 changed files with 160 additions and 38 deletions

View File

@ -31,7 +31,7 @@ struct TBStringFuncPolicy
static status_t Replace(BMessage& msg, const char* name, int32 index,
BString& data);
static status_t AddData(BMessage& msg, const char* name, type_code type,
const BString* data, ssize_t size);
const BString* data, ssize_t size, bool);
static status_t FindData(BMessage& msg, const char* name, type_code type,
int32 index, const void** data, ssize_t* size);
@ -74,7 +74,7 @@ status_t TBStringFuncPolicy::Replace(BMessage& msg, const char* name,
//------------------------------------------------------------------------------
status_t TBStringFuncPolicy::AddData(BMessage& msg, const char* name,
type_code type, const BString* data,
ssize_t size)
ssize_t size, bool)
{
return msg.AddData(name, type, (const void*)data->String(), size,
false);

View File

@ -0,0 +1,114 @@
//------------------------------------------------------------------------------
// MessageCStringItemTest.h
//
//------------------------------------------------------------------------------
#ifndef MESSAGECSTRINGITEMTEST_H
#define MESSAGECSTRINGITEMTEST_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <Debug.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
#include "MessageItemTest.h"
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
typedef TMessageItemFuncPolicy
<
const char*,
&BMessage::AddString,
&BMessage::FindString,
&BMessage::FindString,
&BMessage::HasString,
&BMessage::ReplaceString
>
TCStringFuncPolicy;
struct TCStringInitPolicy : public ArrayTypeBase<const char*>
{
typedef const char* TypePtr;
inline static const char* Zero() { return sStr1; }
inline static const char* Test1() { return sStr2; }
inline static const char* Test2() { return sStr3; }
inline static size_t SizeOf(const char*& data) { return strlen(data) + 1; }
inline static ArrayType Array()
{
ArrayType array;
array.push_back(Zero());
array.push_back(Test1());
array.push_back(Test2());
return array;
}
private:
static const char* sStr1;
static const char* sStr2;
static const char* sStr3;
};
const char* TCStringInitPolicy::sStr1 = "";
const char* TCStringInitPolicy::sStr2 = "cstring one";
const char* TCStringInitPolicy::sStr3 = "Bibbity-bobbity-boo!";
//------------------------------------------------------------------------------
struct TCStringAssertPolicy
{
inline static const char* Zero() { return ""; }
inline static const char* Invalid() { return ""; }
static bool Size(size_t size, const char* data)
;//{ return size == msg.FlattenedSize(); }
};
bool TCStringAssertPolicy::Size(size_t size, const char* data)
{
return size == strlen(data) + 1;
}
//------------------------------------------------------------------------------
struct TCStringComparePolicy
{
static bool Compare(const char* lhs, const char* rhs);
};
bool TCStringComparePolicy::Compare(const char* lhs, const char* rhs)
{
if (!lhs)
return rhs;
if (!rhs)
return lhs;
return strcmp(lhs, rhs) == 0;
}
//------------------------------------------------------------------------------
template<>
struct TypePolicy<const char*>
{
typedef const char* TypePtr;
enum { FixedSize = false };
inline const char* Dereference(TypePtr p) { return p; }
inline const char* AddressOf(const char*& t) { return t; }
};
//------------------------------------------------------------------------------
typedef TMessageItemTest
<
const char*,
B_STRING_TYPE,
TCStringFuncPolicy,
TCStringInitPolicy,
TCStringAssertPolicy,
TCStringComparePolicy
>
TMessageCStringItemTest;
#endif // MESSAGECSTRINGITEMTEST_H
/*
* $Log $
*
* $Id $
*
*/

View File

@ -65,6 +65,18 @@ struct ArrayTypeBase
static SizeType Size(ArrayType& array) { return array.size(); }
};
//------------------------------------------------------------------------------
template<class Type>
struct TypePolicy
{
typedef Type* TypePtr;
enum { FixedSize = true };
inline Type& Dereference(TypePtr p)
{
return *p;
}
inline TypePtr AddressOf(Type& t) { return &t; }
};
//------------------------------------------------------------------------------
template
<
typename Type,
@ -78,16 +90,16 @@ template
status_t (BMessage::*FindDataFunc)(const char*, type_code, int32,
const void**, ssize_t*) const = &BMessage::FindData
>
struct TMessageItemFuncPolicy
struct TMessageItemFuncPolicy : public TypePolicy<Type>
{
static status_t Add(BMessage& msg, const char* name, Type& val)
{
return (msg.*AddFunc)(name, val);
}
static status_t AddData(BMessage& msg, const char* name, type_code type,
Type* val, ssize_t size)
TypePtr val, ssize_t size, bool fixedSize = true)
{
return (msg.*AddDataFunc)(name, type, (const void*)val, size);
return (msg.*AddDataFunc)(name, type, (const void*)val, size, fixedSize);
}
static status_t Find(BMessage& msg, const char* name, int32 index, Type* val)
{
@ -150,7 +162,7 @@ template
class ComparePolicy // bool Compare(const Type& lhs, const Type& rhs)
= TMessageItemComparePolicy<Type>
>
class TMessageItemTest : public TestCase
class TMessageItemTest : public TestCase, public TypePolicy<Type>
{
public:
TMessageItemTest() {;}
@ -199,8 +211,6 @@ MessageItemTest1()
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,
@ -222,16 +232,15 @@ MessageItemTest2()
CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0), in));
CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK);
CPPUNIT_ASSERT(ComparePolicy::Compare(out, in));
Type* pout = NULL;
TypePtr pout = NULL;
ssize_t size;
status_t err = FuncPolicy::FindData(msg, "item", TypeCode, 0,
(const void**)&pout, &size);
CPPUNIT_ASSERT(err == B_OK);
CPPUNIT_ASSERT(ComparePolicy::Compare(*pout, in));
CPPUNIT_ASSERT(AssertPolicy::Size(size, *pout));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
//------------------------------------------------------------------------------
// TODO: test R5 for entry_ref & BMessage
template
<
class Type,
@ -256,12 +265,12 @@ MessageItemTest3()
CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK);
CPPUNIT_ASSERT(ComparePolicy::Compare(out, in2));
out = InitPolicy::Zero();
Type* pout;
TypePtr pout;
ssize_t size;
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));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in2));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
//------------------------------------------------------------------------------
template
@ -291,7 +300,6 @@ MessageItemTest4()
CPPUNIT_ASSERT(ptr == NULL);
}
//------------------------------------------------------------------------------
// TODO: test R5 for entry_ref & BMessage
template
<
class Type,
@ -308,7 +316,7 @@ MessageItemTest5()
BMessage msg;
ArrayType in = InitPolicy::Array();
Type out = InitPolicy::Zero();
Type* pout;
TypePtr pout;
ssize_t size;
for (int32 i = 0; i < InitPolicy::Size(in); ++i)
@ -325,12 +333,11 @@ MessageItemTest5()
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));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in[i]));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
}
//------------------------------------------------------------------------------
// TODO: test R5 for entry_ref & BMessage
template
<
class Type,
@ -362,15 +369,14 @@ MessageItemTest6()
CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", rIndex, &out) == B_OK);
CPPUNIT_ASSERT(ComparePolicy::Compare(out, in2));
out = InitPolicy::Zero();
Type* pout;
TypePtr pout;
ssize_t size;
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));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in2));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
//------------------------------------------------------------------------------
// TODO: test R5 for entry_ref & BMessage
template
<
class Type,
@ -387,21 +393,23 @@ MessageItemTest7()
BMessage msg;
Type in = InitPolicy::Test1();
Type out = InitPolicy::Zero();
CPPUNIT_ASSERT(FuncPolicy::AddData(msg, "item", TypeCode, &in,
InitPolicy::SizeOf(in)) == B_OK);
CPPUNIT_ASSERT(FuncPolicy::AddData(msg, "item", TypeCode, AddressOf(in),
InitPolicy::SizeOf(in),
TypePolicy<Type>::FixedSize) == B_OK);
CPPUNIT_ASSERT(FuncPolicy::Has(msg, "item", 0));
CPPUNIT_ASSERT(ComparePolicy::Compare(FuncPolicy::QuickFind(msg, "item", 0),
in));
CPPUNIT_ASSERT(FuncPolicy::Find(msg, "item", 0, &out) == B_OK);
CPPUNIT_ASSERT(ComparePolicy::Compare(out, in));
Type* pout = NULL;
TypePtr pout = NULL;
ssize_t size;
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));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
//------------------------------------------------------------------------------
#include <stdio.h>
template
<
class Type,
@ -418,13 +426,13 @@ MessageItemTest8()
BMessage msg;
ArrayType in = InitPolicy::Array();
Type out = InitPolicy::Zero();
Type* pout;
TypePtr pout;
ssize_t size;
for (int32 i = 0; i < InitPolicy::Size(in); ++i)
{
CPPUNIT_ASSERT(FuncPolicy::AddData(msg, "item", TypeCode,
&in[i], InitPolicy::SizeOf(in[i])) == B_OK);
AddressOf(in[i]), InitPolicy::SizeOf(in[i]),
TypePolicy<Type>::FixedSize) == B_OK);
}
for (int32 i = 0; i < InitPolicy::Size(in); ++i)
@ -436,8 +444,8 @@ MessageItemTest8()
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));
CPPUNIT_ASSERT(ComparePolicy::Compare(Dereference(pout), in[i]));
CPPUNIT_ASSERT(AssertPolicy::Size(size, Dereference(pout)));
}
}
//------------------------------------------------------------------------------

View File

@ -25,7 +25,7 @@ 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);
BMessage* data, ssize_t size, bool);
static status_t Find(BMessage& msg, const char* name, int32 index,
BMessage* val);
static BMessage QuickFind(BMessage& msg, const char* name, int32 index);
@ -48,7 +48,7 @@ status_t TMessageFuncPolicy::Add(BMessage& msg, const char* name,
//------------------------------------------------------------------------------
status_t TMessageFuncPolicy::AddData(BMessage& msg, const char* name,
type_code type, BMessage* data,
ssize_t size)
ssize_t size, bool)
{
char* buf = new char[size];
status_t err = data->Flatten(buf, size);

View File

@ -27,7 +27,7 @@ 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);
entry_ref* data, ssize_t size, bool);
static status_t Find(BMessage& msg, const char* name, int32 index,
entry_ref* val)
{
@ -50,7 +50,7 @@ struct TRefFuncPolicy
static entry_ref sRef;
};
status_t TRefFuncPolicy::AddData(BMessage& msg, const char* name, type_code type,
entry_ref* data, ssize_t size)
entry_ref* data, ssize_t size, bool)
{
BPath Path(data);
status_t err = Path.InitCheck();