Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestHashtables.cpp File Reference
#include "nsTHashtable.h"
#include "nsBaseHashtable.h"
#include "nsDataHashtable.h"
#include "nsInterfaceHashtable.h"
#include "nsClassHashtable.h"
#include "nsCOMPtr.h"
#include "nsISupports.h"
#include "nsCRT.h"
#include "nsCOMArray.h"

Go to the source code of this file.

Classes

class  TestUniChar
struct  EntityNode
class  EntityToUnicodeEntry
class  IFoo

Defines

#define ENTITY_COUNT   (sizeof(gEntities)/sizeof(EntityNode))
#define NS_IFOO_IID

Functions

PLDHashOperator nsTEnumGo (EntityToUnicodeEntry *aEntry, void *userArg)
PLDHashOperator nsTEnumStop (EntityToUnicodeEntry *aEntry, void *userArg)
void testTHashtable (nsTHashtable< EntityToUnicodeEntry > &hash, PRUint32 numEntries)
PLDHashOperator nsDEnumRead (const PRUint32 &aKey, const char *aData, void *userArg)
PLDHashOperator nsDEnum (const PRUint32 &aKey, const char *&aData, void *userArg)
PLDHashOperator nsCEnumRead (const nsACString &aKey, TestUniChar *aData, void *userArg)
PLDHashOperator nsCEnum (const nsACString &aKey, nsAutoPtr< TestUniChar > &aData, void *userArg)
nsresult CreateIFoo (IFoo **result)
PLDHashOperator nsIEnumRead (const PRUint32 &aKey, IFoo *aFoo, void *userArg)
PLDHashOperator nsIEnum (const PRUint32 &aKey, nsCOMPtr< IFoo > &aData, void *userArg)
PLDHashOperator nsIEnum2Read (nsISupports *aKey, PRUint32 aData, void *userArg)
PLDHashOperator nsIEnum2 (nsISupports *aKey, PRUint32 &aData, void *userArg)
int main (void)
 main()

Variables

EntityNode gEntities []

Class Documentation

struct EntityNode

Definition at line 47 of file nsHTMLEntities.cpp.

Class Members
const char * mStr
PRInt32 mUnicode
PRUint32 mUnicode

Define Documentation

#define ENTITY_COUNT   (sizeof(gEntities)/sizeof(EntityNode))

Definition at line 93 of file TestHashtables.cpp.

Value:
{ 0x6f7652e0,  0xee43, 0x11d1, \
 { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }

Definition at line 220 of file TestHashtables.cpp.


Function Documentation

nsresult CreateIFoo ( IFoo **  result)

Definition at line 347 of file TestHashtables.cpp.

  {
    printf("    >>CreateIFoo() --> ");
    IFoo* foop = new IFoo();
    printf("IFoo@%p\n", NS_STATIC_CAST(void*, foop));

    foop->AddRef();
    *result = foop;

    printf("<<CreateIFoo()\n");
    return 0;
  }
int main ( void  )

main()

Definition at line 401 of file TestHashtables.cpp.

           {
  // check an nsTHashtable
  nsTHashtable<EntityToUnicodeEntry> EntityToUnicode;

  printf("Initializing nsTHashtable...");
  if (!EntityToUnicode.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (1);
  }
  printf("OK\n");

  printf("Partially filling nsTHashtable:\n");
  testTHashtable(EntityToUnicode, 5);

  printf("Enumerate-removing...\n");
  PRUint32 count = EntityToUnicode.EnumerateEntries(nsTEnumStop, nsnull);
  if (count != 5) {
    printf("wrong count\n");
    exit (7);
  }
  printf("OK\n");

  printf("Check enumeration...");
  count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nsnull);
  if (count) {
    printf("entries remain in table!\n");
    exit (8);
  }
  printf("OK\n");

  printf("Filling nsTHashtable:\n");
  testTHashtable(EntityToUnicode, ENTITY_COUNT);

  printf("Clearing...");
  EntityToUnicode.Clear();
  printf("OK\n");

  printf("Check enumeration...");
  count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nsnull);
  if (count) {
    printf("entries remain in table!\n");
    exit (9);
  }
  printf("OK\n");

  //
  // now check a data-hashtable
  //

  nsDataHashtable<nsUint32HashKey,const char*> UniToEntity;

  printf("Initializing nsDataHashtable...");
  if (!UniToEntity.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (10);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  PRUint32 i;
  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    if (!UniToEntity.Put(gEntities[i].mUnicode, gEntities[i].mStr)) {
      printf("FAILED\n");
      exit (11);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");
  const char* str;

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %u...", gEntities[i].mUnicode);
    if (!UniToEntity.Get(gEntities[i].mUnicode, &str)) {
      printf("FAILED\n");
      exit (12);
    }

    printf("Found %s\n", str);
  }

  printf("Testing non-existent entries...");
  if (UniToEntity.Get(99446, &str)) {
    printf("FOUND! BAD!\n");
    exit (13);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = UniToEntity.EnumerateRead(nsDEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (14);
  }
  
  printf("Clearing...");
  UniToEntity.Clear();
  printf("OK\n");

  printf("Checking count...");
  count = UniToEntity.Enumerate(nsDEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (15);
  }

  printf("OK\n");

  //
  // now check a thread-safe data-hashtable
  //

  nsDataHashtableMT<nsUint32HashKey,const char*> UniToEntityL;

  printf("Initializing nsDataHashtableMT...");
  if (!UniToEntityL.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (10);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    if (!UniToEntityL.Put(gEntities[i].mUnicode, gEntities[i].mStr)) {
      printf("FAILED\n");
      exit (11);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %u...", gEntities[i].mUnicode);
    if (!UniToEntityL.Get(gEntities[i].mUnicode, &str)) {
      printf("FAILED\n");
      exit (12);
    }

    printf("Found %s\n", str);
  }

  printf("Testing non-existent entries...");
  if (UniToEntityL.Get(99446, &str)) {
    printf("FOUND! BAD!\n");
    exit (13);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = UniToEntityL.EnumerateRead(nsDEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (14);
  }
  
  printf("Clearing...");
  UniToEntityL.Clear();
  printf("OK\n");

  printf("Checking count...");
  count = UniToEntityL.Enumerate(nsDEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (15);
  }

  printf("OK\n");

  //
  // now check a class-hashtable
  //

  nsClassHashtable<nsCStringHashKey,TestUniChar> EntToUniClass;

  printf("Initializing nsClassHashtable...");
  if (!EntToUniClass.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (16);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    TestUniChar* temp = new TestUniChar(gEntities[i].mUnicode);

    if (!EntToUniClass.Put(nsDependentCString(gEntities[i].mStr), temp)) {
      printf("FAILED\n");
      delete temp;
      exit (17);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");
  TestUniChar* myChar;

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %s...", gEntities[i].mStr);
    if (!EntToUniClass.Get(nsDependentCString(gEntities[i].mStr), &myChar)) {
      printf("FAILED\n");
      exit (18);
    }

    printf("Found %c\n", myChar->GetChar());
  }

  printf("Testing non-existent entries...");
  if (EntToUniClass.Get(NS_LITERAL_CSTRING("xxxx"), &myChar)) {
    printf("FOUND! BAD!\n");
    exit (19);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = EntToUniClass.EnumerateRead(nsCEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (20);
  }
  
  printf("Clearing...\n");
  EntToUniClass.Clear();
  printf("  Clearing OK\n");

  printf("Checking count...");
  count = EntToUniClass.Enumerate(nsCEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (21);
  }

  printf("OK\n");

  //
  // now check a thread-safe class-hashtable
  //

  nsClassHashtableMT<nsCStringHashKey,TestUniChar> EntToUniClassL;

  printf("Initializing nsClassHashtableMT...");
  if (!EntToUniClassL.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (16);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    TestUniChar* temp = new TestUniChar(gEntities[i].mUnicode);

    if (!EntToUniClassL.Put(nsDependentCString(gEntities[i].mStr), temp)) {
      printf("FAILED\n");
      delete temp;
      exit (17);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %s...", gEntities[i].mStr);
    if (!EntToUniClassL.Get(nsDependentCString(gEntities[i].mStr), &myChar)) {
      printf("FAILED\n");
      exit (18);
    }

    printf("Found %c\n", myChar->GetChar());
  }

  printf("Testing non-existent entries...");
  if (EntToUniClassL.Get(NS_LITERAL_CSTRING("xxxx"), &myChar)) {
    printf("FOUND! BAD!\n");
    exit (19);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = EntToUniClassL.EnumerateRead(nsCEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (20);
  }
  
  printf("Clearing...\n");
  EntToUniClassL.Clear();
  printf("  Clearing OK\n");

  printf("Checking count...");
  count = EntToUniClassL.Enumerate(nsCEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (21);
  }

  printf("OK\n");

  //
  // now check a data-hashtable with an interface key
  //

  nsDataHashtable<nsISupportsHashKey,PRUint32> EntToUniClass2;

  printf("Initializing nsDataHashtable with interface key...");
  if (!EntToUniClass2.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (22);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  nsCOMArray<IFoo> fooArray;

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    nsCOMPtr<IFoo> foo;
    CreateIFoo(getter_AddRefs(foo));
    foo->SetString(nsDependentCString(gEntities[i].mStr));
    
    
    fooArray.InsertObjectAt(foo, i);

    if (!EntToUniClass2.Put(foo, gEntities[i].mUnicode)) {
      printf("FAILED\n");
      exit (23);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");
  PRUint32 myChar2;

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %s...", gEntities[i].mStr);
    
    if (!EntToUniClass2.Get(fooArray[i], &myChar2)) {
      printf("FAILED\n");
      exit (24);
    }

    printf("Found %c\n", myChar2);
  }

  printf("Testing non-existent entries...");
  if (EntToUniClass2.Get((nsISupports*) 0x55443316, &myChar2)) {
    printf("FOUND! BAD!\n");
    exit (25);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = EntToUniClass2.EnumerateRead(nsIEnum2Read, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (26);
  }
  
  printf("Clearing...\n");
  EntToUniClass2.Clear();
  printf("  Clearing OK\n");

  printf("Checking count...");
  count = EntToUniClass2.Enumerate(nsIEnum2, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (27);
  }

  printf("OK\n");

  //
  // now check an interface-hashtable with an PRUint32 key
  //

  nsInterfaceHashtable<nsUint32HashKey,IFoo> UniToEntClass2;

  printf("Initializing nsInterfaceHashtable...");
  if (!UniToEntClass2.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (28);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    nsCOMPtr<IFoo> foo;
    CreateIFoo(getter_AddRefs(foo));
    foo->SetString(nsDependentCString(gEntities[i].mStr));
    
    if (!UniToEntClass2.Put(gEntities[i].mUnicode, foo)) {
      printf("FAILED\n");
      exit (29);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %s...", gEntities[i].mStr);
    
    nsCOMPtr<IFoo> myEnt;
    if (!UniToEntClass2.Get(gEntities[i].mUnicode, getter_AddRefs(myEnt))) {
      printf("FAILED\n");
      exit (30);
    }
    
    nsCAutoString str;
    myEnt->GetString(str);
    printf("Found %s\n", str.get());
  }

  printf("Testing non-existent entries...");
  nsCOMPtr<IFoo> myEnt;
  if (UniToEntClass2.Get(9462, getter_AddRefs(myEnt))) {
    printf("FOUND! BAD!\n");
    exit (31);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = UniToEntClass2.EnumerateRead(nsIEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (32);
  }
  
  printf("Clearing...\n");
  UniToEntClass2.Clear();
  printf("  Clearing OK\n");

  printf("Checking count...");
  count = UniToEntClass2.Enumerate(nsIEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (33);
  }

  printf("OK\n");

  //
  // now check a thread-safe interface hashtable
  //

  nsInterfaceHashtableMT<nsUint32HashKey,IFoo> UniToEntClass2L;

  printf("Initializing nsInterfaceHashtableMT...");
  if (!UniToEntClass2L.Init(ENTITY_COUNT)) {
    printf("FAILED\n");
    exit (28);
  }
  printf("OK\n");

  printf("Filling hash with %d entries.\n", ENTITY_COUNT);

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Putting entry %u...", gEntities[i].mUnicode);
    nsCOMPtr<IFoo> foo;
    CreateIFoo(getter_AddRefs(foo));
    foo->SetString(nsDependentCString(gEntities[i].mStr));
    
    if (!UniToEntClass2L.Put(gEntities[i].mUnicode, foo)) {
      printf("FAILED\n");
      exit (29);
    }
    printf("OK...\n");
  }

  printf("Testing Get:\n");

  for (i = 0; i < ENTITY_COUNT; ++i) {
    printf("  Getting entry %s...", gEntities[i].mStr);
    
    nsCOMPtr<IFoo> myEnt;
    if (!UniToEntClass2L.Get(gEntities[i].mUnicode, getter_AddRefs(myEnt))) {
      printf("FAILED\n");
      exit (30);
    }
    
    nsCAutoString str;
    myEnt->GetString(str);
    printf("Found %s\n", str.get());
  }

  printf("Testing non-existent entries...");
  if (UniToEntClass2L.Get(9462, getter_AddRefs(myEnt))) {
    printf("FOUND! BAD!\n");
    exit (31);
  }
      
  printf("not found; good.\n");
      
  printf("Enumerating:\n");
  
  count = UniToEntClass2L.EnumerateRead(nsIEnumRead, nsnull);
  if (count != ENTITY_COUNT) {
    printf("  Bad count!\n");
    exit (32);
  }
  
  printf("Clearing...\n");
  UniToEntClass2L.Clear();
  printf("  Clearing OK\n");

  printf("Checking count...");
  count = UniToEntClass2L.Enumerate(nsIEnum, nsnull);
  if (count) {
    printf("  Clear did not remove all entries.\n");
    exit (33);
  }

  printf("OK\n");

  return 0;
}
PLDHashOperator nsCEnum ( const nsACString &  aKey,
nsAutoPtr< TestUniChar > &  aData,
void userArg 
)

Definition at line 210 of file TestHashtables.cpp.

                                                                              {
    printf("  enumerated \"%s\" = %c\n", 
           PromiseFlatCString(aKey).get(), aData->GetChar());
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsCEnumRead ( const nsACString &  aKey,
TestUniChar aData,
void userArg 
)

Definition at line 203 of file TestHashtables.cpp.

                                                                       {
  printf("  enumerated \"%s\" = %c\n",
         PromiseFlatCString(aKey).get(), aData->GetChar());
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsDEnum ( const PRUint32 aKey,
const char *&  aData,
void userArg 
)

Definition at line 197 of file TestHashtables.cpp.

                                                                 {
  printf("  enumerated %u = \"%s\"\n", aKey, aData);
  return PL_DHASH_NEXT;
}

Here is the caller graph for this function:

PLDHashOperator nsDEnumRead ( const PRUint32 aKey,
const char *  aData,
void userArg 
)

Definition at line 191 of file TestHashtables.cpp.

                                                                    {
  printf("  enumerated %u = \"%s\"\n", aKey, aData);
  return PL_DHASH_NEXT;
}

Here is the caller graph for this function:

PLDHashOperator nsIEnum ( const PRUint32 aKey,
nsCOMPtr< IFoo > &  aData,
void userArg 
)

Definition at line 371 of file TestHashtables.cpp.

                                                                    {
  nsCAutoString str;
  aData->GetString(str);

  printf("  enumerated %u = \"%s\"\n", aKey, str.get());
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsIEnum2 ( nsISupports *  aKey,
PRUint32 aData,
void userArg 
)

Definition at line 391 of file TestHashtables.cpp.

                                                            {
  nsCAutoString str;
  nsCOMPtr<IFoo> foo = do_QueryInterface(aKey);
  foo->GetString(str);

  printf("  enumerated \"%s\" = %u\n", str.get(), aData);
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsIEnum2Read ( nsISupports *  aKey,
PRUint32  aData,
void userArg 
)

Definition at line 380 of file TestHashtables.cpp.

                                                               {
  nsCAutoString str;
  nsCOMPtr<IFoo> foo = do_QueryInterface(aKey);
  foo->GetString(str);


  printf("  enumerated \"%s\" = %u\n", str.get(), aData);
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsIEnumRead ( const PRUint32 aKey,
IFoo aFoo,
void userArg 
)

Definition at line 362 of file TestHashtables.cpp.

                                                             {
  nsCAutoString str;
  aFoo->GetString(str);

  printf("  enumerated %u = \"%s\"\n", aKey, str.get());
  return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator nsTEnumGo ( EntityToUnicodeEntry aEntry,
void userArg 
)

Definition at line 115 of file TestHashtables.cpp.

                                                       {
  printf("  enumerated \"%s\" = %u\n", 
         aEntry->mNode->mStr, aEntry->mNode->mUnicode);

  return PL_DHASH_NEXT;
}

Here is the caller graph for this function:

PLDHashOperator nsTEnumStop ( EntityToUnicodeEntry aEntry,
void userArg 
)

Definition at line 123 of file TestHashtables.cpp.

                                                         {
  printf("  enumerated \"%s\" = %u\n",
         aEntry->mNode->mStr, aEntry->mNode->mUnicode);

  return PL_DHASH_REMOVE;
}

Here is the caller graph for this function:

Definition at line 131 of file TestHashtables.cpp.

                                                                              {
  printf("Filling hash with %d entries.\n", numEntries);

  PRUint32 i;
  for (i = 0; i < numEntries; ++i) {
    printf("  Putting entry \"%s\"...", gEntities[i].mStr);
    EntityToUnicodeEntry* entry =
      hash.PutEntry(gEntities[i].mStr);

    if (!entry) {
      printf("FAILED\n");
      exit (2);
    }
    printf("OK...");

    if (entry->mNode) {
      printf("entry already exists!\n");
      exit (3);
    }
    printf("\n");

    entry->mNode = &gEntities[i];
  }

  printf("Testing Get:\n");

  for (i = 0; i < numEntries; ++i) {
    printf("  Getting entry \"%s\"...", gEntities[i].mStr);
    EntityToUnicodeEntry* entry =
      hash.GetEntry(gEntities[i].mStr);

    if (!entry) {
      printf("FAILED\n");
      exit (4);
    }

    printf("Found %u\n", entry->mNode->mUnicode);
  }

  printf("Testing non-existent entries...");

  EntityToUnicodeEntry* entry =
    hash.GetEntry("xxxy");

  if (entry) {
    printf("FOUND! BAD!\n");
    exit (5);
  }

  printf("not found; good.\n");

  printf("Enumerating:\n");
  PRUint32 count = hash.EnumerateEntries(nsTEnumGo, nsnull);
  if (count != numEntries) {
    printf("  Bad count!\n");
    exit (6);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
 {
  {"nbsp",160},
  {"iexcl",161},
  {"cent",162},
  {"pound",163},
  {"curren",164},
  {"yen",165},
  {"brvbar",166},
  {"sect",167},
  {"uml",168},
  {"copy",169},
  {"ordf",170},
  {"laquo",171},
  {"not",172},
  {"shy",173},
  {"reg",174},
  {"macr",175}
}

Definition at line 74 of file TestHashtables.cpp.