Skip to content

Commit

Permalink
added test case for std::bad_alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 8, 2016
1 parent 85a3081 commit fadf286
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/unit.cpp
Expand Up @@ -13993,3 +13993,34 @@ TEST_CASE("regression tests")
CHECK(dest == expected);
}
}

// special test case to check if memory is leaked if constructor throws

template<class T>
struct my_allocator : std::allocator<T>
{
template<class... Args>
void construct(T*, Args&& ...)
{
throw std::bad_alloc();
}
};

TEST_CASE("bad_alloc")
{
SECTION("bad_alloc")
{
// create JSON type using the throwing allocator
using my_json = nlohmann::basic_json<std::map,
std::vector,
std::string,
bool,
std::int64_t,
std::uint64_t,
double,
my_allocator>;

// creating an object should throw
CHECK_THROWS_AS(my_json j(my_json::value_t::object), std::bad_alloc);
}
}

0 comments on commit fadf286

Please sign in to comment.