Clang Project

clang_source_code/unittests/AST/ASTVectorTest.cpp
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/ASTContext.h"
14#include "clang/AST/ASTVector.h"
15#include "clang/Basic/Builtins.h"
16#include "gtest/gtest.h"
17
18using namespace clang;
19
20namespace 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<DiagnosticIDsDiagID;
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) {
46  ASTVector<intV;
47  V.insert(Ctxt, V.begin(), 0);
48}
49
50TEST_F(ASTVectorTest, InsertFill) {
51  ASTVector<doubleV;
52
53  // Ensure returned iterator points to first of inserted elements
54  auto I = V.insert(Ctxt, V.begin(), 51.0);
55  ASSERT_EQ(V.begin(), I);
56
57  // Check non-empty case as well
58  I = V.insert(Ctxt, V.begin() + 151.0);
59  ASSERT_EQ(V.begin() + 1, I);
60
61  // And insert-at-end
62  I = V.insert(Ctxt, V.end(), 51.0);
63  ASSERT_EQ(V.end() - 5, I);
64}
65
66TEST_F(ASTVectorTest, InsertEmpty) {
67  ASTVector<doubleV;
68
69  // Ensure no pointer overflow when inserting empty range
70  int Values[] = { 0123 };
71  ArrayRef<intIntVec(Values);
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
91