Skip to content

Commit f5ffe62

Browse files
committed
Add tests for NativeMath + code clean up
1 parent 4a7ba4d commit f5ffe62

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

jsoniter-scala-core/native/src/main/resources/scala-native/multiply_high.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ unsigned long long jsoniter_scala_unsigned_multiply_high(unsigned long long x, u
1515
#elif defined(__SIZEOF_INT128__)
1616

1717
long long jsoniter_scala_multiply_high(long long x, long long y) {
18-
return x * (unsigned __int128) y >> 64;
18+
return x * (__int128) y >> 64;
1919
}
2020

2121
unsigned long long jsoniter_scala_unsigned_multiply_high(unsigned long long x, unsigned long long y) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.plokhotnyuk.jsoniter_scala.core
2+
3+
import org.scalacheck.Arbitrary.arbitrary
4+
import org.scalatest.matchers.should.Matchers
5+
import org.scalatest.wordspec.AnyWordSpec
6+
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
7+
8+
class NativeMathSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyChecks {
9+
"NativeMath" should {
10+
"have multiplyHigh consistent with Math.multiplyHigh" in {
11+
def check(x: Long, y: Long): Unit = NativeMath.multiplyHigh(x, y) shouldBe Math.multiplyHigh(x, y)
12+
13+
check(Long.MaxValue, Long.MaxValue)
14+
check(Long.MinValue, Long.MaxValue)
15+
check(Long.MaxValue, Long.MinValue)
16+
check(Long.MinValue, Long.MinValue)
17+
forAll(arbitrary[Long], arbitrary[Long], minSuccessful(100000))(check)
18+
}
19+
"have unsignedMultiplyHigh consistent with Math.unsignedMultiplyHigh" in {
20+
def check(x: Long, y: Long): Unit = NativeMath.unsignedMultiplyHigh(x, y) shouldBe unsignedMultiplyHigh(x, y)
21+
22+
check(Long.MaxValue, Long.MaxValue)
23+
check(Long.MinValue, Long.MaxValue)
24+
check(Long.MaxValue, Long.MinValue)
25+
check(Long.MinValue, Long.MinValue)
26+
forAll(arbitrary[Long], arbitrary[Long], minSuccessful(100000))(check)
27+
}
28+
}
29+
30+
def unsignedMultiplyHigh(x: Long, y: Long): Long = { // FIXME: replace by Math.unsignedMultiplyHigh after dropping JDK 17 support
31+
var r = Math.multiplyHigh(x, y)
32+
r += y & (x >> 63)
33+
r += x & (y >> 63)
34+
r
35+
}
36+
}

0 commit comments

Comments
 (0)