/
githubmirror
/
guava
Обзор
Документация
Войти
/
githubmirror
/
guava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
android/guava-tests/test/com/google/common/graph/GraphMutationTest.java
136 строк
5 KB
cpovirk
Make `GraphMutationTest` less flaky under Android by reducing work done there.
20 май 2026, 03:04
20 май 2026, 03:04
6ae7659
Код
Авторство
О чём код?
/* * Copyright (C) 2016 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.graph; import static com.google.common.truth.Truth.assertThat; import static java.util.Collections.shuffle; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.RandomAccess; import org.jspecify.annotations.NullUnmarked; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; /** Tests for repeated node and edge addition and removal in a {@link Graph}. */ @RunWith(JUnit4.class) @NullUnmarked public final class GraphMutationTest { private static final int NUM_TRIALS = isAndroid() ? 10 : 50; private static final int NUM_NODES = isAndroid() ? 20 : 100; private static final int NUM_EDGES = isAndroid() ? 100 : 1000; private static final int NODE_POOL_SIZE = isAndroid() ? 200 : 1000; // must be >> NUM_NODES @BeforeClass public static void checkGraphSize() { /* * We ensure that NUM_EDGES is possible for NUM_NODES for both directed and undirected graphs. * Undirected graphs allow for fewer edges, so we check against the number possible there. */ long maxEdges = (long) NUM_NODES * (NUM_NODES + 1) / 2; assertThat((long) NUM_EDGES).isAtMost(maxEdges); } @Test public void directedGraph() { testGraphMutation(GraphBuilder.directed()); } @Test public void undirectedGraph() { testGraphMutation(GraphBuilder.undirected()); } private static void testGraphMutation(GraphBuilder<? super Integer> graphBuilder) { Random gen = new Random(42); // Fixed seed so test results are deterministic. for (int trial = 0; trial < NUM_TRIALS; ++trial) { MutableGraph<Integer> graph = graphBuilder.allowsSelfLoops(true).build(); assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); AbstractGraphTest.validateGraph(graph); while (graph.nodes().size() < NUM_NODES) { graph.addNode(gen.nextInt(NODE_POOL_SIZE)); } ArrayList<Integer> nodeList = new ArrayList<>(graph.nodes()); while (graph.edges().size() < NUM_EDGES) { graph.putEdge(getRandomElement(nodeList, gen), getRandomElement(nodeList, gen)); } ArrayList<EndpointPair<Integer>> edgeList = new ArrayList<>(graph.edges()); assertThat(graph.nodes()).hasSize(NUM_NODES); assertThat(graph.edges()).hasSize(NUM_EDGES); AbstractGraphTest.validateGraph(graph); shuffle(edgeList, gen); int numEdgesToRemove = gen.nextInt(NUM_EDGES); for (int i = 0; i < numEdgesToRemove; ++i) { EndpointPair<Integer> edge = edgeList.get(i); assertThat(graph.removeEdge(edge.nodeU(), edge.nodeV())).isTrue(); } assertThat(graph.nodes()).hasSize(NUM_NODES); assertThat(graph.edges()).hasSize(NUM_EDGES - numEdgesToRemove); AbstractGraphTest.validateGraph(graph); shuffle(nodeList, gen); int numNodesToRemove = gen.nextInt(NUM_NODES); for (int i = 0; i < numNodesToRemove; ++i) { assertThat(graph.removeNode(nodeList.get(i))).isTrue(); } assertThat(graph.nodes()).hasSize(NUM_NODES - numNodesToRemove); // Number of edges remaining is unknown (node's incident edges have been removed). AbstractGraphTest.validateGraph(graph); for (int i = numNodesToRemove; i < NUM_NODES; ++i) { assertThat(graph.removeNode(nodeList.get(i))).isTrue(); } assertThat(graph.nodes()).isEmpty(); assertThat(graph.edges()).isEmpty(); // no edges can remain if there's no nodes AbstractGraphTest.validateGraph(graph); shuffle(nodeList, gen); for (Integer node : nodeList) { assertThat(graph.addNode(node)).isTrue(); } shuffle(edgeList, gen); for (EndpointPair<Integer> edge : edgeList) { assertThat(graph.putEdge(edge.nodeU(), edge.nodeV())).isTrue(); } assertThat(graph.nodes()).hasSize(NUM_NODES); assertThat(graph.edges()).hasSize(NUM_EDGES); AbstractGraphTest.validateGraph(graph); } } private static <L extends List<T> & RandomAccess, T> T getRandomElement(L list, Random gen) { return list.get(gen.nextInt(list.size())); } private static boolean isAndroid() { return System.getProperty("java.runtime.name", "").contains("Android"); } }