RedBlackTree.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2011 Apple Inc. 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
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include <wtf/RedBlackTree.h>
  30. #include <wtf/Vector.h>
  31. using namespace WTF;
  32. namespace TestWebKitAPI {
  33. class TestNode : public RedBlackTree<TestNode, char>::Node {
  34. public:
  35. TestNode(char key, unsigned value)
  36. : m_key(key)
  37. , m_value(value)
  38. {
  39. }
  40. char key()
  41. {
  42. return m_key;
  43. }
  44. char m_key;
  45. unsigned m_value;
  46. };
  47. class RedBlackTreeTest : public testing::Test {
  48. public:
  49. unsigned m_counter;
  50. virtual void SetUp()
  51. {
  52. m_counter = 0;
  53. }
  54. virtual void TearDown()
  55. {
  56. }
  57. struct Pair {
  58. char key;
  59. unsigned value;
  60. Pair() { }
  61. Pair(char key, unsigned value)
  62. : key(key)
  63. , value(value)
  64. {
  65. }
  66. bool operator==(const Pair& other) const
  67. {
  68. return key == other.key;
  69. }
  70. bool operator<(const Pair& other) const
  71. {
  72. return key < other.key;
  73. }
  74. };
  75. typedef Vector<Pair, 16> PairVector;
  76. PairVector findExact(PairVector& asVector, char key)
  77. {
  78. PairVector result;
  79. for (size_t index = 0; index < asVector.size(); ++index) {
  80. if (asVector.at(index).key == key)
  81. result.append(asVector.at(index));
  82. }
  83. std::sort(result.begin(), result.end());
  84. return result;
  85. }
  86. void remove(PairVector& asVector, size_t index)
  87. {
  88. asVector.at(index) = asVector.last();
  89. asVector.removeLast();
  90. }
  91. PairVector findLeastGreaterThanOrEqual(PairVector& asVector, char key)
  92. {
  93. char bestKey = 0; // assignment to make gcc happy
  94. bool foundKey = false;
  95. for (size_t index = 0; index < asVector.size(); ++index) {
  96. if (asVector.at(index).key >= key) {
  97. if (asVector.at(index).key < bestKey || !foundKey) {
  98. foundKey = true;
  99. bestKey = asVector.at(index).key;
  100. }
  101. }
  102. }
  103. PairVector result;
  104. if (!foundKey)
  105. return result;
  106. return findExact(asVector, bestKey);
  107. }
  108. void assertFoundAndRemove(PairVector& asVector, char key, unsigned value)
  109. {
  110. bool found = false;
  111. size_t foundIndex = 0; // make compilers happy
  112. for (size_t index = 0; index < asVector.size(); ++index) {
  113. if (asVector.at(index).key == key
  114. && asVector.at(index).value == value) {
  115. EXPECT_TRUE(!found);
  116. found = true;
  117. foundIndex = index;
  118. }
  119. }
  120. EXPECT_TRUE(found);
  121. remove(asVector, foundIndex);
  122. }
  123. // This deliberately passes a copy of the vector.
  124. void assertEqual(RedBlackTree<TestNode, char>& asTree, PairVector asVector)
  125. {
  126. for (TestNode* current = asTree.first(); current; current = current->successor())
  127. assertFoundAndRemove(asVector, current->m_key, current->m_value);
  128. }
  129. void assertSameValuesForKey(RedBlackTree<TestNode, char>& asTree, TestNode* node, PairVector foundValues, char key)
  130. {
  131. if (node) {
  132. EXPECT_EQ(node->m_key, key);
  133. TestNode* prevNode = node;
  134. do {
  135. node = prevNode;
  136. prevNode = prevNode->predecessor();
  137. } while (prevNode && prevNode->m_key == key);
  138. EXPECT_EQ(node->m_key, key);
  139. EXPECT_TRUE(!prevNode || prevNode->m_key < key);
  140. do {
  141. assertFoundAndRemove(foundValues, node->m_key, node->m_value);
  142. node = node->successor();
  143. EXPECT_TRUE(!node || node->m_key >= key);
  144. } while (node && node->m_key == key);
  145. }
  146. EXPECT_TRUE(foundValues.isEmpty());
  147. }
  148. // The control string is a null-terminated list of commands. Each
  149. // command is two characters, with the first identifying the operation
  150. // and the second giving a key. The commands are:
  151. // +x Add x
  152. // *x Find all elements equal to x
  153. // @x Find all elements that have the smallest key that is greater than or equal to x
  154. // !x Remove all elements equal to x
  155. void testDriver(const char* controlString)
  156. {
  157. PairVector asVector;
  158. RedBlackTree<TestNode, char> asTree;
  159. for (const char* current = controlString; *current; current += 2) {
  160. char command = current[0];
  161. char key = current[1];
  162. unsigned value = ++m_counter;
  163. ASSERT(command);
  164. ASSERT(key);
  165. switch (command) {
  166. case '+': {
  167. TestNode* node = new TestNode(key, value);
  168. asTree.insert(node);
  169. asVector.append(Pair(key, value));
  170. break;
  171. }
  172. case '*': {
  173. TestNode* node = asTree.findExact(key);
  174. if (node)
  175. EXPECT_EQ(node->m_key, key);
  176. assertSameValuesForKey(asTree, node, findExact(asVector, key), key);
  177. break;
  178. }
  179. case '@': {
  180. TestNode* node = asTree.findLeastGreaterThanOrEqual(key);
  181. if (node) {
  182. EXPECT_TRUE(node->m_key >= key);
  183. assertSameValuesForKey(asTree, node, findLeastGreaterThanOrEqual(asVector, key), node->m_key);
  184. } else
  185. EXPECT_TRUE(findLeastGreaterThanOrEqual(asVector, key).isEmpty());
  186. break;
  187. }
  188. case '!': {
  189. while (true) {
  190. TestNode* node = asTree.remove(key);
  191. if (node) {
  192. EXPECT_EQ(node->m_key, key);
  193. assertFoundAndRemove(asVector, node->m_key, node->m_value);
  194. } else {
  195. EXPECT_TRUE(findExact(asVector, key).isEmpty());
  196. break;
  197. }
  198. }
  199. break;
  200. }
  201. default:
  202. ASSERT_NOT_REACHED();
  203. break;
  204. }
  205. EXPECT_EQ(asTree.size(), asVector.size());
  206. assertEqual(asTree, asVector);
  207. }
  208. }
  209. };
  210. TEST_F(RedBlackTreeTest, Empty)
  211. {
  212. testDriver("");
  213. }
  214. TEST_F(RedBlackTreeTest, EmptyGetFindRemove)
  215. {
  216. testDriver("*x@y!z");
  217. }
  218. TEST_F(RedBlackTreeTest, SingleAdd)
  219. {
  220. testDriver("+a");
  221. }
  222. TEST_F(RedBlackTreeTest, SingleAddGetFindRemoveNotFound)
  223. {
  224. testDriver("+a*x@y!z");
  225. }
  226. TEST_F(RedBlackTreeTest, SingleAddGetFindRemove)
  227. {
  228. testDriver("+a*a@a!a");
  229. }
  230. TEST_F(RedBlackTreeTest, TwoAdds)
  231. {
  232. testDriver("+a+b");
  233. }
  234. TEST_F(RedBlackTreeTest, ThreeAdds)
  235. {
  236. testDriver("+a+b+c");
  237. }
  238. TEST_F(RedBlackTreeTest, FourAdds)
  239. {
  240. testDriver("+a+b+c+d");
  241. }
  242. TEST_F(RedBlackTreeTest, LotsOfRepeatAdds)
  243. {
  244. testDriver("+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d");
  245. }
  246. TEST_F(RedBlackTreeTest, LotsOfRepeatAndUniqueAdds)
  247. {
  248. testDriver("+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+x+y+z");
  249. }
  250. TEST_F(RedBlackTreeTest, LotsOfRepeatAndUniqueAddsAndGetsAndRemoves)
  251. {
  252. testDriver("+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+x+y+z*a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z!a!b!c!d!e!f!g!h!i!j!k!l!m!n!o!p!q!r!s!t!u!v!w!x!y!z");
  253. }
  254. TEST_F(RedBlackTreeTest, SimpleBestFitSearch)
  255. {
  256. testDriver("+d+d+m+w@d@m@w@a@g@q");
  257. }
  258. TEST_F(RedBlackTreeTest, BiggerBestFitSearch)
  259. {
  260. testDriver("+d+d+d+d+d+d+d+d+d+d+f+f+f+f+f+f+f+h+h+i+j+k+l+m+o+p+q+r+z@a@b@c@d@e@f@g@h@i@j@k@l@m@n@o@p@q@r@s@t@u@v@w@x@y@z");
  261. }
  262. } // namespace TestWebKitAPI