blob: f5b208ab16875ee69e7c5cb14b64c3a8b5a150d1 [file] [log] [blame]
David Blaikie67fc79f2013-07-13 19:23:35 +00001//===- unittests/AST/DeclTest.cpp --- Declaration tests -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Blaikie67fc79f2013-07-13 19:23:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Unit tests for the ASTVector container.
10//
11//===----------------------------------------------------------------------===//
12
David Blaikie67fc79f2013-07-13 19:23:35 +000013#include "clang/AST/ASTContext.h"
14#include "clang/AST/ASTVector.h"
Will Dietzdff50cb2014-08-25 16:09:51 +000015#include "clang/Basic/Builtins.h"
Will Dietzdff50cb2014-08-25 16:09:51 +000016#include "gtest/gtest.h"
17
David Blaikie67fc79f2013-07-13 19:23:35 +000018using namespace clang;
19
Will Dietzdff50cb2014-08-25 16:09:51 +000020namespace clang {
21namespace ast {
22
23namespace {
24class ASTVectorTest : public ::testing::Test {
25protected:
26 ASTVectorTest()
27 : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()),
28 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
29 SourceMgr(Diags, FileMgr), Idents(LangOpts, nullptr),
30 Ctxt(LangOpts, SourceMgr, Idents, Sels, Builtins) {}
31
32 FileSystemOptions FileMgrOpts;
33 FileManager FileMgr;
34 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
35 DiagnosticsEngine Diags;
36 SourceManager SourceMgr;
37 LangOptions LangOpts;
38 IdentifierTable Idents;
39 SelectorTable Sels;
40 Builtin::Context Builtins;
41 ASTContext Ctxt;
42};
43} // unnamed namespace
44
45TEST_F(ASTVectorTest, Compile) {
David Blaikie67fc79f2013-07-13 19:23:35 +000046 ASTVector<int> V;
Will Dietzdff50cb2014-08-25 16:09:51 +000047 V.insert(Ctxt, V.begin(), 0);
David Blaikie67fc79f2013-07-13 19:23:35 +000048}
Will Dietzdff50cb2014-08-25 16:09:51 +000049
50TEST_F(ASTVectorTest, InsertFill) {
51 ASTVector<double> V;
52
53 // Ensure returned iterator points to first of inserted elements
54 auto I = V.insert(Ctxt, V.begin(), 5, 1.0);
55 ASSERT_EQ(V.begin(), I);
56
57 // Check non-empty case as well
58 I = V.insert(Ctxt, V.begin() + 1, 5, 1.0);
59 ASSERT_EQ(V.begin() + 1, I);
60
61 // And insert-at-end
62 I = V.insert(Ctxt, V.end(), 5, 1.0);
63 ASSERT_EQ(V.end() - 5, I);
64}
65
66TEST_F(ASTVectorTest, InsertEmpty) {
67 ASTVector<double> V;
68
69 // Ensure no pointer overflow when inserting empty range
Aaron Ballmancc7f4aa2014-08-26 14:17:25 +000070 int Values[] = { 0, 1, 2, 3 };
Aaron Ballmand71a5c72014-08-26 17:05:57 +000071 ArrayRef<int> IntVec(Values);
Will Dietzdff50cb2014-08-25 16:09:51 +000072 auto I = V.insert(Ctxt, V.begin(), IntVec.begin(), IntVec.begin());
73 ASSERT_EQ(V.begin(), I);
74 ASSERT_TRUE(V.empty());
75
76 // Non-empty range
77 I = V.insert(Ctxt, V.begin(), IntVec.begin(), IntVec.end());
78 ASSERT_EQ(V.begin(), I);
79
80 // Non-Empty Vector, empty range
81 I = V.insert(Ctxt, V.end(), IntVec.begin(), IntVec.begin());
82 ASSERT_EQ(V.begin() + IntVec.size(), I);
83
84 // Non-Empty Vector, non-empty range
85 I = V.insert(Ctxt, V.end(), IntVec.begin(), IntVec.end());
86 ASSERT_EQ(V.begin() + IntVec.size(), I);
87}
88
89} // end namespace ast
90} // end namespace clang