checked_iterators_unittest.nc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This is a "No Compile Test" suite.
  5. // http://dev.chromium.org/developers/testing/no-compile-tests
  6. #include "base/containers/checked_iterators.h"
  7. namespace base {
  8. #if defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_START_END) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
  9. constexpr int kArray[] = {1, 2, 3, 4, 5};
  10. void WontCompile() {
  11. // start can't be larger than end
  12. constexpr CheckedContiguousIterator<const int> iter(kArray + 1, kArray);
  13. }
  14. #elif defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_START_CURRENT) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
  15. constexpr int kArray[] = {1, 2, 3, 4, 5};
  16. void WontCompile() {
  17. // current can't be larger than start
  18. constexpr CheckedContiguousIterator<const int> iter(kArray + 1, kArray, kArray + 5);
  19. }
  20. #elif defined(NCTEST_CHECKED_ITERATORS_CONSTRUCTOR_CURRENT_END) // [r"constexpr variable 'iter' must be initialized by a constant expression"]
  21. constexpr int kArray[] = {1, 2, 3, 4, 5};
  22. void WontCompile() {
  23. // current can't be larger than end
  24. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 2, kArray + 1);
  25. }
  26. #elif defined(NCTEST_CHECKED_ITERATORS_EQ_DIFFERENT_ITER) // [r"constexpr variable 'equal' must be initialized by a constant expression"]
  27. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  28. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  29. void WontCompile() {
  30. // Can't compare iterators into different containers
  31. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  32. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  33. constexpr bool equal = iter1 == iter2;
  34. }
  35. #elif defined(NCTEST_CHECKED_ITERATORS_NE_DIFFERENT_ITER) // [r"constexpr variable 'not_equal' must be initialized by a constant expression"]
  36. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  37. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  38. void WontCompile() {
  39. // Can't compare iterators into different containers
  40. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  41. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  42. constexpr bool not_equal = iter1 != iter2;
  43. }
  44. #elif defined(NCTEST_CHECKED_ITERATORS_LT_DIFFERENT_ITER) // [r"constexpr variable 'less_than' must be initialized by a constant expression"]
  45. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  46. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  47. void WontCompile() {
  48. // Can't compare iterators into different containers
  49. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  50. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  51. constexpr bool less_than = iter1 < iter2;
  52. }
  53. #elif defined(NCTEST_CHECKED_ITERATORS_LE_DIFFERENT_ITER) // [r"constexpr variable 'less_equal' must be initialized by a constant expression"]
  54. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  55. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  56. void WontCompile() {
  57. // Can't compare iterators into different containers
  58. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  59. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  60. constexpr bool less_equal = iter1 <= iter2;
  61. }
  62. #elif defined(NCTEST_CHECKED_ITERATORS_GT_DIFFERENT_ITER) // [r"constexpr variable 'greater_than' must be initialized by a constant expression"]
  63. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  64. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  65. void WontCompile() {
  66. // Can't compare iterators into different containers
  67. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  68. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  69. constexpr bool greater_than = iter1 > iter2;
  70. }
  71. #elif defined(NCTEST_CHECKED_ITERATORS_GE_DIFFERENT_ITER) // [r"constexpr variable 'greater_equal' must be initialized by a constant expression"]
  72. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  73. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  74. void WontCompile() {
  75. // Can't compare iterators into different containers
  76. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  77. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  78. constexpr bool greater_equal = iter1 >= iter2;
  79. }
  80. #elif defined(NCTEST_CHECKED_ITERATORS_PRE_INCR_END) // [r"constexpr variable 'pre_incr' must be initialized by a constant expression"]
  81. constexpr int kArray[] = {1, 2, 3, 4, 5};
  82. constexpr int PreIncr() {
  83. // Can't pre-increment the end iterator.
  84. CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
  85. ++end_iter;
  86. return 0;
  87. }
  88. void WontCompile() {
  89. constexpr int pre_incr = PreIncr();
  90. }
  91. #elif defined(NCTEST_CHECKED_ITERATORS_POST_INCR_END) // [r"constexpr variable 'post_incr' must be initialized by a constant expression"]
  92. constexpr int kArray[] = {1, 2, 3, 4, 5};
  93. constexpr int PostIncr() {
  94. // Can't post-increment the end iterator.
  95. CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
  96. end_iter++;
  97. return 0;
  98. }
  99. void WontCompile() {
  100. constexpr int post_incr = PostIncr();
  101. }
  102. #elif defined(NCTEST_CHECKED_ITERATORS_PRE_DECR_BEGIN) // [r"constexpr variable 'pre_decr' must be initialized by a constant expression"]
  103. constexpr int kArray[] = {1, 2, 3, 4, 5};
  104. constexpr int PreDecr() {
  105. // Can't pre-decrement the begin iterator.
  106. CheckedContiguousIterator<const int> begin_iter(kArray, kArray + 5);
  107. --begin_iter;
  108. return 0;
  109. }
  110. void WontCompile() {
  111. constexpr int pre_decr = PreDecr();
  112. }
  113. #elif defined(NCTEST_CHECKED_ITERATORS_POST_DECR_BEGIN) // [r"constexpr variable 'post_decr' must be initialized by a constant expression"]
  114. constexpr int kArray[] = {1, 2, 3, 4, 5};
  115. constexpr int PostDecr() {
  116. // Can't post-decrement the begin iterator.
  117. CheckedContiguousIterator<const int> begin_iter(kArray, kArray + 5);
  118. begin_iter--;
  119. return 0;
  120. }
  121. void WontCompile() {
  122. constexpr int post_decr = PostDecr();
  123. }
  124. #elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END) // [r"constexpr variable 'incr_past_end' must be initialized by a constant expression"]
  125. constexpr int kArray[] = {1, 2, 3, 4, 5};
  126. constexpr int IncrPastEnd() {
  127. // Can't increment iterator past the end.
  128. CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  129. iter += 6;
  130. return 0;
  131. }
  132. void WontCompile() {
  133. constexpr int incr_past_end = IncrPastEnd();
  134. }
  135. #elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN) // [r"constexpr variable 'decr_past_begin' must be initialized by a constant expression"]
  136. constexpr int kArray[] = {1, 2, 3, 4, 5};
  137. constexpr int DecrPastBegin() {
  138. // Can't decrement iterator past the begin.
  139. CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  140. iter += -1;
  141. return 0;
  142. }
  143. void WontCompile() {
  144. constexpr int decr_past_begin = DecrPastBegin();
  145. }
  146. #elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_2) // [r"constexpr variable 'iter_past_end' must be initialized by a constant expression"]
  147. constexpr int kArray[] = {1, 2, 3, 4, 5};
  148. void WontCompile() {
  149. // Can't increment iterator past the end.
  150. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  151. constexpr auto iter_past_end = iter + 6;
  152. }
  153. #elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_2) // [r"constexpr variable 'iter_past_begin' must be initialized by a constant expression"]
  154. constexpr int kArray[] = {1, 2, 3, 4, 5};
  155. void WontCompile() {
  156. // Can't decrement iterator past the begin.
  157. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  158. constexpr auto iter_past_begin = iter + (-1);
  159. }
  160. #elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_3) // [r"constexpr variable 'decr_past_begin' must be initialized by a constant expression"]
  161. constexpr int kArray[] = {1, 2, 3, 4, 5};
  162. constexpr int DecrPastBegin() {
  163. // Can't decrement iterator past the begin.
  164. CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  165. iter -= 1;
  166. return 0;
  167. }
  168. void WontCompile() {
  169. constexpr int decr_past_begin = DecrPastBegin();
  170. }
  171. #elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_3) // [r"constexpr variable 'incr_past_end' must be initialized by a constant expression"]
  172. constexpr int kArray[] = {1, 2, 3, 4, 5};
  173. constexpr int IncrPastEnd() {
  174. // Can't increment iterator past the end.
  175. CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  176. iter -= (-6);
  177. return 0;
  178. }
  179. void WontCompile() {
  180. constexpr int incr_past_end = IncrPastEnd();
  181. }
  182. #elif defined(NCTEST_CHECKED_ITERATORS_DECR_PAST_BEGIN_4) // [r"constexpr variable 'iter_past_begin' must be initialized by a constant expression"]
  183. constexpr int kArray[] = {1, 2, 3, 4, 5};
  184. void WontCompile() {
  185. // Can't decrement iterator past the begin.
  186. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  187. constexpr auto iter_past_begin = iter - 1;
  188. }
  189. #elif defined(NCTEST_CHECKED_ITERATORS_INCR_PAST_END_4) // [r"constexpr variable 'iter_past_end' must be initialized by a constant expression"]
  190. constexpr int kArray[] = {1, 2, 3, 4, 5};
  191. void WontCompile() {
  192. // Can't increment iterator past the end.
  193. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  194. constexpr auto iter_past_end = iter - (-6);
  195. }
  196. #elif defined(NCTEST_CHECKED_ITERATORS_DIFFERENCE_DIFFERENT_ITER) // [r"constexpr variable 'difference' must be initialized by a constant expression"]
  197. constexpr int kArray1[] = {1, 2, 3, 4, 5};
  198. constexpr int kArray2[] = {1, 2, 3, 4, 5};
  199. void WontCompile() {
  200. // Can't compare iterators into different containers
  201. constexpr CheckedContiguousIterator<const int> iter1(kArray1, kArray1 + 5);
  202. constexpr CheckedContiguousIterator<const int> iter2(kArray2, kArray2 + 5);
  203. constexpr auto difference = iter1 - iter2;
  204. }
  205. #elif defined(NCTEST_CHECKED_ITERATORS_STAR_END) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
  206. constexpr int kArray[] = {1, 2, 3, 4, 5};
  207. void WontCompile() {
  208. // Can't dereference the end iterator by star.
  209. constexpr CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
  210. constexpr auto& ref = *end_iter;
  211. }
  212. #elif defined(NCTEST_CHECKED_ITERATORS_ARROW_END) // [r"constexpr variable 'ptr' must be initialized by a constant expression"]
  213. constexpr int kArray[] = {1, 2, 3, 4, 5};
  214. void WontCompile() {
  215. // Can't dereference the end iterator by arrow.
  216. constexpr CheckedContiguousIterator<const int> end_iter(kArray, kArray + 5, kArray + 5);
  217. constexpr auto* ptr = end_iter.operator->();
  218. }
  219. #elif defined(NCTEST_CHECKED_ITERATORS_NEGATIVE_OPERATOR_AT) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
  220. constexpr int kArray[] = {1, 2, 3, 4, 5};
  221. void WontCompile() {
  222. // Can't use a negative index in operator[].
  223. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  224. constexpr auto& ref = iter[-1];
  225. }
  226. #elif defined(NCTEST_CHECKED_ITERATORS_OPERATOR_AT_END) // [r"constexpr variable 'ref' must be initialized by a constant expression"]
  227. constexpr int kArray[] = {1, 2, 3, 4, 5};
  228. void WontCompile() {
  229. // Can't use a operator[] to deref the end.
  230. constexpr CheckedContiguousIterator<const int> iter(kArray, kArray + 5);
  231. constexpr auto& ref = iter[5];
  232. }
  233. #endif
  234. } // namespace base