-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
compat/src/main/scala-2.11_2.12/scala/collection/compat/RandomExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.collection.compat | ||
|
||
import scala.util.Random | ||
|
||
final class RandomExtensions(private val self: Random) extends AnyVal { | ||
def nextLong(n: Long): Long = { | ||
require(n > 0, "n must be positive") | ||
|
||
var offset = 0L | ||
var _n = n | ||
|
||
while (_n >= Integer.MAX_VALUE) { | ||
val bits = self.nextInt(2) | ||
val halfn = _n >>> 1 | ||
val nextn = | ||
if ((bits & 2) == 0) halfn | ||
else _n - halfn | ||
if ((bits & 1) == 0) | ||
offset += _n - nextn | ||
_n = nextn | ||
} | ||
offset + self.nextInt(_n.toInt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package test.scala.util | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
import scala.collection.compat._ | ||
import scala.util.Random | ||
import test.scala.collection.AssertThrown | ||
|
||
class RandomTest extends AssertThrown { | ||
@Test | ||
def nextLong(): Unit = { | ||
val rand = new Random(12345) | ||
|
||
assertEquals(4896762128577075113L, rand.nextLong(Long.MaxValue)) | ||
assertEquals(2005076556L, rand.nextLong(Int.MaxValue)) | ||
assertEquals(0L, rand.nextLong(1L)) | ||
|
||
assertThrows[IllegalArgumentException](rand.nextLong(0L)) | ||
assertThrows[IllegalArgumentException](rand.nextLong(-2L)) | ||
assertThrows[IllegalArgumentException](rand.nextLong(Long.MinValue)) | ||
} | ||
} |