Skip to content

Commit 34bd48f

Browse files
committed
More tests
1 parent 7f5a368 commit 34bd48f

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

tests/neg/BigFloat/BigFloat_1.scala

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import scala.util.FromDigits
2+
import scala.quoted._
3+
import scala.quoted.matching._
4+
5+
case class BigFloat(mantissa: BigInt, exponent: Int) {
6+
override def toString = s"${mantissa}e${exponent}"
7+
}
8+
9+
object BigFloat extends App {
10+
def apply(digits: String): BigFloat = {
11+
val (mantissaDigits, givenExponent) = digits.toUpperCase.split('E') match {
12+
case Array(mantissaDigits, edigits) =>
13+
val expo =
14+
try FromDigits.intFromDigits(edigits)
15+
catch {
16+
case ex: FromDigits.NumberTooLarge =>
17+
throw FromDigits.NumberTooLarge(s"exponent too large: $edigits")
18+
}
19+
(mantissaDigits, expo)
20+
case Array(mantissaDigits) =>
21+
(mantissaDigits, 0)
22+
}
23+
val (intPart, exponent) = mantissaDigits.split('.') match {
24+
case Array(intPart, decimalPart) =>
25+
(intPart ++ decimalPart, givenExponent - decimalPart.length)
26+
case Array(intPart) =>
27+
(intPart, givenExponent)
28+
}
29+
BigFloat(BigInt(intPart), exponent)
30+
}
31+
32+
private def fromDigitsImpl(digits: Expr[String]) given (ctx: QuoteContext): Expr[BigFloat] =
33+
digits match {
34+
case Const(ds) =>
35+
try {
36+
val BigFloat(m, e) = apply(ds)
37+
'{BigFloat(${m.toExpr}, ${e.toExpr})}
38+
}
39+
catch {
40+
case ex: FromDigits.FromDigitsException =>
41+
ctx.error(ex.getMessage)
42+
'{BigFloat(0, 0)}
43+
}
44+
case digits =>
45+
'{apply($digits)}
46+
}
47+
48+
class BigFloatFromDigits extends FromDigits.Floating[BigFloat] {
49+
def fromDigits(digits: String) = apply(digits)
50+
}
51+
52+
given as BigFloatFromDigits {
53+
override inline def fromDigits(digits: String) = ${
54+
fromDigitsImpl('digits)
55+
}
56+
}
57+
}
58+
59+
// Should be in StdLib:
60+
61+
given as Liftable[BigInt] {
62+
def toExpr(x: BigInt) given (qctx: QuoteContext): Expr[BigInt] =
63+
'{BigInt(${x.toString.toExpr})}
64+
}
65+
66+

tests/neg/BigFloat/Test_2.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.util.FromDigits
2+
object Test extends App {
3+
val x: BigFloat = 1234.45e3333333333 // error: exponent too large
4+
}

tests/run/BigFloat/BigFloat_1.scala

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import scala.util.FromDigits
2+
import scala.quoted._
3+
import scala.quoted.matching._
4+
5+
case class BigFloat(mantissa: BigInt, exponent: Int) {
6+
override def toString = s"${mantissa}e${exponent}"
7+
}
8+
9+
object BigFloat extends App {
10+
def apply(digits: String): BigFloat = {
11+
val (mantissaDigits, givenExponent) = digits.toUpperCase.split('E') match {
12+
case Array(mantissaDigits, edigits) =>
13+
val expo =
14+
try FromDigits.intFromDigits(edigits)
15+
catch {
16+
case ex: FromDigits.NumberTooLarge =>
17+
throw FromDigits.NumberTooLarge(s"exponent too large: $edigits")
18+
}
19+
(mantissaDigits, expo)
20+
case Array(mantissaDigits) =>
21+
(mantissaDigits, 0)
22+
}
23+
val (intPart, exponent) = mantissaDigits.split('.') match {
24+
case Array(intPart, decimalPart) =>
25+
(intPart ++ decimalPart, givenExponent - decimalPart.length)
26+
case Array(intPart) =>
27+
(intPart, givenExponent)
28+
}
29+
BigFloat(BigInt(intPart), exponent)
30+
}
31+
32+
private def fromDigitsImpl(digits: Expr[String]) given (ctx: QuoteContext): Expr[BigFloat] =
33+
digits match {
34+
case Const(ds) =>
35+
try {
36+
val BigFloat(m, e) = apply(ds)
37+
'{BigFloat(${m.toExpr}, ${e.toExpr})}
38+
}
39+
catch {
40+
case ex: FromDigits.FromDigitsException =>
41+
ctx.error(ex.getMessage)
42+
'{BigFloat(0, 0)}
43+
}
44+
case digits =>
45+
'{apply($digits)}
46+
}
47+
48+
class BigFloatFromDigits extends FromDigits.Floating[BigFloat] {
49+
def fromDigits(digits: String) = apply(digits)
50+
}
51+
52+
given as BigFloatFromDigits {
53+
override inline def fromDigits(digits: String) = ${
54+
fromDigitsImpl('digits)
55+
}
56+
}
57+
}
58+
59+
// Should be in StdLib:
60+
61+
given as Liftable[BigInt] {
62+
def toExpr(x: BigInt) given (qctx: QuoteContext): Expr[BigInt] =
63+
'{BigInt(${x.toString.toExpr})}
64+
}
65+
66+

tests/run/BigFloat/Test_2.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.util.FromDigits
2+
object Test extends App {
3+
4+
5+
println(BigFloat("1234.45"))
6+
println(BigFloat("1234.45" ++ "678"))
7+
println(BigFloat("1234.45e3"))
8+
try println(BigFloat("1234.45e3333333333"))
9+
catch {
10+
case ex: FromDigits.FromDigitsException => println("too large")
11+
}
12+
13+
def check(x: BigFloat, digits: String) =
14+
assert(x == BigFloat(digits), x)
15+
16+
check(1234.45, "1234.45")
17+
check(1234.45e3, "1234.45e3")
18+
}

tests/run/genericNumLits.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Test extends App {
3030
val N = 10
3131

3232
x match {
33-
case 13232202002020202020202 => ()
33+
case 13_232_202_002_020_202_020_202 => ()
3434
}
3535
(x: Any) match {
3636
case 13232202002020202020202: BigInt => ()
@@ -43,7 +43,7 @@ object Test extends App {
4343
case 132322020020.223 => ()
4444
}
4545
(z: Any) match {
46-
case 132322020020.223: BigDecimal => ()
46+
case 132_322_020_020.223: BigDecimal => ()
4747
}
4848

4949
e match {

0 commit comments

Comments
 (0)