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