Skip to content

Commit 3ff9b7e

Browse files
authored
Update unit-ordered_map-fuzzer.cpp to doctest
Signed-off-by: Andrea Cocito <[email protected]>
1 parent 730b148 commit 3ff9b7e

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed
Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,42 @@
1-
#include <catch2/catch.hpp>
1+
#include <doctest/doctest.h>
22
#include "ordered_map.hpp"
33

44
#include <random>
5-
#include <set>
6-
#include <string>
5+
#include <algorithm>
76
#include <vector>
87

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")
8+
TEST_CASE("ordered_map fuzz stress test")
199
{
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;
10+
nlohmann::ordered_map<std::string,int> m;
11+
constexpr int initial = 50, iterations=500;
12+
for (int i=0;i<initial;++i) m[std::to_string(i)]=i;
13+
14+
std::vector<std::string> protected_keys={"0","10","20","30"};
15+
std::vector<decltype(m)::iterator> protected_its;
16+
for (auto& k:protected_keys){
17+
auto it=m.find(k);
18+
REQUIRE(it!=m.end());
19+
protected_its.push_back(it);
2920
}
3021

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);
22+
std::mt19937 rng(42);
23+
std::uniform_int_distribution<int> act(0,1);
24+
std::uniform_int_distribution<int> keydist(0,1000);
4425

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-
{
26+
for(int step=0;step<iterations;++step){
27+
if(act(rng)==0){
28+
m["rand"+std::to_string(keydist(rng))]=step;
29+
} else {
30+
std::string k=std::to_string(keydist(rng)%initial);
31+
if(std::find(protected_keys.begin(),protected_keys.end(),k)==protected_keys.end()){
5932
m.erase(k);
6033
}
6134
}
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]));
35+
for(size_t i=0;i<protected_keys.size();++i){
36+
auto it=protected_its[i];
37+
REQUIRE(it!=m.end());
38+
CHECK(it->first==protected_keys[i]);
39+
CHECK(it->second==std::stoi(protected_keys[i]));
7040
}
7141
}
7242
}

0 commit comments

Comments
 (0)