2015-07-23 12:36:13 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsInterfaceHashtable.h"
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsCOMArray.h"
|
2012-06-20 07:41:56 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
#include "gtest/gtest.h"
|
2005-11-08 21:17:49 +03:00
|
|
|
|
2008-08-18 20:45:38 +04:00
|
|
|
namespace TestHashtables {
|
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
class TestUniChar // for nsClassHashtable
|
|
|
|
{
|
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit TestUniChar(uint32_t aWord) { mWord = aWord; }
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-16 15:25:31 +03:00
|
|
|
~TestUniChar() = default;
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetChar() const { return mWord; }
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mWord;
|
2003-04-24 05:54:57 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EntityNode {
|
|
|
|
const char* mStr; // never owns buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mUnicode;
|
2003-04-24 05:54:57 +04:00
|
|
|
};
|
|
|
|
|
2019-02-25 04:35:59 +03:00
|
|
|
static const EntityNode gEntities[] = {
|
2003-04-24 05:54:57 +04:00
|
|
|
{"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}};
|
|
|
|
|
2010-05-18 03:36:31 +04:00
|
|
|
#define ENTITY_COUNT (unsigned(sizeof(gEntities) / sizeof(EntityNode)))
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
class EntityToUnicodeEntry : public PLDHashEntryHdr {
|
|
|
|
public:
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit EntityToUnicodeEntry(const char* aKey) { mNode = nullptr; }
|
2003-04-24 05:54:57 +04:00
|
|
|
EntityToUnicodeEntry(const EntityToUnicodeEntry& aEntry) {
|
|
|
|
mNode = aEntry.mNode;
|
|
|
|
}
|
2016-11-16 15:25:31 +03:00
|
|
|
~EntityToUnicodeEntry() = default;
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(const char* aEntity) const {
|
|
|
|
return !strcmp(mNode->mStr, aEntity);
|
|
|
|
}
|
2003-04-24 05:54:57 +04:00
|
|
|
static const char* KeyToPointer(const char* aEntity) { return aEntity; }
|
2012-03-13 02:53:18 +04:00
|
|
|
static PLDHashNumber HashKey(const char* aEntity) {
|
|
|
|
return mozilla::HashString(aEntity);
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
const EntityNode* mNode;
|
|
|
|
};
|
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
static uint32_t nsTIterPrint(nsTHashtable<EntityToUnicodeEntry>& hash) {
|
|
|
|
uint32_t n = 0;
|
|
|
|
for (auto iter = hash.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
static uint32_t nsTIterPrintRemove(nsTHashtable<EntityToUnicodeEntry>& hash) {
|
|
|
|
uint32_t n = 0;
|
|
|
|
for (auto iter = hash.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Remove();
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2019-02-25 04:35:59 +03:00
|
|
|
static void testTHashtable(nsTHashtable<EntityToUnicodeEntry>& hash,
|
|
|
|
uint32_t numEntries) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2003-04-24 05:54:57 +04:00
|
|
|
for (i = 0; i < numEntries; ++i) {
|
|
|
|
EntityToUnicodeEntry* entry = hash.PutEntry(gEntities[i].mStr);
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
EXPECT_TRUE(entry);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
EXPECT_FALSE(entry->mNode);
|
2003-04-24 05:54:57 +04:00
|
|
|
entry->mNode = &gEntities[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < numEntries; ++i) {
|
|
|
|
EntityToUnicodeEntry* entry = hash.GetEntry(gEntities[i].mStr);
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
EXPECT_TRUE(entry);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EntityToUnicodeEntry* entry = hash.GetEntry("xxxy");
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
EXPECT_FALSE(entry);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
uint32_t count = nsTIterPrint(hash);
|
2016-11-01 02:58:42 +03:00
|
|
|
EXPECT_EQ(count, numEntries);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2008-08-18 21:38:50 +04:00
|
|
|
// all this nsIFoo stuff was copied wholesale from TestCOMPtr.cpp
|
2003-04-24 05:54:57 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
#define NS_IFOO_IID \
|
2018-12-14 21:10:35 +03:00
|
|
|
{ \
|
2003-04-24 05:54:57 +04:00
|
|
|
0x6f7652e0, 0xee43, 0x11d1, { \
|
|
|
|
0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class IFoo final : public nsISupports {
|
2004-12-27 06:23:01 +03:00
|
|
|
public:
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
IFoo();
|
|
|
|
|
2016-11-16 15:27:07 +03:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef() override;
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release() override;
|
|
|
|
NS_IMETHOD QueryInterface(const nsIID&, void**) override;
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
NS_IMETHOD SetString(const nsACString& /*in*/ aString);
|
|
|
|
NS_IMETHOD GetString(nsACString& /*out*/ aString);
|
|
|
|
|
|
|
|
static void print_totals();
|
|
|
|
|
|
|
|
private:
|
2004-01-15 09:14:18 +03:00
|
|
|
~IFoo();
|
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
unsigned int refcount_;
|
|
|
|
|
|
|
|
static unsigned int total_constructions_;
|
|
|
|
static unsigned int total_destructions_;
|
|
|
|
nsCString mString;
|
|
|
|
};
|
|
|
|
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
|
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
unsigned int IFoo::total_constructions_;
|
|
|
|
unsigned int IFoo::total_destructions_;
|
|
|
|
|
|
|
|
void IFoo::print_totals() {}
|
|
|
|
|
|
|
|
IFoo::IFoo() : refcount_(0) { ++total_constructions_; }
|
|
|
|
|
|
|
|
IFoo::~IFoo() { ++total_destructions_; }
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
MozExternalRefCountType IFoo::AddRef() {
|
2003-04-24 05:54:57 +04:00
|
|
|
++refcount_;
|
|
|
|
return refcount_;
|
|
|
|
}
|
|
|
|
|
2007-01-26 05:50:18 +03:00
|
|
|
MozExternalRefCountType IFoo::Release() {
|
|
|
|
int newcount = --refcount_;
|
|
|
|
if (newcount == 0) {
|
|
|
|
delete this;
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return newcount;
|
2018-12-14 21:10:35 +03:00
|
|
|
}
|
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
nsresult IFoo::QueryInterface(const nsIID& aIID, void** aResult) {
|
2004-12-27 06:23:01 +03:00
|
|
|
nsISupports* rawPtr = 0;
|
|
|
|
nsresult status = NS_OK;
|
|
|
|
|
2014-03-18 04:23:11 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(IFoo)))
|
2004-12-27 06:23:01 +03:00
|
|
|
rawPtr = this;
|
2018-12-14 21:10:35 +03:00
|
|
|
else {
|
2004-12-27 06:23:01 +03:00
|
|
|
nsID iid_of_ISupports = NS_ISUPPORTS_IID;
|
|
|
|
if (aIID.Equals(iid_of_ISupports))
|
2007-07-08 11:08:04 +04:00
|
|
|
rawPtr = static_cast<nsISupports*>(this);
|
2004-12-27 06:23:01 +03:00
|
|
|
else
|
|
|
|
status = NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(rawPtr);
|
|
|
|
*aResult = rawPtr;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
nsresult IFoo::SetString(const nsACString& aString) {
|
|
|
|
mString = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult IFoo::GetString(nsACString& aString) {
|
|
|
|
aString = mString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-25 04:35:59 +03:00
|
|
|
static nsresult CreateIFoo(IFoo** result)
|
2003-04-24 05:54:57 +04:00
|
|
|
// a typical factory function (that calls AddRef)
|
|
|
|
{
|
2016-11-16 15:24:59 +03:00
|
|
|
auto* foop = new IFoo();
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
foop->AddRef();
|
|
|
|
*result = foop;
|
|
|
|
|
2012-07-27 17:59:29 +04:00
|
|
|
return NS_OK;
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace TestHashtables
|
2008-08-18 20:45:38 +04:00
|
|
|
|
|
|
|
using namespace TestHashtables;
|
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtable, THashtable)
|
|
|
|
{
|
2003-04-24 05:54:57 +04:00
|
|
|
// check an nsTHashtable
|
2013-09-02 12:41:57 +04:00
|
|
|
nsTHashtable<EntityToUnicodeEntry> EntityToUnicode(ENTITY_COUNT);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
testTHashtable(EntityToUnicode, 5);
|
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
uint32_t count = nsTIterPrintRemove(EntityToUnicode);
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(5));
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
count = nsTIterPrint(EntityToUnicode);
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
testTHashtable(EntityToUnicode, ENTITY_COUNT);
|
|
|
|
|
|
|
|
EntityToUnicode.Clear();
|
|
|
|
|
2015-07-23 12:36:13 +03:00
|
|
|
count = nsTIterPrint(EntityToUnicode);
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2016-11-01 02:34:24 +03:00
|
|
|
}
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtable, Move)
|
|
|
|
{
|
2018-07-16 17:03:30 +03:00
|
|
|
const void* kPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(0xbadc0de));
|
|
|
|
|
|
|
|
nsTHashtable<nsPtrHashKey<const void>> table;
|
|
|
|
table.PutEntry(kPtr);
|
|
|
|
|
|
|
|
nsTHashtable<nsPtrHashKey<const void>> moved = std::move(table);
|
|
|
|
ASSERT_EQ(table.Count(), 0u);
|
|
|
|
ASSERT_EQ(moved.Count(), 1u);
|
|
|
|
|
|
|
|
EXPECT_TRUE(moved.Contains(kPtr));
|
|
|
|
EXPECT_FALSE(table.Contains(kPtr));
|
|
|
|
}
|
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, DataHashtable)
|
|
|
|
{
|
2016-11-01 02:58:42 +03:00
|
|
|
// check a data-hashtable
|
2013-09-02 12:41:57 +04:00
|
|
|
nsDataHashtable<nsUint32HashKey, const char*> UniToEntity(ENTITY_COUNT);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
UniToEntity.Put(entity.mUnicode, entity.mStr);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* str;
|
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(UniToEntity.Get(entity.mUnicode, &str));
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_FALSE(UniToEntity.Get(99446, &str));
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
uint32_t count = 0;
|
2015-11-23 01:39:01 +03:00
|
|
|
for (auto iter = UniToEntity.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, ENTITY_COUNT);
|
2015-11-23 01:39:01 +03:00
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
UniToEntity.Clear();
|
|
|
|
|
2015-11-25 06:42:28 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto iter = UniToEntity.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
printf(" enumerated %u = \"%s\"\n", iter.Key(), iter.Data());
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2016-11-01 02:34:24 +03:00
|
|
|
}
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, ClassHashtable)
|
|
|
|
{
|
2016-11-01 02:58:42 +03:00
|
|
|
// check a class-hashtable
|
2013-09-02 12:41:57 +04:00
|
|
|
nsClassHashtable<nsCStringHashKey, TestUniChar> EntToUniClass(ENTITY_COUNT);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
2016-11-16 15:24:59 +03:00
|
|
|
auto* temp = new TestUniChar(entity.mUnicode);
|
2016-11-16 15:24:21 +03:00
|
|
|
EntToUniClass.Put(nsDependentCString(entity.mStr), temp);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TestUniChar* myChar;
|
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(EntToUniClass.Get(nsDependentCString(entity.mStr), &myChar));
|
2016-11-04 09:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_FALSE(EntToUniClass.Get(NS_LITERAL_CSTRING("xxxx"), &myChar));
|
2015-11-23 01:39:01 +03:00
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
uint32_t count = 0;
|
2015-11-23 01:39:01 +03:00
|
|
|
for (auto iter = EntToUniClass.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, ENTITY_COUNT);
|
2015-11-23 01:39:01 +03:00
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
EntToUniClass.Clear();
|
|
|
|
|
2015-11-25 06:42:28 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto iter = EntToUniClass.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2016-11-01 02:34:24 +03:00
|
|
|
}
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, DataHashtableWithInterfaceKey)
|
|
|
|
{
|
2016-11-01 02:58:42 +03:00
|
|
|
// check a data-hashtable with an interface key
|
2013-09-02 12:41:57 +04:00
|
|
|
nsDataHashtable<nsISupportsHashKey, uint32_t> EntToUniClass2(ENTITY_COUNT);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
|
|
|
nsCOMArray<IFoo> fooArray;
|
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
for (uint32_t i = 0; i < ENTITY_COUNT; ++i) {
|
2003-04-24 05:54:57 +04:00
|
|
|
nsCOMPtr<IFoo> foo;
|
|
|
|
CreateIFoo(getter_AddRefs(foo));
|
2003-06-18 15:26:27 +04:00
|
|
|
foo->SetString(nsDependentCString(gEntities[i].mStr));
|
2014-08-06 17:31:21 +04:00
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
fooArray.InsertObjectAt(foo, i);
|
|
|
|
|
2012-05-18 21:30:49 +04:00
|
|
|
EntToUniClass2.Put(foo, gEntities[i].mUnicode);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t myChar2;
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
for (uint32_t i = 0; i < ENTITY_COUNT; ++i) {
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_TRUE(EntToUniClass2.Get(fooArray[i], &myChar2));
|
2016-11-04 09:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_FALSE(EntToUniClass2.Get((nsISupports*)0x55443316, &myChar2));
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
uint32_t count = 0;
|
2015-11-23 01:39:01 +03:00
|
|
|
for (auto iter = EntToUniClass2.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoCString s;
|
|
|
|
nsCOMPtr<IFoo> foo = do_QueryInterface(iter.Key());
|
|
|
|
foo->GetString(s);
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, ENTITY_COUNT);
|
2015-11-23 01:39:01 +03:00
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
EntToUniClass2.Clear();
|
|
|
|
|
2015-11-25 06:42:28 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto iter = EntToUniClass2.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoCString s;
|
|
|
|
nsCOMPtr<IFoo> foo = do_QueryInterface(iter.Key());
|
|
|
|
foo->GetString(s);
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2016-11-01 02:34:24 +03:00
|
|
|
}
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, InterfaceHashtable)
|
|
|
|
{
|
2016-11-01 02:58:42 +03:00
|
|
|
// check an interface-hashtable with an uint32_t key
|
2013-09-02 12:41:57 +04:00
|
|
|
nsInterfaceHashtable<nsUint32HashKey, IFoo> UniToEntClass2(ENTITY_COUNT);
|
2003-04-24 05:54:57 +04:00
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
2003-04-24 05:54:57 +04:00
|
|
|
nsCOMPtr<IFoo> foo;
|
|
|
|
CreateIFoo(getter_AddRefs(foo));
|
2016-11-16 15:24:21 +03:00
|
|
|
foo->SetString(nsDependentCString(entity.mStr));
|
2014-08-06 17:31:21 +04:00
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
UniToEntClass2.Put(entity.mUnicode, foo);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:24:21 +03:00
|
|
|
for (auto& entity : gEntities) {
|
2003-04-24 05:54:57 +04:00
|
|
|
nsCOMPtr<IFoo> myEnt;
|
2016-11-16 15:24:21 +03:00
|
|
|
ASSERT_TRUE(UniToEntClass2.Get(entity.mUnicode, getter_AddRefs(myEnt)));
|
2015-09-08 09:56:16 +03:00
|
|
|
|
|
|
|
nsAutoCString myEntStr;
|
|
|
|
myEnt->GetString(myEntStr);
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<IFoo> myEnt;
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_FALSE(UniToEntClass2.Get(9462, getter_AddRefs(myEnt)));
|
2016-11-04 09:00:47 +03:00
|
|
|
|
2016-11-01 02:34:24 +03:00
|
|
|
uint32_t count = 0;
|
2015-11-23 01:39:01 +03:00
|
|
|
for (auto iter = UniToEntClass2.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoCString s;
|
|
|
|
iter.UserData()->GetString(s);
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, ENTITY_COUNT);
|
2015-11-23 01:39:01 +03:00
|
|
|
|
2003-04-24 05:54:57 +04:00
|
|
|
UniToEntClass2.Clear();
|
|
|
|
|
2015-11-25 06:42:28 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto iter = UniToEntClass2.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsAutoCString s;
|
|
|
|
iter.Data()->GetString(s);
|
|
|
|
count++;
|
|
|
|
}
|
2016-11-01 02:58:42 +03:00
|
|
|
ASSERT_EQ(count, uint32_t(0));
|
2003-04-24 05:54:57 +04:00
|
|
|
}
|
2017-06-14 02:03:38 +03:00
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, DataHashtable_LookupForAdd)
|
|
|
|
{
|
2017-06-14 02:03:38 +03:00
|
|
|
// check LookupForAdd/OrInsert
|
|
|
|
nsDataHashtable<nsUint32HashKey, const char*> UniToEntity(ENTITY_COUNT);
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = UniToEntity.LookupForAdd(entity.mUnicode);
|
|
|
|
const char* val = entry.OrInsert([&entity]() { return entity.mStr; });
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
ASSERT_TRUE(val == entity.mStr);
|
|
|
|
ASSERT_TRUE(entry.Data() == entity.mStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(UniToEntity.LookupForAdd(entity.mUnicode));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 should not be found
|
|
|
|
size_t count = UniToEntity.Count();
|
2017-06-18 18:07:54 +03:00
|
|
|
UniToEntity.Lookup(0U).Remove();
|
2017-06-14 02:03:38 +03:00
|
|
|
ASSERT_TRUE(count == UniToEntity.Count());
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
// Lookup should find all entries
|
2017-06-14 02:03:38 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto& entity : gEntities) {
|
2017-06-18 18:07:54 +03:00
|
|
|
if (UniToEntity.Lookup(entity.mUnicode)) {
|
|
|
|
count++;
|
|
|
|
}
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(count == UniToEntity.Count());
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(UniToEntity.LookupForAdd(entity.mUnicode));
|
|
|
|
}
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
// Lookup().Remove() should remove all entries.
|
2017-06-14 02:03:38 +03:00
|
|
|
for (auto& entity : gEntities) {
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = UniToEntity.Lookup(entity.mUnicode)) {
|
|
|
|
entry.Remove();
|
|
|
|
}
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == UniToEntity.Count());
|
2018-03-28 19:58:49 +03:00
|
|
|
|
|
|
|
// Remove newly added entries via OrRemove.
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = UniToEntity.LookupForAdd(entity.mUnicode);
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
entry.OrRemove();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == UniToEntity.Count());
|
|
|
|
|
|
|
|
// Remove existing entries via OrRemove.
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = UniToEntity.LookupForAdd(entity.mUnicode);
|
|
|
|
const char* val = entry.OrInsert([&entity]() { return entity.mStr; });
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
ASSERT_TRUE(val == entity.mStr);
|
|
|
|
ASSERT_TRUE(entry.Data() == entity.mStr);
|
|
|
|
|
|
|
|
auto entry2 = UniToEntity.LookupForAdd(entity.mUnicode);
|
|
|
|
ASSERT_TRUE(entry2);
|
|
|
|
entry2.OrRemove();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == UniToEntity.Count());
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|
|
|
|
|
2019-04-06 00:42:17 +03:00
|
|
|
TEST(Hashtables, ClassHashtable_LookupForAdd)
|
|
|
|
{
|
2017-06-14 02:03:38 +03:00
|
|
|
// check a class-hashtable LookupForAdd with null values
|
|
|
|
nsClassHashtable<nsCStringHashKey, TestUniChar> EntToUniClass(ENTITY_COUNT);
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr));
|
|
|
|
const TestUniChar* val = entry.OrInsert([]() { return nullptr; });
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
ASSERT_TRUE(val == nullptr);
|
|
|
|
ASSERT_TRUE(entry.Data() == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr)));
|
|
|
|
ASSERT_TRUE(
|
|
|
|
EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr)).Data() ==
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "" should not be found
|
|
|
|
size_t count = EntToUniClass.Count();
|
2017-06-18 18:07:54 +03:00
|
|
|
EntToUniClass.Lookup(nsDependentCString("")).Remove();
|
2017-06-14 02:03:38 +03:00
|
|
|
ASSERT_TRUE(count == EntToUniClass.Count());
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
// Lookup should find all entries.
|
2017-06-14 02:03:38 +03:00
|
|
|
count = 0;
|
|
|
|
for (auto& entity : gEntities) {
|
2017-06-18 18:07:54 +03:00
|
|
|
if (EntToUniClass.Lookup(nsDependentCString(entity.mStr))) {
|
|
|
|
count++;
|
|
|
|
}
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(count == EntToUniClass.Count());
|
|
|
|
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
ASSERT_TRUE(EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr)));
|
|
|
|
}
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
// Lookup().Remove() should remove all entries.
|
2017-06-14 02:03:38 +03:00
|
|
|
for (auto& entity : gEntities) {
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = EntToUniClass.Lookup(nsDependentCString(entity.mStr))) {
|
|
|
|
entry.Remove();
|
|
|
|
}
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == EntToUniClass.Count());
|
2018-03-28 19:58:49 +03:00
|
|
|
|
|
|
|
// Remove newly added entries via OrRemove.
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr));
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
entry.OrRemove();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == EntToUniClass.Count());
|
|
|
|
|
|
|
|
// Remove existing entries via OrRemove.
|
|
|
|
for (auto& entity : gEntities) {
|
|
|
|
auto entry = EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr));
|
|
|
|
const TestUniChar* val = entry.OrInsert([]() { return nullptr; });
|
|
|
|
ASSERT_FALSE(entry);
|
|
|
|
ASSERT_TRUE(val == nullptr);
|
|
|
|
ASSERT_TRUE(entry.Data() == nullptr);
|
|
|
|
|
|
|
|
auto entry2 = EntToUniClass.LookupForAdd(nsDependentCString(entity.mStr));
|
|
|
|
ASSERT_TRUE(entry2);
|
|
|
|
entry2.OrRemove();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(0 == EntToUniClass.Count());
|
2017-06-14 02:03:38 +03:00
|
|
|
}
|