David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 1 | //===- unittests/AST/DeclTest.cpp --- Declaration tests -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Unit tests for the ASTVector container. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/ASTVector.h" |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Builtins.h" |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
| 17 | |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 20 | namespace clang { |
| 21 | namespace ast { |
| 22 | |
| 23 | namespace { |
| 24 | class ASTVectorTest : public ::testing::Test { |
| 25 | protected: |
| 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 | |
| 45 | TEST_F(ASTVectorTest, Compile) { |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 46 | ASTVector<int> V; |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 47 | V.insert(Ctxt, V.begin(), 0); |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 48 | } |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 49 | |
| 50 | TEST_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 | |
| 66 | TEST_F(ASTVectorTest, InsertEmpty) { |
| 67 | ASTVector<double> V; |
| 68 | |
| 69 | // Ensure no pointer overflow when inserting empty range |
Aaron Ballman | cc7f4aa | 2014-08-26 14:17:25 +0000 | [diff] [blame] | 70 | int Values[] = { 0, 1, 2, 3 }; |
Aaron Ballman | d71a5c7 | 2014-08-26 17:05:57 +0000 | [diff] [blame] | 71 | ArrayRef<int> IntVec(Values); |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame] | 72 | 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 |