Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsTHashtable.h" |
michael@0 | 7 | #include "nsBaseHashtable.h" |
michael@0 | 8 | #include "nsDataHashtable.h" |
michael@0 | 9 | #include "nsInterfaceHashtable.h" |
michael@0 | 10 | #include "nsClassHashtable.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsISupports.h" |
michael@0 | 14 | #include "nsCOMArray.h" |
michael@0 | 15 | #include "mozilla/Attributes.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <stdio.h> |
michael@0 | 18 | |
michael@0 | 19 | namespace TestHashtables { |
michael@0 | 20 | |
michael@0 | 21 | class TestUniChar // for nsClassHashtable |
michael@0 | 22 | { |
michael@0 | 23 | public: |
michael@0 | 24 | TestUniChar(uint32_t aWord) |
michael@0 | 25 | { |
michael@0 | 26 | printf(" TestUniChar::TestUniChar() %u\n", aWord); |
michael@0 | 27 | mWord = aWord; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | ~TestUniChar() |
michael@0 | 31 | { |
michael@0 | 32 | printf(" TestUniChar::~TestUniChar() %u\n", mWord); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | uint32_t GetChar() const { return mWord; } |
michael@0 | 36 | |
michael@0 | 37 | private: |
michael@0 | 38 | uint32_t mWord; |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | struct EntityNode { |
michael@0 | 42 | const char* mStr; // never owns buffer |
michael@0 | 43 | uint32_t mUnicode; |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | EntityNode gEntities[] = { |
michael@0 | 47 | {"nbsp",160}, |
michael@0 | 48 | {"iexcl",161}, |
michael@0 | 49 | {"cent",162}, |
michael@0 | 50 | {"pound",163}, |
michael@0 | 51 | {"curren",164}, |
michael@0 | 52 | {"yen",165}, |
michael@0 | 53 | {"brvbar",166}, |
michael@0 | 54 | {"sect",167}, |
michael@0 | 55 | {"uml",168}, |
michael@0 | 56 | {"copy",169}, |
michael@0 | 57 | {"ordf",170}, |
michael@0 | 58 | {"laquo",171}, |
michael@0 | 59 | {"not",172}, |
michael@0 | 60 | {"shy",173}, |
michael@0 | 61 | {"reg",174}, |
michael@0 | 62 | {"macr",175} |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | #define ENTITY_COUNT (unsigned(sizeof(gEntities)/sizeof(EntityNode))) |
michael@0 | 66 | |
michael@0 | 67 | class EntityToUnicodeEntry : public PLDHashEntryHdr |
michael@0 | 68 | { |
michael@0 | 69 | public: |
michael@0 | 70 | typedef const char* KeyType; |
michael@0 | 71 | typedef const char* KeyTypePointer; |
michael@0 | 72 | |
michael@0 | 73 | EntityToUnicodeEntry(const char* aKey) { mNode = nullptr; } |
michael@0 | 74 | EntityToUnicodeEntry(const EntityToUnicodeEntry& aEntry) { mNode = aEntry.mNode; } |
michael@0 | 75 | ~EntityToUnicodeEntry() { } |
michael@0 | 76 | |
michael@0 | 77 | bool KeyEquals(const char* aEntity) const { return !strcmp(mNode->mStr, aEntity); } |
michael@0 | 78 | static const char* KeyToPointer(const char* aEntity) { return aEntity; } |
michael@0 | 79 | static PLDHashNumber HashKey(const char* aEntity) { return mozilla::HashString(aEntity); } |
michael@0 | 80 | enum { ALLOW_MEMMOVE = true }; |
michael@0 | 81 | |
michael@0 | 82 | const EntityNode* mNode; |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | PLDHashOperator |
michael@0 | 86 | nsTEnumGo(EntityToUnicodeEntry* aEntry, void* userArg) { |
michael@0 | 87 | printf(" enumerated \"%s\" = %u\n", |
michael@0 | 88 | aEntry->mNode->mStr, aEntry->mNode->mUnicode); |
michael@0 | 89 | |
michael@0 | 90 | return PL_DHASH_NEXT; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | PLDHashOperator |
michael@0 | 94 | nsTEnumStop(EntityToUnicodeEntry* aEntry, void* userArg) { |
michael@0 | 95 | printf(" enumerated \"%s\" = %u\n", |
michael@0 | 96 | aEntry->mNode->mStr, aEntry->mNode->mUnicode); |
michael@0 | 97 | |
michael@0 | 98 | return PL_DHASH_REMOVE; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | void |
michael@0 | 102 | testTHashtable(nsTHashtable<EntityToUnicodeEntry>& hash, uint32_t numEntries) { |
michael@0 | 103 | printf("Filling hash with %d entries.\n", numEntries); |
michael@0 | 104 | |
michael@0 | 105 | uint32_t i; |
michael@0 | 106 | for (i = 0; i < numEntries; ++i) { |
michael@0 | 107 | printf(" Putting entry \"%s\"...", gEntities[i].mStr); |
michael@0 | 108 | EntityToUnicodeEntry* entry = |
michael@0 | 109 | hash.PutEntry(gEntities[i].mStr); |
michael@0 | 110 | |
michael@0 | 111 | if (!entry) { |
michael@0 | 112 | printf("FAILED\n"); |
michael@0 | 113 | exit (2); |
michael@0 | 114 | } |
michael@0 | 115 | printf("OK..."); |
michael@0 | 116 | |
michael@0 | 117 | if (entry->mNode) { |
michael@0 | 118 | printf("entry already exists!\n"); |
michael@0 | 119 | exit (3); |
michael@0 | 120 | } |
michael@0 | 121 | printf("\n"); |
michael@0 | 122 | |
michael@0 | 123 | entry->mNode = &gEntities[i]; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | printf("Testing Get:\n"); |
michael@0 | 127 | |
michael@0 | 128 | for (i = 0; i < numEntries; ++i) { |
michael@0 | 129 | printf(" Getting entry \"%s\"...", gEntities[i].mStr); |
michael@0 | 130 | EntityToUnicodeEntry* entry = |
michael@0 | 131 | hash.GetEntry(gEntities[i].mStr); |
michael@0 | 132 | |
michael@0 | 133 | if (!entry) { |
michael@0 | 134 | printf("FAILED\n"); |
michael@0 | 135 | exit (4); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | printf("Found %u\n", entry->mNode->mUnicode); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | printf("Testing nonexistent entries..."); |
michael@0 | 142 | |
michael@0 | 143 | EntityToUnicodeEntry* entry = |
michael@0 | 144 | hash.GetEntry("xxxy"); |
michael@0 | 145 | |
michael@0 | 146 | if (entry) { |
michael@0 | 147 | printf("FOUND! BAD!\n"); |
michael@0 | 148 | exit (5); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | printf("not found; good.\n"); |
michael@0 | 152 | |
michael@0 | 153 | printf("Enumerating:\n"); |
michael@0 | 154 | uint32_t count = hash.EnumerateEntries(nsTEnumGo, nullptr); |
michael@0 | 155 | if (count != numEntries) { |
michael@0 | 156 | printf(" Bad count!\n"); |
michael@0 | 157 | exit (6); |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | PLDHashOperator |
michael@0 | 162 | nsDEnumRead(const uint32_t& aKey, const char* aData, void* userArg) { |
michael@0 | 163 | printf(" enumerated %u = \"%s\"\n", aKey, aData); |
michael@0 | 164 | return PL_DHASH_NEXT; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | PLDHashOperator |
michael@0 | 168 | nsDEnum(const uint32_t& aKey, const char*& aData, void* userArg) { |
michael@0 | 169 | printf(" enumerated %u = \"%s\"\n", aKey, aData); |
michael@0 | 170 | return PL_DHASH_NEXT; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | PLDHashOperator |
michael@0 | 174 | nsCEnumRead(const nsACString& aKey, TestUniChar* aData, void* userArg) { |
michael@0 | 175 | printf(" enumerated \"%s\" = %c\n", |
michael@0 | 176 | PromiseFlatCString(aKey).get(), aData->GetChar()); |
michael@0 | 177 | return PL_DHASH_NEXT; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | PLDHashOperator |
michael@0 | 181 | nsCEnum(const nsACString& aKey, nsAutoPtr<TestUniChar>& aData, void* userArg) { |
michael@0 | 182 | printf(" enumerated \"%s\" = %c\n", |
michael@0 | 183 | PromiseFlatCString(aKey).get(), aData->GetChar()); |
michael@0 | 184 | return PL_DHASH_NEXT; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | // |
michael@0 | 188 | // all this nsIFoo stuff was copied wholesale from TestCOMPtr.cpp |
michael@0 | 189 | // |
michael@0 | 190 | |
michael@0 | 191 | #define NS_IFOO_IID \ |
michael@0 | 192 | { 0x6f7652e0, 0xee43, 0x11d1, \ |
michael@0 | 193 | { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } } |
michael@0 | 194 | |
michael@0 | 195 | class IFoo MOZ_FINAL : public nsISupports |
michael@0 | 196 | { |
michael@0 | 197 | public: |
michael@0 | 198 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID) |
michael@0 | 199 | |
michael@0 | 200 | IFoo(); |
michael@0 | 201 | |
michael@0 | 202 | NS_IMETHOD_(MozExternalRefCountType) AddRef(); |
michael@0 | 203 | NS_IMETHOD_(MozExternalRefCountType) Release(); |
michael@0 | 204 | NS_IMETHOD QueryInterface( const nsIID&, void** ); |
michael@0 | 205 | |
michael@0 | 206 | NS_IMETHOD SetString(const nsACString& /*in*/ aString); |
michael@0 | 207 | NS_IMETHOD GetString(nsACString& /*out*/ aString); |
michael@0 | 208 | |
michael@0 | 209 | static void print_totals(); |
michael@0 | 210 | |
michael@0 | 211 | private: |
michael@0 | 212 | ~IFoo(); |
michael@0 | 213 | |
michael@0 | 214 | unsigned int refcount_; |
michael@0 | 215 | |
michael@0 | 216 | static unsigned int total_constructions_; |
michael@0 | 217 | static unsigned int total_destructions_; |
michael@0 | 218 | nsCString mString; |
michael@0 | 219 | }; |
michael@0 | 220 | |
michael@0 | 221 | NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID) |
michael@0 | 222 | |
michael@0 | 223 | unsigned int IFoo::total_constructions_; |
michael@0 | 224 | unsigned int IFoo::total_destructions_; |
michael@0 | 225 | |
michael@0 | 226 | void |
michael@0 | 227 | IFoo::print_totals() |
michael@0 | 228 | { |
michael@0 | 229 | printf("total constructions/destructions --> %d/%d\n", |
michael@0 | 230 | total_constructions_, total_destructions_); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | IFoo::IFoo() |
michael@0 | 234 | : refcount_(0) |
michael@0 | 235 | { |
michael@0 | 236 | ++total_constructions_; |
michael@0 | 237 | printf(" new IFoo@%p [#%d]\n", |
michael@0 | 238 | static_cast<void*>(this), total_constructions_); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | IFoo::~IFoo() |
michael@0 | 242 | { |
michael@0 | 243 | ++total_destructions_; |
michael@0 | 244 | printf("IFoo@%p::~IFoo() [#%d]\n", |
michael@0 | 245 | static_cast<void*>(this), total_destructions_); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | MozExternalRefCountType |
michael@0 | 249 | IFoo::AddRef() |
michael@0 | 250 | { |
michael@0 | 251 | ++refcount_; |
michael@0 | 252 | printf("IFoo@%p::AddRef(), refcount --> %d\n", |
michael@0 | 253 | static_cast<void*>(this), refcount_); |
michael@0 | 254 | return refcount_; |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | MozExternalRefCountType |
michael@0 | 258 | IFoo::Release() |
michael@0 | 259 | { |
michael@0 | 260 | int newcount = --refcount_; |
michael@0 | 261 | if ( newcount == 0 ) |
michael@0 | 262 | printf(">>"); |
michael@0 | 263 | |
michael@0 | 264 | printf("IFoo@%p::Release(), refcount --> %d\n", |
michael@0 | 265 | static_cast<void*>(this), refcount_); |
michael@0 | 266 | |
michael@0 | 267 | if ( newcount == 0 ) |
michael@0 | 268 | { |
michael@0 | 269 | printf(" delete IFoo@%p\n", static_cast<void*>(this)); |
michael@0 | 270 | printf("<<IFoo@%p::Release()\n", static_cast<void*>(this)); |
michael@0 | 271 | delete this; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | return newcount; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | nsresult |
michael@0 | 278 | IFoo::QueryInterface( const nsIID& aIID, void** aResult ) |
michael@0 | 279 | { |
michael@0 | 280 | printf("IFoo@%p::QueryInterface()\n", static_cast<void*>(this)); |
michael@0 | 281 | nsISupports* rawPtr = 0; |
michael@0 | 282 | nsresult status = NS_OK; |
michael@0 | 283 | |
michael@0 | 284 | if ( aIID.Equals(NS_GET_IID(IFoo)) ) |
michael@0 | 285 | rawPtr = this; |
michael@0 | 286 | else |
michael@0 | 287 | { |
michael@0 | 288 | nsID iid_of_ISupports = NS_ISUPPORTS_IID; |
michael@0 | 289 | if ( aIID.Equals(iid_of_ISupports) ) |
michael@0 | 290 | rawPtr = static_cast<nsISupports*>(this); |
michael@0 | 291 | else |
michael@0 | 292 | status = NS_ERROR_NO_INTERFACE; |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | NS_IF_ADDREF(rawPtr); |
michael@0 | 296 | *aResult = rawPtr; |
michael@0 | 297 | |
michael@0 | 298 | return status; |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | nsresult |
michael@0 | 302 | IFoo::SetString(const nsACString& aString) |
michael@0 | 303 | { |
michael@0 | 304 | mString = aString; |
michael@0 | 305 | return NS_OK; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | nsresult |
michael@0 | 309 | IFoo::GetString(nsACString& aString) |
michael@0 | 310 | { |
michael@0 | 311 | aString = mString; |
michael@0 | 312 | return NS_OK; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | nsresult |
michael@0 | 316 | CreateIFoo( IFoo** result ) |
michael@0 | 317 | // a typical factory function (that calls AddRef) |
michael@0 | 318 | { |
michael@0 | 319 | printf(" >>CreateIFoo() --> "); |
michael@0 | 320 | IFoo* foop = new IFoo(); |
michael@0 | 321 | printf("IFoo@%p\n", static_cast<void*>(foop)); |
michael@0 | 322 | |
michael@0 | 323 | foop->AddRef(); |
michael@0 | 324 | *result = foop; |
michael@0 | 325 | |
michael@0 | 326 | printf("<<CreateIFoo()\n"); |
michael@0 | 327 | return NS_OK; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | PLDHashOperator |
michael@0 | 331 | nsIEnumRead(const uint32_t& aKey, IFoo* aFoo, void* userArg) { |
michael@0 | 332 | nsAutoCString str; |
michael@0 | 333 | aFoo->GetString(str); |
michael@0 | 334 | |
michael@0 | 335 | printf(" enumerated %u = \"%s\"\n", aKey, str.get()); |
michael@0 | 336 | return PL_DHASH_NEXT; |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | PLDHashOperator |
michael@0 | 340 | nsIEnum(const uint32_t& aKey, nsCOMPtr<IFoo>& aData, void* userArg) { |
michael@0 | 341 | nsAutoCString str; |
michael@0 | 342 | aData->GetString(str); |
michael@0 | 343 | |
michael@0 | 344 | printf(" enumerated %u = \"%s\"\n", aKey, str.get()); |
michael@0 | 345 | return PL_DHASH_NEXT; |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | PLDHashOperator |
michael@0 | 349 | nsIEnum2Read(nsISupports* aKey, uint32_t aData, void* userArg) { |
michael@0 | 350 | nsAutoCString str; |
michael@0 | 351 | nsCOMPtr<IFoo> foo = do_QueryInterface(aKey); |
michael@0 | 352 | foo->GetString(str); |
michael@0 | 353 | |
michael@0 | 354 | |
michael@0 | 355 | printf(" enumerated \"%s\" = %u\n", str.get(), aData); |
michael@0 | 356 | return PL_DHASH_NEXT; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | PLDHashOperator |
michael@0 | 360 | nsIEnum2(nsISupports* aKey, uint32_t& aData, void* userArg) { |
michael@0 | 361 | nsAutoCString str; |
michael@0 | 362 | nsCOMPtr<IFoo> foo = do_QueryInterface(aKey); |
michael@0 | 363 | foo->GetString(str); |
michael@0 | 364 | |
michael@0 | 365 | printf(" enumerated \"%s\" = %u\n", str.get(), aData); |
michael@0 | 366 | return PL_DHASH_NEXT; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | using namespace TestHashtables; |
michael@0 | 372 | |
michael@0 | 373 | int |
michael@0 | 374 | main(void) { |
michael@0 | 375 | // check an nsTHashtable |
michael@0 | 376 | printf("Initializing nsTHashtable..."); |
michael@0 | 377 | nsTHashtable<EntityToUnicodeEntry> EntityToUnicode(ENTITY_COUNT); |
michael@0 | 378 | printf("OK\n"); |
michael@0 | 379 | |
michael@0 | 380 | printf("Partially filling nsTHashtable:\n"); |
michael@0 | 381 | testTHashtable(EntityToUnicode, 5); |
michael@0 | 382 | |
michael@0 | 383 | printf("Enumerate-removing...\n"); |
michael@0 | 384 | uint32_t count = EntityToUnicode.EnumerateEntries(nsTEnumStop, nullptr); |
michael@0 | 385 | if (count != 5) { |
michael@0 | 386 | printf("wrong count\n"); |
michael@0 | 387 | exit (7); |
michael@0 | 388 | } |
michael@0 | 389 | printf("OK\n"); |
michael@0 | 390 | |
michael@0 | 391 | printf("Check enumeration..."); |
michael@0 | 392 | count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nullptr); |
michael@0 | 393 | if (count) { |
michael@0 | 394 | printf("entries remain in table!\n"); |
michael@0 | 395 | exit (8); |
michael@0 | 396 | } |
michael@0 | 397 | printf("OK\n"); |
michael@0 | 398 | |
michael@0 | 399 | printf("Filling nsTHashtable:\n"); |
michael@0 | 400 | testTHashtable(EntityToUnicode, ENTITY_COUNT); |
michael@0 | 401 | |
michael@0 | 402 | printf("Clearing..."); |
michael@0 | 403 | EntityToUnicode.Clear(); |
michael@0 | 404 | printf("OK\n"); |
michael@0 | 405 | |
michael@0 | 406 | printf("Check enumeration..."); |
michael@0 | 407 | count = EntityToUnicode.EnumerateEntries(nsTEnumGo, nullptr); |
michael@0 | 408 | if (count) { |
michael@0 | 409 | printf("entries remain in table!\n"); |
michael@0 | 410 | exit (9); |
michael@0 | 411 | } |
michael@0 | 412 | printf("OK\n"); |
michael@0 | 413 | |
michael@0 | 414 | // |
michael@0 | 415 | // now check a data-hashtable |
michael@0 | 416 | // |
michael@0 | 417 | |
michael@0 | 418 | printf("Initializing nsDataHashtable..."); |
michael@0 | 419 | nsDataHashtable<nsUint32HashKey,const char*> UniToEntity(ENTITY_COUNT); |
michael@0 | 420 | printf("OK\n"); |
michael@0 | 421 | |
michael@0 | 422 | printf("Filling hash with %u entries.\n", ENTITY_COUNT); |
michael@0 | 423 | |
michael@0 | 424 | uint32_t i; |
michael@0 | 425 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 426 | printf(" Putting entry %u...", gEntities[i].mUnicode); |
michael@0 | 427 | UniToEntity.Put(gEntities[i].mUnicode, gEntities[i].mStr); |
michael@0 | 428 | printf("OK...\n"); |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | printf("Testing Get:\n"); |
michael@0 | 432 | const char* str; |
michael@0 | 433 | |
michael@0 | 434 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 435 | printf(" Getting entry %u...", gEntities[i].mUnicode); |
michael@0 | 436 | if (!UniToEntity.Get(gEntities[i].mUnicode, &str)) { |
michael@0 | 437 | printf("FAILED\n"); |
michael@0 | 438 | exit (12); |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | printf("Found %s\n", str); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | printf("Testing nonexistent entries..."); |
michael@0 | 445 | if (UniToEntity.Get(99446, &str)) { |
michael@0 | 446 | printf("FOUND! BAD!\n"); |
michael@0 | 447 | exit (13); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | printf("not found; good.\n"); |
michael@0 | 451 | |
michael@0 | 452 | printf("Enumerating:\n"); |
michael@0 | 453 | |
michael@0 | 454 | count = UniToEntity.EnumerateRead(nsDEnumRead, nullptr); |
michael@0 | 455 | if (count != ENTITY_COUNT) { |
michael@0 | 456 | printf(" Bad count!\n"); |
michael@0 | 457 | exit (14); |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | printf("Clearing..."); |
michael@0 | 461 | UniToEntity.Clear(); |
michael@0 | 462 | printf("OK\n"); |
michael@0 | 463 | |
michael@0 | 464 | printf("Checking count..."); |
michael@0 | 465 | count = UniToEntity.Enumerate(nsDEnum, nullptr); |
michael@0 | 466 | if (count) { |
michael@0 | 467 | printf(" Clear did not remove all entries.\n"); |
michael@0 | 468 | exit (15); |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | printf("OK\n"); |
michael@0 | 472 | |
michael@0 | 473 | // |
michael@0 | 474 | // now check a class-hashtable |
michael@0 | 475 | // |
michael@0 | 476 | |
michael@0 | 477 | printf("Initializing nsClassHashtable..."); |
michael@0 | 478 | nsClassHashtable<nsCStringHashKey,TestUniChar> EntToUniClass(ENTITY_COUNT); |
michael@0 | 479 | printf("OK\n"); |
michael@0 | 480 | |
michael@0 | 481 | printf("Filling hash with %u entries.\n", ENTITY_COUNT); |
michael@0 | 482 | |
michael@0 | 483 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 484 | printf(" Putting entry %u...", gEntities[i].mUnicode); |
michael@0 | 485 | TestUniChar* temp = new TestUniChar(gEntities[i].mUnicode); |
michael@0 | 486 | |
michael@0 | 487 | EntToUniClass.Put(nsDependentCString(gEntities[i].mStr), temp); |
michael@0 | 488 | printf("OK...\n"); |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | printf("Testing Get:\n"); |
michael@0 | 492 | TestUniChar* myChar; |
michael@0 | 493 | |
michael@0 | 494 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 495 | printf(" Getting entry %s...", gEntities[i].mStr); |
michael@0 | 496 | if (!EntToUniClass.Get(nsDependentCString(gEntities[i].mStr), &myChar)) { |
michael@0 | 497 | printf("FAILED\n"); |
michael@0 | 498 | exit (18); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | printf("Found %c\n", myChar->GetChar()); |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | printf("Testing nonexistent entries..."); |
michael@0 | 505 | if (EntToUniClass.Get(NS_LITERAL_CSTRING("xxxx"), &myChar)) { |
michael@0 | 506 | printf("FOUND! BAD!\n"); |
michael@0 | 507 | exit (19); |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | printf("not found; good.\n"); |
michael@0 | 511 | |
michael@0 | 512 | printf("Enumerating:\n"); |
michael@0 | 513 | |
michael@0 | 514 | count = EntToUniClass.EnumerateRead(nsCEnumRead, nullptr); |
michael@0 | 515 | if (count != ENTITY_COUNT) { |
michael@0 | 516 | printf(" Bad count!\n"); |
michael@0 | 517 | exit (20); |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | printf("Clearing...\n"); |
michael@0 | 521 | EntToUniClass.Clear(); |
michael@0 | 522 | printf(" Clearing OK\n"); |
michael@0 | 523 | |
michael@0 | 524 | printf("Checking count..."); |
michael@0 | 525 | count = EntToUniClass.Enumerate(nsCEnum, nullptr); |
michael@0 | 526 | if (count) { |
michael@0 | 527 | printf(" Clear did not remove all entries.\n"); |
michael@0 | 528 | exit (21); |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | printf("OK\n"); |
michael@0 | 532 | |
michael@0 | 533 | // |
michael@0 | 534 | // now check a data-hashtable with an interface key |
michael@0 | 535 | // |
michael@0 | 536 | |
michael@0 | 537 | printf("Initializing nsDataHashtable with interface key..."); |
michael@0 | 538 | nsDataHashtable<nsISupportsHashKey,uint32_t> EntToUniClass2(ENTITY_COUNT); |
michael@0 | 539 | printf("OK\n"); |
michael@0 | 540 | |
michael@0 | 541 | printf("Filling hash with %u entries.\n", ENTITY_COUNT); |
michael@0 | 542 | |
michael@0 | 543 | nsCOMArray<IFoo> fooArray; |
michael@0 | 544 | |
michael@0 | 545 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 546 | printf(" Putting entry %u...", gEntities[i].mUnicode); |
michael@0 | 547 | nsCOMPtr<IFoo> foo; |
michael@0 | 548 | CreateIFoo(getter_AddRefs(foo)); |
michael@0 | 549 | foo->SetString(nsDependentCString(gEntities[i].mStr)); |
michael@0 | 550 | |
michael@0 | 551 | |
michael@0 | 552 | fooArray.InsertObjectAt(foo, i); |
michael@0 | 553 | |
michael@0 | 554 | EntToUniClass2.Put(foo, gEntities[i].mUnicode); |
michael@0 | 555 | printf("OK...\n"); |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | printf("Testing Get:\n"); |
michael@0 | 559 | uint32_t myChar2; |
michael@0 | 560 | |
michael@0 | 561 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 562 | printf(" Getting entry %s...", gEntities[i].mStr); |
michael@0 | 563 | |
michael@0 | 564 | if (!EntToUniClass2.Get(fooArray[i], &myChar2)) { |
michael@0 | 565 | printf("FAILED\n"); |
michael@0 | 566 | exit (24); |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | printf("Found %c\n", myChar2); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | printf("Testing nonexistent entries..."); |
michael@0 | 573 | if (EntToUniClass2.Get((nsISupports*) 0x55443316, &myChar2)) { |
michael@0 | 574 | printf("FOUND! BAD!\n"); |
michael@0 | 575 | exit (25); |
michael@0 | 576 | } |
michael@0 | 577 | |
michael@0 | 578 | printf("not found; good.\n"); |
michael@0 | 579 | |
michael@0 | 580 | printf("Enumerating:\n"); |
michael@0 | 581 | |
michael@0 | 582 | count = EntToUniClass2.EnumerateRead(nsIEnum2Read, nullptr); |
michael@0 | 583 | if (count != ENTITY_COUNT) { |
michael@0 | 584 | printf(" Bad count!\n"); |
michael@0 | 585 | exit (26); |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | printf("Clearing...\n"); |
michael@0 | 589 | EntToUniClass2.Clear(); |
michael@0 | 590 | printf(" Clearing OK\n"); |
michael@0 | 591 | |
michael@0 | 592 | printf("Checking count..."); |
michael@0 | 593 | count = EntToUniClass2.Enumerate(nsIEnum2, nullptr); |
michael@0 | 594 | if (count) { |
michael@0 | 595 | printf(" Clear did not remove all entries.\n"); |
michael@0 | 596 | exit (27); |
michael@0 | 597 | } |
michael@0 | 598 | |
michael@0 | 599 | printf("OK\n"); |
michael@0 | 600 | |
michael@0 | 601 | // |
michael@0 | 602 | // now check an interface-hashtable with an uint32_t key |
michael@0 | 603 | // |
michael@0 | 604 | |
michael@0 | 605 | printf("Initializing nsInterfaceHashtable..."); |
michael@0 | 606 | nsInterfaceHashtable<nsUint32HashKey,IFoo> UniToEntClass2(ENTITY_COUNT); |
michael@0 | 607 | printf("OK\n"); |
michael@0 | 608 | |
michael@0 | 609 | printf("Filling hash with %u entries.\n", ENTITY_COUNT); |
michael@0 | 610 | |
michael@0 | 611 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 612 | printf(" Putting entry %u...", gEntities[i].mUnicode); |
michael@0 | 613 | nsCOMPtr<IFoo> foo; |
michael@0 | 614 | CreateIFoo(getter_AddRefs(foo)); |
michael@0 | 615 | foo->SetString(nsDependentCString(gEntities[i].mStr)); |
michael@0 | 616 | |
michael@0 | 617 | UniToEntClass2.Put(gEntities[i].mUnicode, foo); |
michael@0 | 618 | printf("OK...\n"); |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | printf("Testing Get:\n"); |
michael@0 | 622 | |
michael@0 | 623 | for (i = 0; i < ENTITY_COUNT; ++i) { |
michael@0 | 624 | printf(" Getting entry %s...", gEntities[i].mStr); |
michael@0 | 625 | |
michael@0 | 626 | nsCOMPtr<IFoo> myEnt; |
michael@0 | 627 | if (!UniToEntClass2.Get(gEntities[i].mUnicode, getter_AddRefs(myEnt))) { |
michael@0 | 628 | printf("FAILED\n"); |
michael@0 | 629 | exit (30); |
michael@0 | 630 | } |
michael@0 | 631 | |
michael@0 | 632 | nsAutoCString str; |
michael@0 | 633 | myEnt->GetString(str); |
michael@0 | 634 | printf("Found %s\n", str.get()); |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | printf("Testing nonexistent entries..."); |
michael@0 | 638 | nsCOMPtr<IFoo> myEnt; |
michael@0 | 639 | if (UniToEntClass2.Get(9462, getter_AddRefs(myEnt))) { |
michael@0 | 640 | printf("FOUND! BAD!\n"); |
michael@0 | 641 | exit (31); |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | printf("not found; good.\n"); |
michael@0 | 645 | |
michael@0 | 646 | printf("Enumerating:\n"); |
michael@0 | 647 | |
michael@0 | 648 | count = UniToEntClass2.EnumerateRead(nsIEnumRead, nullptr); |
michael@0 | 649 | if (count != ENTITY_COUNT) { |
michael@0 | 650 | printf(" Bad count!\n"); |
michael@0 | 651 | exit (32); |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | printf("Clearing...\n"); |
michael@0 | 655 | UniToEntClass2.Clear(); |
michael@0 | 656 | printf(" Clearing OK\n"); |
michael@0 | 657 | |
michael@0 | 658 | printf("Checking count..."); |
michael@0 | 659 | count = UniToEntClass2.Enumerate(nsIEnum, nullptr); |
michael@0 | 660 | if (count) { |
michael@0 | 661 | printf(" Clear did not remove all entries.\n"); |
michael@0 | 662 | exit (33); |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | printf("OK\n"); |
michael@0 | 666 | |
michael@0 | 667 | return 0; |
michael@0 | 668 | } |