ASTVectorTest.cpp 2.5 KB

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