xpcom/tests/TestHashtables.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestHashtables.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,668 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsTHashtable.h"
    1.10 +#include "nsBaseHashtable.h"
    1.11 +#include "nsDataHashtable.h"
    1.12 +#include "nsInterfaceHashtable.h"
    1.13 +#include "nsClassHashtable.h"
    1.14 +
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsISupports.h"
    1.17 +#include "nsCOMArray.h"
    1.18 +#include "mozilla/Attributes.h"
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +
    1.22 +namespace TestHashtables {
    1.23 +
    1.24 +class TestUniChar // for nsClassHashtable
    1.25 +{
    1.26 +public:
    1.27 +  TestUniChar(uint32_t aWord)
    1.28 +  {
    1.29 +    printf("    TestUniChar::TestUniChar() %u\n", aWord);
    1.30 +    mWord = aWord;
    1.31 +  }
    1.32 +
    1.33 +  ~TestUniChar()
    1.34 +  {
    1.35 +    printf("    TestUniChar::~TestUniChar() %u\n", mWord);
    1.36 +  }
    1.37 +
    1.38 +  uint32_t GetChar() const { return mWord; }
    1.39 +
    1.40 +private:
    1.41 +  uint32_t mWord;
    1.42 +};
    1.43 +
    1.44 +struct EntityNode {
    1.45 +  const char*   mStr; // never owns buffer
    1.46 +  uint32_t       mUnicode;
    1.47 +};
    1.48 +
    1.49 +EntityNode gEntities[] = {
    1.50 +  {"nbsp",160},
    1.51 +  {"iexcl",161},
    1.52 +  {"cent",162},
    1.53 +  {"pound",163},
    1.54 +  {"curren",164},
    1.55 +  {"yen",165},
    1.56 +  {"brvbar",166},
    1.57 +  {"sect",167},
    1.58 +  {"uml",168},
    1.59 +  {"copy",169},
    1.60 +  {"ordf",170},
    1.61 +  {"laquo",171},
    1.62 +  {"not",172},
    1.63 +  {"shy",173},
    1.64 +  {"reg",174},
    1.65 +  {"macr",175}
    1.66 +};
    1.67 +
    1.68 +#define ENTITY_COUNT (unsigned(sizeof(gEntities)/sizeof(EntityNode)))
    1.69 +
    1.70 +class EntityToUnicodeEntry : public PLDHashEntryHdr
    1.71 +{
    1.72 +public:
    1.73 +  typedef const char* KeyType;
    1.74 +  typedef const char* KeyTypePointer;
    1.75 +
    1.76 +  EntityToUnicodeEntry(const char* aKey) { mNode = nullptr; }
    1.77 +  EntityToUnicodeEntry(const EntityToUnicodeEntry& aEntry) { mNode = aEntry.mNode; }
    1.78 +  ~EntityToUnicodeEntry() { }
    1.79 +
    1.80 +  bool KeyEquals(const char* aEntity) const { return !strcmp(mNode->mStr, aEntity); }
    1.81 +  static const char* KeyToPointer(const char* aEntity) { return aEntity; }
    1.82 +  static PLDHashNumber HashKey(const char* aEntity) { return mozilla::HashString(aEntity); }
    1.83 +  enum { ALLOW_MEMMOVE = true };
    1.84 +
    1.85 +  const EntityNode* mNode;
    1.86 +};
    1.87 +
    1.88 +PLDHashOperator
    1.89 +nsTEnumGo(EntityToUnicodeEntry* aEntry, void* userArg) {
    1.90 +  printf("  enumerated \"%s\" = %u\n", 
    1.91 +         aEntry->mNode->mStr, aEntry->mNode->mUnicode);
    1.92 +
    1.93 +  return PL_DHASH_NEXT;
    1.94 +}
    1.95 +
    1.96 +PLDHashOperator
    1.97 +nsTEnumStop(EntityToUnicodeEntry* aEntry, void* userArg) {
    1.98 +  printf("  enumerated \"%s\" = %u\n",
    1.99 +         aEntry->mNode->mStr, aEntry->mNode->mUnicode);
   1.100 +
   1.101 +  return PL_DHASH_REMOVE;
   1.102 +}
   1.103 +
   1.104 +void
   1.105 +testTHashtable(nsTHashtable<EntityToUnicodeEntry>& hash, uint32_t numEntries) {
   1.106 +  printf("Filling hash with %d entries.\n", numEntries);
   1.107 +
   1.108 +  uint32_t i;
   1.109 +  for (i = 0; i < numEntries; ++i) {
   1.110 +    printf("  Putting entry \"%s\"...", gEntities[i].mStr);
   1.111 +    EntityToUnicodeEntry* entry =
   1.112 +      hash.PutEntry(gEntities[i].mStr);
   1.113 +
   1.114 +    if (!entry) {
   1.115 +      printf("FAILED\n");
   1.116 +      exit (2);
   1.117 +    }
   1.118 +    printf("OK...");
   1.119 +
   1.120 +    if (entry->mNode) {
   1.121 +      printf("entry already exists!\n");
   1.122 +      exit (3);
   1.123 +    }
   1.124 +    printf("\n");
   1.125 +
   1.126 +    entry->mNode = &gEntities[i];
   1.127 +  }
   1.128 +
   1.129 +  printf("Testing Get:\n");
   1.130 +
   1.131 +  for (i = 0; i < numEntries; ++i) {
   1.132 +    printf("  Getting entry \"%s\"...", gEntities[i].mStr);
   1.133 +    EntityToUnicodeEntry* entry =
   1.134 +      hash.GetEntry(gEntities[i].mStr);
   1.135 +
   1.136 +    if (!entry) {
   1.137 +      printf("FAILED\n");
   1.138 +      exit (4);
   1.139 +    }
   1.140 +
   1.141 +    printf("Found %u\n", entry->mNode->mUnicode);
   1.142 +  }
   1.143 +
   1.144 +  printf("Testing nonexistent entries...");
   1.145 +
   1.146 +  EntityToUnicodeEntry* entry =
   1.147 +    hash.GetEntry("xxxy");
   1.148 +
   1.149 +  if (entry) {
   1.150 +    printf("FOUND! BAD!\n");
   1.151 +    exit (5);
   1.152 +  }
   1.153 +
   1.154 +  printf("not found; good.\n");
   1.155 +
   1.156 +  printf("Enumerating:\n");
   1.157 +  uint32_t count = hash.EnumerateEntries(nsTEnumGo, nullptr);
   1.158 +  if (count != numEntries) {
   1.159 +    printf("  Bad count!\n");
   1.160 +    exit (6);
   1.161 +  }
   1.162 +}
   1.163 +
   1.164 +PLDHashOperator
   1.165 +nsDEnumRead(const uint32_t& aKey, const char* aData, void* userArg) {
   1.166 +  printf("  enumerated %u = \"%s\"\n", aKey, aData);
   1.167 +  return PL_DHASH_NEXT;
   1.168 +}
   1.169 +
   1.170 +PLDHashOperator
   1.171 +nsDEnum(const uint32_t& aKey, const char*& aData, void* userArg) {
   1.172 +  printf("  enumerated %u = \"%s\"\n", aKey, aData);
   1.173 +  return PL_DHASH_NEXT;
   1.174 +}
   1.175 +
   1.176 +PLDHashOperator
   1.177 +nsCEnumRead(const nsACString& aKey, TestUniChar* aData, void* userArg) {
   1.178 +  printf("  enumerated \"%s\" = %c\n",
   1.179 +         PromiseFlatCString(aKey).get(), aData->GetChar());
   1.180 +  return PL_DHASH_NEXT;
   1.181 +}
   1.182 +
   1.183 +PLDHashOperator
   1.184 +nsCEnum(const nsACString& aKey, nsAutoPtr<TestUniChar>& aData, void* userArg) {
   1.185 +    printf("  enumerated \"%s\" = %c\n", 
   1.186 +           PromiseFlatCString(aKey).get(), aData->GetChar());
   1.187 +  return PL_DHASH_NEXT;
   1.188 +}
   1.189 +
   1.190 +//
   1.191 +// all this nsIFoo stuff was copied wholesale from TestCOMPtr.cpp
   1.192 +//
   1.193 +
   1.194 +#define NS_IFOO_IID \
   1.195 +{ 0x6f7652e0,  0xee43, 0x11d1, \
   1.196 + { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
   1.197 +
   1.198 +class IFoo MOZ_FINAL : public nsISupports
   1.199 +  {
   1.200 +    public:
   1.201 +      NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
   1.202 +
   1.203 +      IFoo();
   1.204 +
   1.205 +      NS_IMETHOD_(MozExternalRefCountType) AddRef();
   1.206 +      NS_IMETHOD_(MozExternalRefCountType) Release();
   1.207 +      NS_IMETHOD QueryInterface( const nsIID&, void** );
   1.208 +
   1.209 +      NS_IMETHOD SetString(const nsACString& /*in*/ aString);
   1.210 +      NS_IMETHOD GetString(nsACString& /*out*/ aString);
   1.211 +
   1.212 +      static void print_totals();
   1.213 +
   1.214 +    private:
   1.215 +      ~IFoo();
   1.216 +
   1.217 +      unsigned int refcount_;
   1.218 +
   1.219 +      static unsigned int total_constructions_;
   1.220 +      static unsigned int total_destructions_;
   1.221 +      nsCString mString;
   1.222 +  };
   1.223 +
   1.224 +NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
   1.225 +
   1.226 +unsigned int IFoo::total_constructions_;
   1.227 +unsigned int IFoo::total_destructions_;
   1.228 +
   1.229 +void
   1.230 +IFoo::print_totals()
   1.231 +  {
   1.232 +    printf("total constructions/destructions --> %d/%d\n",
   1.233 +           total_constructions_, total_destructions_);
   1.234 +  }
   1.235 +
   1.236 +IFoo::IFoo()
   1.237 +    : refcount_(0)
   1.238 +  {
   1.239 +    ++total_constructions_;
   1.240 +    printf("  new IFoo@%p [#%d]\n",
   1.241 +           static_cast<void*>(this), total_constructions_);
   1.242 +  }
   1.243 +
   1.244 +IFoo::~IFoo()
   1.245 +  {
   1.246 +    ++total_destructions_;
   1.247 +    printf("IFoo@%p::~IFoo() [#%d]\n",
   1.248 +           static_cast<void*>(this), total_destructions_);
   1.249 +  }
   1.250 +
   1.251 +MozExternalRefCountType
   1.252 +IFoo::AddRef()
   1.253 +  {
   1.254 +    ++refcount_;
   1.255 +    printf("IFoo@%p::AddRef(), refcount --> %d\n", 
   1.256 +           static_cast<void*>(this), refcount_);
   1.257 +    return refcount_;
   1.258 +  }
   1.259 +
   1.260 +MozExternalRefCountType
   1.261 +IFoo::Release()
   1.262 +  {
   1.263 +    int newcount = --refcount_;
   1.264 +    if ( newcount == 0 )
   1.265 +      printf(">>");
   1.266 +
   1.267 +    printf("IFoo@%p::Release(), refcount --> %d\n",
   1.268 +           static_cast<void*>(this), refcount_);
   1.269 +
   1.270 +    if ( newcount == 0 )
   1.271 +      {
   1.272 +        printf("  delete IFoo@%p\n", static_cast<void*>(this));
   1.273 +        printf("<<IFoo@%p::Release()\n", static_cast<void*>(this));
   1.274 +        delete this;
   1.275 +      }
   1.276 +
   1.277 +    return newcount;
   1.278 +  }
   1.279 +
   1.280 +nsresult
   1.281 +IFoo::QueryInterface( const nsIID& aIID, void** aResult )
   1.282 +  {
   1.283 +    printf("IFoo@%p::QueryInterface()\n", static_cast<void*>(this));
   1.284 +    nsISupports* rawPtr = 0;
   1.285 +    nsresult status = NS_OK;
   1.286 +
   1.287 +    if ( aIID.Equals(NS_GET_IID(IFoo)) )
   1.288 +      rawPtr = this;
   1.289 +    else
   1.290 +      {
   1.291 +        nsID iid_of_ISupports = NS_ISUPPORTS_IID;
   1.292 +        if ( aIID.Equals(iid_of_ISupports) )
   1.293 +          rawPtr = static_cast<nsISupports*>(this);
   1.294 +        else
   1.295 +          status = NS_ERROR_NO_INTERFACE;
   1.296 +      }
   1.297 +
   1.298 +    NS_IF_ADDREF(rawPtr);
   1.299 +    *aResult = rawPtr;
   1.300 +
   1.301 +    return status;
   1.302 +  }
   1.303 +
   1.304 +nsresult
   1.305 +IFoo::SetString(const nsACString& aString)
   1.306 +{
   1.307 +  mString = aString;
   1.308 +  return NS_OK;
   1.309 +}
   1.310 +
   1.311 +nsresult
   1.312 +IFoo::GetString(nsACString& aString)
   1.313 +{
   1.314 +  aString = mString;
   1.315 +  return NS_OK;
   1.316 +}
   1.317 +
   1.318 +nsresult
   1.319 +CreateIFoo( IFoo** result )
   1.320 +    // a typical factory function (that calls AddRef)
   1.321 +  {
   1.322 +    printf("    >>CreateIFoo() --> ");
   1.323 +    IFoo* foop = new IFoo();
   1.324 +    printf("IFoo@%p\n", static_cast<void*>(foop));
   1.325 +
   1.326 +    foop->AddRef();
   1.327 +    *result = foop;
   1.328 +
   1.329 +    printf("<<CreateIFoo()\n");
   1.330 +    return NS_OK;
   1.331 +  }
   1.332 +
   1.333 +PLDHashOperator
   1.334 +nsIEnumRead(const uint32_t& aKey, IFoo* aFoo, void* userArg) {
   1.335 +  nsAutoCString str;
   1.336 +  aFoo->GetString(str);
   1.337 +
   1.338 +  printf("  enumerated %u = \"%s\"\n", aKey, str.get());
   1.339 +  return PL_DHASH_NEXT;
   1.340 +}
   1.341 +
   1.342 +PLDHashOperator
   1.343 +nsIEnum(const uint32_t& aKey, nsCOMPtr<IFoo>& aData, void* userArg) {
   1.344 +  nsAutoCString str;
   1.345 +  aData->GetString(str);
   1.346 +
   1.347 +  printf("  enumerated %u = \"%s\"\n", aKey, str.get());
   1.348 +  return PL_DHASH_NEXT;
   1.349 +}
   1.350 +
   1.351 +PLDHashOperator
   1.352 +nsIEnum2Read(nsISupports* aKey, uint32_t aData, void* userArg) {
   1.353 +  nsAutoCString str;
   1.354 +  nsCOMPtr<IFoo> foo = do_QueryInterface(aKey);
   1.355 +  foo->GetString(str);
   1.356 +
   1.357 +
   1.358 +  printf("  enumerated \"%s\" = %u\n", str.get(), aData);
   1.359 +  return PL_DHASH_NEXT;
   1.360 +}
   1.361 +
   1.362 +PLDHashOperator
   1.363 +nsIEnum2(nsISupports* aKey, uint32_t& aData, void* userArg) {
   1.364 +  nsAutoCString str;
   1.365 +  nsCOMPtr<IFoo> foo = do_QueryInterface(aKey);
   1.366 +  foo->GetString(str);
   1.367 +
   1.368 +  printf("  enumerated \"%s\" = %u\n", str.get(), aData);
   1.369 +  return PL_DHASH_NEXT;
   1.370 +}
   1.371 +
   1.372 +}
   1.373 +
   1.374 +using namespace TestHashtables;
   1.375 +
   1.376 +int
   1.377 +main(void) {
   1.378 +  // check an nsTHashtable
   1.379 +  printf("Initializing nsTHashtable...");
   1.380 +  nsTHashtable<EntityToUnicodeEntry> EntityToUnicode(ENTITY_COUNT);
   1.381 +  printf("OK\n");
   1.382 +
   1.383 +  printf("Partially filling nsTHashtable:\n");
   1.384 +  testTHashtable(EntityToUnicode, 5);
   1.385 +
   1.386 +  printf("Enumerate-removing...\n");
   1.387 +  uint32_t count = EntityToUnicode.EnumerateEntries(nsTEnumStop, nullptr);
   1.388 +  if (count != 5) {
   1.389 +    printf("wrong count\n");
   1.390 +    exit (7);
   1.391 +  }
   1.392 +  printf("OK\n");
   1.393 +
   1.394 +  printf("Check enumeration...");
   1.395 +  count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nullptr);
   1.396 +  if (count) {
   1.397 +    printf("entries remain in table!\n");
   1.398 +    exit (8);
   1.399 +  }
   1.400 +  printf("OK\n");
   1.401 +
   1.402 +  printf("Filling nsTHashtable:\n");
   1.403 +  testTHashtable(EntityToUnicode, ENTITY_COUNT);
   1.404 +
   1.405 +  printf("Clearing...");
   1.406 +  EntityToUnicode.Clear();
   1.407 +  printf("OK\n");
   1.408 +
   1.409 +  printf("Check enumeration...");
   1.410 +  count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nullptr);
   1.411 +  if (count) {
   1.412 +    printf("entries remain in table!\n");
   1.413 +    exit (9);
   1.414 +  }
   1.415 +  printf("OK\n");
   1.416 +
   1.417 +  //
   1.418 +  // now check a data-hashtable
   1.419 +  //
   1.420 +
   1.421 +  printf("Initializing nsDataHashtable...");
   1.422 +  nsDataHashtable<nsUint32HashKey,const char*> UniToEntity(ENTITY_COUNT);
   1.423 +  printf("OK\n");
   1.424 +
   1.425 +  printf("Filling hash with %u entries.\n", ENTITY_COUNT);
   1.426 +
   1.427 +  uint32_t i;
   1.428 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.429 +    printf("  Putting entry %u...", gEntities[i].mUnicode);
   1.430 +    UniToEntity.Put(gEntities[i].mUnicode, gEntities[i].mStr);
   1.431 +    printf("OK...\n");
   1.432 +  }
   1.433 +
   1.434 +  printf("Testing Get:\n");
   1.435 +  const char* str;
   1.436 +
   1.437 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.438 +    printf("  Getting entry %u...", gEntities[i].mUnicode);
   1.439 +    if (!UniToEntity.Get(gEntities[i].mUnicode, &str)) {
   1.440 +      printf("FAILED\n");
   1.441 +      exit (12);
   1.442 +    }
   1.443 +
   1.444 +    printf("Found %s\n", str);
   1.445 +  }
   1.446 +
   1.447 +  printf("Testing nonexistent entries...");
   1.448 +  if (UniToEntity.Get(99446, &str)) {
   1.449 +    printf("FOUND! BAD!\n");
   1.450 +    exit (13);
   1.451 +  }
   1.452 +      
   1.453 +  printf("not found; good.\n");
   1.454 +      
   1.455 +  printf("Enumerating:\n");
   1.456 +  
   1.457 +  count = UniToEntity.EnumerateRead(nsDEnumRead, nullptr);
   1.458 +  if (count != ENTITY_COUNT) {
   1.459 +    printf("  Bad count!\n");
   1.460 +    exit (14);
   1.461 +  }
   1.462 +  
   1.463 +  printf("Clearing...");
   1.464 +  UniToEntity.Clear();
   1.465 +  printf("OK\n");
   1.466 +
   1.467 +  printf("Checking count...");
   1.468 +  count = UniToEntity.Enumerate(nsDEnum, nullptr);
   1.469 +  if (count) {
   1.470 +    printf("  Clear did not remove all entries.\n");
   1.471 +    exit (15);
   1.472 +  }
   1.473 +
   1.474 +  printf("OK\n");
   1.475 +
   1.476 +  //
   1.477 +  // now check a class-hashtable
   1.478 +  //
   1.479 +
   1.480 +  printf("Initializing nsClassHashtable...");
   1.481 +  nsClassHashtable<nsCStringHashKey,TestUniChar> EntToUniClass(ENTITY_COUNT);
   1.482 +  printf("OK\n");
   1.483 +
   1.484 +  printf("Filling hash with %u entries.\n", ENTITY_COUNT);
   1.485 +
   1.486 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.487 +    printf("  Putting entry %u...", gEntities[i].mUnicode);
   1.488 +    TestUniChar* temp = new TestUniChar(gEntities[i].mUnicode);
   1.489 +
   1.490 +    EntToUniClass.Put(nsDependentCString(gEntities[i].mStr), temp);
   1.491 +    printf("OK...\n");
   1.492 +  }
   1.493 +
   1.494 +  printf("Testing Get:\n");
   1.495 +  TestUniChar* myChar;
   1.496 +
   1.497 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.498 +    printf("  Getting entry %s...", gEntities[i].mStr);
   1.499 +    if (!EntToUniClass.Get(nsDependentCString(gEntities[i].mStr), &myChar)) {
   1.500 +      printf("FAILED\n");
   1.501 +      exit (18);
   1.502 +    }
   1.503 +
   1.504 +    printf("Found %c\n", myChar->GetChar());
   1.505 +  }
   1.506 +
   1.507 +  printf("Testing nonexistent entries...");
   1.508 +  if (EntToUniClass.Get(NS_LITERAL_CSTRING("xxxx"), &myChar)) {
   1.509 +    printf("FOUND! BAD!\n");
   1.510 +    exit (19);
   1.511 +  }
   1.512 +      
   1.513 +  printf("not found; good.\n");
   1.514 +      
   1.515 +  printf("Enumerating:\n");
   1.516 +  
   1.517 +  count = EntToUniClass.EnumerateRead(nsCEnumRead, nullptr);
   1.518 +  if (count != ENTITY_COUNT) {
   1.519 +    printf("  Bad count!\n");
   1.520 +    exit (20);
   1.521 +  }
   1.522 +  
   1.523 +  printf("Clearing...\n");
   1.524 +  EntToUniClass.Clear();
   1.525 +  printf("  Clearing OK\n");
   1.526 +
   1.527 +  printf("Checking count...");
   1.528 +  count = EntToUniClass.Enumerate(nsCEnum, nullptr);
   1.529 +  if (count) {
   1.530 +    printf("  Clear did not remove all entries.\n");
   1.531 +    exit (21);
   1.532 +  }
   1.533 +
   1.534 +  printf("OK\n");
   1.535 +
   1.536 +  //
   1.537 +  // now check a data-hashtable with an interface key
   1.538 +  //
   1.539 +
   1.540 +  printf("Initializing nsDataHashtable with interface key...");
   1.541 +  nsDataHashtable<nsISupportsHashKey,uint32_t> EntToUniClass2(ENTITY_COUNT);
   1.542 +  printf("OK\n");
   1.543 +
   1.544 +  printf("Filling hash with %u entries.\n", ENTITY_COUNT);
   1.545 +
   1.546 +  nsCOMArray<IFoo> fooArray;
   1.547 +
   1.548 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.549 +    printf("  Putting entry %u...", gEntities[i].mUnicode);
   1.550 +    nsCOMPtr<IFoo> foo;
   1.551 +    CreateIFoo(getter_AddRefs(foo));
   1.552 +    foo->SetString(nsDependentCString(gEntities[i].mStr));
   1.553 +    
   1.554 +    
   1.555 +    fooArray.InsertObjectAt(foo, i);
   1.556 +
   1.557 +    EntToUniClass2.Put(foo, gEntities[i].mUnicode);
   1.558 +    printf("OK...\n");
   1.559 +  }
   1.560 +
   1.561 +  printf("Testing Get:\n");
   1.562 +  uint32_t myChar2;
   1.563 +
   1.564 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.565 +    printf("  Getting entry %s...", gEntities[i].mStr);
   1.566 +    
   1.567 +    if (!EntToUniClass2.Get(fooArray[i], &myChar2)) {
   1.568 +      printf("FAILED\n");
   1.569 +      exit (24);
   1.570 +    }
   1.571 +
   1.572 +    printf("Found %c\n", myChar2);
   1.573 +  }
   1.574 +
   1.575 +  printf("Testing nonexistent entries...");
   1.576 +  if (EntToUniClass2.Get((nsISupports*) 0x55443316, &myChar2)) {
   1.577 +    printf("FOUND! BAD!\n");
   1.578 +    exit (25);
   1.579 +  }
   1.580 +      
   1.581 +  printf("not found; good.\n");
   1.582 +      
   1.583 +  printf("Enumerating:\n");
   1.584 +  
   1.585 +  count = EntToUniClass2.EnumerateRead(nsIEnum2Read, nullptr);
   1.586 +  if (count != ENTITY_COUNT) {
   1.587 +    printf("  Bad count!\n");
   1.588 +    exit (26);
   1.589 +  }
   1.590 +  
   1.591 +  printf("Clearing...\n");
   1.592 +  EntToUniClass2.Clear();
   1.593 +  printf("  Clearing OK\n");
   1.594 +
   1.595 +  printf("Checking count...");
   1.596 +  count = EntToUniClass2.Enumerate(nsIEnum2, nullptr);
   1.597 +  if (count) {
   1.598 +    printf("  Clear did not remove all entries.\n");
   1.599 +    exit (27);
   1.600 +  }
   1.601 +
   1.602 +  printf("OK\n");
   1.603 +
   1.604 +  //
   1.605 +  // now check an interface-hashtable with an uint32_t key
   1.606 +  //
   1.607 +
   1.608 +  printf("Initializing nsInterfaceHashtable...");
   1.609 +  nsInterfaceHashtable<nsUint32HashKey,IFoo> UniToEntClass2(ENTITY_COUNT);
   1.610 +  printf("OK\n");
   1.611 +
   1.612 +  printf("Filling hash with %u entries.\n", ENTITY_COUNT);
   1.613 +
   1.614 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.615 +    printf("  Putting entry %u...", gEntities[i].mUnicode);
   1.616 +    nsCOMPtr<IFoo> foo;
   1.617 +    CreateIFoo(getter_AddRefs(foo));
   1.618 +    foo->SetString(nsDependentCString(gEntities[i].mStr));
   1.619 +    
   1.620 +    UniToEntClass2.Put(gEntities[i].mUnicode, foo);
   1.621 +    printf("OK...\n");
   1.622 +  }
   1.623 +
   1.624 +  printf("Testing Get:\n");
   1.625 +
   1.626 +  for (i = 0; i < ENTITY_COUNT; ++i) {
   1.627 +    printf("  Getting entry %s...", gEntities[i].mStr);
   1.628 +    
   1.629 +    nsCOMPtr<IFoo> myEnt;
   1.630 +    if (!UniToEntClass2.Get(gEntities[i].mUnicode, getter_AddRefs(myEnt))) {
   1.631 +      printf("FAILED\n");
   1.632 +      exit (30);
   1.633 +    }
   1.634 +    
   1.635 +    nsAutoCString str;
   1.636 +    myEnt->GetString(str);
   1.637 +    printf("Found %s\n", str.get());
   1.638 +  }
   1.639 +
   1.640 +  printf("Testing nonexistent entries...");
   1.641 +  nsCOMPtr<IFoo> myEnt;
   1.642 +  if (UniToEntClass2.Get(9462, getter_AddRefs(myEnt))) {
   1.643 +    printf("FOUND! BAD!\n");
   1.644 +    exit (31);
   1.645 +  }
   1.646 +      
   1.647 +  printf("not found; good.\n");
   1.648 +      
   1.649 +  printf("Enumerating:\n");
   1.650 +  
   1.651 +  count = UniToEntClass2.EnumerateRead(nsIEnumRead, nullptr);
   1.652 +  if (count != ENTITY_COUNT) {
   1.653 +    printf("  Bad count!\n");
   1.654 +    exit (32);
   1.655 +  }
   1.656 +  
   1.657 +  printf("Clearing...\n");
   1.658 +  UniToEntClass2.Clear();
   1.659 +  printf("  Clearing OK\n");
   1.660 +
   1.661 +  printf("Checking count...");
   1.662 +  count = UniToEntClass2.Enumerate(nsIEnum, nullptr);
   1.663 +  if (count) {
   1.664 +    printf("  Clear did not remove all entries.\n");
   1.665 +    exit (33);
   1.666 +  }
   1.667 +
   1.668 +  printf("OK\n");
   1.669 +
   1.670 +  return 0;
   1.671 +}

mercurial