mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-27 15:32:10 +08:00
Add SimpleStringDictionary and its test.
TEST=client_test SimpleStringDictionary*.* R=rsesek@chromium.org Review URL: https://codereview.chromium.org/489993002
This commit is contained in:
parent
9168ba47ac
commit
b980a5984b
@ -26,6 +26,8 @@
|
||||
'sources': [
|
||||
'capture_context_mac.h',
|
||||
'capture_context_mac.S',
|
||||
'simple_string_dictionary.cc',
|
||||
'simple_string_dictionary.h',
|
||||
],
|
||||
},
|
||||
{
|
||||
@ -42,6 +44,7 @@
|
||||
],
|
||||
'sources': [
|
||||
'capture_context_mac_test.cc',
|
||||
'simple_string_dictionary_test.cc',
|
||||
],
|
||||
},
|
||||
],
|
||||
|
46
client/simple_string_dictionary.cc
Normal file
46
client/simple_string_dictionary.cc
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "client/simple_string_dictionary.h"
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "util/stdlib/cxx.h"
|
||||
|
||||
#if CXX_LIBRARY_VERSION >= 2011
|
||||
#include <type_traits>
|
||||
#endif
|
||||
|
||||
namespace crashpad {
|
||||
namespace {
|
||||
|
||||
typedef TSimpleStringDictionary<1, 1, 1> SimpleStringDictionaryForAssertion;
|
||||
|
||||
#if CXX_LIBRARY_VERSION >= 2011
|
||||
// In C++11, check that TSimpleStringDictionary has standard layout, which is
|
||||
// what is actually important.
|
||||
COMPILE_ASSERT(
|
||||
std::is_standard_layout<SimpleStringDictionaryForAssertion>::value,
|
||||
SimpleStringDictionary_must_be_standard_layout);
|
||||
#else
|
||||
// In C++98 (ISO 14882), section 9.5.1 says that a union cannot have a member
|
||||
// with a non-trivial ctor, copy ctor, dtor, or assignment operator. Use this
|
||||
// property to ensure that Entry remains POD. This doesn’t work for C++11
|
||||
// because the requirements for unions have been relaxed.
|
||||
union Compile_Assert {
|
||||
SimpleStringDictionaryForAssertion::Entry Compile_Assert__entry_must_be_pod;
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
} // namespace crashpad
|
246
client/simple_string_dictionary.h
Normal file
246
client/simple_string_dictionary.h
Normal file
@ -0,0 +1,246 @@
|
||||
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|
||||
#define CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/logging.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
// Opaque type for the serialized representation of a TSimpleStringDictionary.
|
||||
// One is created in TSimpleStringDictionary::Serialize and can be deserialized
|
||||
// using one of the constructors.
|
||||
struct SerializedSimpleStringDictionary;
|
||||
|
||||
// TSimpleStringDictionary is an implementation of a map/dictionary collection
|
||||
// that uses a fixed amount of storage, so that it does not perform any dynamic
|
||||
// allocations for its operations.
|
||||
//
|
||||
// The actual map storage (the Entry) is guaranteed to be POD, so that it can be
|
||||
// transmitted over various IPC mechanisms.
|
||||
//
|
||||
// The template parameters control the amount of storage used for the key,
|
||||
// value, and map. The KeySize and ValueSize are measured in bytes, not glyphs,
|
||||
// and includes space for a \0 byte. This gives space for KeySize-1 and
|
||||
// ValueSize-1 characters in an entry. NumEntries is the total number of entries
|
||||
// that will fit in the map.
|
||||
template <size_t KeySize = 256, size_t ValueSize = 256, size_t NumEntries = 64>
|
||||
class TSimpleStringDictionary {
|
||||
public:
|
||||
// Constant and publicly accessible versions of the template parameters.
|
||||
static const size_t key_size = KeySize;
|
||||
static const size_t value_size = ValueSize;
|
||||
static const size_t num_entries = NumEntries;
|
||||
|
||||
// An Entry object is a single entry in the map. If the key is a 0-length
|
||||
// NUL-terminated string, the entry is empty.
|
||||
struct Entry {
|
||||
char key[KeySize];
|
||||
char value[ValueSize];
|
||||
|
||||
bool is_active() const {
|
||||
return key[0] != '\0';
|
||||
}
|
||||
};
|
||||
|
||||
// An Iterator can be used to iterate over all the active entries in a
|
||||
// TSimpleStringDictionary.
|
||||
class Iterator {
|
||||
public:
|
||||
explicit Iterator(const TSimpleStringDictionary& map)
|
||||
: map_(map),
|
||||
current_(0) {
|
||||
}
|
||||
|
||||
// Returns the next entry in the map, or NULL if at the end of the
|
||||
// collection.
|
||||
const Entry* Next() {
|
||||
while (current_ < map_.num_entries) {
|
||||
const Entry* entry = &map_.entries_[current_++];
|
||||
if (entry->is_active()) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
private:
|
||||
const TSimpleStringDictionary& map_;
|
||||
size_t current_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Iterator);
|
||||
};
|
||||
|
||||
TSimpleStringDictionary()
|
||||
: entries_() {
|
||||
}
|
||||
|
||||
TSimpleStringDictionary(const TSimpleStringDictionary& other) {
|
||||
*this = other;
|
||||
}
|
||||
|
||||
TSimpleStringDictionary& operator=(const TSimpleStringDictionary& other) {
|
||||
memcpy(entries_, other.entries_, sizeof(entries_));
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Constructs a map from its serialized form. |map| should be the out
|
||||
// parameter from Serialize() and |size| should be its return value.
|
||||
TSimpleStringDictionary(
|
||||
const SerializedSimpleStringDictionary* map, size_t size) {
|
||||
DCHECK_EQ(size, sizeof(entries_));
|
||||
if (size == sizeof(entries_)) {
|
||||
memcpy(entries_, map, size);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the number of active key/value pairs. The upper limit for this is
|
||||
// NumEntries.
|
||||
size_t GetCount() const {
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < num_entries; ++i) {
|
||||
if (entries_[i].is_active()) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Given |key|, returns its corresponding |value|. |key| must not be NULL. If
|
||||
// the key is not found, NULL is returned.
|
||||
const char* GetValueForKey(const char* key) const {
|
||||
DCHECK(key);
|
||||
if (!key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const Entry* entry = GetConstEntryForKey(key);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return entry->value;
|
||||
}
|
||||
|
||||
// Stores |value| into |key|, replacing the existing value if |key| is already
|
||||
// present. |key| must not be NULL. If |value| is NULL, the key is removed
|
||||
// from the map. If there is no more space in the map, then the operation
|
||||
// silently fails.
|
||||
void SetKeyValue(const char* key, const char* value) {
|
||||
if (!value) {
|
||||
RemoveKey(key);
|
||||
return;
|
||||
}
|
||||
|
||||
DCHECK(key);
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Key must not be an empty string.
|
||||
DCHECK_NE(key[0], '\0');
|
||||
if (key[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
|
||||
Entry* entry = GetEntryForKey(key);
|
||||
|
||||
// If it does not yet exist, attempt to insert it.
|
||||
if (!entry) {
|
||||
for (size_t i = 0; i < num_entries; ++i) {
|
||||
if (!entries_[i].is_active()) {
|
||||
entry = &entries_[i];
|
||||
|
||||
strncpy(entry->key, key, key_size);
|
||||
entry->key[key_size - 1] = '\0';
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the map is out of space, entry will be NULL.
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Sanity check that the key only appears once.
|
||||
int count = 0;
|
||||
for (size_t i = 0; i < num_entries; ++i) {
|
||||
if (strncmp(entries_[i].key, key, key_size) == 0) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
DCHECK_EQ(count, 1);
|
||||
#endif
|
||||
|
||||
strncpy(entry->value, value, value_size);
|
||||
entry->value[value_size - 1] = '\0';
|
||||
}
|
||||
|
||||
// Given |key|, removes any associated value. |key| must not be NULL. If the
|
||||
// key is not found, this is a noop.
|
||||
void RemoveKey(const char* key) {
|
||||
DCHECK(key);
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
|
||||
Entry* entry = GetEntryForKey(key);
|
||||
if (entry) {
|
||||
entry->key[0] = '\0';
|
||||
entry->value[0] = '\0';
|
||||
}
|
||||
|
||||
DCHECK_EQ(GetEntryForKey(key), static_cast<void*>(NULL));
|
||||
}
|
||||
|
||||
// Places a serialized version of the map into |map| and returns the size.
|
||||
// Both of these should be passed to the deserializing constructor. Note that
|
||||
// the serialized |map| is scoped to the lifetime of the non-serialized
|
||||
// instance of this class. The |map| can be copied across IPC boundaries.
|
||||
size_t Serialize(const SerializedSimpleStringDictionary** map) const {
|
||||
*map = reinterpret_cast<const SerializedSimpleStringDictionary*>(entries_);
|
||||
return sizeof(entries_);
|
||||
}
|
||||
|
||||
private:
|
||||
const Entry* GetConstEntryForKey(const char* key) const {
|
||||
for (size_t i = 0; i < num_entries; ++i) {
|
||||
if (strncmp(key, entries_[i].key, key_size) == 0) {
|
||||
return &entries_[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Entry* GetEntryForKey(const char* key) {
|
||||
return const_cast<Entry*>(GetConstEntryForKey(key));
|
||||
}
|
||||
|
||||
Entry entries_[NumEntries];
|
||||
};
|
||||
|
||||
// For historical reasons this specialized version is available with the same
|
||||
// size factors as a previous implementation.
|
||||
typedef TSimpleStringDictionary<256, 256, 64> SimpleStringDictionary;
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|
295
client/simple_string_dictionary_test.cc
Normal file
295
client/simple_string_dictionary_test.cc
Normal file
@ -0,0 +1,295 @@
|
||||
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "client/simple_string_dictionary.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace crashpad;
|
||||
|
||||
TEST(SimpleStringDictionary, Entry) {
|
||||
typedef TSimpleStringDictionary<5, 9, 15> TestMap;
|
||||
TestMap map;
|
||||
|
||||
const TestMap::Entry* entry = TestMap::Iterator(map).Next();
|
||||
EXPECT_FALSE(entry);
|
||||
|
||||
// Try setting a key/value and then verify.
|
||||
map.SetKeyValue("key1", "value1");
|
||||
entry = TestMap::Iterator(map).Next();
|
||||
ASSERT_TRUE(entry);
|
||||
EXPECT_STREQ(entry->key, "key1");
|
||||
EXPECT_STREQ(entry->value, "value1");
|
||||
|
||||
// Try setting a new value.
|
||||
map.SetKeyValue("key1", "value3");
|
||||
EXPECT_STREQ(entry->value, "value3");
|
||||
|
||||
// Make sure the key didn't change.
|
||||
EXPECT_STREQ(entry->key, "key1");
|
||||
|
||||
// Clear the entry and verify the key and value are empty strings.
|
||||
map.RemoveKey("key1");
|
||||
EXPECT_FALSE(entry->is_active());
|
||||
EXPECT_EQ(strlen(entry->key), 0u);
|
||||
EXPECT_EQ(strlen(entry->value), 0u);
|
||||
}
|
||||
|
||||
TEST(SimpleStringDictionary, SimpleStringDictionary) {
|
||||
// Make a new dictionary
|
||||
SimpleStringDictionary dict;
|
||||
|
||||
// Set three distinct values on three keys
|
||||
dict.SetKeyValue("key1", "value1");
|
||||
dict.SetKeyValue("key2", "value2");
|
||||
dict.SetKeyValue("key3", "value3");
|
||||
|
||||
EXPECT_NE(dict.GetValueForKey("key1"), "value1");
|
||||
EXPECT_NE(dict.GetValueForKey("key2"), "value2");
|
||||
EXPECT_NE(dict.GetValueForKey("key3"), "value3");
|
||||
EXPECT_EQ(dict.GetCount(), 3u);
|
||||
// try an unknown key
|
||||
EXPECT_FALSE(dict.GetValueForKey("key4"));
|
||||
|
||||
// Remove a key
|
||||
dict.RemoveKey("key3");
|
||||
|
||||
// Now make sure it's not there anymore
|
||||
EXPECT_FALSE(dict.GetValueForKey("key3"));
|
||||
|
||||
// Remove by setting value to NULL
|
||||
dict.SetKeyValue("key2", NULL);
|
||||
|
||||
// Now make sure it's not there anymore
|
||||
EXPECT_FALSE(dict.GetValueForKey("key2"));
|
||||
}
|
||||
|
||||
TEST(SimpleStringDictionary, CopyAndAssign) {
|
||||
TSimpleStringDictionary<10, 10, 10> map;
|
||||
map.SetKeyValue("one", "a");
|
||||
map.SetKeyValue("two", "b");
|
||||
map.SetKeyValue("three", "c");
|
||||
map.RemoveKey("two");
|
||||
EXPECT_EQ(2u, map.GetCount());
|
||||
|
||||
// Test copy.
|
||||
TSimpleStringDictionary<10, 10, 10> map_copy(map);
|
||||
EXPECT_EQ(2u, map_copy.GetCount());
|
||||
EXPECT_STREQ("a", map_copy.GetValueForKey("one"));
|
||||
EXPECT_STREQ("c", map_copy.GetValueForKey("three"));
|
||||
map_copy.SetKeyValue("four", "d");
|
||||
EXPECT_STREQ("d", map_copy.GetValueForKey("four"));
|
||||
EXPECT_FALSE(map.GetValueForKey("four"));
|
||||
|
||||
// Test assign.
|
||||
TSimpleStringDictionary<10, 10, 10> map_assign;
|
||||
map_assign = map;
|
||||
EXPECT_EQ(2u, map_assign.GetCount());
|
||||
EXPECT_STREQ("a", map_assign.GetValueForKey("one"));
|
||||
EXPECT_STREQ("c", map_assign.GetValueForKey("three"));
|
||||
map_assign.SetKeyValue("four", "d");
|
||||
EXPECT_STREQ("d", map_assign.GetValueForKey("four"));
|
||||
EXPECT_FALSE(map.GetValueForKey("four"));
|
||||
|
||||
map.RemoveKey("one");
|
||||
EXPECT_FALSE(map.GetValueForKey("one"));
|
||||
EXPECT_STREQ("a", map_copy.GetValueForKey("one"));
|
||||
EXPECT_STREQ("a", map_assign.GetValueForKey("one"));
|
||||
}
|
||||
|
||||
// Add a bunch of values to the dictionary, remove some entries in the middle,
|
||||
// and then add more.
|
||||
TEST(SimpleStringDictionary, Iterator) {
|
||||
SimpleStringDictionary* dict = new SimpleStringDictionary;
|
||||
ASSERT_TRUE(dict);
|
||||
|
||||
char key[SimpleStringDictionary::key_size];
|
||||
char value[SimpleStringDictionary::value_size];
|
||||
|
||||
const int kDictionaryCapacity = SimpleStringDictionary::num_entries;
|
||||
const int kPartitionIndex = kDictionaryCapacity - 5;
|
||||
|
||||
// We assume at least this size in the tests below
|
||||
ASSERT_GE(kDictionaryCapacity, 64);
|
||||
|
||||
// We'll keep track of the number of key/value pairs we think should be in the
|
||||
// dictionary
|
||||
int expectedDictionarySize = 0;
|
||||
|
||||
// Set a bunch of key/value pairs like key0/value0, key1/value1, ...
|
||||
for (int i = 0; i < kPartitionIndex; ++i) {
|
||||
sprintf(key, "key%d", i);
|
||||
sprintf(value, "value%d", i);
|
||||
dict->SetKeyValue(key, value);
|
||||
}
|
||||
expectedDictionarySize = kPartitionIndex;
|
||||
|
||||
// set a couple of the keys twice (with the same value) - should be nop
|
||||
dict->SetKeyValue("key2", "value2");
|
||||
dict->SetKeyValue("key4", "value4");
|
||||
dict->SetKeyValue("key15", "value15");
|
||||
|
||||
// Remove some random elements in the middle
|
||||
dict->RemoveKey("key7");
|
||||
dict->RemoveKey("key18");
|
||||
dict->RemoveKey("key23");
|
||||
dict->RemoveKey("key31");
|
||||
expectedDictionarySize -= 4; // we just removed four key/value pairs
|
||||
|
||||
// Set some more key/value pairs like key59/value59, key60/value60, ...
|
||||
for (int i = kPartitionIndex; i < kDictionaryCapacity; ++i) {
|
||||
sprintf(key, "key%d", i);
|
||||
sprintf(value, "value%d", i);
|
||||
dict->SetKeyValue(key, value);
|
||||
}
|
||||
expectedDictionarySize += kDictionaryCapacity - kPartitionIndex;
|
||||
|
||||
// Now create an iterator on the dictionary
|
||||
SimpleStringDictionary::Iterator iter(*dict);
|
||||
|
||||
// We then verify that it iterates through exactly the number of key/value
|
||||
// pairs we expect, and that they match one-for-one with what we would expect.
|
||||
// The ordering of the iteration does not matter...
|
||||
|
||||
// used to keep track of number of occurrences found for key/value pairs
|
||||
int count[kDictionaryCapacity];
|
||||
memset(count, 0, sizeof(count));
|
||||
|
||||
int totalCount = 0;
|
||||
|
||||
const SimpleStringDictionary::Entry* entry;
|
||||
while ((entry = iter.Next())) {
|
||||
totalCount++;
|
||||
|
||||
// Extract keyNumber from a string of the form key<keyNumber>
|
||||
int keyNumber;
|
||||
sscanf(entry->key, "key%d", &keyNumber);
|
||||
|
||||
// Extract valueNumber from a string of the form value<valueNumber>
|
||||
int valueNumber;
|
||||
sscanf(entry->value, "value%d", &valueNumber);
|
||||
|
||||
// The value number should equal the key number since that's how we set them
|
||||
EXPECT_EQ(keyNumber, valueNumber);
|
||||
|
||||
// Key and value numbers should be in proper range: 0 <= keyNumber <
|
||||
// kDictionaryCapacity
|
||||
bool isKeyInGoodRange = (keyNumber >= 0 && keyNumber < kDictionaryCapacity);
|
||||
bool isValueInGoodRange =
|
||||
(valueNumber >= 0 && valueNumber < kDictionaryCapacity);
|
||||
EXPECT_TRUE(isKeyInGoodRange);
|
||||
EXPECT_TRUE(isValueInGoodRange);
|
||||
|
||||
if (isKeyInGoodRange && isValueInGoodRange) {
|
||||
++count[keyNumber];
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure each of the key/value pairs showed up exactly one time, except
|
||||
// for the ones which we removed.
|
||||
for (size_t i = 0; i < kDictionaryCapacity; ++i) {
|
||||
// Skip over key7, key18, key23, and key31, since we removed them
|
||||
if (!(i == 7 || i == 18 || i == 23 || i == 31)) {
|
||||
EXPECT_EQ(count[i], 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the number of iterations matches the expected dictionary size.
|
||||
EXPECT_EQ(totalCount, expectedDictionarySize);
|
||||
}
|
||||
|
||||
TEST(SimpleStringDictionary, AddRemove) {
|
||||
TSimpleStringDictionary<5, 7, 6> map;
|
||||
map.SetKeyValue("rob", "ert");
|
||||
map.SetKeyValue("mike", "pink");
|
||||
map.SetKeyValue("mark", "allays");
|
||||
|
||||
EXPECT_EQ(3u, map.GetCount());
|
||||
EXPECT_STREQ("ert", map.GetValueForKey("rob"));
|
||||
EXPECT_STREQ("pink", map.GetValueForKey("mike"));
|
||||
EXPECT_STREQ("allays", map.GetValueForKey("mark"));
|
||||
|
||||
map.RemoveKey("mike");
|
||||
|
||||
EXPECT_EQ(2u, map.GetCount());
|
||||
EXPECT_FALSE(map.GetValueForKey("mike"));
|
||||
|
||||
map.SetKeyValue("mark", "mal");
|
||||
EXPECT_EQ(2u, map.GetCount());
|
||||
EXPECT_STREQ("mal", map.GetValueForKey("mark"));
|
||||
|
||||
map.RemoveKey("mark");
|
||||
EXPECT_EQ(1u, map.GetCount());
|
||||
EXPECT_FALSE(map.GetValueForKey("mark"));
|
||||
}
|
||||
|
||||
TEST(SimpleStringDictionary, Serialize) {
|
||||
typedef TSimpleStringDictionary<4, 5, 7> TestMap;
|
||||
TestMap map;
|
||||
map.SetKeyValue("one", "abc");
|
||||
map.SetKeyValue("two", "def");
|
||||
map.SetKeyValue("tre", "hig");
|
||||
|
||||
EXPECT_STREQ("abc", map.GetValueForKey("one"));
|
||||
EXPECT_STREQ("def", map.GetValueForKey("two"));
|
||||
EXPECT_STREQ("hig", map.GetValueForKey("tre"));
|
||||
|
||||
const SerializedSimpleStringDictionary* serialized;
|
||||
size_t size = map.Serialize(&serialized);
|
||||
|
||||
SerializedSimpleStringDictionary* serialized_copy =
|
||||
reinterpret_cast<SerializedSimpleStringDictionary*>(malloc(size));
|
||||
ASSERT_TRUE(serialized_copy);
|
||||
memcpy(serialized_copy, serialized, size);
|
||||
|
||||
TestMap deserialized(serialized_copy, size);
|
||||
free(serialized_copy);
|
||||
|
||||
EXPECT_EQ(3u, deserialized.GetCount());
|
||||
EXPECT_STREQ("abc", deserialized.GetValueForKey("one"));
|
||||
EXPECT_STREQ("def", deserialized.GetValueForKey("two"));
|
||||
EXPECT_STREQ("hig", deserialized.GetValueForKey("tre"));
|
||||
}
|
||||
|
||||
// Running out of space shouldn't crash.
|
||||
TEST(SimpleStringDictionary, OutOfSpace) {
|
||||
TSimpleStringDictionary<3, 2, 2> map;
|
||||
map.SetKeyValue("a", "1");
|
||||
map.SetKeyValue("b", "2");
|
||||
map.SetKeyValue("c", "3");
|
||||
EXPECT_EQ(2u, map.GetCount());
|
||||
EXPECT_FALSE(map.GetValueForKey("c"));
|
||||
}
|
||||
|
||||
#if DCHECK_IS_ON
|
||||
|
||||
TEST(SimpleStringDictionaryDeathTest, NullKey) {
|
||||
TSimpleStringDictionary<4, 6, 6> map;
|
||||
ASSERT_DEATH(map.SetKeyValue(NULL, "hello"), "key");
|
||||
|
||||
map.SetKeyValue("hi", "there");
|
||||
ASSERT_DEATH(map.GetValueForKey(NULL), "key");
|
||||
EXPECT_STREQ("there", map.GetValueForKey("hi"));
|
||||
|
||||
ASSERT_DEATH(map.GetValueForKey(NULL), "key");
|
||||
map.RemoveKey("hi");
|
||||
EXPECT_EQ(0u, map.GetCount());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace
|
Loading…
x
Reference in New Issue
Block a user