|
21 | 21 | #include <cassert> |
22 | 22 | #include <vector> |
23 | 23 |
|
| 24 | +#include "support/index.h" |
24 | 25 | namespace wasm { |
25 | 26 |
|
26 | | -// Use the delta debugging algorithm (Zeller 1999, |
| 27 | +// Use the delta debugging algorithm (Zeller 2002, |
27 | 28 | // https://dl.acm.org/doi/10.1109/32.988498) to find the minimal set of |
28 | | -// items necessary to preserve some property. Returns that minimal set of |
29 | | -// items, preserving their input order. `tryPartition` should have this |
30 | | -// signature: |
31 | | -// |
32 | | -// bool tryPartition(size_t partitionIndex, |
33 | | -// size_t numPartitions, |
34 | | -// const std::vector<T>& partition) |
35 | | -// |
36 | | -// It should return true iff the property is preserved while keeping only |
37 | | -// `partition` items. |
38 | | -template<typename T, typename F> |
39 | | -std::vector<T> deltaDebugging(std::vector<T> items, const F& tryPartition) { |
40 | | - if (items.empty()) { |
41 | | - return items; |
| 29 | +// items necessary to preserve some property. `working` is the minimal set of |
| 30 | +// items found so far and `test` is the smaller set of items that should be |
| 31 | +// tested next. After testing, call `accept()`, `reject()`, or `resolve(bool |
| 32 | +// accepted)` to update the working and test sets appropriately. |
| 33 | +template<typename T> struct DeltaDebugger { |
| 34 | + std::vector<T> working; |
| 35 | + std::vector<T> test; |
| 36 | + |
| 37 | +private: |
| 38 | + Index numPartitions = 1; |
| 39 | + Index currentPartition = 0; |
| 40 | + bool testingComplements = false; |
| 41 | + bool triedEmpty = false; |
| 42 | + bool isFinished = false; |
| 43 | + std::vector<std::vector<T>> partitions; |
| 44 | + |
| 45 | +public: |
| 46 | + DeltaDebugger(std::vector<T> items) : working(std::move(items)) {} |
| 47 | + |
| 48 | + bool finished() const { |
| 49 | + return isFinished || (triedEmpty && working.size() <= 1); |
42 | 50 | } |
43 | | - // First try removing everything. |
44 | | - if (tryPartition(0, 1, {})) { |
45 | | - return {}; |
| 51 | + Index partitionCount() { return numPartitions; } |
| 52 | + Index partitionIndex() { return currentPartition; } |
| 53 | + |
| 54 | + void accept() { |
| 55 | + if (finished()) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (test.empty()) { |
| 60 | + triedEmpty = true; |
| 61 | + } |
| 62 | + |
| 63 | + working = std::move(test); |
| 64 | + |
| 65 | + // We might be finished now even if we weren't before. |
| 66 | + if (finished()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (!testingComplements) { |
| 71 | + numPartitions = 2; |
| 72 | + } else { |
| 73 | + numPartitions = std::max(numPartitions - 1, Index(2)); |
| 74 | + } |
| 75 | + testingComplements = false; |
| 76 | + currentPartition = 0; |
| 77 | + updateTest(); |
46 | 78 | } |
47 | | - size_t numPartitions = 2; |
48 | | - while (numPartitions <= items.size()) { |
49 | | - // Partition the items. |
50 | | - std::vector<std::vector<T>> partitions; |
51 | | - size_t size = items.size(); |
52 | | - size_t basePartitionSize = size / numPartitions; |
53 | | - size_t rem = size % numPartitions; |
54 | | - size_t idx = 0; |
55 | | - for (size_t i = 0; i < numPartitions; ++i) { |
56 | | - size_t partitionSize = basePartitionSize + (i < rem ? 1 : 0); |
57 | | - if (partitionSize > 0) { |
58 | | - std::vector<T> partition; |
59 | | - partition.reserve(partitionSize); |
60 | | - for (size_t j = 0; j < partitionSize; ++j) { |
61 | | - partition.push_back(items[idx++]); |
| 79 | + |
| 80 | + void reject() { |
| 81 | + if (test.empty()) { |
| 82 | + triedEmpty = true; |
| 83 | + numPartitions = 2; |
| 84 | + updateTest(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (finished()) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + ++currentPartition; |
| 93 | + if (currentPartition >= partitions.size()) { |
| 94 | + // No need to test complements if there are only two partitions, since |
| 95 | + // that is no different. |
| 96 | + if (!testingComplements && numPartitions > 2) { |
| 97 | + testingComplements = true; |
| 98 | + currentPartition = 0; |
| 99 | + } else { |
| 100 | + if (numPartitions >= working.size()) { |
| 101 | + isFinished = true; |
| 102 | + return; |
62 | 103 | } |
63 | | - partitions.emplace_back(std::move(partition)); |
| 104 | + // Refine the partitions. |
| 105 | + numPartitions = std::min(Index(working.size()), 2 * numPartitions); |
| 106 | + testingComplements = false; |
| 107 | + currentPartition = 0; |
64 | 108 | } |
65 | 109 | } |
66 | | - assert(numPartitions == partitions.size()); |
| 110 | + updateTest(); |
| 111 | + } |
67 | 112 |
|
68 | | - bool reduced = false; |
| 113 | + // Convenience wrapper for when there is already a bool determining whether to |
| 114 | + // accept or reject the current test sequence. |
| 115 | + void resolve(bool success) { |
| 116 | + if (success) { |
| 117 | + accept(); |
| 118 | + } else { |
| 119 | + reject(); |
| 120 | + } |
| 121 | + } |
69 | 122 |
|
70 | | - // Try keeping only one partition. Try each partition in turn. |
71 | | - for (size_t i = 0; i < numPartitions; ++i) { |
72 | | - if (tryPartition(i, numPartitions, partitions[i])) { |
73 | | - items = std::move(partitions[i]); |
74 | | - numPartitions = 2; |
75 | | - reduced = true; |
76 | | - break; |
77 | | - } |
| 123 | +private: |
| 124 | + void updateTest() { |
| 125 | + if (finished()) { |
| 126 | + test.clear(); |
| 127 | + return; |
78 | 128 | } |
79 | | - if (reduced) { |
80 | | - continue; |
| 129 | + |
| 130 | + if (currentPartition == 0 && !testingComplements) { |
| 131 | + generatePartitions(); |
81 | 132 | } |
82 | 133 |
|
83 | | - // Otherwise, try keeping the complement of a partition. Do not do this with |
84 | | - // only two partitions because that would be no different from what we |
85 | | - // already tried. |
86 | | - if (numPartitions > 2) { |
87 | | - for (size_t i = 0; i < numPartitions; ++i) { |
88 | | - std::vector<T> complement; |
89 | | - complement.reserve(items.size() - partitions[i].size()); |
90 | | - for (size_t j = 0; j < numPartitions; ++j) { |
91 | | - if (j != i) { |
92 | | - complement.insert( |
93 | | - complement.end(), partitions[j].begin(), partitions[j].end()); |
94 | | - } |
95 | | - } |
96 | | - if (tryPartition(i, numPartitions, complement)) { |
97 | | - items = std::move(complement); |
98 | | - numPartitions = std::max(numPartitions - 1, size_t(2)); |
99 | | - reduced = true; |
100 | | - break; |
| 134 | + if (!testingComplements) { |
| 135 | + test = partitions[currentPartition]; |
| 136 | + } else { |
| 137 | + test.clear(); |
| 138 | + test.reserve(working.size() - partitions[currentPartition].size()); |
| 139 | + for (size_t i = 0; i < partitions.size(); ++i) { |
| 140 | + if (i != currentPartition) { |
| 141 | + test.insert(test.end(), partitions[i].begin(), partitions[i].end()); |
101 | 142 | } |
102 | 143 | } |
103 | | - if (reduced) { |
104 | | - continue; |
105 | | - } |
106 | 144 | } |
| 145 | + } |
107 | 146 |
|
108 | | - if (numPartitions == items.size()) { |
109 | | - // Cannot further refine the partitions. We're done. |
110 | | - break; |
111 | | - } |
| 147 | + void generatePartitions() { |
| 148 | + partitions.clear(); |
| 149 | + size_t size = working.size(); |
| 150 | + assert(numPartitions != 0 && numPartitions <= size); |
112 | 151 |
|
113 | | - // Otherwise, make the partitions finer grained. |
114 | | - numPartitions = std::min(items.size(), 2 * numPartitions); |
| 152 | + size_t basePartitionSize = size / numPartitions; |
| 153 | + size_t rem = size % numPartitions; |
| 154 | + size_t idx = 0; |
| 155 | + for (size_t i = 0; i < numPartitions; ++i) { |
| 156 | + size_t partitionSize = basePartitionSize + (i < rem ? 1 : 0); |
| 157 | + if (partitionSize > 0) { |
| 158 | + std::vector<T> partition; |
| 159 | + partition.reserve(partitionSize); |
| 160 | + for (size_t j = 0; j < partitionSize; ++j) { |
| 161 | + partition.push_back(working[idx++]); |
| 162 | + } |
| 163 | + partitions.emplace_back(std::move(partition)); |
| 164 | + } |
| 165 | + } |
115 | 166 | } |
116 | | - return items; |
117 | | -} |
| 167 | +}; |
118 | 168 |
|
119 | 169 | } // namespace wasm |
120 | 170 |
|
|
0 commit comments