|
| 1 | +// Copyright 2010-2024 Google LLC |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +#include "ortools/graph/line_graph.h" |
| 15 | + |
| 16 | +#include "absl/base/macros.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "ortools/graph/graph.h" |
| 19 | + |
| 20 | +namespace operations_research { |
| 21 | +namespace { |
| 22 | + |
| 23 | +// Empty fixture templates to collect the types of graphs on which we want to |
| 24 | +// base the shortest paths template instances that we test. |
| 25 | +template <typename GraphType> |
| 26 | +class LineGraphDeathTest : public testing::Test {}; |
| 27 | +template <typename GraphType> |
| 28 | +class LineGraphTest : public testing::Test {}; |
| 29 | + |
| 30 | +typedef testing::Types<::util::ListGraph<>, ::util::ReverseArcListGraph<>> |
| 31 | + GraphTypesForLineGraphTesting; |
| 32 | + |
| 33 | +TYPED_TEST_SUITE(LineGraphDeathTest, GraphTypesForLineGraphTesting); |
| 34 | +TYPED_TEST_SUITE(LineGraphTest, GraphTypesForLineGraphTesting); |
| 35 | + |
| 36 | +TYPED_TEST(LineGraphDeathTest, NullLineGraph) { |
| 37 | + TypeParam graph; |
| 38 | +#ifndef NDEBUG |
| 39 | + EXPECT_DEATH(BuildLineGraph<TypeParam>(graph, nullptr), |
| 40 | + "line_graph must not be NULL"); |
| 41 | +#else |
| 42 | + EXPECT_FALSE(BuildLineGraph<TypeParam>(graph, nullptr)); |
| 43 | +#endif |
| 44 | +} |
| 45 | + |
| 46 | +TYPED_TEST(LineGraphDeathTest, NonEmptyLineGraph) { |
| 47 | + TypeParam graph; |
| 48 | + TypeParam line_graph(1, 1); |
| 49 | + line_graph.AddArc(0, 0); |
| 50 | +#ifndef NDEBUG |
| 51 | + EXPECT_DEATH(BuildLineGraph<TypeParam>(graph, &line_graph), |
| 52 | + "line_graph must be empty"); |
| 53 | +#else |
| 54 | + EXPECT_FALSE(BuildLineGraph<TypeParam>(graph, &line_graph)); |
| 55 | +#endif |
| 56 | +} |
| 57 | + |
| 58 | +TYPED_TEST(LineGraphDeathTest, LineGraphOfEmptyGraph) { |
| 59 | + TypeParam graph; |
| 60 | + TypeParam line_graph; |
| 61 | + EXPECT_TRUE(BuildLineGraph<TypeParam>(graph, &line_graph)); |
| 62 | + EXPECT_EQ(0, line_graph.num_nodes()); |
| 63 | + EXPECT_EQ(0, line_graph.num_arcs()); |
| 64 | +} |
| 65 | + |
| 66 | +TYPED_TEST(LineGraphTest, LineGraphOfSingleton) { |
| 67 | + TypeParam graph(1, 1); |
| 68 | + graph.AddArc(0, 0); |
| 69 | + TypeParam line_graph; |
| 70 | + EXPECT_TRUE(BuildLineGraph<TypeParam>(graph, &line_graph)); |
| 71 | + EXPECT_EQ(1, line_graph.num_nodes()); |
| 72 | + EXPECT_EQ(1, line_graph.num_arcs()); |
| 73 | +} |
| 74 | + |
| 75 | +TYPED_TEST(LineGraphTest, LineGraph) { |
| 76 | + const typename TypeParam::NodeIndex kNodes = 4; |
| 77 | + const typename TypeParam::ArcIndex kArcs[][2] = { |
| 78 | + {0, 1}, {0, 2}, {1, 2}, {2, 0}, {2, 3}}; |
| 79 | + const typename TypeParam::ArcIndex kExpectedLineArcs[][2] = { |
| 80 | + {0, 2}, {2, 3}, {3, 0}, {3, 1}, {2, 4}, {1, 4}, {1, 3}}; |
| 81 | + TypeParam graph(kNodes, ABSL_ARRAYSIZE(kArcs)); |
| 82 | + for (int i = 0; i < ABSL_ARRAYSIZE(kArcs); ++i) { |
| 83 | + graph.AddArc(kArcs[i][0], kArcs[i][1]); |
| 84 | + } |
| 85 | + TypeParam line_graph; |
| 86 | + EXPECT_TRUE(BuildLineGraph<TypeParam>(graph, &line_graph)); |
| 87 | + EXPECT_EQ(ABSL_ARRAYSIZE(kArcs), line_graph.num_nodes()); |
| 88 | + EXPECT_EQ(ABSL_ARRAYSIZE(kExpectedLineArcs), line_graph.num_arcs()); |
| 89 | + for (int i = 0; i < ABSL_ARRAYSIZE(kExpectedLineArcs); ++i) { |
| 90 | + const typename TypeParam::NodeIndex expected_tail = kExpectedLineArcs[i][0]; |
| 91 | + const typename TypeParam::NodeIndex expected_head = kExpectedLineArcs[i][1]; |
| 92 | + bool found = false; |
| 93 | + for (typename TypeParam::OutgoingArcIterator out_iterator(line_graph, |
| 94 | + expected_tail); |
| 95 | + out_iterator.Ok(); out_iterator.Next()) { |
| 96 | + const typename TypeParam::ArcIndex arc = out_iterator.Index(); |
| 97 | + if (line_graph.Head(arc) == expected_head) { |
| 98 | + found = true; |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + EXPECT_TRUE(found) << expected_tail << " " << expected_head; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace |
| 107 | +} // namespace operations_research |
0 commit comments