David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 1 | //===- 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 Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame^] | 17 | #include "clang/Basic/Builtins.h" |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | #include <vector> |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame^] | 25 | namespace clang { |
| 26 | namespace ast { |
| 27 | |
| 28 | namespace { |
| 29 | class ASTVectorTest : public ::testing::Test { |
| 30 | protected: |
| 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 | |
| 50 | TEST_F(ASTVectorTest, Compile) { |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 51 | ASTVector<int> V; |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame^] | 52 | V.insert(Ctxt, V.begin(), 0); |
David Blaikie | 67fc79f | 2013-07-13 19:23:35 +0000 | [diff] [blame] | 53 | } |
Will Dietz | dff50cb | 2014-08-25 16:09:51 +0000 | [diff] [blame^] | 54 | |
| 55 | TEST_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 | |
| 71 | TEST_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 |