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