Skip to content

Commit

Permalink
Merge pull request #835 from kadzuya/fix_arbitrary_char
Browse files Browse the repository at this point in the history
Fix Arbitrary[Char] generates 0xFFFE (not a character in Unicode)
  • Loading branch information
ashawley authored Oct 7, 2021
2 parents c00729b + 902121e commit 6ba8dbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

package org.scalacheck

import java.util.concurrent.TimeUnit

import Prop._
import Arbitrary._
import java.util.concurrent.TimeUnit

object ArbitrarySpecification extends Properties("Arbitrary") {
val genOptionUnits =
Expand All @@ -23,6 +24,11 @@ object ArbitrarySpecification extends Properties("Arbitrary") {
property("arbOption coverage") =
exists(genOptionUnits) { case (a, b) => a.isDefined != b.isDefined }

property("arbChar") =
Prop.forAll { (c: Char) =>
0x0000 <= c && c <= 0xD7FF || 0xE000 <= c && c <= 0xFFFD
}

property("arbString") =
Prop.forAll { (s: String) =>
s ne new String(s)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/org/scalacheck/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ private[scalacheck] sealed trait ArbitraryLowPriority {
implicit lazy val arbChar: Arbitrary[Char] = Arbitrary {
// valid ranges are [0x0000, 0xD7FF] and [0xE000, 0xFFFD].
//
// ((0xFFFD + 1) - 0xE000) + ((0xD7FF + 1) - 0x0000)
choose(0, 63486).map { i =>
// ((0xFFFD + 1) - 0xE000) + ((0xD7FF + 1) - 0x0000) - 1
choose(0, 63485).map { i =>
if (i <= 0xD7FF) i.toChar
else (i + 2048).toChar
}
Expand Down

0 comments on commit 6ba8dbd

Please sign in to comment.