IteratorTest.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/iterator.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. TEST(PointeeIteratorTest, Basic) {
  16. int arr[4] = { 1, 2, 3, 4 };
  17. SmallVector<int *, 4> V;
  18. V.push_back(&arr[0]);
  19. V.push_back(&arr[1]);
  20. V.push_back(&arr[2]);
  21. V.push_back(&arr[3]);
  22. typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator;
  23. test_iterator Begin, End;
  24. Begin = V.begin();
  25. End = test_iterator(V.end());
  26. test_iterator I = Begin;
  27. for (int i = 0; i < 4; ++i) {
  28. EXPECT_EQ(*V[i], *I);
  29. EXPECT_EQ(I, Begin + i);
  30. EXPECT_EQ(I, std::next(Begin, i));
  31. test_iterator J = Begin;
  32. J += i;
  33. EXPECT_EQ(I, J);
  34. EXPECT_EQ(*V[i], Begin[i]);
  35. EXPECT_NE(I, End);
  36. EXPECT_GT(End, I);
  37. EXPECT_LT(I, End);
  38. EXPECT_GE(I, Begin);
  39. EXPECT_LE(Begin, I);
  40. EXPECT_EQ(i, I - Begin);
  41. EXPECT_EQ(i, std::distance(Begin, I));
  42. EXPECT_EQ(Begin, I - i);
  43. test_iterator K = I++;
  44. EXPECT_EQ(K, std::prev(I));
  45. }
  46. EXPECT_EQ(End, I);
  47. }
  48. TEST(PointeeIteratorTest, SmartPointer) {
  49. SmallVector<std::unique_ptr<int>, 4> V;
  50. V.push_back(make_unique<int>(1));
  51. V.push_back(make_unique<int>(2));
  52. V.push_back(make_unique<int>(3));
  53. V.push_back(make_unique<int>(4));
  54. typedef pointee_iterator<
  55. SmallVectorImpl<std::unique_ptr<int>>::const_iterator> test_iterator;
  56. test_iterator Begin, End;
  57. Begin = V.begin();
  58. End = test_iterator(V.end());
  59. test_iterator I = Begin;
  60. for (int i = 0; i < 4; ++i) {
  61. EXPECT_EQ(*V[i], *I);
  62. EXPECT_EQ(I, Begin + i);
  63. EXPECT_EQ(I, std::next(Begin, i));
  64. test_iterator J = Begin;
  65. J += i;
  66. EXPECT_EQ(I, J);
  67. EXPECT_EQ(*V[i], Begin[i]);
  68. EXPECT_NE(I, End);
  69. EXPECT_GT(End, I);
  70. EXPECT_LT(I, End);
  71. EXPECT_GE(I, Begin);
  72. EXPECT_LE(Begin, I);
  73. EXPECT_EQ(i, I - Begin);
  74. EXPECT_EQ(i, std::distance(Begin, I));
  75. EXPECT_EQ(Begin, I - i);
  76. test_iterator K = I++;
  77. EXPECT_EQ(K, std::prev(I));
  78. }
  79. EXPECT_EQ(End, I);
  80. }
  81. } // anonymous namespace