|
10 | 10 | #include "./test_lib/callbacks_tester.hpp" |
11 | 11 |
|
12 | 12 | #include <gtest/gtest.h> |
| 13 | +#include <unordered_map> |
13 | 14 |
|
14 | 15 | #if defined(_MSC_VER) |
15 | 16 | # pragma warning(push) |
@@ -938,6 +939,46 @@ TEST(Tree, reserve_arena_issue288) |
938 | 939 | EXPECT_EQ(t.arena().last(pluses.size()), to_csubstr(pluses)); |
939 | 940 | } |
940 | 941 |
|
| 942 | +// https://github.com/biojppm/rapidyaml/issues/564 |
| 943 | +namespace issue564 { |
| 944 | +struct AdjacentSizedBlocks { |
| 945 | + std::vector<char> mem; |
| 946 | + char* start; |
| 947 | + char* next; |
| 948 | +}; |
| 949 | +using AvailableBlocks = std::unordered_map<size_t, AdjacentSizedBlocks>; |
| 950 | +static void free(void* , size_t size, void* user_data) { |
| 951 | + (*static_cast<AvailableBlocks *>(user_data))[size].next -= size; |
| 952 | +} |
| 953 | +static void* alloc(size_t len, void* /*hint*/, void* user_data) { |
| 954 | + auto& blocks = (*static_cast<AvailableBlocks*>(user_data))[len]; |
| 955 | + if (blocks.start == nullptr) { |
| 956 | + blocks.mem.resize(10 * len); |
| 957 | + blocks.start = blocks.next = blocks.mem.data(); |
| 958 | + } |
| 959 | + void *ret = blocks.next; |
| 960 | + blocks.next += len; |
| 961 | + return ret; |
| 962 | +} |
| 963 | +} // namespace issue564 |
| 964 | +TEST(Tree, issue564_relocate_arena_0) |
| 965 | +{ |
| 966 | + issue564::AvailableBlocks blocks_by_size; |
| 967 | + Callbacks cb(&blocks_by_size, issue564::alloc, issue564::free, nullptr); |
| 968 | + Tree dest(cb); |
| 969 | + Tree source(cb); |
| 970 | + parse_in_arena("[]", &dest); |
| 971 | + parse_in_arena("[]", &source); |
| 972 | + // Shallow copy `source` as a child of `dest` |
| 973 | + NodeRef child = dest.rootref().append_child(); |
| 974 | + dest.duplicate_contents(&source, source.root_id(), child.id()); |
| 975 | + // trigger a relocation in dest |
| 976 | + std::string loong(10000, ' '); |
| 977 | + ExpectError::check_success([&]{ |
| 978 | + dest.to_arena(to_csubstr(loong)); // error |
| 979 | + }); |
| 980 | +} |
| 981 | + |
941 | 982 | TEST(Tree, clear) |
942 | 983 | { |
943 | 984 | Tree t(16, 64); |
|
0 commit comments