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