Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1025#discussion_r14694380
  
    --- Diff: 
core/src/test/scala/org/apache/spark/rdd/PairRDDFunctionsSuite.scala ---
    @@ -83,6 +83,125 @@ class PairRDDFunctionsSuite extends FunSuite with 
SharedSparkContext {
         assert(valuesFor2.toList.sorted === List(1))
       }
     
    +  test("sampleByKey") {
    +    def stratifier (fractionPositive: Double) = {
    +      (x: Int) => if (x % 10 < (10 * fractionPositive).toInt) "1" else "0"
    +    }
    +
    +    def checkSize(exact: Boolean,
    +        withReplacement: Boolean,
    +        expected: Long,
    +        actual: Long,
    +        p: Double): Boolean = {
    +      if (exact) {
    +        return expected == actual
    +      }
    +      val stdev = if (withReplacement) math.sqrt(expected) else 
math.sqrt(expected * p * (1 - p))
    +      // Very forgiving margin since we're dealing with very small sample 
sizes most of the time
    +      math.abs(actual - expected) <= 6 * stdev
    +    }
    +
    +    // Without replacement validation
    +    def takeSampleAndValidateBernoulli(stratifiedData: RDD[(String, Int)],
    +        exact: Boolean,
    +        samplingRate: Double,
    +        seed: Long,
    +        n: Long) = {
    +      val expectedSampleSize = stratifiedData.countByKey().mapValues(count 
=>
    +        math.ceil(count * samplingRate).toInt)
    +      val fractions = Map("1" -> samplingRate, "0" -> samplingRate)
    +      val sample = stratifiedData.sampleByKey(false, fractions, exact, 
seed)
    +      val sampleCounts = sample.countByKey()
    +      val takeSample = sample.collect()
    +      assert(sampleCounts.forall {case(k,v) =>
    --- End diff --
    
    Put spaces after `{` and `,`.
    
    Better put assert inside foreach so we can output more info if something 
goes wrong.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to