ilistTest.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- llvm/unittest/ADT/APInt.cpp - APInt unit 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. #include "llvm/ADT/ilist.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/ilist_node.h"
  12. #include "gtest/gtest.h"
  13. #include <ostream>
  14. using namespace llvm;
  15. namespace {
  16. struct Node : ilist_node<Node> {
  17. int Value;
  18. Node() {}
  19. Node(int Value) : Value(Value) {}
  20. Node(const Node&) = default;
  21. ~Node() { Value = -1; }
  22. };
  23. TEST(ilistTest, Basic) {
  24. ilist<Node> List;
  25. List.push_back(Node(1));
  26. EXPECT_EQ(1, List.back().Value);
  27. EXPECT_EQ(nullptr, List.back().getPrevNode());
  28. EXPECT_EQ(nullptr, List.back().getNextNode());
  29. List.push_back(Node(2));
  30. EXPECT_EQ(2, List.back().Value);
  31. EXPECT_EQ(2, List.front().getNextNode()->Value);
  32. EXPECT_EQ(1, List.back().getPrevNode()->Value);
  33. const ilist<Node> &ConstList = List;
  34. EXPECT_EQ(2, ConstList.back().Value);
  35. EXPECT_EQ(2, ConstList.front().getNextNode()->Value);
  36. EXPECT_EQ(1, ConstList.back().getPrevNode()->Value);
  37. }
  38. TEST(ilistTest, SpliceOne) {
  39. ilist<Node> List;
  40. List.push_back(1);
  41. // The single-element splice operation supports noops.
  42. List.splice(List.begin(), List, List.begin());
  43. EXPECT_EQ(1u, List.size());
  44. EXPECT_EQ(1, List.front().Value);
  45. EXPECT_TRUE(std::next(List.begin()) == List.end());
  46. // Altenative noop. Move the first element behind itself.
  47. List.push_back(2);
  48. List.push_back(3);
  49. List.splice(std::next(List.begin()), List, List.begin());
  50. EXPECT_EQ(3u, List.size());
  51. EXPECT_EQ(1, List.front().Value);
  52. EXPECT_EQ(2, std::next(List.begin())->Value);
  53. EXPECT_EQ(3, List.back().Value);
  54. }
  55. TEST(ilistTest, UnsafeClear) {
  56. ilist<Node> List;
  57. // Before even allocating a sentinel.
  58. List.clearAndLeakNodesUnsafely();
  59. EXPECT_EQ(0u, List.size());
  60. // Empty list with sentinel.
  61. ilist<Node>::iterator E = List.end();
  62. List.clearAndLeakNodesUnsafely();
  63. EXPECT_EQ(0u, List.size());
  64. // The sentinel shouldn't change.
  65. EXPECT_TRUE(E == List.end());
  66. // List with contents.
  67. List.push_back(1);
  68. ASSERT_EQ(1u, List.size());
  69. Node *N = List.begin();
  70. EXPECT_EQ(1, N->Value);
  71. List.clearAndLeakNodesUnsafely();
  72. EXPECT_EQ(0u, List.size());
  73. ASSERT_EQ(1, N->Value);
  74. delete N;
  75. // List is still functional.
  76. List.push_back(5);
  77. List.push_back(6);
  78. ASSERT_EQ(2u, List.size());
  79. EXPECT_EQ(5, List.front().Value);
  80. EXPECT_EQ(6, List.back().Value);
  81. }
  82. }