/3rd_party/llvm/unittests/ADT/DeltaAlgorithmTest.cpp

https://code.google.com/p/softart/ · C++ · 100 lines · 64 code · 17 blank · 19 comment · 8 complexity · 1ea32f051ab746d061714258dea73703 MD5 · raw file

  1. //===- llvm/unittest/ADT/DeltaAlgorithmTest.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/DeltaAlgorithm.h"
  11. #include <algorithm>
  12. #include <cstdarg>
  13. using namespace llvm;
  14. namespace std {
  15. std::ostream &operator<<(std::ostream &OS,
  16. const std::set<unsigned> &S) {
  17. OS << "{";
  18. for (std::set<unsigned>::const_iterator it = S.begin(),
  19. ie = S.end(); it != ie; ++it) {
  20. if (it != S.begin())
  21. OS << ",";
  22. OS << *it;
  23. }
  24. OS << "}";
  25. return OS;
  26. }
  27. }
  28. namespace {
  29. class FixedDeltaAlgorithm : public DeltaAlgorithm {
  30. changeset_ty FailingSet;
  31. unsigned NumTests;
  32. protected:
  33. virtual bool ExecuteOneTest(const changeset_ty &Changes) {
  34. ++NumTests;
  35. return std::includes(Changes.begin(), Changes.end(),
  36. FailingSet.begin(), FailingSet.end());
  37. }
  38. public:
  39. FixedDeltaAlgorithm(const changeset_ty &_FailingSet)
  40. : FailingSet(_FailingSet),
  41. NumTests(0) {}
  42. unsigned getNumTests() const { return NumTests; }
  43. };
  44. std::set<unsigned> fixed_set(unsigned N, ...) {
  45. std::set<unsigned> S;
  46. va_list ap;
  47. va_start(ap, N);
  48. for (unsigned i = 0; i != N; ++i)
  49. S.insert(va_arg(ap, unsigned));
  50. va_end(ap);
  51. return S;
  52. }
  53. std::set<unsigned> range(unsigned Start, unsigned End) {
  54. std::set<unsigned> S;
  55. while (Start != End)
  56. S.insert(Start++);
  57. return S;
  58. }
  59. std::set<unsigned> range(unsigned N) {
  60. return range(0, N);
  61. }
  62. TEST(DeltaAlgorithmTest, Basic) {
  63. // P = {3,5,7} \in S
  64. // [0, 20) should minimize to {3,5,7} in a reasonable number of tests.
  65. std::set<unsigned> Fails = fixed_set(3, 3, 5, 7);
  66. FixedDeltaAlgorithm FDA(Fails);
  67. EXPECT_EQ(fixed_set(3, 3, 5, 7), FDA.Run(range(20)));
  68. EXPECT_GE(33U, FDA.getNumTests());
  69. // P = {3,5,7} \in S
  70. // [10, 20) should minimize to [10,20)
  71. EXPECT_EQ(range(10,20), FDA.Run(range(10,20)));
  72. // P = [0,4) \in S
  73. // [0, 4) should minimize to [0,4) in 11 tests.
  74. //
  75. // 11 = |{ {},
  76. // {0}, {1}, {2}, {3},
  77. // {1, 2, 3}, {0, 2, 3}, {0, 1, 3}, {0, 1, 2},
  78. // {0, 1}, {2, 3} }|
  79. FDA = FixedDeltaAlgorithm(range(10));
  80. EXPECT_EQ(range(4), FDA.Run(range(4)));
  81. EXPECT_EQ(11U, FDA.getNumTests());
  82. }
  83. }