brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · ff3c876 Raw
109 lines · cpp
1//===- llvm/unittest/ADT/SetVector.cpp ------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// SetVector unit tests.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/ADT/SetVector.h"14#include "llvm/ADT/SmallPtrSet.h"15#include "gmock/gmock.h"16#include "gtest/gtest.h"17 18using namespace llvm;19 20TEST(SetVector, EraseTest) {21  SetVector<int> S;22  S.insert(0);23  S.insert(1);24  S.insert(2);25 26  auto I = S.erase(std::next(S.begin()));27 28  // Test that the returned iterator is the expected one-after-erase29  // and the size/contents is the expected sequence {0, 2}.30  EXPECT_EQ(std::next(S.begin()), I);31  EXPECT_EQ(2u, S.size());32  EXPECT_EQ(0, *S.begin());33  EXPECT_EQ(2, *std::next(S.begin()));34}35 36TEST(SetVector, ContainsTest) {37  SetVector<int> S;38  S.insert(0);39  S.insert(1);40  S.insert(2);41 42  EXPECT_TRUE(S.contains(0));43  EXPECT_TRUE(S.contains(1));44  EXPECT_TRUE(S.contains(2));45  EXPECT_FALSE(S.contains(-1));46 47  S.insert(2);48  EXPECT_TRUE(S.contains(2));49 50  S.remove(2);51  EXPECT_FALSE(S.contains(2));52}53 54TEST(SetVector, ConstPtrKeyTest) {55  SetVector<int *, SmallVector<int *, 8>, SmallPtrSet<const int *, 8>> S, T;56  int i, j, k, m, n;57 58  S.insert(&i);59  S.insert(&j);60  S.insert(&k);61 62  EXPECT_TRUE(S.contains(&i));63  EXPECT_TRUE(S.contains(&j));64  EXPECT_TRUE(S.contains(&k));65 66  EXPECT_TRUE(S.contains((const int *)&i));67  EXPECT_TRUE(S.contains((const int *)&j));68  EXPECT_TRUE(S.contains((const int *)&k));69 70  EXPECT_TRUE(S.contains(S[0]));71  EXPECT_TRUE(S.contains(S[1]));72  EXPECT_TRUE(S.contains(S[2]));73 74  S.remove(&k);75  EXPECT_FALSE(S.contains(&k));76  EXPECT_FALSE(S.contains((const int *)&k));77 78  T.insert(&j);79  T.insert(&m);80  T.insert(&n);81 82  EXPECT_TRUE(S.set_union(T));83  EXPECT_TRUE(S.contains(&m));84  EXPECT_TRUE(S.contains((const int *)&m));85 86  S.set_subtract(T);87  EXPECT_FALSE(S.contains(&j));88  EXPECT_FALSE(S.contains((const int *)&j));89}90 91TEST(SetVector, CtorRange) {92  constexpr unsigned Args[] = {3, 1, 2};93  SetVector<unsigned> Set(llvm::from_range, Args);94  EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));95}96 97TEST(SetVector, InsertRange) {98  SetVector<unsigned> Set;99  constexpr unsigned Args[] = {3, 1, 2};100  Set.insert_range(Args);101  EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));102}103 104TEST(SmallSetVector, CtorRange) {105  constexpr unsigned Args[] = {3, 1, 2};106  SmallSetVector<unsigned, 4> Set(llvm::from_range, Args);107  EXPECT_THAT(Set, ::testing::ElementsAre(3, 1, 2));108}109