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