ASTVectorTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===- unittests/AST/DeclTest.cpp --- Declaration tests -------------------===//
  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. //
  10. // Unit tests for the ASTVector container.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/Compiler.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ASTVector.h"
  16. #include "clang/Basic/Builtins.h"
  17. #include "gtest/gtest.h"
  18. using namespace clang;
  19. namespace clang {
  20. namespace ast {
  21. namespace {
  22. class ASTVectorTest : public ::testing::Test {
  23. protected:
  24. ASTVectorTest()
  25. : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()),
  26. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  27. SourceMgr(Diags, FileMgr), Idents(LangOpts, nullptr),
  28. Ctxt(LangOpts, SourceMgr, Idents, Sels, Builtins) {}
  29. FileSystemOptions FileMgrOpts;
  30. FileManager FileMgr;
  31. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  32. DiagnosticsEngine Diags;
  33. SourceManager SourceMgr;
  34. LangOptions LangOpts;
  35. IdentifierTable Idents;
  36. SelectorTable Sels;
  37. Builtin::Context Builtins;
  38. ASTContext Ctxt;
  39. };
  40. } // unnamed namespace
  41. TEST_F(ASTVectorTest, Compile) {
  42. ASTVector<int> V;
  43. V.insert(Ctxt, V.begin(), 0);
  44. }
  45. TEST_F(ASTVectorTest, InsertFill) {
  46. ASTVector<double> V;
  47. // Ensure returned iterator points to first of inserted elements
  48. auto I = V.insert(Ctxt, V.begin(), 5, 1.0);
  49. ASSERT_EQ(V.begin(), I);
  50. // Check non-empty case as well
  51. I = V.insert(Ctxt, V.begin() + 1, 5, 1.0);
  52. ASSERT_EQ(V.begin() + 1, I);
  53. // And insert-at-end
  54. I = V.insert(Ctxt, V.end(), 5, 1.0);
  55. ASSERT_EQ(V.end() - 5, I);
  56. }
  57. TEST_F(ASTVectorTest, InsertEmpty) {
  58. ASTVector<double> V;
  59. // Ensure no pointer overflow when inserting empty range
  60. int Values[] = { 0, 1, 2, 3 };
  61. ArrayRef<int> IntVec(Values);
  62. auto I = V.insert(Ctxt, V.begin(), IntVec.begin(), IntVec.begin());
  63. ASSERT_EQ(V.begin(), I);
  64. ASSERT_TRUE(V.empty());
  65. // Non-empty range
  66. I = V.insert(Ctxt, V.begin(), IntVec.begin(), IntVec.end());
  67. ASSERT_EQ(V.begin(), I);
  68. // Non-Empty Vector, empty range
  69. I = V.insert(Ctxt, V.end(), IntVec.begin(), IntVec.begin());
  70. ASSERT_EQ(V.begin() + IntVec.size(), I);
  71. // Non-Empty Vector, non-empty range
  72. I = V.insert(Ctxt, V.end(), IntVec.begin(), IntVec.end());
  73. ASSERT_EQ(V.begin() + IntVec.size(), I);
  74. }
  75. } // end namespace ast
  76. } // end namespace clang