ListHashSet.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2012 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include <wtf/ListHashSet.h>
  27. namespace TestWebKitAPI {
  28. TEST(WTF, ListHashSetRemoveFirst)
  29. {
  30. ListHashSet<int> list;
  31. list.add(1);
  32. list.add(2);
  33. list.add(3);
  34. ASSERT_EQ(1, list.first());
  35. list.removeFirst();
  36. ASSERT_EQ(2, list.first());
  37. list.removeFirst();
  38. ASSERT_EQ(3, list.first());
  39. list.removeFirst();
  40. ASSERT_TRUE(list.isEmpty());
  41. }
  42. TEST(WTF, ListHashSetAppendOrMoveToLastNewItems)
  43. {
  44. ListHashSet<int> list;
  45. ListHashSet<int>::AddResult result = list.appendOrMoveToLast(1);
  46. ASSERT_TRUE(result.isNewEntry);
  47. result = list.add(2);
  48. ASSERT_TRUE(result.isNewEntry);
  49. result = list.appendOrMoveToLast(3);
  50. ASSERT_TRUE(result.isNewEntry);
  51. ASSERT_EQ(list.size(), 3);
  52. // The list should be in order 1, 2, 3.
  53. ListHashSet<int>::iterator iterator = list.begin();
  54. ASSERT_EQ(1, *iterator);
  55. ++iterator;
  56. ASSERT_EQ(2, *iterator);
  57. ++iterator;
  58. ASSERT_EQ(3, *iterator);
  59. ++iterator;
  60. }
  61. TEST(WTF, ListHashSetAppendOrMoveToLastWithDuplicates)
  62. {
  63. ListHashSet<int> list;
  64. // Add a single element twice.
  65. ListHashSet<int>::AddResult result = list.add(1);
  66. ASSERT_TRUE(result.isNewEntry);
  67. result = list.appendOrMoveToLast(1);
  68. ASSERT_FALSE(result.isNewEntry);
  69. ASSERT_EQ(1, list.size());
  70. list.add(2);
  71. list.add(3);
  72. ASSERT_EQ(3, list.size());
  73. // Appending 2 move it to the end.
  74. ASSERT_EQ(3, list.last());
  75. result = list.appendOrMoveToLast(2);
  76. ASSERT_FALSE(result.isNewEntry);
  77. ASSERT_EQ(2, list.last());
  78. // Inverse the list by moving each element to end end.
  79. result = list.appendOrMoveToLast(3);
  80. ASSERT_FALSE(result.isNewEntry);
  81. result = list.appendOrMoveToLast(2);
  82. ASSERT_FALSE(result.isNewEntry);
  83. result = list.appendOrMoveToLast(1);
  84. ASSERT_FALSE(result.isNewEntry);
  85. ASSERT_EQ(3, list.size());
  86. ListHashSet<int>::iterator iterator = list.begin();
  87. ASSERT_EQ(3, *iterator);
  88. ++iterator;
  89. ASSERT_EQ(2, *iterator);
  90. ++iterator;
  91. ASSERT_EQ(1, *iterator);
  92. ++iterator;
  93. }
  94. TEST(WTF, ListHashSetPrependOrMoveToLastNewItems)
  95. {
  96. ListHashSet<int> list;
  97. ListHashSet<int>::AddResult result = list.prependOrMoveToFirst(1);
  98. ASSERT_TRUE(result.isNewEntry);
  99. result = list.add(2);
  100. ASSERT_TRUE(result.isNewEntry);
  101. result = list.prependOrMoveToFirst(3);
  102. ASSERT_TRUE(result.isNewEntry);
  103. ASSERT_EQ(list.size(), 3);
  104. // The list should be in order 3, 1, 2.
  105. ListHashSet<int>::iterator iterator = list.begin();
  106. ASSERT_EQ(3, *iterator);
  107. ++iterator;
  108. ASSERT_EQ(1, *iterator);
  109. ++iterator;
  110. ASSERT_EQ(2, *iterator);
  111. ++iterator;
  112. }
  113. TEST(WTF, ListHashSetPrependOrMoveToLastWithDuplicates)
  114. {
  115. ListHashSet<int> list;
  116. // Add a single element twice.
  117. ListHashSet<int>::AddResult result = list.add(1);
  118. ASSERT_TRUE(result.isNewEntry);
  119. result = list.prependOrMoveToFirst(1);
  120. ASSERT_FALSE(result.isNewEntry);
  121. ASSERT_EQ(1, list.size());
  122. list.add(2);
  123. list.add(3);
  124. ASSERT_EQ(3, list.size());
  125. // Prepending 2 move it to the beginning.
  126. ASSERT_EQ(1, list.first());
  127. result = list.prependOrMoveToFirst(2);
  128. ASSERT_FALSE(result.isNewEntry);
  129. ASSERT_EQ(2, list.first());
  130. // Inverse the list by moving each element to the first position.
  131. result = list.prependOrMoveToFirst(1);
  132. ASSERT_FALSE(result.isNewEntry);
  133. result = list.prependOrMoveToFirst(2);
  134. ASSERT_FALSE(result.isNewEntry);
  135. result = list.prependOrMoveToFirst(3);
  136. ASSERT_FALSE(result.isNewEntry);
  137. ASSERT_EQ(3, list.size());
  138. ListHashSet<int>::iterator iterator = list.begin();
  139. ASSERT_EQ(3, *iterator);
  140. ++iterator;
  141. ASSERT_EQ(2, *iterator);
  142. ++iterator;
  143. ASSERT_EQ(1, *iterator);
  144. ++iterator;
  145. }
  146. } // namespace TestWebKitAPI