forked from lingeringsocket/jgrapht
-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Edge Weight Modification in Unmodifiable View (#1217)
* Block edge weight modification in unmodifiable view * Add tests for unmodiable view
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
jgrapht-core/src/test/java/org/jgrapht/graph/AsUnmodifiableGraphTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* (C) Copyright 2024-2024, by Sung Ho Yoon and Contributors. | ||
* | ||
* JGraphT : a free Java graph-theory library | ||
* | ||
* See the CONTRIBUTORS.md file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the | ||
* GNU Lesser General Public License v2.1 or later | ||
* which is available at | ||
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR LGPL-2.1-or-later | ||
*/ | ||
package org.jgrapht.graph; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.jgrapht.*; | ||
import org.junit.jupiter.api.*; | ||
|
||
@DisplayName("Unmodifiable graph view tests") | ||
public class AsUnmodifiableGraphTest { | ||
|
||
private DefaultWeightedEdge loop; | ||
private DefaultWeightedEdge e12; | ||
private DefaultWeightedEdge e23; | ||
private DefaultWeightedEdge e24; | ||
private String v1 = "v1"; | ||
private String v2 = "v2"; | ||
private String v3 = "v3"; | ||
private String v4 = "v4"; | ||
private Graph<String, DefaultWeightedEdge> baseGraph; | ||
private Graph<String, DefaultWeightedEdge> unmodifiableGraph; | ||
|
||
@BeforeEach | ||
public void setUp() | ||
{ | ||
this.baseGraph = | ||
new DefaultUndirectedWeightedGraph<>(DefaultWeightedEdge.class); | ||
|
||
baseGraph.addVertex(v1); | ||
baseGraph.addVertex(v2); | ||
baseGraph.addVertex(v3); | ||
baseGraph.addVertex(v4); | ||
e12 = Graphs.addEdge(baseGraph, v1, v2, 6d); | ||
e23 = Graphs.addEdge(baseGraph, v2, v3, 456d); | ||
e24 = Graphs.addEdge(baseGraph, v2, v4, 0.587d); | ||
loop = Graphs.addEdge(baseGraph, v4, v4, 6781234453486d); | ||
|
||
this.unmodifiableGraph = new AsUnmodifiableGraph<>(this.baseGraph); | ||
} | ||
|
||
@DisplayName("Test null graph") | ||
@Test | ||
public void testNullGraph() { | ||
assertThrows(NullPointerException.class, () -> new AsUnmodifiableGraph<>(null)); | ||
} | ||
|
||
@DisplayName("Test edge addition") | ||
@Test | ||
public void testAddEdge() | ||
{ | ||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableGraph.addEdge(v3, v4)); | ||
assertThrows(UnsupportedOperationException.class, () -> { | ||
DefaultWeightedEdge e34 = new DefaultWeightedEdge(); | ||
e34.source = v3; | ||
e34.target = v4; | ||
e34.weight = 2d; | ||
unmodifiableGraph.addEdge(v3, v4, e34); | ||
}); | ||
} | ||
|
||
@DisplayName("Test vertex addition") | ||
@Test | ||
public void testAddVertex() { | ||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableGraph.addVertex()); | ||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableGraph.addVertex("v5")); | ||
} | ||
|
||
@DisplayName("Test edge weight modification") | ||
@Test | ||
public void testSetEdgeWeight() { | ||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableGraph.setEdgeWeight(v1, v2, 0d)); | ||
assertEquals(6d, baseGraph.getEdgeWeight(e12)); | ||
} | ||
} |