aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala/spark/rdd/ParallelCollectionSplitSuite.scala
blob: d1276d541f7336fb530d7736bfccddf7ef417b2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 spark.rdd

import scala.collection.immutable.NumericRange

import org.scalatest.FunSuite
import org.scalatest.prop.Checkers
import org.scalacheck.Arbitrary._
import org.scalacheck.Gen
import org.scalacheck.Prop._

class ParallelCollectionSplitSuite extends FunSuite with Checkers {
  test("one element per slice") {
    val data = Array(1, 2, 3)
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices(0).mkString(",") === "1")
    assert(slices(1).mkString(",") === "2")
    assert(slices(2).mkString(",") === "3")
  }
  
  test("one slice") {
    val data = Array(1, 2, 3)
    val slices = ParallelCollectionRDD.slice(data, 1)
    assert(slices.size === 1)
    assert(slices(0).mkString(",") === "1,2,3")
  }
  
  test("equal slices") {
    val data = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices(0).mkString(",") === "1,2,3")
    assert(slices(1).mkString(",") === "4,5,6")
    assert(slices(2).mkString(",") === "7,8,9")
  }
  
  test("non-equal slices") {
    val data = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices(0).mkString(",") === "1,2,3")
    assert(slices(1).mkString(",") === "4,5,6")
    assert(slices(2).mkString(",") === "7,8,9,10")
  }

  test("splitting exclusive range") {
    val data = 0 until 100
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices(0).mkString(",") === (0 to 32).mkString(","))
    assert(slices(1).mkString(",") === (33 to 65).mkString(","))
    assert(slices(2).mkString(",") === (66 to 99).mkString(","))
  }

  test("splitting inclusive range") {
    val data = 0 to 100
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices(0).mkString(",") === (0 to 32).mkString(","))
    assert(slices(1).mkString(",") === (33 to 66).mkString(","))
    assert(slices(2).mkString(",") === (67 to 100).mkString(","))
  }
  
  test("empty data") {
    val data = new Array[Int](0)
    val slices = ParallelCollectionRDD.slice(data, 5)
    assert(slices.size === 5)
    for (slice <- slices) assert(slice.size === 0)
  }
 
  test("zero slices") {
    val data = Array(1, 2, 3)
    intercept[IllegalArgumentException] { ParallelCollectionRDD.slice(data, 0) }
  }

  test("negative number of slices") {
    val data = Array(1, 2, 3)
    intercept[IllegalArgumentException] { ParallelCollectionRDD.slice(data, -5) }
  }
  
  test("exclusive ranges sliced into ranges") {
    val data = 1 until 100
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 99)
    assert(slices.forall(_.isInstanceOf[Range]))
  }
  
  test("inclusive ranges sliced into ranges") {
    val data = 1 to 100
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 100)
    assert(slices.forall(_.isInstanceOf[Range]))
  }

  test("large ranges don't overflow") {
    val N = 100 * 1000 * 1000
    val data = 0 until N
    val slices = ParallelCollectionRDD.slice(data, 40)
    assert(slices.size === 40)
    for (i <- 0 until 40) {
      assert(slices(i).isInstanceOf[Range])
      val range = slices(i).asInstanceOf[Range]
      assert(range.start === i * (N / 40), "slice " + i + " start")
      assert(range.end   === (i+1) * (N / 40), "slice " + i + " end")
      assert(range.step  === 1, "slice " + i + " step")
    }
  }
  
  test("random array tests") {
    val gen = for {
      d <- arbitrary[List[Int]]
      n <- Gen.choose(1, 100)
    } yield (d, n)
    val prop = forAll(gen) {
      (tuple: (List[Int], Int)) =>
        val d = tuple._1
        val n = tuple._2
        val slices = ParallelCollectionRDD.slice(d, n)
        ("n slices"    |: slices.size == n) &&
        ("concat to d" |: Seq.concat(slices: _*).mkString(",") == d.mkString(",")) &&
        ("equal sizes" |: slices.map(_.size).forall(x => x==d.size/n || x==d.size/n+1))
    }
    check(prop)
  }
  
  test("random exclusive range tests") {
    val gen = for {
      a <- Gen.choose(-100, 100)
      b <- Gen.choose(-100, 100)
      step <- Gen.choose(-5, 5) suchThat (_ != 0)
      n <- Gen.choose(1, 100)
    } yield (a until b by step, n)
    val prop = forAll(gen) {
      case (d: Range, n: Int) =>
        val slices = ParallelCollectionRDD.slice(d, n)
        ("n slices"    |: slices.size == n) &&
        ("all ranges"  |: slices.forall(_.isInstanceOf[Range])) &&
        ("concat to d" |: Seq.concat(slices: _*).mkString(",") == d.mkString(",")) &&
        ("equal sizes" |: slices.map(_.size).forall(x => x==d.size/n || x==d.size/n+1))
    }
    check(prop)
  }

  test("random inclusive range tests") {
    val gen = for {
      a <- Gen.choose(-100, 100)
      b <- Gen.choose(-100, 100)
      step <- Gen.choose(-5, 5) suchThat (_ != 0)
      n <- Gen.choose(1, 100)
    } yield (a to b by step, n)
    val prop = forAll(gen) {
      case (d: Range, n: Int) =>
        val slices = ParallelCollectionRDD.slice(d, n)
        ("n slices"    |: slices.size == n) &&
        ("all ranges"  |: slices.forall(_.isInstanceOf[Range])) &&
        ("concat to d" |: Seq.concat(slices: _*).mkString(",") == d.mkString(",")) &&
        ("equal sizes" |: slices.map(_.size).forall(x => x==d.size/n || x==d.size/n+1))
    }
    check(prop)
  }
  
  test("exclusive ranges of longs") {
    val data = 1L until 100L
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 99)
    assert(slices.forall(_.isInstanceOf[NumericRange[_]]))
  }
  
  test("inclusive ranges of longs") {
    val data = 1L to 100L
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 100)
    assert(slices.forall(_.isInstanceOf[NumericRange[_]]))
  }
  
  test("exclusive ranges of doubles") {
    val data = 1.0 until 100.0 by 1.0
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 99)
    assert(slices.forall(_.isInstanceOf[NumericRange[_]]))
  }
  
  test("inclusive ranges of doubles") {
    val data = 1.0 to 100.0 by 1.0
    val slices = ParallelCollectionRDD.slice(data, 3)
    assert(slices.size === 3)
    assert(slices.map(_.size).reduceLeft(_+_) === 100)
    assert(slices.forall(_.isInstanceOf[NumericRange[_]]))
  }
}