DAGDeltaAlgorithmTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===- llvm/unittest/ADT/DAGDeltaAlgorithmTest.cpp ------------------------===//
  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 "gtest/gtest.h"
  10. #include "llvm/ADT/DAGDeltaAlgorithm.h"
  11. #include <algorithm>
  12. #include <cstdarg>
  13. using namespace llvm;
  14. namespace {
  15. typedef DAGDeltaAlgorithm::edge_ty edge_ty;
  16. class FixedDAGDeltaAlgorithm : public DAGDeltaAlgorithm {
  17. changeset_ty FailingSet;
  18. unsigned NumTests;
  19. protected:
  20. bool ExecuteOneTest(const changeset_ty &Changes) override {
  21. ++NumTests;
  22. return std::includes(Changes.begin(), Changes.end(),
  23. FailingSet.begin(), FailingSet.end());
  24. }
  25. public:
  26. FixedDAGDeltaAlgorithm(const changeset_ty &_FailingSet)
  27. : FailingSet(_FailingSet),
  28. NumTests(0) {}
  29. unsigned getNumTests() const { return NumTests; }
  30. };
  31. std::set<unsigned> fixed_set(unsigned N, ...) {
  32. std::set<unsigned> S;
  33. va_list ap;
  34. va_start(ap, N);
  35. for (unsigned i = 0; i != N; ++i)
  36. S.insert(va_arg(ap, unsigned));
  37. va_end(ap);
  38. return S;
  39. }
  40. std::set<unsigned> range(unsigned Start, unsigned End) {
  41. std::set<unsigned> S;
  42. while (Start != End)
  43. S.insert(Start++);
  44. return S;
  45. }
  46. std::set<unsigned> range(unsigned N) {
  47. return range(0, N);
  48. }
  49. TEST(DAGDeltaAlgorithmTest, Basic) {
  50. std::vector<edge_ty> Deps;
  51. // Dependencies:
  52. // 1 - 3
  53. Deps.clear();
  54. Deps.push_back(std::make_pair(3, 1));
  55. // P = {3,5,7} \in S,
  56. // [0, 20),
  57. // should minimize to {1,3,5,7} in a reasonable number of tests.
  58. FixedDAGDeltaAlgorithm FDA(fixed_set(3, 3, 5, 7));
  59. EXPECT_EQ(fixed_set(4, 1, 3, 5, 7), FDA.Run(range(20), Deps));
  60. EXPECT_GE(46U, FDA.getNumTests());
  61. // Dependencies:
  62. // 0 - 1
  63. // \- 2 - 3
  64. // \- 4
  65. Deps.clear();
  66. Deps.push_back(std::make_pair(1, 0));
  67. Deps.push_back(std::make_pair(2, 0));
  68. Deps.push_back(std::make_pair(4, 0));
  69. Deps.push_back(std::make_pair(3, 2));
  70. // This is a case where we must hold required changes.
  71. //
  72. // P = {1,3} \in S,
  73. // [0, 5),
  74. // should minimize to {0,1,2,3} in a small number of tests.
  75. FixedDAGDeltaAlgorithm FDA2(fixed_set(2, 1, 3));
  76. EXPECT_EQ(fixed_set(4, 0, 1, 2, 3), FDA2.Run(range(5), Deps));
  77. EXPECT_GE(9U, FDA2.getNumTests());
  78. // This is a case where we should quickly prune part of the tree.
  79. //
  80. // P = {4} \in S,
  81. // [0, 5),
  82. // should minimize to {0,4} in a small number of tests.
  83. FixedDAGDeltaAlgorithm FDA3(fixed_set(1, 4));
  84. EXPECT_EQ(fixed_set(2, 0, 4), FDA3.Run(range(5), Deps));
  85. EXPECT_GE(6U, FDA3.getNumTests());
  86. }
  87. }