Skip to content

Commit b9b95a3

Browse files
committed
IndexGraphBuilder Test: test edgeSource/edgeTarget, also for invalid edge ids
1 parent ea19066 commit b9b95a3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

jgalgo-core/src/test/java/com/jgalgo/graph/IndexGraphBuilderTest.java

+27
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
import org.junit.jupiter.api.Test;
3636
import com.jgalgo.internal.util.TestBase;
3737
import it.unimi.dsi.fastutil.Pair;
38+
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
39+
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
3840
import it.unimi.dsi.fastutil.ints.IntArrayList;
3941
import it.unimi.dsi.fastutil.ints.IntArrays;
42+
import it.unimi.dsi.fastutil.ints.IntIntPair;
4043
import it.unimi.dsi.fastutil.ints.IntList;
4144
import it.unimi.dsi.fastutil.ints.IntSet;
4245
import it.unimi.dsi.fastutil.ints.IntSets;
@@ -495,6 +498,30 @@ public void addEdgesReassignIdsInvalidEndpoint() {
495498
});
496499
}
497500

501+
@Test
502+
public void edgeSourceAndTarget() {
503+
final Random rand = new Random(0x843e29cab46a15b0L);
504+
foreachBoolConfig(directed -> {
505+
IndexGraphBuilderImpl b = new IndexGraphBuilderImpl(directed);
506+
b.addVertices(range(50));
507+
Int2ObjectMap<IntIntPair> edges = new Int2ObjectOpenHashMap<>();
508+
for (int i = 0; i < 50; i++) {
509+
int u = rand.nextInt(50), v = rand.nextInt(50);
510+
int e = b.addEdge(u, v);
511+
edges.put(e, IntIntPair.of(u, v));
512+
}
513+
for (int e : b.edges()) {
514+
IntIntPair endpoints = edges.get(e);
515+
assertEquals(endpoints.firstInt(), b.edgeSource(e));
516+
assertEquals(endpoints.secondInt(), b.edgeTarget(e));
517+
}
518+
for (int repeat = 0; repeat < 5; repeat++)
519+
assertThrows(NoSuchEdgeException.class, () -> b.edgeSource(b.edges().size() + rand.nextInt(10)));
520+
for (int repeat = 0; repeat < 5; repeat++)
521+
assertThrows(NoSuchEdgeException.class, () -> b.edgeTarget(b.edges().size() + rand.nextInt(10)));
522+
});
523+
}
524+
498525
@Test
499526
public void clear() {
500527
foreachBoolConfig(directed -> {

0 commit comments

Comments
 (0)