From b5a4f4a533e9d0902cf2b07e0ed79e8c504133ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=89=E7=90=86?= Date: Thu, 13 Apr 2023 20:16:45 +0800 Subject: [PATCH] Add validation --- cpp/include/gar/graph.h | 7 ++++--- cpp/test/test_graph.cc | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cpp/include/gar/graph.h b/cpp/include/gar/graph.h index 2d667d929..1512e1c69 100644 --- a/cpp/include/gar/graph.h +++ b/cpp/include/gar/graph.h @@ -502,9 +502,7 @@ class EdgeIter { } /** Check if the current position is the end. */ - bool is_end() const { - return global_chunk_index_ == chunk_end_ && cur_offset_ == 0; - } + bool is_end() const { return global_chunk_index_ >= chunk_end_; } /** Point to the next edge with the same source, return false if not found. */ bool next_src() { @@ -1237,6 +1235,9 @@ static inline Result ConstructEdgesCollection( EdgeInfo edge_info; GAR_ASSIGN_OR_RAISE(edge_info, graph_info.GetEdgeInfo(src_label, edge_label, dst_label)); + if (!edge_info.ContainAdjList(adj_list_type)) { + return Status::Invalid("Invalid adj list type"); + } switch (adj_list_type) { case AdjListType::ordered_by_source: return EdgesCollection( diff --git a/cpp/test/test_graph.cc b/cpp/test/test_graph.cc index ac2128a18..a433c8547 100644 --- a/cpp/test/test_graph.cc +++ b/cpp/test/test_graph.cc @@ -119,4 +119,26 @@ TEST_CASE("test_edges_collection", "[Slow]") { } std::cout << "edge_count=" << count2 << std::endl; REQUIRE(edges2.size() == count2); + + // empty collection + auto expect3 = GAR_NAMESPACE::ConstructEdgesCollection( + graph_info, src_label, edge_label, dst_label, + GAR_NAMESPACE::AdjListType::unordered_by_source, 5, 5); + REQUIRE(!expect2.has_error()); + auto& edges3 = std::get>(expect3.value()); + auto end3 = edges3.end(); + GAR_NAMESPACE::IdType count3 = 0; + for (auto it = edges3.begin(); it != end3; ++it) { + count3++; + } + std::cout << "edge_count=" << count3 << std::endl; + REQUIRE(count3 == 0); + REQUIRE(edges3.size() == 0); + + // invalid adjlist type + auto expect4 = GAR_NAMESPACE::ConstructEdgesCollection( + graph_info, src_label, edge_label, dst_label, + GAR_NAMESPACE::AdjListType::unordered_by_dest); + REQUIRE(expect4.status().IsInvalid()); }