|
| 1 | +// deque_erase_fuzz.cc |
| 2 | +#include <cstddef> |
| 3 | +#include <cstdint> |
| 4 | +#include <deque> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include <source_location> |
| 8 | +#include <fast_io.h> |
| 9 | +#include <fast_io_dsal/deque.h> |
| 10 | + |
| 11 | +using T = std::size_t; |
| 12 | + |
| 13 | +// Check equality between fast_io::deque and std::deque |
| 14 | +static void check_equal(::fast_io::deque<T> const &dq, |
| 15 | + ::std::deque<T> const &ref) |
| 16 | +{ |
| 17 | + if (dq.size() != ref.size()) |
| 18 | + { |
| 19 | + __builtin_trap(); // mismatch: size |
| 20 | + } |
| 21 | + |
| 22 | + for (std::size_t i{}; i != dq.size(); ++i) |
| 23 | + { |
| 24 | + if (dq[i] != ref[i]) |
| 25 | + { |
| 26 | + __builtin_trap(); // mismatch: value |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Map a byte to an operation kind |
| 32 | +enum class OpKind : uint8_t |
| 33 | +{ |
| 34 | + PushBack = 0, |
| 35 | + PushFront = 1, |
| 36 | + EraseIndex = 2, |
| 37 | + EraseRangeIter = 3, |
| 38 | + // you can add more later |
| 39 | +}; |
| 40 | + |
| 41 | +extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, std::size_t size) |
| 42 | +{ |
| 43 | + ::fast_io::deque<T> dq; |
| 44 | + ::std::deque<T> ref; |
| 45 | + |
| 46 | + // We interpret the input as a stream of small commands. |
| 47 | + // Each command may consume 1~3 bytes depending on the op. |
| 48 | + std::size_t i = 0; |
| 49 | + while (i < size) |
| 50 | + { |
| 51 | + uint8_t op_raw = data[i++] % 4; // we currently have 4 ops |
| 52 | + OpKind op = static_cast<OpKind>(op_raw); |
| 53 | + |
| 54 | + switch (op) |
| 55 | + { |
| 56 | + case OpKind::PushBack: |
| 57 | + { |
| 58 | + if (i >= size) |
| 59 | + { |
| 60 | + break; |
| 61 | + } |
| 62 | + // use the next byte as low bits of the value |
| 63 | + T v = static_cast<T>(data[i++]); |
| 64 | + dq.push_back(v); |
| 65 | + ref.push_back(v); |
| 66 | + break; |
| 67 | + } |
| 68 | + case OpKind::PushFront: |
| 69 | + { |
| 70 | + if (i >= size) |
| 71 | + { |
| 72 | + break; |
| 73 | + } |
| 74 | + T v = static_cast<T>(data[i++]); |
| 75 | + dq.push_front(v); |
| 76 | + ref.push_front(v); |
| 77 | + break; |
| 78 | + } |
| 79 | + case OpKind::EraseIndex: |
| 80 | + { |
| 81 | + if (dq.empty()) |
| 82 | + { |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + if (i >= size) |
| 87 | + { |
| 88 | + break; |
| 89 | + } |
| 90 | + std::size_t pos = static_cast<std::size_t>(data[i++]); |
| 91 | + if (!dq.empty()) |
| 92 | + { |
| 93 | + pos %= dq.size(); |
| 94 | + dq.erase_index(pos); |
| 95 | + ref.erase(ref.begin() + static_cast<std::ptrdiff_t>(pos)); |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + case OpKind::EraseRangeIter: |
| 100 | + { |
| 101 | + if (dq.empty()) |
| 102 | + { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + if (i + 1 > size) // need at least 1 byte |
| 107 | + { |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + std::size_t n = dq.size(); |
| 112 | + if (n == 0) |
| 113 | + { |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + // choose two indices from bytes |
| 118 | + std::size_t a = static_cast<std::size_t>(data[i++]) % n; |
| 119 | + std::size_t b = static_cast<std::size_t>(data[i++ % size]) % (n + 1); |
| 120 | + |
| 121 | + std::size_t first_idx = (a < b ? a : b); |
| 122 | + std::size_t last_idx = (a < b ? b : a); |
| 123 | + |
| 124 | + if (last_idx > n) |
| 125 | + { |
| 126 | + last_idx = n; |
| 127 | + } |
| 128 | + if (first_idx > last_idx) |
| 129 | + { |
| 130 | + first_idx = last_idx; |
| 131 | + } |
| 132 | + if (first_idx == last_idx) |
| 133 | + { |
| 134 | + break; // empty range, skip |
| 135 | + } |
| 136 | + |
| 137 | + auto dq_first = dq.begin() + static_cast<std::ptrdiff_t>(first_idx); |
| 138 | + auto dq_last = dq.begin() + static_cast<std::ptrdiff_t>(last_idx); |
| 139 | + auto ref_first = ref.begin() + static_cast<std::ptrdiff_t>(first_idx); |
| 140 | + auto ref_last = ref.begin() + static_cast<std::ptrdiff_t>(last_idx); |
| 141 | + |
| 142 | + dq.erase(dq_first, dq_last); |
| 143 | + ref.erase(ref_first, ref_last); |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // After each operation, verify dq == ref |
| 149 | + check_equal(dq, ref); |
| 150 | + } |
| 151 | + |
| 152 | + return 0; |
| 153 | +} |
0 commit comments