Skip to content

Commit f5f5e0c

Browse files
committed
invalid_argument instead of out_of_range for invalid vertices and hyperedges
1 parent d2f4b51 commit f5f5e0c

2 files changed

Lines changed: 63 additions & 42 deletions

File tree

include/hgl/hypergraph.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ class hypergraph final {
12391239

12401240
gl_attr_force_inline void _verify_vertex_id(const id_type vertex_id) const {
12411241
if (not this->has_vertex(vertex_id))
1242-
throw std::out_of_range(std::format("Got invalid vertex id [{}]", vertex_id));
1242+
throw std::invalid_argument(std::format("Got invalid vertex id [{}]", vertex_id));
12431243
}
12441244

12451245
void _remove_vertex_impl(const id_type vertex_id) {
@@ -1270,7 +1270,7 @@ class hypergraph final {
12701270

12711271
gl_attr_force_inline void _verify_hyperedge_id(const id_type hyperedge_id) const {
12721272
if (not this->has_hyperedge(hyperedge_id))
1273-
throw std::out_of_range(std::format("Got invalid hyperedge id [{}]", hyperedge_id));
1273+
throw std::invalid_argument(std::format("Got invalid hyperedge id [{}]", hyperedge_id));
12741274
}
12751275

12761276
void _remove_hyperedge_impl(const id_type hyperedge_id) {

tests/source/hgl/test_hypergraph.cpp

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ TEST_CASE_TEMPLATE_DEFINE(
7777
REQUIRE(rng::equal(sut.vertices() | vw::transform(get_id), constants::vertex_ids_view));
7878
REQUIRE(rng::equal(sut.vertex_ids(), constants::vertex_ids_view));
7979

80-
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::out_of_range);
80+
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::invalid_argument);
8181
}
8282

8383
SUBCASE("a hypergraph constructed with n_vertices and n_hyperedges parameters should contain "
@@ -89,12 +89,12 @@ TEST_CASE_TEMPLATE_DEFINE(
8989

9090
REQUIRE(rng::equal(sut.vertices() | vw::transform(get_id), constants::vertex_ids_view));
9191
REQUIRE(rng::equal(sut.vertex_ids(), constants::vertex_ids_view));
92-
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::out_of_range);
92+
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::invalid_argument);
9393

9494
REQUIRE(rng::equal(sut.hyperedges() | vw::transform(get_id), constants::hyperedge_ids_view)
9595
);
9696
REQUIRE(rng::equal(sut.hyperedge_ids(), constants::hyperedge_ids_view));
97-
CHECK_THROWS_AS(discard(sut.hyperedge(constants::out_of_rng_eid)), std::out_of_range);
97+
CHECK_THROWS_AS(discard(sut.hyperedge(constants::out_of_rng_eid)), std::invalid_argument);
9898
}
9999

100100
// --- vertex modifiers ---
@@ -166,7 +166,7 @@ TEST_CASE_TEMPLATE_DEFINE(
166166
sut.remove_vertex(constants::id1);
167167

168168
REQUIRE_EQ(sut.n_vertices(), constants::n_vertices - 1uz);
169-
CHECK_THROWS_AS(discard(sut.vertex(constants::n_vertices - 1uz)), std::out_of_range);
169+
CHECK_THROWS_AS(discard(sut.vertex(constants::n_vertices - 1uz)), std::invalid_argument);
170170
}
171171

172172
SUBCASE("remove_vertex(id) should do nothing if the given id is invalid") {
@@ -183,7 +183,7 @@ TEST_CASE_TEMPLATE_DEFINE(
183183
sut.remove_vertex(constants::id1);
184184

185185
REQUIRE_EQ(sut.n_vertices(), constants::n_vertices - 1uz);
186-
CHECK_THROWS_AS(discard(sut.vertex(constants::n_vertices - 1uz)), std::out_of_range);
186+
CHECK_THROWS_AS(discard(sut.vertex(constants::n_vertices - 1uz)), std::invalid_argument);
187187
}
188188

189189
SUBCASE("remove_vertices(ids) should properly remove elements at given indices (ignoring "
@@ -226,8 +226,10 @@ TEST_CASE_TEMPLATE_DEFINE(
226226

227227
SUBCASE("vertex/at should throw if the given id is invalid") {
228228
sut_type sut{constants::n_vertices};
229-
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::out_of_range);
230-
CHECK_THROWS_AS(discard(sut.at(hgl::vertex, constants::out_of_rng_vid)), std::out_of_range);
229+
CHECK_THROWS_AS(discard(sut.vertex(constants::out_of_rng_vid)), std::invalid_argument);
230+
CHECK_THROWS_AS(
231+
discard(sut.at(hgl::vertex, constants::out_of_rng_vid)), std::invalid_argument
232+
);
231233
}
232234

233235
SUBCASE("vertex/at should return a vertex with the given id") {
@@ -483,7 +485,9 @@ TEST_CASE_TEMPLATE_DEFINE(
483485
sut.remove_hyperedge(constants::id1);
484486

485487
REQUIRE_EQ(sut.n_hyperedges(), constants::n_hyperedges - 1uz);
486-
CHECK_THROWS_AS(discard(sut.hyperedge(constants::n_hyperedges - 1uz)), std::out_of_range);
488+
CHECK_THROWS_AS(
489+
discard(sut.hyperedge(constants::n_hyperedges - 1uz)), std::invalid_argument
490+
);
487491
}
488492

489493
SUBCASE("remove_hyperedge(id) should do nothing if the given id is invalid") {
@@ -500,7 +504,9 @@ TEST_CASE_TEMPLATE_DEFINE(
500504
sut.remove_hyperedge(constants::id1);
501505

502506
REQUIRE_EQ(sut.n_hyperedges(), constants::n_hyperedges - 1uz);
503-
CHECK_THROWS_AS(discard(sut.hyperedge(constants::n_hyperedges - 1uz)), std::out_of_range);
507+
CHECK_THROWS_AS(
508+
discard(sut.hyperedge(constants::n_hyperedges - 1uz)), std::invalid_argument
509+
);
504510
}
505511

506512
SUBCASE("remove_hyperedges_from(ids) should properly remove elements at given indices "
@@ -543,9 +549,9 @@ TEST_CASE_TEMPLATE_DEFINE(
543549

544550
SUBCASE("hyperedge/at should throw if the given id is invalid") {
545551
sut_type sut{constants::n_vertices, constants::n_hyperedges};
546-
CHECK_THROWS_AS(discard(sut.hyperedge(constants::out_of_rng_eid)), std::out_of_range);
552+
CHECK_THROWS_AS(discard(sut.hyperedge(constants::out_of_rng_eid)), std::invalid_argument);
547553
CHECK_THROWS_AS(
548-
discard(sut.at(hgl::hyperedge, constants::out_of_rng_eid)), std::out_of_range
554+
discard(sut.at(hgl::hyperedge, constants::out_of_rng_eid)), std::invalid_argument
549555
);
550556
}
551557

@@ -579,74 +585,89 @@ TEST_CASE_TEMPLATE_DEFINE(
579585

580586
if constexpr (std::same_as<directional_tag, hgl::undirected_t>) {
581587
CHECK_THROWS_AS(
582-
sut.bind(constants::out_of_rng_vid, constants::out_of_rng_eid), std::out_of_range
588+
sut.bind(constants::out_of_rng_vid, constants::out_of_rng_eid),
589+
std::invalid_argument
590+
);
591+
CHECK_THROWS_AS(
592+
sut.bind(constants::id1, constants::out_of_rng_eid), std::invalid_argument
593+
);
594+
CHECK_THROWS_AS(
595+
sut.bind(constants::out_of_rng_vid, constants::id1), std::invalid_argument
583596
);
584-
CHECK_THROWS_AS(sut.bind(constants::id1, constants::out_of_rng_eid), std::out_of_range);
585-
CHECK_THROWS_AS(sut.bind(constants::out_of_rng_vid, constants::id1), std::out_of_range);
586597
}
587598

588599
if constexpr (std::same_as<directional_tag, hgl::bf_directed_t>) {
589600
CHECK_THROWS_AS(
590601
sut.bind_tail(constants::out_of_rng_vid, constants::out_of_rng_eid),
591-
std::out_of_range
602+
std::invalid_argument
592603
);
593604
CHECK_THROWS_AS(
594-
sut.bind_tail(constants::id1, constants::out_of_rng_eid), std::out_of_range
605+
sut.bind_tail(constants::id1, constants::out_of_rng_eid), std::invalid_argument
595606
);
596607
CHECK_THROWS_AS(
597-
sut.bind_tail(constants::out_of_rng_vid, constants::id1), std::out_of_range
608+
sut.bind_tail(constants::out_of_rng_vid, constants::id1), std::invalid_argument
598609
);
599610

600611
CHECK_THROWS_AS(
601612
sut.bind_head(constants::out_of_rng_vid, constants::out_of_rng_eid),
602-
std::out_of_range
613+
std::invalid_argument
603614
);
604615
CHECK_THROWS_AS(
605-
sut.bind_head(constants::id1, constants::out_of_rng_eid), std::out_of_range
616+
sut.bind_head(constants::id1, constants::out_of_rng_eid), std::invalid_argument
606617
);
607618
CHECK_THROWS_AS(
608-
sut.bind_head(constants::out_of_rng_vid, constants::id1), std::out_of_range
619+
sut.bind_head(constants::out_of_rng_vid, constants::id1), std::invalid_argument
609620
);
610621
}
611622

612623
CHECK_THROWS_AS(
613-
sut.unbind(constants::out_of_rng_vid, constants::out_of_rng_eid), std::out_of_range
624+
sut.unbind(constants::out_of_rng_vid, constants::out_of_rng_eid), std::invalid_argument
625+
);
626+
CHECK_THROWS_AS(
627+
sut.unbind(constants::id1, constants::out_of_rng_eid), std::invalid_argument
628+
);
629+
CHECK_THROWS_AS(
630+
sut.unbind(constants::out_of_rng_vid, constants::id1), std::invalid_argument
614631
);
615-
CHECK_THROWS_AS(sut.unbind(constants::id1, constants::out_of_rng_eid), std::out_of_range);
616-
CHECK_THROWS_AS(sut.unbind(constants::out_of_rng_vid, constants::id1), std::out_of_range);
617632

618633
CHECK_THROWS_AS(
619634
discard(sut.are_incident(constants::out_of_rng_vid, constants::out_of_rng_eid)),
620-
std::out_of_range
635+
std::invalid_argument
621636
);
622637
CHECK_THROWS_AS(
623-
discard(sut.are_incident(constants::id1, constants::out_of_rng_eid)), std::out_of_range
638+
discard(sut.are_incident(constants::id1, constants::out_of_rng_eid)),
639+
std::invalid_argument
624640
);
625641
CHECK_THROWS_AS(
626-
discard(sut.are_incident(constants::out_of_rng_vid, constants::id1)), std::out_of_range
642+
discard(sut.are_incident(constants::out_of_rng_vid, constants::id1)),
643+
std::invalid_argument
627644
);
628645

629646
if constexpr (std::same_as<directional_tag, hgl::bf_directed_t>) {
630647
CHECK_THROWS_AS(
631648
discard(sut.is_tail(constants::out_of_rng_vid, constants::out_of_rng_eid)),
632-
std::out_of_range
649+
std::invalid_argument
633650
);
634651
CHECK_THROWS_AS(
635-
discard(sut.is_tail(constants::id1, constants::out_of_rng_eid)), std::out_of_range
652+
discard(sut.is_tail(constants::id1, constants::out_of_rng_eid)),
653+
std::invalid_argument
636654
);
637655
CHECK_THROWS_AS(
638-
discard(sut.is_tail(constants::out_of_rng_vid, constants::id1)), std::out_of_range
656+
discard(sut.is_tail(constants::out_of_rng_vid, constants::id1)),
657+
std::invalid_argument
639658
);
640659

641660
CHECK_THROWS_AS(
642661
discard(sut.is_head(constants::out_of_rng_vid, constants::out_of_rng_eid)),
643-
std::out_of_range
662+
std::invalid_argument
644663
);
645664
CHECK_THROWS_AS(
646-
discard(sut.is_head(constants::id1, constants::out_of_rng_eid)), std::out_of_range
665+
discard(sut.is_head(constants::id1, constants::out_of_rng_eid)),
666+
std::invalid_argument
647667
);
648668
CHECK_THROWS_AS(
649-
discard(sut.is_head(constants::out_of_rng_vid, constants::id1)), std::out_of_range
669+
discard(sut.is_head(constants::out_of_rng_vid, constants::id1)),
670+
std::invalid_argument
650671
);
651672
}
652673

@@ -842,10 +863,10 @@ TEST_CASE_TEMPLATE_DEFINE(
842863
sut_type sut{constants::n_vertices, constants::n_hyperedges};
843864
CHECK_THROWS_AS(
844865
discard(sut.incident_hyperedges(vertex_type{constants::out_of_rng_vid})),
845-
std::out_of_range
866+
std::invalid_argument
846867
);
847868
CHECK_THROWS_AS(
848-
discard(sut.degree(vertex_type{constants::out_of_rng_vid})), std::out_of_range
869+
discard(sut.degree(vertex_type{constants::out_of_rng_vid})), std::invalid_argument
849870
);
850871

851872
GL_SUPPRESS_WARNING_END;
@@ -974,11 +995,11 @@ TEST_CASE_TEMPLATE_DEFINE(
974995
sut_type sut{constants::n_vertices, constants::n_hyperedges};
975996
CHECK_THROWS_AS(
976997
discard(sut.incident_vertices(hyperedge_type{constants::out_of_rng_eid})),
977-
std::out_of_range
998+
std::invalid_argument
978999
);
9791000
CHECK_THROWS_AS(
9801001
discard(sut.hyperedge_size(hyperedge_type{constants::out_of_rng_eid})),
981-
std::out_of_range
1002+
std::invalid_argument
9821003
);
9831004

9841005
GL_SUPPRESS_WARNING_END;
@@ -1312,7 +1333,7 @@ TEST_CASE_TEMPLATE_DEFINE(
13121333
CHECK_EQ(sut.vertex_properties(id), std::format("vertex_{}", id));
13131334
}
13141335
CHECK_THROWS_AS(
1315-
discard(sut.vertex_properties(constants::out_of_rng_vid)), std::out_of_range
1336+
discard(sut.vertex_properties(constants::out_of_rng_vid)), std::invalid_argument
13161337
);
13171338
}
13181339

@@ -1325,7 +1346,7 @@ TEST_CASE_TEMPLATE_DEFINE(
13251346
CHECK_EQ(sut.hyperedge_properties(id), std::format("hyperedge_{}", id));
13261347
}
13271348
CHECK_THROWS_AS(
1328-
discard(sut.hyperedge_properties(constants::out_of_rng_eid)), std::out_of_range
1349+
discard(sut.hyperedge_properties(constants::out_of_rng_eid)), std::invalid_argument
13291350
);
13301351
}
13311352

@@ -1481,7 +1502,7 @@ TEST_CASE_TEMPLATE_DEFINE(
14811502
CHECK_EQ(sut.vertex_properties(id), std::format("vertex_{}", id));
14821503
}
14831504
CHECK_THROWS_AS(
1484-
discard(sut.vertex_properties(constants::out_of_rng_vid)), std::out_of_range
1505+
discard(sut.vertex_properties(constants::out_of_rng_vid)), std::invalid_argument
14851506
);
14861507
}
14871508

@@ -1494,7 +1515,7 @@ TEST_CASE_TEMPLATE_DEFINE(
14941515
CHECK_EQ(sut.hyperedge_properties(id), std::format("hyperedge_{}", id));
14951516
}
14961517
CHECK_THROWS_AS(
1497-
discard(sut.hyperedge_properties(constants::out_of_rng_eid)), std::out_of_range
1518+
discard(sut.hyperedge_properties(constants::out_of_rng_eid)), std::invalid_argument
14981519
);
14991520
}
15001521

0 commit comments

Comments
 (0)