Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
aa7f5ad8
Commit
aa7f5ad8
authored
Aug 21, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minor changes
parent
585a39a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
32 deletions
+61
-32
test/src/unit-allocator.cpp
test/src/unit-allocator.cpp
+61
-32
No files found.
test/src/unit-allocator.cpp
View file @
aa7f5ad8
...
...
@@ -63,7 +63,9 @@ TEST_CASE("bad_alloc")
}
}
bool
next_allocation_fails
=
false
;
bool
next_construct_fails
=
false
;
bool
next_destroy_fails
=
false
;
bool
next_deallocate_fails
=
false
;
template
<
class
T
>
struct
my_allocator
:
std
::
allocator
<
T
>
...
...
@@ -71,8 +73,9 @@ struct my_allocator : std::allocator<T>
template
<
class
...
Args
>
void
construct
(
T
*
p
,
Args
&&
...
args
)
{
if
(
next_
allocation
_fails
)
if
(
next_
construct
_fails
)
{
next_construct_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
...
...
@@ -80,6 +83,32 @@ struct my_allocator : std::allocator<T>
::
new
(
reinterpret_cast
<
void
*>
(
p
))
T
(
std
::
forward
<
Args
>
(
args
)...);
}
}
void
deallocate
(
T
*
p
,
std
::
size_t
n
)
{
if
(
next_deallocate_fails
)
{
next_deallocate_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
{
std
::
allocator
<
T
>::
deallocate
(
p
,
n
);
}
}
void
destroy
(
T
*
p
)
{
if
(
next_destroy_fails
)
{
next_destroy_fails
=
false
;
throw
std
::
bad_alloc
();
}
else
{
p
->~
T
();
}
}
};
TEST_CASE
(
"controlled bad_alloc"
)
...
...
@@ -100,63 +129,63 @@ TEST_CASE("controlled bad_alloc")
{
SECTION
(
"object"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
auto
t
=
my_json
::
value_t
::
object
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
SECTION
(
"array"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
auto
t
=
my_json
::
value_t
::
array
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
SECTION
(
"string"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
auto
t
=
my_json
::
value_t
::
string
;
CHECK_NOTHROW
(
my_json
::
json_value
j
(
t
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
t
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
}
SECTION
(
"json_value(const string_t&)"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
my_json
::
string_t
v
(
"foo"
);
CHECK_NOTHROW
(
my_json
::
json_value
j
(
v
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
::
json_value
j
(
v
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
/*
SECTION("json_value(const object_t&)")
{
next_
allocation
_fails = false;
next_
construct
_fails = false;
my_json::object_t v {{"foo", "bar"}};
CHECK_NOTHROW(my_json::json_value j(v));
next_
allocation
_fails = true;
next_
construct
_fails = true;
CHECK_THROWS_AS(my_json::json_value j(v), std::bad_alloc);
next_
allocation
_fails = false;
next_
construct
_fails = false;
}
*/
/*
SECTION("json_value(const array_t&)")
{
next_
allocation
_fails = false;
next_
construct
_fails = false;
my_json::array_t v = {"foo", "bar", "baz"};
CHECK_NOTHROW(my_json::json_value j(v));
next_
allocation
_fails = true;
next_
construct
_fails = true;
CHECK_THROWS_AS(my_json::json_value j(v), std::bad_alloc);
next_
allocation
_fails = false;
next_
construct
_fails = false;
}
*/
}
...
...
@@ -165,41 +194,41 @@ TEST_CASE("controlled bad_alloc")
{
SECTION
(
"basic_json(const CompatibleObjectType&)"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
std
::
map
<
std
::
string
,
std
::
string
>
v
{{
"foo"
,
"bar"
}};
CHECK_NOTHROW
(
my_json
j
(
v
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
j
(
v
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
SECTION
(
"basic_json(const CompatibleArrayType&)"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
std
::
vector
<
std
::
string
>
v
{
"foo"
,
"bar"
,
"baz"
};
CHECK_NOTHROW
(
my_json
j
(
v
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
j
(
v
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
SECTION
(
"basic_json(const typename string_t::value_type*)"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
CHECK_NOTHROW
(
my_json
v
(
"foo"
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
v
(
"foo"
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
SECTION
(
"basic_json(const typename string_t::value_type*)"
)
{
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
std
::
string
s
(
"foo"
);
CHECK_NOTHROW
(
my_json
v
(
s
));
next_
allocation
_fails
=
true
;
next_
construct
_fails
=
true
;
CHECK_THROWS_AS
(
my_json
v
(
s
),
std
::
bad_alloc
);
next_
allocation
_fails
=
false
;
next_
construct
_fails
=
false
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment