1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
"""
test some jython internals
"""
import gc
import unittest
import time
from test.test_support import run_suite
import java
import jarray
from org.python.core import Py
from org.python.util import PythonInterpreter
from javatests.TestSupport import getField
from java.sql import Date, Time, Timestamp
import datetime
class MemoryLeakTests(unittest.TestCase):
def test_class_to_test_weakness(self):
# regrtest for bug 1522, adapted from test code submitted by Matt Brinkley
# work around the fact that we can't look at PyType directly
# by using this helper function that reflects on PyType (and
# demonstrates here that it's the same as the builtin function
# `type`!)
class_to_type_map = getField(type, 'class_to_type').get(None)
def make_clean():
# gc a few times just to be really sure, since in this
# case we don't really care if it takes a few cycles of GC
# for the garbage to be reached
gc.collect()
time.sleep(0.1)
gc.collect()
time.sleep(0.5)
gc.collect()
def create_proxies():
pi = PythonInterpreter()
pi.exec("""
from java.lang import Comparable
class Dog(Comparable):
def compareTo(self, o):
return 0
def bark(self):
return 'woof woof'
Dog().bark()
""")
make_clean()
# get to steady state first, then verify we don't create new proxies
for i in xrange(2):
create_proxies()
start_size = class_to_type_map.size()
for i in xrange(5):
create_proxies()
make_clean()
self.assertEqual(start_size, class_to_type_map.size())
class WeakIdentityMapTests(unittest.TestCase):
def test_functionality(self):
from org.python.core import IdImpl
i = java.lang.Integer(2)
j = java.lang.Integer(2)
# sanity check
assert i == j and i is not j
h = java.lang.Integer(2)
widmap = IdImpl.WeakIdentityMap()
widmap.put(i,'i')
widmap.put(j,'j')
widmap.put(h,'h')
assert widmap.get(i) == 'i'
assert widmap.get(j) == 'j'
assert widmap.get(h) == 'h'
# white box double-check
assert widmap._internal_map_size() == 3
widmap.remove(h)
assert widmap.get(h) is None
# white box double-check
assert widmap._internal_map_size() == 2
# white box test for weak referencing of keys
del j
java.lang.System.gc()
java.lang.System.runFinalization()
java.lang.System.gc()
java.lang.System.runFinalization()
time.sleep(1)
assert widmap.get(i) == 'i' # triggers stale weak refs cleanup
assert widmap._internal_map_size() == 1
class LongAsScaledDoubleValueTests(unittest.TestCase):
def setUp(self):
self.v = v = 2**53-1 # full mag bits of a double value
self.v8 = v * 8 # fills up 7 bytes
self.e = jarray.zeros(1,'i')
iarr = java.lang.Object.getClass(self.e)
sdv = java.lang.Class.getMethod(long, 'scaledDoubleValue', [iarr])
import org.python.core.PyReflectedFunction as ReflFunc
self.sdv = ReflFunc([sdv])
def test_basic_roundtrip(self):
e = self.e
sdv = self.sdv
assert long(sdv(0L, e)) == 0
assert e[0] == 0
assert long(sdv(1L, e)) == 1
assert e[0] == 0
assert long(sdv(-1L, e)) == -1
assert e[0] == 0
assert long(sdv(self.v, e)) == self.v
assert e[0] == 0
def test_scale_3_v(self):
e = self.e
v = self.v8
sdv = self.sdv
assert long(sdv(v, e)) == v
assert e[0] == 0
assert long(sdv(v+1, e)) - v == 0
assert e[0] == 0
def test_no_worse_than_doubleValue(self):
e = self.e
v = self.v8
sdv = self.sdv
for d in range(8):
assert float(v+d) == sdv(v+d, e)
assert e[0] == 0
for d in range(8):
for y in [0,255]:
assert float((v+d)*256+y) == sdv((v+d)*256+y, e)
assert e[0] == 0
for d in range(8):
for y in [0,255]:
assert float((v+d)*256+y) == sdv(((v+d)*256+y)*256, e)
assert e[0] == 1
class ExtraMathTests(unittest.TestCase):
def test_epsilon(self):
from org.python.core.util import ExtraMath
self.assertNotEqual(1.0 + ExtraMath.EPSILON, 1.0)
self.assertEqual(1.0 + (ExtraMath.EPSILON/2.0), 1.0)
def test_close(self):
from org.python.core.util import ExtraMath
self.assert_(ExtraMath.close(3.0, 3.0))
self.assert_(ExtraMath.close(3.0, 3.0 + ExtraMath.CLOSE))
self.assert_(not ExtraMath.close(3.0, 3.0 + 4.0*ExtraMath.CLOSE))
def test_closeFloor(self):
from org.python.core.util import ExtraMath
import math
self.assertEquals(ExtraMath.closeFloor(3.5), 3.0)
self.assertEquals(ExtraMath.closeFloor(3.0 - ExtraMath.EPSILON), 3.0)
self.assertEquals(
ExtraMath.closeFloor(3.0 - 3.0 * ExtraMath.CLOSE), 2.0)
self.assertEquals(ExtraMath.closeFloor(math.log10(10**3)), 3.0)
class DatetimeTypeMappingTest(unittest.TestCase):
def test_date(self):
self.assertEquals(datetime.date(2008, 5, 29),
Py.newDate(Date(108, 4, 29)))
self.assertEquals(datetime.date(1999, 1, 1),
Py.newDate(Date(99, 0, 1)))
def test_time(self):
self.assertEquals(datetime.time(0, 0, 0),
Py.newTime(Time(0, 0, 0)))
self.assertEquals(datetime.time(23, 59, 59),
Py.newTime(Time(23, 59, 59)))
def test_datetime(self):
self.assertEquals(datetime.datetime(2008, 1, 1),
Py.newDatetime(Timestamp(108, 0, 1, 0, 0, 0, 0)))
self.assertEquals(datetime.datetime(2008, 5, 29, 16, 50, 0),
Py.newDatetime(Timestamp(108, 4, 29, 16, 50, 0, 0)))
self.assertEquals(datetime.datetime(2008, 5, 29, 16, 50, 1, 1),
Py.newDatetime(Timestamp(108, 4, 29, 16, 50, 1, 1000)))
class IdTest(unittest.TestCase):
def test_unique_ids(self):
d = {}
cnt = 0
for i in xrange(100000):
s = "test" + repr(i)
j = id(s)
if d.has_key(j):
cnt = cnt + 1
d[j] = s
self.assertEquals(cnt, 0)
class FrameTest(unittest.TestCase):
def test_stack_frame_locals(self):
def h():
a = 1
b = 2
raise AttributeError("spam")
try:
h()
except:
import sys
tb = sys.exc_info()[2]
while tb.tb_next is not None:
tb = tb.tb_next
vars = tb.tb_frame.f_locals
self.assertEquals(sorted(vars.items()), [('a',1), ('b',2)])
def test_frame_info(self):
import sys
from types import ClassType
def getinfo():
""" Returns a tuple consisting of:
the name of the current module
the name of the current class or None
the name of the current function
the current line number
"""
try:
1/0
except:
tb = sys.exc_info()[-1]
frame = tb.tb_frame.f_back
modulename = frame.f_globals['__name__']
funcname = frame.f_code.co_name
lineno = frame.f_lineno
if len(frame.f_code.co_varnames) == 0:
classname = None
else:
self = frame.f_locals[frame.f_code.co_varnames[0]]
myclass = self.__class__
if type(myclass) == ClassType:
classname = myclass.__name__
else:
classname = None
return modulename, classname, funcname, lineno
def foo():
x = 99
g = getinfo()
assert (g[0] == "__main__" or g[0] == "test.test_jy_internals")
self.assertEquals(g[1], None)
self.assertEquals(g[2], "foo")
class Bar:
def baz(self):
g = getinfo()
assert (g[0] == "__main__" or g[0] == "test.test_jy_internals")
assert (g[1] == "Bar")
assert (g[2] == "baz")
g = getinfo()
assert (g[0] == "__main__" or g[0] == "test.test_jy_internals")
self.assertEquals(g[1], None)
self.assertEquals(g[2], "test_frame_info")
foo()
Bar().baz()
class ModuleTest(unittest.TestCase):
def test_create_module(self):
from org.python.core import PyModule, PyInstance
test = PyModule("test", {})
exec "a = 2" in test.__dict__
self.assertEquals(len(test.__dict__), 3)
#test = PyInstance.__tojava__(test, PyModule)
exec "b = 3" in test.__dict__
self.assertEquals(len(test.__dict__), 4)
def test_main():
test_suite = unittest.TestSuite()
test_loader = unittest.TestLoader()
def suite_add(case):
test_suite.addTest(test_loader.loadTestsFromTestCase(case))
suite_add(WeakIdentityMapTests)
suite_add(LongAsScaledDoubleValueTests)
suite_add(ExtraMathTests)
suite_add(DatetimeTypeMappingTest)
suite_add(IdTest)
suite_add(FrameTest)
suite_add(ModuleTest)
suite_add(MemoryLeakTests)
run_suite(test_suite)
if __name__ == "__main__":
test_main()
|