Skip to content

Commit

Permalink
[tests] floating point almost equal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marscher committed Nov 19, 2024
1 parent 100065a commit f4d524f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 4 additions & 2 deletions test/jpypetest/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ def assertElementsEqual(self, a, b):
for i in range(len(a)):
self.assertEqual(a[i], b[i])

def assertElementsAlmostEqual(self, a, b):
def assertElementsAlmostEqual(self, a, b, places=None, msg=None,
delta=None):
self.assertEqual(len(a), len(b))
for i in range(len(a)):
self.assertAlmostEqual(a[i], b[i])
self.assertAlmostEqual(a[i], b[i], places, msg, delta)

def useEqualityFunc(self, func):
return UseFunc(self, func, 'assertEqual')
Expand All @@ -153,4 +154,5 @@ def java_version():
"import jpype; jpype.startJVM(); "
"print(jpype.java.lang.System.getProperty('java.version'))"]),
encoding='ascii')
# todo: make this robust for version "numbers" containing strings (e.g.) 22.1-internal
return tuple(map(int, java_version.split(".")))
8 changes: 4 additions & 4 deletions test/jpypetest/test_jdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# See NOTICE file for details.
#
# *****************************************************************************
import sys
from unittest.util import safe_repr

import jpype
import common
import random
import _jpype
import jpype
from jpype import java
from jpype.types import *
try:
Expand Down Expand Up @@ -49,8 +49,8 @@ def compareDoubleEqual(self, x, y, msg=None):
b = -b
if b < a * 1e-14:
return
msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),
safe_repr(second)))
msg = self._formatMessage(msg, '%s == %s' % (safe_repr(x),
safe_repr(y)))
raise self.failureException(msg)

@common.requireInstrumentation
Expand Down
13 changes: 7 additions & 6 deletions test/jpypetest/test_jfloat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
# See NOTICE file for details.
#
# *****************************************************************************
import sys
import jpype
import common
import random
from unittest.util import safe_repr

import _jpype

import common
import jpype
from jpype import java
from jpype.types import *
Expand Down Expand Up @@ -49,8 +50,8 @@ def compareFloatEqual(self, x, y, msg=None):
b = -b
if b < a * 1e-7:
return
msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),
safe_repr(second)))
msg = self._formatMessage(msg, '%s == %s' % (safe_repr(x),
safe_repr(y)))
raise self.failureException(msg)

@common.requireInstrumentation
Expand Down Expand Up @@ -385,7 +386,7 @@ def testArraySetFromNPDouble(self):
def testArrayInitFromNPFloat16(self):
a = np.random.random(100).astype(np.float16)
jarr = JArray(JFloat)(a)
self.assertElementsAlmostEqual(a, jarr)
self.assertElementsAlmostEqual(a, jarr, places=5)

@common.requireNumpy
def testArrayInitFromNPFloat32(self):
Expand Down

0 comments on commit f4d524f

Please sign in to comment.