private void assertSnapshot(PlatformLruCache cache, Object... keysAndValues) { List<Object> actualKeysAndValues = new ArrayList<>(); for (Map.Entry<String, PlatformLruCache.BitmapAndSize> entry : cache.cache.snapshot().entrySet()) { actualKeysAndValues.add(entry.getKey()); actualKeysAndValues.add(entry.getValue().bitmap); } // assert using lists because order is important for LRUs assertThat(actualKeysAndValues).isEqualTo(Arrays.asList(keysAndValues)); } }
/** * In some cases our graph implementations return custom sets that define their own size() and * contains(). Verify that these sets are consistent with the elements of their iterator. */ @CanIgnoreReturnValue static <T> Set<T> sanityCheckSet(Set<T> set) { assertThat(set).hasSize(Iterators.size(set.iterator())); for (Object element : set) { assertThat(set).contains(element); } assertThat(set).doesNotContain(new Object()); assertThat(set).isEqualTo(ImmutableSet.copyOf(set)); return set; } }
@Test public void createUndirected_expectedNodeCount() { MutableNetwork<Integer, String> undirectedGraph = NetworkBuilder.undirected().expectedNodeCount(NODE_COUNT).build(); assertThat(undirectedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(undirectedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(undirectedGraph.edgesConnecting(N2, N1)).isEqualTo(ImmutableSet.of(E12)); }
@Test public void createUndirected_expectedEdgeCount() { MutableNetwork<Integer, String> undirectedGraph = NetworkBuilder.undirected().expectedEdgeCount(EDGE_COUNT).build(); assertThat(undirectedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(undirectedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(undirectedGraph.edgesConnecting(N2, N1)).isEqualTo(ImmutableSet.of(E12)); }
private static <N> void checkTransitiveClosure(Graph<N> originalGraph, Graph<N> expectedClosure) { for (N node : originalGraph.nodes()) { assertThat(reachableNodes(originalGraph, node)).isEqualTo(expectedClosure.successors(node)); } assertThat(transitiveClosure(originalGraph)).isEqualTo(expectedClosure); }
public void testMultipleQuantile() { ImmutableSet<Integer> indexes = ImmutableSet.of(50, 90, 99); Map<Integer, Double> referenceQuantiles = REFERENCE_ALGORITHM.multipleQuantiles(indexes, 100, dataset.clone()); assertThat(referenceQuantiles.keySet()).isEqualTo(indexes); for (QuantilesAlgorithm algorithm : NON_REFERENCE_ALGORITHMS) { Map<Integer, Double> quantiles = algorithm.multipleQuantiles(indexes, 100, dataset.clone()); assertWithMessage("Wrong keys from " + algorithm).that(quantiles.keySet()).isEqualTo(indexes); for (int i : indexes) { assertWithMessage("Mismatch between %s and %s at %s", algorithm, REFERENCE_ALGORITHM, i) .that(quantiles.get(i)) .isWithin(ALLOWED_ERROR) .of(referenceQuantiles.get(i)); } } } }
@Test public void createDirected_expectedEdgeCount() { MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().expectedEdgeCount(EDGE_COUNT).build(); assertThat(directedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(directedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(directedGraph.edgesConnecting(N2, N1)).isEmpty(); }
@Test public void createDirected_expectedNodeCount() { MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().expectedNodeCount(NODE_COUNT).build(); assertThat(directedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(directedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(directedGraph.edgesConnecting(N2, N1)).isEmpty(); }
@Test public void createUndirected_multigraph() { MutableNetwork<Integer, String> undirectedMultigraph = NetworkBuilder.undirected().allowsParallelEdges(true).build(); assertThat(undirectedMultigraph.addEdge(N1, N2, E12)).isTrue(); assertThat(undirectedMultigraph.addEdge(N1, N2, E12_A)).isTrue(); assertThat(undirectedMultigraph.addEdge(N2, N1, E21)).isTrue(); assertThat(undirectedMultigraph.edgesConnecting(N1, N2)) .isEqualTo(ImmutableSet.of(E12, E12_A, E21)); }
@Test public void createDirected_multigraph() { MutableNetwork<Integer, String> directedMultigraph = NetworkBuilder.directed().allowsParallelEdges(true).build(); assertThat(directedMultigraph.addEdge(N1, N2, E12)).isTrue(); assertThat(directedMultigraph.addEdge(N1, N2, E12_A)).isTrue(); assertThat(directedMultigraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12, E12_A)); assertThat(directedMultigraph.edgesConnecting(N2, N1)).isEmpty(); }
@Test public void testSingleOnePacketSection() { packetPayload[0] = 3; insertTableSection(4, (byte) 99, 3); reader.consume(new ParsableByteArray(packetPayload), FLAG_PAYLOAD_UNIT_START_INDICATOR); assertThat(payloadReader.parsedTableIds).isEqualTo(singletonList(99)); }
public void testScale_indexes_varargs_compute_longVarargs() { long[] dataset = Longs.toArray(SIXTEEN_SQUARES_LONGS); assertThat(Quantiles.scale(10).indexes(0, 10, 5, 1, 8, 1).compute(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 0, SIXTEEN_SQUARES_MIN, 10, SIXTEEN_SQUARES_MAX, 5, SIXTEEN_SQUARES_MEDIAN, 1, SIXTEEN_SQUARES_DECILE_1, 8, SIXTEEN_SQUARES_DECILE_8); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_LONGS); }
public void testScale_index_compute_intVarargs() { int[] dataset = Ints.toArray(SIXTEEN_SQUARES_INTEGERS); assertThat(Quantiles.scale(10).index(1).compute(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_INTEGERS); }
public void testScale_indexes_varargs_compute_intVarargs() { int[] dataset = Ints.toArray(SIXTEEN_SQUARES_INTEGERS); assertThat(Quantiles.scale(10).indexes(0, 10, 5, 1, 8, 1).compute(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 0, SIXTEEN_SQUARES_MIN, 10, SIXTEEN_SQUARES_MAX, 5, SIXTEEN_SQUARES_MEDIAN, 1, SIXTEEN_SQUARES_DECILE_1, 8, SIXTEEN_SQUARES_DECILE_8); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_INTEGERS); }
@Test public void injectParameterizedType() { class TestEntryPoint { @Inject List<String> listOfStrings; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides List<String> provideList() { return Arrays.asList("a", "b"); } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.listOfStrings).isEqualTo(Arrays.asList("a", "b")); }
public void testScale_index_compute_longVarargs() { long[] dataset = Longs.toArray(SIXTEEN_SQUARES_LONGS); assertThat(Quantiles.scale(10).index(1).compute(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_LONGS); }
@Test public void createUndirected() { MutableNetwork<Integer, String> undirectedGraph = NetworkBuilder.undirected().build(); assertThat(undirectedGraph.nodes()).isEmpty(); assertThat(undirectedGraph.edges()).isEmpty(); assertThat(undirectedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(undirectedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(undirectedGraph.edgesConnecting(N2, N1)).isEqualTo(ImmutableSet.of(E12)); // By default, parallel edges are not allowed. try { undirectedGraph.addEdge(N1, N2, E12_A); fail(ERROR_ADDED_PARALLEL_EDGE); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); } try { undirectedGraph.addEdge(N2, N1, E21); fail(ERROR_ADDED_PARALLEL_EDGE); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); } // By default, self-loop edges are not allowed. try { undirectedGraph.addEdge(N1, N1, E11); fail(ERROR_ADDED_SELF_LOOP); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains(ERROR_SELF_LOOP); } }
@Test public void createDirected() { MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build(); assertThat(directedGraph.nodes()).isEmpty(); assertThat(directedGraph.edges()).isEmpty(); assertThat(directedGraph.addEdge(N1, N2, E12)).isTrue(); assertThat(directedGraph.edgesConnecting(N1, N2)).isEqualTo(ImmutableSet.of(E12)); assertThat(directedGraph.edgesConnecting(N2, N1)).isEmpty(); // By default, parallel edges are not allowed. try { directedGraph.addEdge(N1, N2, E12_A); fail(ERROR_ADDED_PARALLEL_EDGE); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE); } // By default, self-loop edges are not allowed. try { directedGraph.addEdge(N1, N1, E11); fail(ERROR_ADDED_SELF_LOOP); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains(ERROR_SELF_LOOP); } }
@Test public void testHeaderSplitAcrossPackets() { packetPayload[0] = 3; // The first packet includes a pointer_field. insertTableSection(4, (byte) 100, 3); // This section header spreads across both packets. ParsableByteArray firstPacket = new ParsableByteArray(packetPayload, 5); reader.consume(firstPacket, FLAG_PAYLOAD_UNIT_START_INDICATOR); assertThat(payloadReader.parsedTableIds).isEmpty(); ParsableByteArray secondPacket = new ParsableByteArray(packetPayload); secondPacket.setPosition(5); reader.consume(secondPacket, /* flags= */ 0); assertThat(payloadReader.parsedTableIds).isEqualTo(singletonList(100)); }
@After public void validateGraphState() { assertStronglyEquivalent(graph, Graphs.copyOf(graph)); assertStronglyEquivalent(graph, ImmutableValueGraph.copyOf(graph)); Graph<Integer> asGraph = graph.asGraph(); AbstractGraphTest.validateGraph(asGraph); assertThat(graph.nodes()).isEqualTo(asGraph.nodes()); assertThat(graph.edges()).isEqualTo(asGraph.edges()); assertThat(graph.nodeOrder()).isEqualTo(asGraph.nodeOrder()); assertThat(graph.isDirected()).isEqualTo(asGraph.isDirected()); assertThat(graph.allowsSelfLoops()).isEqualTo(asGraph.allowsSelfLoops()); for (Integer node : graph.nodes()) { assertThat(graph.adjacentNodes(node)).isEqualTo(asGraph.adjacentNodes(node)); assertThat(graph.predecessors(node)).isEqualTo(asGraph.predecessors(node)); assertThat(graph.successors(node)).isEqualTo(asGraph.successors(node)); assertThat(graph.degree(node)).isEqualTo(asGraph.degree(node)); assertThat(graph.inDegree(node)).isEqualTo(asGraph.inDegree(node)); assertThat(graph.outDegree(node)).isEqualTo(asGraph.outDegree(node)); for (Integer otherNode : graph.nodes()) { boolean hasEdge = graph.hasEdgeConnecting(node, otherNode); assertThat(hasEdge).isEqualTo(asGraph.hasEdgeConnecting(node, otherNode)); assertThat(graph.edgeValueOrDefault(node, otherNode, null) != null).isEqualTo(hasEdge); assertThat(!graph.edgeValueOrDefault(node, otherNode, DEFAULT).equals(DEFAULT)) .isEqualTo(hasEdge); } } }