/thirdparty/breakpad/client/mac/tests/SimpleStringDictionaryTest.mm

http://github.com/tomahawk-player/tomahawk · Objective C++ · 243 lines · 115 code · 46 blank · 82 comment · 24 complexity · d63678579b41af30c904eb96d8099af1 MD5 · raw file

  1. // Copyright (c) 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #import "SimpleStringDictionaryTest.h"
  30. #import "SimpleStringDictionary.h"
  31. using google_breakpad::KeyValueEntry;
  32. using google_breakpad::SimpleStringDictionary;
  33. using google_breakpad::SimpleStringDictionaryIterator;
  34. @implementation SimpleStringDictionaryTest
  35. //==============================================================================
  36. - (void)testKeyValueEntry {
  37. KeyValueEntry entry;
  38. // Verify that initial state is correct
  39. STAssertFalse(entry.IsActive(), @"Initial key value entry is active!");
  40. STAssertEquals(strlen(entry.GetKey()), (size_t)0, @"Empty key value did not "
  41. @"have length 0");
  42. STAssertEquals(strlen(entry.GetValue()), (size_t)0, @"Empty key value did not "
  43. @"have length 0");
  44. // Try setting a key/value and then verify
  45. entry.SetKeyValue("key1", "value1");
  46. STAssertEqualCStrings(entry.GetKey(), "key1", @"key was not equal to key1");
  47. STAssertEqualCStrings(entry.GetValue(), "value1", @"value was not equal");
  48. // Try setting a new value
  49. entry.SetValue("value3");
  50. // Make sure the new value took
  51. STAssertEqualCStrings(entry.GetValue(), "value3", @"value was not equal");
  52. // Make sure the key didn't change
  53. STAssertEqualCStrings(entry.GetKey(), "key1", @"key changed after setting "
  54. @"value!");
  55. // Try setting a new key/value and then verify
  56. entry.SetKeyValue("key2", "value2");
  57. STAssertEqualCStrings(entry.GetKey(), "key2", @"New key was not equal to "
  58. @"key2");
  59. STAssertEqualCStrings(entry.GetValue(), "value2", @"New value was not equal "
  60. @"to value2");
  61. // Clear the entry and verify the key and value are empty strings
  62. entry.Clear();
  63. STAssertFalse(entry.IsActive(), @"Key value clear did not clear object");
  64. STAssertEquals(strlen(entry.GetKey()), (size_t)0, @"Length of cleared key "
  65. @"was not 0");
  66. STAssertEquals(strlen(entry.GetValue()), (size_t)0, @"Length of cleared "
  67. @"value was not 0!");
  68. }
  69. - (void)testEmptyKeyValueCombos {
  70. KeyValueEntry entry;
  71. entry.SetKeyValue(NULL, NULL);
  72. STAssertEqualCStrings(entry.GetKey(), "", @"Setting NULL key did not return "
  73. @"empty key!");
  74. STAssertEqualCStrings(entry.GetValue(), "", @"Setting NULL value did not "
  75. @"set empty string value!");
  76. }
  77. //==============================================================================
  78. - (void)testSimpleStringDictionary {
  79. // Make a new dictionary
  80. SimpleStringDictionary *dict = new SimpleStringDictionary();
  81. STAssertTrue(dict != NULL, nil);
  82. // try passing in NULL for key
  83. //dict->SetKeyValue(NULL, "bad"); // causes assert() to fire
  84. // Set three distinct values on three keys
  85. dict->SetKeyValue("key1", "value1");
  86. dict->SetKeyValue("key2", "value2");
  87. dict->SetKeyValue("key3", "value3");
  88. STAssertTrue(!strcmp(dict->GetValueForKey("key1"), "value1"), nil);
  89. STAssertTrue(!strcmp(dict->GetValueForKey("key2"), "value2"), nil);
  90. STAssertTrue(!strcmp(dict->GetValueForKey("key3"), "value3"), nil);
  91. STAssertEquals(dict->GetCount(), 3, @"GetCount did not return 3");
  92. // try an unknown key
  93. STAssertTrue(dict->GetValueForKey("key4") == NULL, nil);
  94. // try a NULL key
  95. //STAssertTrue(dict->GetValueForKey(NULL) == NULL, nil); // asserts
  96. // Remove a key
  97. dict->RemoveKey("key3");
  98. // Now make sure it's not there anymore
  99. STAssertTrue(dict->GetValueForKey("key3") == NULL, nil);
  100. // Remove a NULL key
  101. //dict->RemoveKey(NULL); // will cause assert() to fire
  102. // Remove by setting value to NULL
  103. dict->SetKeyValue("key2", NULL);
  104. // Now make sure it's not there anymore
  105. STAssertTrue(dict->GetValueForKey("key2") == NULL, nil);
  106. }
  107. //==============================================================================
  108. // The idea behind this test is to add a bunch of values to the dictionary,
  109. // remove some in the middle, then add a few more in. We then create a
  110. // SimpleStringDictionaryIterator and iterate through the dictionary, taking
  111. // note of the key/value pairs we see. We then verify that it iterates
  112. // through exactly the number of key/value pairs we expect, and that they
  113. // match one-for-one with what we would expect. In all cases we're setting
  114. // key value pairs of the form:
  115. //
  116. // key<n>/value<n> (like key0/value0, key17,value17, etc.)
  117. //
  118. - (void)testSimpleStringDictionaryIterator {
  119. SimpleStringDictionary *dict = new SimpleStringDictionary();
  120. STAssertTrue(dict != NULL, nil);
  121. char key[KeyValueEntry::MAX_STRING_STORAGE_SIZE];
  122. char value[KeyValueEntry::MAX_STRING_STORAGE_SIZE];
  123. const int kDictionaryCapacity = SimpleStringDictionary::MAX_NUM_ENTRIES;
  124. const int kPartitionIndex = kDictionaryCapacity - 5;
  125. // We assume at least this size in the tests below
  126. STAssertTrue(kDictionaryCapacity >= 64, nil);
  127. // We'll keep track of the number of key/value pairs we think should
  128. // be in the dictionary
  129. int expectedDictionarySize = 0;
  130. // Set a bunch of key/value pairs like key0/value0, key1/value1, ...
  131. for (int i = 0; i < kPartitionIndex; ++i) {
  132. sprintf(key, "key%d", i);
  133. sprintf(value, "value%d", i);
  134. dict->SetKeyValue(key, value);
  135. }
  136. expectedDictionarySize = kPartitionIndex;
  137. // set a couple of the keys twice (with the same value) - should be nop
  138. dict->SetKeyValue("key2", "value2");
  139. dict->SetKeyValue("key4", "value4");
  140. dict->SetKeyValue("key15", "value15");
  141. // Remove some random elements in the middle
  142. dict->RemoveKey("key7");
  143. dict->RemoveKey("key18");
  144. dict->RemoveKey("key23");
  145. dict->RemoveKey("key31");
  146. expectedDictionarySize -= 4; // we just removed four key/value pairs
  147. // Set some more key/value pairs like key59/value59, key60/value60, ...
  148. for (int i = kPartitionIndex; i < kDictionaryCapacity; ++i) {
  149. sprintf(key, "key%d", i);
  150. sprintf(value, "value%d", i);
  151. dict->SetKeyValue(key, value);
  152. }
  153. expectedDictionarySize += kDictionaryCapacity - kPartitionIndex;
  154. // Now create an iterator on the dictionary
  155. SimpleStringDictionaryIterator iter(*dict);
  156. // We then verify that it iterates through exactly the number of
  157. // key/value pairs we expect, and that they match one-for-one with what we
  158. // would expect. The ordering of the iteration does not matter...
  159. // used to keep track of number of occurrences found for key/value pairs
  160. int count[kDictionaryCapacity];
  161. memset(count, 0, sizeof(count));
  162. int totalCount = 0;
  163. const KeyValueEntry *entry;
  164. while ((entry = iter.Next())) {
  165. totalCount++;
  166. // Extract keyNumber from a string of the form key<keyNumber>
  167. int keyNumber;
  168. sscanf(entry->GetKey(), "key%d", &keyNumber);
  169. // Extract valueNumber from a string of the form value<valueNumber>
  170. int valueNumber;
  171. sscanf(entry->GetValue(), "value%d", &valueNumber);
  172. // The value number should equal the key number since that's how we set them
  173. STAssertTrue(keyNumber == valueNumber, nil);
  174. // Key and value numbers should be in proper range:
  175. // 0 <= keyNumber < kDictionaryCapacity
  176. bool isKeyInGoodRange =
  177. (keyNumber >= 0 && keyNumber < kDictionaryCapacity);
  178. bool isValueInGoodRange =
  179. (valueNumber >= 0 && valueNumber < kDictionaryCapacity);
  180. STAssertTrue(isKeyInGoodRange, nil);
  181. STAssertTrue(isValueInGoodRange, nil);
  182. if (isKeyInGoodRange && isValueInGoodRange) {
  183. ++count[keyNumber];
  184. }
  185. }
  186. // Make sure each of the key/value pairs showed up exactly one time, except
  187. // for the ones which we removed.
  188. for (int i = 0; i < kDictionaryCapacity; ++i) {
  189. // Skip over key7, key18, key23, and key31, since we removed them
  190. if (!(i == 7 || i == 18 || i == 23 || i == 31)) {
  191. STAssertTrue(count[i] == 1, nil);
  192. }
  193. }
  194. // Make sure the number of iterations matches the expected dictionary size.
  195. STAssertTrue(totalCount == expectedDictionarySize, nil);
  196. }
  197. @end