Repository: flink Updated Branches: refs/heads/master 6612c0e13 -> 2ac09c084
http://git-wip-us.apache.org/repos/asf/flink/blob/9437a0ff/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/EdgeMetricsTest.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/EdgeMetricsTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/EdgeMetricsTest.java index 3e23906..ae97c60 100644 --- a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/EdgeMetricsTest.java +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/EdgeMetricsTest.java @@ -18,10 +18,9 @@ package org.apache.flink.graph.library.metric.undirected; +import org.apache.flink.graph.Graph; import org.apache.flink.graph.asm.AsmTestBase; import org.apache.flink.graph.library.metric.undirected.EdgeMetrics.Result; -import org.apache.flink.types.IntValue; -import org.apache.flink.types.LongValue; import org.apache.flink.types.NullValue; import org.apache.commons.math3.util.CombinatoricsUtils; @@ -32,24 +31,32 @@ import static org.junit.Assert.assertEquals; /** * Tests for {@link EdgeMetrics}. */ -public class EdgeMetricsTest -extends AsmTestBase { - - @Test - public void testWithSimpleGraph() - throws Exception { - Result expectedResult = new Result(2, 6, 1, 3); - - Result edgeMetrics = new EdgeMetrics<IntValue, NullValue, NullValue>() - .run(undirectedSimpleGraph) +public class EdgeMetricsTest extends AsmTestBase { + + /** + * Validate a test result. + * + * @param graph input graph + * @param result expected {@link Result} + * @param <T> graph ID type + * @throws Exception on error + */ + private static <T extends Comparable<T>> void validate( + Graph<T, NullValue, NullValue> graph, Result result) throws Exception { + Result edgeMetrics = new EdgeMetrics<T, NullValue, NullValue>() + .run(graph) .execute(); - assertEquals(expectedResult, edgeMetrics); + assertEquals(result, edgeMetrics); + } + + @Test + public void testWithSimpleGraph() throws Exception { + validate(undirectedSimpleGraph, new Result(2, 6, 1, 3)); } @Test - public void testWithCompleteGraph() - throws Exception { + public void testWithCompleteGraph() throws Exception { long expectedDegree = completeGraphVertexCount - 1; long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2); long expectedTriplets = completeGraphVertexCount * expectedMaximumTriplets; @@ -57,36 +64,21 @@ extends AsmTestBase { Result expectedResult = new Result(expectedTriplets / 3, 2 * expectedTriplets / 3, expectedMaximumTriplets, expectedMaximumTriplets); - Result edgeMetrics = new EdgeMetrics<LongValue, NullValue, NullValue>() - .run(completeGraph) - .execute(); - - assertEquals(expectedResult, edgeMetrics); + validate(completeGraph, expectedResult); } @Test - public void testWithEmptyGraph() - throws Exception { - Result expectedResult; - - expectedResult = new Result(0, 0, 0, 0); - - Result withoutZeroDegreeVertices = new EdgeMetrics<LongValue, NullValue, NullValue>() - .run(emptyGraph) - .execute(); - - assertEquals(withoutZeroDegreeVertices, expectedResult); + public void testWithEmptyGraphWithVertices() throws Exception { + validate(emptyGraphWithVertices, new Result(0, 0, 0, 0)); } @Test - public void testWithRMatGraph() - throws Exception { - Result expectedResult = new Result(107817, 315537, 820, 3822); - - Result withoutZeroDegreeVertices = new EdgeMetrics<LongValue, NullValue, NullValue>() - .run(undirectedRMatGraph(10, 16)) - .execute(); + public void testWithEmptyGraphWithoutVertices() throws Exception { + validate(emptyGraphWithoutVertices, new Result(0, 0, 0, 0)); + } - assertEquals(expectedResult, withoutZeroDegreeVertices); + @Test + public void testWithRMatGraph() throws Exception { + validate(undirectedRMatGraph(10, 16), new Result(107817, 315537, 820, 3822)); } } http://git-wip-us.apache.org/repos/asf/flink/blob/9437a0ff/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/VertexMetricsTest.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/VertexMetricsTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/VertexMetricsTest.java index 71e587b..d0d0a1c 100644 --- a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/VertexMetricsTest.java +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/metric/undirected/VertexMetricsTest.java @@ -18,10 +18,9 @@ package org.apache.flink.graph.library.metric.undirected; +import org.apache.flink.graph.Graph; import org.apache.flink.graph.asm.AsmTestBase; import org.apache.flink.graph.library.metric.undirected.VertexMetrics.Result; -import org.apache.flink.types.IntValue; -import org.apache.flink.types.LongValue; import org.apache.flink.types.NullValue; import org.apache.commons.math3.util.CombinatoricsUtils; @@ -32,24 +31,39 @@ import static org.junit.Assert.assertEquals; /** * Tests for {@link VertexMetrics}. */ -public class VertexMetricsTest -extends AsmTestBase { - - @Test - public void testWithSimpleGraph() - throws Exception { - Result expectedResult = new Result(6, 7, 13, 4, 6); - - Result vertexMetrics = new VertexMetrics<IntValue, NullValue, NullValue>() - .run(undirectedSimpleGraph) +public class VertexMetricsTest extends AsmTestBase { + + /** + * Validate a test result. + * + * @param graph input graph + * @param includeZeroDegreeVertices whether to include zero-degree vertices + * @param result expected {@link Result} + * @param averageDegree result average degree + * @param density result density + * @param <T> graph ID type + * @throws Exception on error + */ + private static <T extends Comparable<T>> void validate( + Graph<T, NullValue, NullValue> graph, boolean includeZeroDegreeVertices, + Result result, float averageDegree, float density) throws Exception { + Result vertexMetrics = new VertexMetrics<T, NullValue, NullValue>() + .setIncludeZeroDegreeVertices(includeZeroDegreeVertices) + .run(graph) .execute(); - assertEquals(expectedResult, vertexMetrics); + assertEquals(result, vertexMetrics); + assertEquals(averageDegree, vertexMetrics.getAverageDegree(), ACCURACY); + assertEquals(density, vertexMetrics.getDensity(), ACCURACY); + } + + @Test + public void testWithSimpleGraph() throws Exception { + validate(undirectedSimpleGraph, false, new Result(6, 7, 13, 4, 6), 14f / 6, 7f / 15); } @Test - public void testWithCompleteGraph() - throws Exception { + public void testWithCompleteGraph() throws Exception { long expectedDegree = completeGraphVertexCount - 1; long expectedEdges = completeGraphVertexCount * expectedDegree / 2; long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2); @@ -58,52 +72,25 @@ extends AsmTestBase { Result expectedResult = new Result(completeGraphVertexCount, expectedEdges, expectedTriplets, expectedDegree, expectedMaximumTriplets); - Result vertexMetrics = new VertexMetrics<LongValue, NullValue, NullValue>() - .run(completeGraph) - .execute(); - - assertEquals(expectedResult, vertexMetrics); - assertEquals(expectedDegree, vertexMetrics.getAverageDegree(), ACCURACY); - assertEquals(1.0f, vertexMetrics.getDensity(), ACCURACY); + validate(completeGraph, false, expectedResult, expectedDegree, 1.0f); } @Test - public void testWithEmptyGraph() - throws Exception { - Result expectedResult; - - expectedResult = new Result(0, 0, 0, 0, 0); - - Result withoutZeroDegreeVertices = new VertexMetrics<LongValue, NullValue, NullValue>() - .setIncludeZeroDegreeVertices(false) - .run(emptyGraph) - .execute(); - - assertEquals(expectedResult, withoutZeroDegreeVertices); - assertEquals(Float.NaN, withoutZeroDegreeVertices.getAverageDegree(), ACCURACY); - assertEquals(Float.NaN, withoutZeroDegreeVertices.getDensity(), ACCURACY); - - expectedResult = new Result(3, 0, 0, 0, 0); - - Result withZeroDegreeVertices = new VertexMetrics<LongValue, NullValue, NullValue>() - .setIncludeZeroDegreeVertices(true) - .run(emptyGraph) - .execute(); + public void testWithEmptyGraphWithVertices() throws Exception { + validate(emptyGraphWithVertices, false, new Result(0, 0, 0, 0, 0), Float.NaN, Float.NaN); + validate(emptyGraphWithVertices, true, new Result(3, 0, 0, 0, 0), 0.0f, 0.0f); + } - assertEquals(expectedResult, withZeroDegreeVertices); - assertEquals(0.0f, withZeroDegreeVertices.getAverageDegree(), ACCURACY); - assertEquals(0.0f, withZeroDegreeVertices.getDensity(), ACCURACY); + @Test + public void testWithEmptyGraphWithoutVertices() throws Exception { + Result expectedResult = new Result(0, 0, 0, 0, 0); + validate(emptyGraphWithoutVertices, false, expectedResult, Float.NaN, Float.NaN); + validate(emptyGraphWithoutVertices, true, expectedResult, Float.NaN, Float.NaN); } @Test - public void testWithRMatGraph() - throws Exception { + public void testWithRMatGraph() throws Exception { Result expectedResult = new Result(902, 10442, 1003442, 463, 106953); - - Result withoutZeroDegreeVertices = new VertexMetrics<LongValue, NullValue, NullValue>() - .run(undirectedRMatGraph(10, 16)) - .execute(); - - assertEquals(expectedResult, withoutZeroDegreeVertices); + validate(undirectedRMatGraph(10, 16), false, expectedResult, 23.1529933f, 0.0256969f); } } http://git-wip-us.apache.org/repos/asf/flink/blob/9437a0ff/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/AdamicAdarTest.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/AdamicAdarTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/AdamicAdarTest.java index 5afd0ee..ef5e863 100644 --- a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/AdamicAdarTest.java +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/AdamicAdarTest.java @@ -19,22 +19,26 @@ package org.apache.flink.graph.library.similarity; import org.apache.flink.api.java.DataSet; +import org.apache.flink.graph.Graph; import org.apache.flink.graph.asm.AsmTestBase; import org.apache.flink.graph.library.similarity.AdamicAdar.Result; import org.apache.flink.test.util.TestBaseUtils; +import org.apache.flink.types.CopyableValue; import org.apache.flink.types.IntValue; import org.apache.flink.types.LongValue; import org.apache.flink.types.NullValue; +import org.apache.commons.math3.util.CombinatoricsUtils; import org.junit.Test; +import java.util.List; + import static org.junit.Assert.assertEquals; /** * Tests for {@link AdamicAdar}. */ -public class AdamicAdarTest -extends AsmTestBase { +public class AdamicAdarTest extends AsmTestBase { private float[] ilog = { 1.0f / (float) Math.log(2), @@ -46,8 +50,7 @@ extends AsmTestBase { }; @Test - public void testSimpleGraph() - throws Exception { + public void testWithSimpleGraph() throws Exception { DataSet<Result<IntValue>> aa = undirectedSimpleGraph .run(new AdamicAdar<>()); @@ -68,8 +71,7 @@ extends AsmTestBase { } @Test - public void testSimpleGraphWithMinimumScore() - throws Exception { + public void testWithSimpleGraphWithMinimumScore() throws Exception { DataSet<Result<IntValue>> aa = undirectedSimpleGraph .run(new AdamicAdar<IntValue, NullValue, NullValue>() .setMinimumScore(0.75f)); @@ -86,8 +88,7 @@ extends AsmTestBase { } @Test - public void testSimpleGraphWithMinimumRatio() - throws Exception { + public void testWithSimpleGraphWithMinimumRatio() throws Exception { DataSet<Result<IntValue>> aa = undirectedSimpleGraph .run(new AdamicAdar<IntValue, NullValue, NullValue>() .setMinimumRatio(1.5f)); @@ -99,22 +100,63 @@ extends AsmTestBase { TestBaseUtils.compareResultAsText(aa.collect(), expectedResult); } + /** + * Validate a test where each result has the same values. + * + * @param graph input graph + * @param count number of results + * @param score result score + * @param <T> graph ID type + * @throws Exception on error + */ + private static <T extends CopyableValue<T>> void validate( + Graph<T, NullValue, NullValue> graph, long count, double score) throws Exception { + DataSet<Result<T>> aa = graph + .run(new AdamicAdar<>()); + + List<Result<T>> results = aa.collect(); + + assertEquals(count, results.size()); + + for (Result<T> result : results) { + assertEquals(score, result.getAdamicAdarScore().getValue(), ACCURACY); + } + } + @Test - public void testCompleteGraph() - throws Exception { + public void testWithCompleteGraph() throws Exception { + // all vertex pairs are linked + long expectedCount = CombinatoricsUtils.binomialCoefficient((int) completeGraphVertexCount, 2); + float expectedScore = (completeGraphVertexCount - 2) / (float) Math.log(completeGraphVertexCount - 1); - DataSet<Result<LongValue>> aa = completeGraph - .run(new AdamicAdar<>()); + validate(completeGraph, expectedCount, expectedScore); + } - for (Result<LongValue> result : aa.collect()) { - assertEquals(expectedScore, result.getAdamicAdarScore().getValue(), 0.00001); - } + @Test + public void testWithEmptyGraphWithVertices() throws Exception { + validate(emptyGraphWithVertices, 0, Double.NaN); + } + + @Test + public void testWithEmptyGraphWithoutVertices() throws Exception { + validate(emptyGraphWithoutVertices, 0, Double.NaN); + } + + @Test + public void testWithStarGraph() throws Exception { + // all leaf vertices form a triplet with all other leaf vertices; + // only the center vertex is excluded + long expectedCount = CombinatoricsUtils.binomialCoefficient((int) starGraphVertexCount - 1, 2); + + // the intersection includes only the center vertex + float expectedScore = 1 / (float) Math.log(starGraphVertexCount - 1); + + validate(starGraph, expectedCount, expectedScore); } @Test - public void testRMatGraph() - throws Exception { + public void testWithRMatGraph() throws Exception { DataSet<Result<LongValue>> aa = undirectedRMatGraph(8, 8) .run(new AdamicAdar<>()); http://git-wip-us.apache.org/repos/asf/flink/blob/9437a0ff/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/JaccardIndexTest.java ---------------------------------------------------------------------- diff --git a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/JaccardIndexTest.java b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/JaccardIndexTest.java index 2e59f93..0fc4656 100644 --- a/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/JaccardIndexTest.java +++ b/flink-libraries/flink-gelly/src/test/java/org/apache/flink/graph/library/similarity/JaccardIndexTest.java @@ -19,28 +19,31 @@ package org.apache.flink.graph.library.similarity; import org.apache.flink.api.java.DataSet; +import org.apache.flink.graph.Graph; import org.apache.flink.graph.asm.AsmTestBase; import org.apache.flink.graph.asm.dataset.ChecksumHashCode; import org.apache.flink.graph.asm.dataset.ChecksumHashCode.Checksum; import org.apache.flink.graph.library.similarity.JaccardIndex.Result; import org.apache.flink.test.util.TestBaseUtils; +import org.apache.flink.types.CopyableValue; import org.apache.flink.types.IntValue; import org.apache.flink.types.LongValue; import org.apache.flink.types.NullValue; +import org.apache.commons.math3.util.CombinatoricsUtils; import org.junit.Test; +import java.util.List; + import static org.junit.Assert.assertEquals; /** * Tests for {@link JaccardIndex}. */ -public class JaccardIndexTest -extends AsmTestBase { +public class JaccardIndexTest extends AsmTestBase { @Test - public void testSimpleGraph() - throws Exception { + public void testSimpleGraph() throws Exception { DataSet<Result<IntValue>> ji = undirectedSimpleGraph .run(new JaccardIndex<>()); @@ -61,8 +64,7 @@ extends AsmTestBase { } @Test - public void testSimpleGraphWithMinimumScore() - throws Exception { + public void testWithSimpleGraphWithMinimumScore() throws Exception { DataSet<Result<IntValue>> ji = undirectedSimpleGraph .run(new JaccardIndex<IntValue, NullValue, NullValue>() .setMinimumScore(1, 2)); @@ -76,8 +78,7 @@ extends AsmTestBase { } @Test - public void testSimpleGraphWithMaximumScore() - throws Exception { + public void testWithSimpleGraphWithMaximumScore() throws Exception { DataSet<Result<IntValue>> ji = undirectedSimpleGraph .run(new JaccardIndex<IntValue, NullValue, NullValue>() .setMaximumScore(1, 2)); @@ -97,25 +98,73 @@ extends AsmTestBase { TestBaseUtils.compareResultAsText(ji.collect(), expectedResult); } - @Test - public void testCompleteGraph() - throws Exception { - DataSet<Result<LongValue>> ji = completeGraph - .run(new JaccardIndex<LongValue, NullValue, NullValue>() + /** + * Validate a test where each result has the same values. + * + * @param graph input graph + * @param count number of results + * @param distinctNeighborCount result distinct neighbor count + * @param sharedNeighborCount result shared neighbor count + * @param <T> graph ID type + * @throws Exception on error + */ + private static <T extends CopyableValue<T>> void validate( + Graph<T, NullValue, NullValue> graph, long count, long distinctNeighborCount, long sharedNeighborCount) throws Exception { + DataSet<Result<T>> ji = graph + .run(new JaccardIndex<T, NullValue, NullValue>() .setGroupSize(4)); - for (Result<LongValue> result : ji.collect()) { - // the intersection includes every vertex - assertEquals(completeGraphVertexCount, result.getDistinctNeighborCount().getValue()); + List<Result<T>> results = ji.collect(); - // the union only excludes the two vertices from the similarity score - assertEquals(completeGraphVertexCount - 2, result.getSharedNeighborCount().getValue()); + assertEquals(count, results.size()); + + for (Result<T> result : results) { + assertEquals(distinctNeighborCount, result.getDistinctNeighborCount().getValue()); + assertEquals(sharedNeighborCount, result.getSharedNeighborCount().getValue()); } } @Test - public void testRMatGraph() - throws Exception { + public void testWithCompleteGraph() throws Exception { + // all vertex pairs are linked + long expectedCount = CombinatoricsUtils.binomialCoefficient((int) completeGraphVertexCount, 2); + + // the intersection includes every vertex + long expectedDistinctNeighborCount = completeGraphVertexCount; + + // the union only excludes the two vertices from the similarity score + long expectedSharedNeighborCount = completeGraphVertexCount - 2; + + validate(completeGraph, expectedCount, expectedDistinctNeighborCount, expectedSharedNeighborCount); + } + + @Test + public void testWithEmptyGraphWithVertices() throws Exception { + validate(emptyGraphWithVertices, 0, 0, 0); + } + + @Test + public void testWithEmptyGraphWithoutVertices() throws Exception { + validate(emptyGraphWithoutVertices, 0, 0, 0); + } + + @Test + public void testWithStarGraph() throws Exception { + // all leaf vertices form a triplet with all other leaf vertices; + // only the center vertex is excluded + long expectedCount = CombinatoricsUtils.binomialCoefficient((int) starGraphVertexCount - 1, 2); + + // the intersection includes only the center vertex + long expectedDistinctNeighborCount = 1; + + // the union includes only the center vertex + long expectedSharedNeighborCount = 1; + + validate(starGraph, expectedCount, expectedDistinctNeighborCount, expectedSharedNeighborCount); + } + + @Test + public void testWithRMatGraph() throws Exception { DataSet<Result<LongValue>> ji = undirectedRMatGraph(8, 8) .run(new JaccardIndex<LongValue, NullValue, NullValue>() .setGroupSize(4));