iterator_test.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <vector>
  16. #include "gmock/gmock.h"
  17. #include "source/opt/iterator.h"
  18. #include "source/util/make_unique.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace {
  22. using ::testing::ContainerEq;
  23. TEST(Iterator, IncrementDeref) {
  24. const int count = 100;
  25. std::vector<std::unique_ptr<int>> data;
  26. for (int i = 0; i < count; ++i) {
  27. data.emplace_back(new int(i));
  28. }
  29. UptrVectorIterator<int> it(&data, data.begin());
  30. UptrVectorIterator<int> end(&data, data.end());
  31. EXPECT_EQ(*data[0], *it);
  32. for (int i = 1; i < count; ++i) {
  33. EXPECT_NE(end, it);
  34. EXPECT_EQ(*data[i], *(++it));
  35. }
  36. EXPECT_EQ(end, ++it);
  37. }
  38. TEST(Iterator, DecrementDeref) {
  39. const int count = 100;
  40. std::vector<std::unique_ptr<int>> data;
  41. for (int i = 0; i < count; ++i) {
  42. data.emplace_back(new int(i));
  43. }
  44. UptrVectorIterator<int> begin(&data, data.begin());
  45. UptrVectorIterator<int> it(&data, data.end());
  46. for (int i = count - 1; i >= 0; --i) {
  47. EXPECT_NE(begin, it);
  48. EXPECT_EQ(*data[i], *(--it));
  49. }
  50. EXPECT_EQ(begin, it);
  51. }
  52. TEST(Iterator, PostIncrementDeref) {
  53. const int count = 100;
  54. std::vector<std::unique_ptr<int>> data;
  55. for (int i = 0; i < count; ++i) {
  56. data.emplace_back(new int(i));
  57. }
  58. UptrVectorIterator<int> it(&data, data.begin());
  59. UptrVectorIterator<int> end(&data, data.end());
  60. for (int i = 0; i < count; ++i) {
  61. EXPECT_NE(end, it);
  62. EXPECT_EQ(*data[i], *(it++));
  63. }
  64. EXPECT_EQ(end, it);
  65. }
  66. TEST(Iterator, PostDecrementDeref) {
  67. const int count = 100;
  68. std::vector<std::unique_ptr<int>> data;
  69. for (int i = 0; i < count; ++i) {
  70. data.emplace_back(new int(i));
  71. }
  72. UptrVectorIterator<int> begin(&data, data.begin());
  73. UptrVectorIterator<int> end(&data, data.end());
  74. UptrVectorIterator<int> it(&data, data.end());
  75. EXPECT_EQ(end, it--);
  76. for (int i = count - 1; i >= 1; --i) {
  77. EXPECT_EQ(*data[i], *(it--));
  78. }
  79. // Decrementing .begin() is undefined behavior.
  80. EXPECT_EQ(*data[0], *it);
  81. }
  82. TEST(Iterator, Access) {
  83. const int count = 100;
  84. std::vector<std::unique_ptr<int>> data;
  85. for (int i = 0; i < count; ++i) {
  86. data.emplace_back(new int(i));
  87. }
  88. UptrVectorIterator<int> it(&data, data.begin());
  89. for (int i = 0; i < count; ++i) EXPECT_EQ(*data[i], it[i]);
  90. }
  91. TEST(Iterator, Comparison) {
  92. const int count = 100;
  93. std::vector<std::unique_ptr<int>> data;
  94. for (int i = 0; i < count; ++i) {
  95. data.emplace_back(new int(i));
  96. }
  97. UptrVectorIterator<int> it(&data, data.begin());
  98. UptrVectorIterator<int> end(&data, data.end());
  99. for (int i = 0; i < count; ++i, ++it) EXPECT_TRUE(it < end);
  100. EXPECT_EQ(end, it);
  101. }
  102. TEST(Iterator, InsertBeginEnd) {
  103. const int count = 100;
  104. std::vector<std::unique_ptr<int>> data;
  105. std::vector<int> expected;
  106. std::vector<int> actual;
  107. for (int i = 0; i < count; ++i) {
  108. data.emplace_back(new int(i));
  109. expected.push_back(i);
  110. }
  111. // Insert at the beginning
  112. expected.insert(expected.begin(), -100);
  113. UptrVectorIterator<int> begin(&data, data.begin());
  114. auto insert_point = begin.InsertBefore(MakeUnique<int>(-100));
  115. for (int i = 0; i < count + 1; ++i) {
  116. actual.push_back(*(insert_point++));
  117. }
  118. EXPECT_THAT(actual, ContainerEq(expected));
  119. // Insert at the end
  120. expected.push_back(-42);
  121. expected.push_back(-36);
  122. expected.push_back(-77);
  123. UptrVectorIterator<int> end(&data, data.end());
  124. end = end.InsertBefore(MakeUnique<int>(-77));
  125. end = end.InsertBefore(MakeUnique<int>(-36));
  126. end = end.InsertBefore(MakeUnique<int>(-42));
  127. actual.clear();
  128. begin = UptrVectorIterator<int>(&data, data.begin());
  129. for (int i = 0; i < count + 4; ++i) {
  130. actual.push_back(*(begin++));
  131. }
  132. EXPECT_THAT(actual, ContainerEq(expected));
  133. }
  134. TEST(Iterator, InsertMiddle) {
  135. const int count = 100;
  136. std::vector<std::unique_ptr<int>> data;
  137. std::vector<int> expected;
  138. std::vector<int> actual;
  139. for (int i = 0; i < count; ++i) {
  140. data.emplace_back(new int(i));
  141. expected.push_back(i);
  142. }
  143. const int insert_pos = 42;
  144. expected.insert(expected.begin() + insert_pos, -100);
  145. expected.insert(expected.begin() + insert_pos, -42);
  146. UptrVectorIterator<int> it(&data, data.begin());
  147. for (int i = 0; i < insert_pos; ++i) ++it;
  148. it = it.InsertBefore(MakeUnique<int>(-100));
  149. it = it.InsertBefore(MakeUnique<int>(-42));
  150. auto begin = UptrVectorIterator<int>(&data, data.begin());
  151. for (int i = 0; i < count + 2; ++i) {
  152. actual.push_back(*(begin++));
  153. }
  154. EXPECT_THAT(actual, ContainerEq(expected));
  155. }
  156. TEST(IteratorRange, Interface) {
  157. const uint32_t count = 100;
  158. std::vector<std::unique_ptr<uint32_t>> data;
  159. for (uint32_t i = 0; i < count; ++i) {
  160. data.emplace_back(new uint32_t(i));
  161. }
  162. auto b = UptrVectorIterator<uint32_t>(&data, data.begin());
  163. auto e = UptrVectorIterator<uint32_t>(&data, data.end());
  164. auto range = IteratorRange<decltype(b)>(b, e);
  165. EXPECT_EQ(b, range.begin());
  166. EXPECT_EQ(e, range.end());
  167. EXPECT_FALSE(range.empty());
  168. EXPECT_EQ(count, range.size());
  169. EXPECT_EQ(0u, *range.begin());
  170. EXPECT_EQ(99u, *(--range.end()));
  171. // IteratorRange itself is immutable.
  172. ++b, --e;
  173. EXPECT_EQ(count, range.size());
  174. ++range.begin(), --range.end();
  175. EXPECT_EQ(count, range.size());
  176. }
  177. TEST(Iterator, FilterIterator) {
  178. struct Placeholder {
  179. int val;
  180. };
  181. std::vector<Placeholder> data = {{1}, {2}, {3}, {4}, {5},
  182. {6}, {7}, {8}, {9}, {10}};
  183. // Predicate to only consider odd values.
  184. struct Predicate {
  185. bool operator()(const Placeholder& data) { return data.val % 2; }
  186. };
  187. Predicate pred;
  188. auto filter_range = MakeFilterIteratorRange(data.begin(), data.end(), pred);
  189. EXPECT_EQ(filter_range.begin().Get(), data.begin());
  190. EXPECT_EQ(filter_range.end(), filter_range.begin().GetEnd());
  191. for (Placeholder& data : filter_range) {
  192. EXPECT_EQ(data.val % 2, 1);
  193. }
  194. for (auto it = filter_range.begin(); it != filter_range.end(); it++) {
  195. EXPECT_EQ(it->val % 2, 1);
  196. EXPECT_EQ((*it).val % 2, 1);
  197. }
  198. for (auto it = filter_range.begin(); it != filter_range.end(); ++it) {
  199. EXPECT_EQ(it->val % 2, 1);
  200. EXPECT_EQ((*it).val % 2, 1);
  201. }
  202. EXPECT_EQ(MakeFilterIterator(data.begin(), data.end(), pred).Get(),
  203. data.begin());
  204. EXPECT_EQ(MakeFilterIterator(data.end(), data.end(), pred).Get(), data.end());
  205. EXPECT_EQ(MakeFilterIterator(data.begin(), data.end(), pred).GetEnd(),
  206. MakeFilterIterator(data.end(), data.end(), pred));
  207. EXPECT_NE(MakeFilterIterator(data.begin(), data.end(), pred),
  208. MakeFilterIterator(data.end(), data.end(), pred));
  209. // Empty range: no values satisfies the predicate.
  210. auto empty_range = MakeFilterIteratorRange(
  211. data.begin(), data.end(),
  212. [](const Placeholder& data) { return data.val > 10; });
  213. EXPECT_EQ(empty_range.begin(), empty_range.end());
  214. }
  215. } // namespace
  216. } // namespace opt
  217. } // namespace spvtools