|
| 1 | +#include <catch2/catch.hpp> |
| 2 | +#include "ordered_map.hpp" |
| 3 | + |
| 4 | +#include <random> |
| 5 | +#include <set> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +using nlohmann::ordered_map; |
| 10 | + |
| 11 | +// (Previous test cases remain unchanged...) |
| 12 | +// See previous message for all the basic/consistency/stability tests. |
| 13 | + |
| 14 | +// ------------------------------------------------------------------ |
| 15 | +// Randomized stress test for iterator stability |
| 16 | +// ------------------------------------------------------------------ |
| 17 | + |
| 18 | +TEST_CASE("ordered_map randomized iterator stability stress test") |
| 19 | +{ |
| 20 | + constexpr int initial_keys = 20; |
| 21 | + constexpr int iterations = 200; |
| 22 | + |
| 23 | + ordered_map<std::string, int> m; |
| 24 | + |
| 25 | + // insert initial keys |
| 26 | + for (int i = 0; i < initial_keys; ++i) |
| 27 | + { |
| 28 | + m[std::to_string(i)] = i; |
| 29 | + } |
| 30 | + |
| 31 | + // pick a few "protected" keys to keep iterators to |
| 32 | + std::vector<std::string> protected_keys = {"0", "5", "10", "15"}; |
| 33 | + std::vector<ordered_map<std::string,int>::iterator> protected_iterators; |
| 34 | + for (auto& k : protected_keys) |
| 35 | + { |
| 36 | + auto it = m.find(k); |
| 37 | + REQUIRE(it != m.end()); |
| 38 | + protected_iterators.push_back(it); |
| 39 | + } |
| 40 | + |
| 41 | + std::mt19937 rng(12345); // fixed seed for reproducibility |
| 42 | + std::uniform_int_distribution<int> action(0, 1); // 0=insert, 1=erase |
| 43 | + std::uniform_int_distribution<int> keygen(0, 1000); |
| 44 | + |
| 45 | + for (int step = 0; step < iterations; ++step) |
| 46 | + { |
| 47 | + if (action(rng) == 0) |
| 48 | + { |
| 49 | + // random insert |
| 50 | + std::string k = "k" + std::to_string(keygen(rng)); |
| 51 | + m[k] = step; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // random erase, but never erase protected keys |
| 56 | + std::string k = std::to_string(keygen(rng) % initial_keys); |
| 57 | + if (std::find(protected_keys.begin(), protected_keys.end(), k) == protected_keys.end()) |
| 58 | + { |
| 59 | + m.erase(k); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // after each op, check protected iterators still valid |
| 64 | + for (size_t i = 0; i < protected_keys.size(); ++i) |
| 65 | + { |
| 66 | + auto it = protected_iterators[i]; |
| 67 | + REQUIRE(it != m.end()); |
| 68 | + CHECK(it->first == protected_keys[i]); |
| 69 | + CHECK(it->second == std::stoi(protected_keys[i])); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments