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