reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
//===- llvm/unittest/ADT/PriorityWorklist.cpp -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// PriorityWorklist unit tests.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/PriorityWorklist.h"
#include "gtest/gtest.h"
#include <list>
#include <vector>

namespace {

using namespace llvm;

template <typename T> class PriorityWorklistTest : public ::testing::Test {};
typedef ::testing::Types<PriorityWorklist<int>, SmallPriorityWorklist<int, 2>>
    TestTypes;
TYPED_TEST_CASE(PriorityWorklistTest, TestTypes);

TYPED_TEST(PriorityWorklistTest, Basic) {
  TypeParam W;
  EXPECT_TRUE(W.empty());
  EXPECT_EQ(0u, W.size());
  EXPECT_FALSE(W.count(42));

  EXPECT_TRUE(W.insert(21));
  EXPECT_TRUE(W.insert(42));
  EXPECT_TRUE(W.insert(17));

  EXPECT_FALSE(W.empty());
  EXPECT_EQ(3u, W.size());
  EXPECT_TRUE(W.count(42));

  EXPECT_FALSE(W.erase(75));
  EXPECT_EQ(3u, W.size());
  EXPECT_EQ(17, W.back());

  EXPECT_TRUE(W.erase(17));
  EXPECT_FALSE(W.count(17));
  EXPECT_EQ(2u, W.size());
  EXPECT_EQ(42, W.back());

  W.clear();
  EXPECT_TRUE(W.empty());
  EXPECT_EQ(0u, W.size());

  EXPECT_TRUE(W.insert(21));
  EXPECT_TRUE(W.insert(42));
  EXPECT_TRUE(W.insert(12));
  EXPECT_TRUE(W.insert(17));
  EXPECT_TRUE(W.count(12));
  EXPECT_TRUE(W.count(17));
  EXPECT_EQ(4u, W.size());
  EXPECT_EQ(17, W.back());
  EXPECT_TRUE(W.erase(12));
  EXPECT_FALSE(W.count(12));
  EXPECT_TRUE(W.count(17));
  EXPECT_EQ(3u, W.size());
  EXPECT_EQ(17, W.back());

  EXPECT_FALSE(W.insert(42));
  EXPECT_EQ(3u, W.size());
  EXPECT_EQ(42, W.pop_back_val());
  EXPECT_EQ(17, W.pop_back_val());
  EXPECT_EQ(21, W.pop_back_val());
  EXPECT_TRUE(W.empty());
}

TYPED_TEST(PriorityWorklistTest, InsertSequence) {
  TypeParam W;
  ASSERT_TRUE(W.insert(2));
  ASSERT_TRUE(W.insert(4));
  ASSERT_TRUE(W.insert(7));
  // Insert a sequence that has internal duplicates and a duplicate among
  // existing entries.
  W.insert(std::vector<int>({42, 13, 42, 7, 8}));
  EXPECT_EQ(8, W.pop_back_val());
  EXPECT_EQ(7, W.pop_back_val());
  EXPECT_EQ(42, W.pop_back_val());
  EXPECT_EQ(13, W.pop_back_val());
  EXPECT_EQ(4, W.pop_back_val());
  EXPECT_EQ(2, W.pop_back_val());
  ASSERT_TRUE(W.empty());

  // Simpler tests with various other input types.
  ASSERT_TRUE(W.insert(2));
  ASSERT_TRUE(W.insert(7));
  // Use a non-random-access container.
  W.insert(std::list<int>({7, 5}));
  EXPECT_EQ(5, W.pop_back_val());
  EXPECT_EQ(7, W.pop_back_val());
  EXPECT_EQ(2, W.pop_back_val());
  ASSERT_TRUE(W.empty());

  ASSERT_TRUE(W.insert(2));
  ASSERT_TRUE(W.insert(7));
  // Use a raw array.
  int A[] = {7, 5};
  W.insert(A);
  EXPECT_EQ(5, W.pop_back_val());
  EXPECT_EQ(7, W.pop_back_val());
  EXPECT_EQ(2, W.pop_back_val());
  ASSERT_TRUE(W.empty());

  ASSERT_TRUE(W.insert(2));
  ASSERT_TRUE(W.insert(7));
  // Inserting an empty sequence does nothing.
  W.insert(std::vector<int>());
  EXPECT_EQ(7, W.pop_back_val());
  EXPECT_EQ(2, W.pop_back_val());
  ASSERT_TRUE(W.empty());
}

TYPED_TEST(PriorityWorklistTest, EraseIf) {
  TypeParam W;
  W.insert(23);
  W.insert(10);
  W.insert(47);
  W.insert(42);
  W.insert(23);
  W.insert(13);
  W.insert(26);
  W.insert(42);
  EXPECT_EQ(6u, W.size());

  EXPECT_FALSE(W.erase_if([](int i) { return i > 100; }));
  EXPECT_EQ(6u, W.size());
  EXPECT_EQ(42, W.back());

  EXPECT_TRUE(W.erase_if([](int i) {
    assert(i != 0 && "Saw a null value!");
    return (i & 1) == 0;
  }));
  EXPECT_EQ(3u, W.size());
  EXPECT_FALSE(W.count(42));
  EXPECT_FALSE(W.count(26));
  EXPECT_FALSE(W.count(10));
  EXPECT_FALSE(W.insert(47));
  EXPECT_FALSE(W.insert(23));
  EXPECT_EQ(23, W.pop_back_val());
  EXPECT_EQ(47, W.pop_back_val());
  EXPECT_EQ(13, W.pop_back_val());
}

}