Skip to content

Commit

Permalink
Merge pull request #130 from AnishShah/issue128
Browse files Browse the repository at this point in the history
Remove warnings post move to scala 2.11
  • Loading branch information
pankajgupta committed Jan 26, 2015
2 parents 11be226 + 8ba5ae4 commit 1603350
Show file tree
Hide file tree
Showing 29 changed files with 84 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ package com.twitter.cassovary.graph

import com.twitter.cassovary.graph.StoredGraphDir._
import com.twitter.cassovary.util.{Sampling, BinomialDistribution}
import java.util.concurrent.ConcurrentLinkedQueue
import scala.collection.mutable
import scala.collection.JavaConverters._
import scala.util.Random
import scala.collection.mutable.ArrayBuffer

/**
* A simple implementation of a DirectedGraph
Expand Down Expand Up @@ -155,8 +156,8 @@ object TestGraphs {
*/
def generateRandomUndirectedGraph(numNodes: Int, probEdge: Double,
graphDir: StoredGraphDir = StoredGraphDir.BothInOut) = {
val nodes = Array.fill[mutable.Buffer[Int]](numNodes){new ArrayBuffer[Int]() with mutable.SynchronizedBuffer[Int]}
def addMutualEdge(i: Int)(j: Int) {nodes(i) += j; nodes(j) += i}
val nodes = Array.fill(numNodes){new ConcurrentLinkedQueue[Int]()}
def addMutualEdge(i: Int)(j: Int) {nodes(i).add(j); nodes(j).add(i)}
val rand = new Random
val binomialDistribution = new BinomialDistribution(numNodes - 1, probEdge)
// Sampling edges only from nodes with lower id to higher id. In order to
Expand All @@ -176,7 +177,7 @@ object TestGraphs {
higherNodeNeighbors map (higherNode + _ + 1) foreach addMutualEdge(higherNode)
}
val nodesEdges = nodes.indices map { i =>
NodeIdEdgesMaxId(i, nodes(i).toArray)
NodeIdEdgesMaxId(i, nodes(i).asScala.toArray)
}
ArrayBasedDirectedGraph( () => nodesEdges.iterator, graphDir)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package com.twitter.cassovary.algorithms

import com.twitter.cassovary.graph.TestGraphs
import org.scalatest.matchers._
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.matchers.{Matcher, MatchResult}

class PageRankSpec extends WordSpec with ShouldMatchers {
class PageRankSpec extends WordSpec with Matchers {

val EPSILON = 1e-6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package com.twitter.cassovary.algorithms

import com.twitter.cassovary.graph.TestGraphs
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class TriangleCountSpec extends WordSpec with ShouldMatchers {
class TriangleCountSpec extends WordSpec with Matchers {

def averageOfPairs(f: => (Double, Double), repetitions: Int): (Double, Double) = {
val sums = (0 until repetitions).map(_ => f).reduce((p1, p2) => (p1._1 + p2._1, p1._2 + p2._2))
Expand Down Expand Up @@ -47,9 +46,9 @@ class TriangleCountSpec extends WordSpec with ShouldMatchers {
val graph = TestGraphs.generateRandomUndirectedGraph(numberOfNodes, 2.0 / numberOfNodes)
val pars = TriangleCountParameters(200, 200)
val (transitivity, triangles) = TriangleCount(graph, pars)
transitivity should be(0.0 plusOrMinus 0.05)
transitivity should be(0.0 +- 0.05)

triangles should be(0.0 plusOrMinus 20.0)
triangles should be(0.0 +- 20.0)
}


Expand All @@ -59,28 +58,28 @@ class TriangleCountSpec extends WordSpec with ShouldMatchers {
val erGraph = TestGraphs.generateRandomUndirectedGraph(numberOfNodes, edgeProbability)
val pars = TriangleCountParameters(500, 500)
val (transitivity, triangles) = averageOfPairs(TriangleCount(erGraph, pars), 10)
transitivity should be(edgeProbability plusOrMinus 0.15 * edgeProbability)
transitivity should be(edgeProbability +- (0.15 * edgeProbability))

def averageTrianglesInERGraph(nodes: Int, p: Double) = {
p * p * p * nodes * (nodes - 1) * (nodes - 2) / 6
}
val expectedTriangles = averageTrianglesInERGraph(numberOfNodes, edgeProbability)
triangles should be(expectedTriangles plusOrMinus (0.3 * expectedTriangles))
triangles should be (expectedTriangles +- (0.3 * expectedTriangles))
}

"Return correct results for complete graph" in {
val nodes = 100
val graph = TestGraphs.generateCompleteGraph(nodes)
val pars = TriangleCountParameters(1000, 1000)
val (transitivity, triangles) = averageOfPairs(TriangleCount(graph, pars), 5)
transitivity should be(1.0 plusOrMinus 0.1)
transitivity should be(1.0 +- 0.1)

def trianglesInCompleteGraph(nodes: Int): Double = {
(nodes * (nodes - 1) * (nodes - 2)) / 6
}

val expectedTriangles = trianglesInCompleteGraph(nodes)
triangles should be(expectedTriangles plusOrMinus (0.25 * expectedTriangles))
triangles should be(expectedTriangles +- (0.25 * expectedTriangles))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
package com.twitter.cassovary.graph

import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class DirectedGraphSpec extends WordSpec with ShouldMatchers {
class DirectedGraphSpec extends WordSpec with Matchers {

val twoNodeGraph = TestGraphs.g2_mutual
val sixNodeGraph = TestGraphs.g6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
package com.twitter.cassovary.graph

import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class DirectedGraphUtilsSpec extends WordSpec with ShouldMatchers {
class DirectedGraphUtilsSpec extends WordSpec with Matchers {

private def utils(graph: DirectedGraph) = {
(graph, new DirectedGraphUtils(graph))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ package com.twitter.cassovary.graph
import com.twitter.cassovary.util.FastUtilUtils
import it.unimi.dsi.fastutil.objects.Object2IntMap
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class DirectedPathCollectionSpec extends WordSpec with ShouldMatchers {
class DirectedPathCollectionSpec extends WordSpec with Matchers {


"DirectedPathCollection" when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
*/
package com.twitter.cassovary.graph

import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class DirectedPathSpec extends WordSpec with ShouldMatchers {
class DirectedPathSpec extends WordSpec with Matchers {

"path of many nodes" should {
"length, append, exists, equals" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ package com.twitter.cassovary.graph

import com.twitter.cassovary.graph.StoredGraphDir._
import com.twitter.cassovary.util.NodeNumberer
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

trait GraphBehaviours extends ShouldMatchers {
trait GraphBehaviours extends Matchers {
this: WordSpec =>

private def correctNumberOfNodesAndEdges(graph: DirectedGraph, numNodes: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import com.twitter.util.Duration
import com.twitter.util.Stopwatch
import it.unimi.dsi.fastutil.ints.Int2IntMap
import it.unimi.dsi.fastutil.objects.Object2IntMap
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

// TODO add a fake random so that the random walk tests can be controlled
class GraphUtilsSpec extends WordSpec with ShouldMatchers {
class GraphUtilsSpec extends WordSpec with Matchers {

def utils(graph: DirectedGraph) = {
(graph, new GraphUtils(graph))
Expand Down Expand Up @@ -185,7 +184,7 @@ class GraphUtilsSpec extends WordSpec with ShouldMatchers {
val walk = graphUtils.randomWalk(OutDir, Seq(startNodeId), walkParams)
val duration: Duration = elapsed()
val (visitsCounter, _) = walk
visitsCounter.infoAllNodes.size should be > (graph.getNodeById(startNodeId).get.outboundCount)
visitsCounter.infoAllNodes.size should be > graph.getNodeById(startNodeId).get.outboundCount
if (times > ignoreFirstNum) {
sumDuration += duration.inMilliseconds
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
*/
package com.twitter.cassovary.graph

import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class NodeSpec extends WordSpec with ShouldMatchers {
class NodeSpec extends WordSpec with Matchers {

def noInboundOrOutboundEdges = TestNode(1, Nil, Nil)
def onlyInboundEdges = TestNode(1, List(2), Nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package com.twitter.cassovary.graph

import com.twitter.cassovary.graph.GraphDir._
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class NodeUtilsSpec extends WordSpec with ShouldMatchers {
class NodeUtilsSpec extends WordSpec with Matchers {

def fixture = TestNode(100, List(1,2,3), List(60, 70))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
package com.twitter.cassovary.graph

import java.util.concurrent.Executors
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}
import StoredGraphDir._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.Duration

class SynchronizedDynamicGraphSpec extends WordSpec with ShouldMatchers {
class SynchronizedDynamicGraphSpec extends WordSpec with Matchers {
implicit val ecctxt = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(4))

"Add new nodes" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

package com.twitter.cassovary.graph
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class TestGraphSpec extends WordSpec with ShouldMatchers with GraphBehaviours {
class TestGraphSpec extends WordSpec with Matchers with GraphBehaviours {

"three node graph g3 stored in both directions" should {
val graph = TestGraphs.g3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
package com.twitter.cassovary.graph

import org.mockito.Mockito._
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.mock.MockitoSugar

import scala.util.Random

class TraverserSpec extends WordSpec with MockitoSugar with ShouldMatchers {
class TraverserSpec extends WordSpec with MockitoSugar with Matchers {

class TestIter extends Iterator[Int] {
var i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
package com.twitter.cassovary.graph.bipartite

import com.twitter.cassovary.graph.GraphDir
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}
import scala.collection.mutable

class BipartiteGraphSpec extends WordSpec with ShouldMatchers {
class BipartiteGraphSpec extends WordSpec with Matchers {

def bipartiteExampleSingleSide() = {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ package com.twitter.cassovary.graph.node

import NodeTestUtils._
import com.twitter.cassovary.graph.Node
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.WordSpec
import org.scalatest.{Matchers, WordSpec}

trait NodeBehaviors extends WordSpec with ShouldMatchers {
trait NodeBehaviors extends WordSpec with Matchers {
val nodeId = 100
val neighbors = Array(1,2,3)
val inEdges = Array(4,5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

package com.twitter.cassovary.graph.node

import com.twitter.cassovary.graph.StoredGraphDir._
import com.twitter.cassovary.graph.{Node, StoredGraphDir}
import com.twitter.cassovary.graph.Node
import org.scalatest.matchers.{MatchResult, Matcher}

object NodeTestUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package com.twitter.cassovary.graph.node

import com.twitter.cassovary.graph.{Node, StoredGraphDir}
import com.twitter.cassovary.graph.StoredGraphDir

class SharedArrayBasedDirectedNodeSpec extends NodeBehaviors {
val sharedArray = Array[Array[Int]](neighbors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
*/
package com.twitter.cassovary.graph.node

import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class SynchronizedDynamicNodeSpec extends WordSpec with ShouldMatchers {
class SynchronizedDynamicNodeSpec extends WordSpec with Matchers {

def fixture(id: Int) = new SynchronizedDynamicNode(id)

Expand All @@ -27,7 +26,7 @@ class SynchronizedDynamicNodeSpec extends WordSpec with ShouldMatchers {
node1.addInBoundNode(2)
val node3 = fixture(2)
node1 shouldEqual node2
node1 should not equal (node3)
node1 should not equal node3
}

"perform add/delete functions correctly" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import com.twitter.cassovary.graph.{DirectedPath, TestNode}
import com.twitter.cassovary.util.FastUtilUtils
import it.unimi.dsi.fastutil.ints.Int2IntMap
import it.unimi.dsi.fastutil.objects.Object2IntMap
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{Matchers, WordSpec}

class NodeTouristSpec extends WordSpec with ShouldMatchers {
class NodeTouristSpec extends WordSpec with Matchers {

def testNode(id: Int) = TestNode(id, Nil, Nil)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ package com.twitter.cassovary.util

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{PrivateMethodTester, WordSpec}
import org.scalatest.{Matchers, PrivateMethodTester, WordSpec}
import scala.util.Random

@RunWith(classOf[JUnitRunner])
class BinomialDistributionSpec extends WordSpec with ShouldMatchers with PrivateMethodTester {
class BinomialDistributionSpec extends WordSpec with Matchers with PrivateMethodTester {
val twoPoint = new BinomialDistribution(1, 0.3)

val twoTrials = new BinomialDistribution(2, 0.8)
Expand All @@ -45,12 +44,12 @@ class BinomialDistributionSpec extends WordSpec with ShouldMatchers with Private
"binomial distribution with 2 trials" should {
val expected = Array(0.2 * 0.2, 2 * 0.2 * 0.8, 0.8 * 0.8)
"have correct pdf" in {
(twoTrials invokePrivate pdf()).zip(expected).foreach{case (x, y) => x should be (y plusOrMinus 0.001)}
(twoTrials invokePrivate pdf()).zip(expected).foreach{case (x, y) => x should be (y +- 0.001)}
}

"have correct cdf" in {
val expected = Array(0.2 * 0.2, 0.2 * 0.2 + 2 * 0.2 * 0.8)
(twoTrials invokePrivate cdf()).zip(expected).foreach{case (x, y) => x should be (y plusOrMinus 0.001)}
(twoTrials invokePrivate cdf()).zip(expected).foreach{case (x, y) => x should be (y +- 0.001)}
}
}

Expand Down Expand Up @@ -80,7 +79,7 @@ class BinomialDistributionSpec extends WordSpec with ShouldMatchers with Private
}

"have pdf that sums up to 1" in {
(distribution10 invokePrivate pdf()).sum should be (1.0 plusOrMinus 0.005)
(distribution10 invokePrivate pdf()).sum should be (1.0 +- 0.005)
}

"have increasing cdf" in {
Expand All @@ -100,7 +99,7 @@ class BinomialDistributionSpec extends WordSpec with ShouldMatchers with Private
val error = (distribution10 invokePrivate pdf())(i) - histogram.getOrElse(i, 0.0)
mse += error * error
}
mse should be (0.0 plusOrMinus 0.01) // checked experimentally to be true with high probability
mse should be (0.0 +- 0.01) // checked experimentally to be true with high probability
}
}
}
Loading

0 comments on commit 1603350

Please sign in to comment.