Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions include/boost/json/impl/array.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,21 @@ insert(
value_ref> init) ->
iterator
{
BOOST_ASSERT(
pos >= begin() && pos <= end());
if(init.size() == 0)
return data() + (pos - data());
// the value_refs in init may point into this
// array, whose storage revert_insert can
// relocate and free, so buffer them first
array temp(init, sp_);
revert_insert r(
pos, init.size(), *this);
value_ref::write_array(
r.p, init, sp_);
pos, temp.size(), *this);
relocate(
r.p,
temp.data(),
temp.size());
temp.t_->size = 0;
return r.commit();
}

Expand Down
39 changes: 28 additions & 11 deletions test/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,18 +1035,35 @@ class array_test
}

// insert(const_iterator, init_list)
fail_loop([&](storage_ptr const& sp)
{
array a({0, 3, 4}, sp);
auto it = a.insert(
a.begin() + 1, {1, str_});
BOOST_TEST(it == a.begin() + 1);
BOOST_TEST(a[0].as_int64() == 0);
BOOST_TEST(a[1].as_int64() == 1);
BOOST_TEST(a[2].as_string() == str_);
BOOST_TEST(a[3].as_int64() == 3);
BOOST_TEST(a[4].as_int64() == 4);
});
fail_loop([&](storage_ptr const& sp)
{
array a({0, 3, 4}, sp);
auto it = a.insert(
a.begin() + 1, {1, str_});
BOOST_TEST(it == a.begin() + 1);
BOOST_TEST(a[0].as_int64() == 0);
BOOST_TEST(a[1].as_int64() == 1);
BOOST_TEST(a[2].as_string() == str_);
BOOST_TEST(a[3].as_int64() == 3);
BOOST_TEST(a[4].as_int64() == 4);
});

// elements of init alias *this and
// insertion reallocates
fail_loop([&](storage_ptr const& sp)
{
array a({1, str_}, sp);
BOOST_TEST(a.capacity() == a.size());
a.insert(a.begin(), {a[0], a[1]});
BOOST_TEST(a.size() == 4);
BOOST_TEST(a[0].as_int64() == 1);
BOOST_TEST(a[1].as_string() == str_);
BOOST_TEST(a[2].as_int64() == 1);
BOOST_TEST(a[3].as_string() == str_);
check_storage(a, sp);
});
}

// emplace(const_iterator, arg)
fail_loop([&](storage_ptr const& sp)
Expand Down
Loading