From: <laz...@us...> - 2004-03-07 21:16:53
|
Update of /cvsroot/rtk/rtk/test/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32596/test/core Modified Files: dict0.cpp Log Message: - Added custom key support for Dict - template based (T)Dict classes - Int / Str Dict added Index: dict0.cpp =================================================================== RCS file: /cvsroot/rtk/rtk/test/core/dict0.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** dict0.cpp 26 Feb 2004 20:02:16 -0000 1.7 --- dict0.cpp 7 Mar 2004 20:53:00 -0000 1.8 *************** *** 43,112 **** #include <rtk/Dict.h> #include <rtk/Thread.h> using namespace Rtk; ! void DictPrint(const RCHAR* oString, void* oData,void *userData) { ! rprintf(_R("Dict[\"%s\"]=%s\n"), oString, (RCHAR *)oData); } int main(int argc, char** argv) { ! Dict oDict(17); ! RCHAR* aKeys[] = { ! _R("One"), ! _R("Two"), ! _R("Three"), ! _R("Four"), ! _R("Five"), ! _R("Six"), ! _R("Seven"), ! _R("Eight"), ! _R("Nine"), ! NULL ! }; ! RCHAR* aValues[] = { ! _R("Mikko"), ! _R("Pali"), ! _R("Dejan"), ! _R("Alexey"), ! _R("Martin"), ! _R("Bo"), ! _R("Milosh"), ! _R("Sanel"), ! _R("Bill") ! }; ! String oString(_R("Hi Dejan, it's me - Mikko")); ! String oTmp(_R("One")); ! for (int ii=0; aKeys[ii] != 0; ii++ ) ! { ! oDict.Insert(aKeys[ii], aValues[ii]); ! } // for ! rprintf(_R("hashed '%s' index is %u\n"), oString.c_str(), Dict::Hash(oString) % 17); ! rprintf(_R("hashed '%s' index is %u\n"), oTmp.c_str(), Dict::Hash(oTmp) % 17); ! rprintf(_R("\n")); ! if (!oDict.Replace(oTmp, (void*)oString.c_str())) ! rprintf(_R("Error!\n")); ! if (!oDict.Replace(_R("Two"), (void*)oTmp.c_str())) ! rprintf(_R("Error!\n")); ! rprintf(_R("Loopup(\"One\") = %s\n"), (RCHAR*)oDict.Lookup(_R("One"))); ! rprintf(_R("oDict[\"Two\"] = %s\n"), (RCHAR*)oDict[_R("Two")]); ! rprintf(_R("\nNumber of DictNodes: %d\n"), oDict.GetCount()); ! oDict.Enumerate(DictPrint); getchar(); return 0; ! } // main() /** --- 43,254 ---- #include <rtk/Dict.h> + #include <rtk/TDict.h> #include <rtk/Thread.h> + #include "../test.h" + using namespace Rtk; ! class Test { ! public: ! int val; ! Test() : val(-1) { ! //printf("Test::Test()\n"); ! } ! Test(const Test &t) : val(t.val) { ! //printf("Test::Test(const Test &t) %d\n", val); ! } ! Test(int v) : val(v) { ! //printf("Test::Test(int v) %d\n", v); ! } ! ~Test() { ! //printf("Test::~Test() %d\n", val); ! } ! ! // Dict custom KEY needed functions: ! bool Compare(const Test *t) const { return val==t->val; } ! uint Hash() const { return (val*2); } ! }; ! ! // Compare needed for Dict custom KEY classes ! static RTK_INLINE bool operator == (const Test &s1, const Test &s2) { return s1.val==s2.val; } ! ! ! void IntDictPrint(int *key, void* oData, void *userData) ! { ! rprintf(_R("\tDict[%d] = %p\n"), *key, oData); ! } ! ! void StrDictPrint(String *key, void* oData, void *userData) ! { ! rprintf(_R("\tDict[\"%s\"] = %p\n"), key->c_str(), oData); ! } ! ! void TCustomDictPrint(Test *key, String *oData, void *userData) ! { ! rprintf(_R("\tDict[Test(%d)] = \"%s\"\n"), key->val, oData->c_str()); ! } ! ! void TIntDictPrint(int *key, String *oData, void *userData) ! { ! rprintf(_R("\tDict[%d] = \"%s\"\n"), *key, oData->c_str()); ! } ! ! void TStrDictPrint(String *key, String *oData, void *userData) ! { ! rprintf(_R("\tDict[\"%s\"] = \"%s\"\n"), key->c_str(), oData->c_str()); ! } ! ! void template_tests() ! { ! rprintf(_R("\ntemplate_tests()\n")); ! ! { ! rprintf(_R("\nCustom Template Dict Test: TDict<Test, String>\n")); ! ! TDict<Test, String> custom; ! //custom.error_return = _R("<NOT FOUND>"); ! ! String val; ! int ret; ! ! STEP( Test a(10) ); ! STEP( custom[a] = _R("Hello") ); ! STEP( custom[Test(14)] = _R("World") ); ! ! STEP( val = custom[a] ); ! OUT("val = %s", val.c_str()); ! ! STEP( val = custom[14] ); ! OUT("val = %s", val.c_str()); ! ! STEP( ret = custom.Contains(10) ); ! OUT("ret = %d", ret); ! ! STEP( ret = custom.Contains(5) ); ! OUT("ret = %d", ret); ! ! STEP( custom.Enumerate((DictEnumFunc)TCustomDictPrint) ); ! } ! ! { ! rprintf(_R("\nInt Template Dict Test: TIntDict<String>\n")); ! ! TIntDict<String> intd; ! ! String val; ! int ret; ! ! STEP( int a = 10 ); ! STEP( intd[a] = _R("Hello") ); ! STEP( intd[14] = _R("World") ); ! ! STEP( val = intd[a] ); ! OUT("val = %s", val.c_str()); ! ! STEP( val = intd[14] ); ! OUT("val = %s", val.c_str()); ! ! STEP( ret = intd.Contains(10) ); ! OUT("ret = %d", ret); ! ! STEP( ret = intd.Contains(5) ); ! OUT("ret = %d", ret); ! ! STEP( intd.Enumerate((DictEnumFunc)TIntDictPrint) ); ! } ! ! { ! rprintf(_R("\nString Template Dict Test: TStrDict<String>\n")); ! ! TStrDict<String> strd; ! ! String val; ! int ret; ! ! STEP( String a(_R("STR123")) ); ! STEP( strd[a] = _R("Hello") ); ! STEP( strd[_R("321STR")] = _R("World") ); ! ! STEP( val = strd[a] ); ! OUT("val = %s", val.c_str()); ! ! STEP( val = strd[_R("321STR")] ); ! OUT("val = %s", val.c_str()); ! ! STEP( ret = strd.Contains(_R("STR123")) ); ! OUT("ret = %d", ret); ! ! STEP( ret = strd.Contains(_R("456STR")) ); ! OUT("ret = %d", ret); ! ! STEP( strd.Enumerate((DictEnumFunc)TStrDictPrint) ); ! } } int main(int argc, char** argv) { ! template_tests(); ! { ! rprintf(_R("\nString Dict Test: StrDict\n")); ! StrDict strd; ! void *val; ! int ret; ! STEP( String a(_R("Hello")) ); ! STEP( strd.Insert(a, (void*)0x1) ); ! STEP( strd.Insert(_R("World"), (void*)0x2) ); ! STEP( val = strd[a] ); ! OUT("val = %p", val); ! STEP( val = strd[_R("World")] ); ! OUT("val = %p", val); ! ! STEP( ret = strd.Contains(_R("World")) ); ! OUT("ret = %d", ret); ! STEP( ret = strd.Contains(_R("test")) ); ! OUT("ret = %d", ret); ! STEP( strd.Enumerate((DictEnumFunc)StrDictPrint) ); ! } ! ! { ! rprintf(_R("\nInt Dict Test: StrDict\n")); ! IntDict intd; ! ! void *val; ! int ret; ! ! STEP( int a = 10 ); ! STEP( intd.Insert(a, (void*)0x1) ); ! STEP( intd.Insert(14, (void*)0x2) ); ! ! STEP( val = intd[a] ); ! OUT("val = %p", val); ! ! STEP( val = intd[14] ); ! OUT("val = %p", val); ! ! STEP( ret = intd.Contains(10) ); ! OUT("ret = %d", ret); ! ! STEP( ret = intd.Contains(5) ); ! OUT("ret = %d", ret); + STEP( intd.Enumerate((DictEnumFunc)IntDictPrint) ); + } + getchar(); + return 0; ! } /** |