blob: 359d2f423259c2e4faa259b8e58a6b756709e025 [file] [log] [blame]
David Blaikie67fc79f2013-07-13 19:23:35 +00001//===- 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
David Blaikie67fc79f2013-07-13 19:23:35 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTVector.h"
Will Dietzdff50cb2014-08-25 16:09:51 +000016#include "clang/Basic/Builtins.h"
Will Dietzdff50cb2014-08-25 16:09:51 +000017#include "gtest/gtest.h"
18
David Blaikie67fc79f2013-07-13 19:23:35 +000019using namespace clang;
20
Will Dietzdff50cb2014-08-25 16:09:51 +000021namespace clang {
22namespace ast {
23
24namespace {
25class ASTVectorTest : public ::testing::Test {
26protected:
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
46TEST_F(ASTVectorTest, Compile) {
David Blaikie67fc79f2013-07-13 19:23:35 +000047 ASTVector<int> V;
Will Dietzdff50cb2014-08-25 16:09:51 +000048 V.insert(Ctxt, V.begin(), 0);
David Blaikie67fc79f2013-07-13 19:23:35 +000049}
Will Dietzdff50cb2014-08-25 16:09:51 +000050
51TEST_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
67TEST_F(ASTVectorTest, InsertEmpty) {
68 ASTVector<double> V;
69
70 // Ensure no pointer overflow when inserting empty range
Aaron Ballmancc7f4aa2014-08-26 14:17:25 +000071 int Values[] = { 0, 1, 2, 3 };
Aaron Ballmand71a5c72014-08-26 17:05:57 +000072 ArrayRef<int> IntVec(Values);
Will Dietzdff50cb2014-08-25 16:09:51 +000073 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