[pypy-svn] r26961 - pypy/dist/pypy/translator/c/test
pedronis at codespeak.net
pedronis at codespeak.net
Mon May 8 11:34:41 CEST 2006
Author: pedronis
Date: Mon May 8 11:34:39 2006
New Revision: 26961
Modified:
pypy/dist/pypy/translator/c/test/test_stackless.py
pypy/dist/pypy/translator/c/test/test_tasklets.py
Log:
run the tasklets tests with the transform too. avoid some code repetition in the tests.
Modified: pypy/dist/pypy/translator/c/test/test_stackless.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_stackless.py (original)
+++ pypy/dist/pypy/translator/c/test/test_stackless.py Mon May 8 11:34:39 2006
@@ -1,4 +1,5 @@
from pypy.translator.translator import TranslationContext
+from pypy.translator.backendopt.all import backend_optimizations
from pypy.translator.c.genc import CStandaloneBuilder
from pypy.translator.c import gc
from pypy.annotation.model import SomeList, SomeString
@@ -8,18 +9,22 @@
import os
-# ____________________________________________________________
-
-
-class TestStackless(object):
- gcpolicy = None # Refcounting
+class StacklessTest(object):
+ backendopt = False
def setup_class(cls):
- # to re-enable this, remove the two characters 'gc' in the
- # declaregcptrtype(rstack.frame_stack_top,...) call in
- # rpython/extfunctable. Doing so breaks translator/stackless/.
import py
- py.test.skip("stackless + refcounting doesn't work any more for now")
+ if cls.gcpolicy is None:
+ # to re-enable this, remove the two characters 'gc' in the
+ # declaregcptrtype(rstack.frame_stack_top,...) call in
+ # rpython/extfunctable. Doing so breaks translator/stackless/.
+ import py
+ py.test.skip("stackless + refcounting doesn't work any more for now")
+ else:
+ assert cls.gcpolicy is gc.BoehmGcPolicy
+ from pypy.translator.tool.cbuild import check_boehm_presence
+ if not check_boehm_presence():
+ py.test.skip("Boehm GC not present")
def wrap_stackless_function(self, fn):
def entry_point(argv):
@@ -31,6 +36,8 @@
t = TranslationContext()
t.buildannotator().build_types(entry_point, [s_list_of_strings])
t.buildrtyper().specialize()
+ if self.backendopt:
+ backend_optimizations(t)
from pypy.translator.transform import insert_ll_stackcheck
insert_ll_stackcheck(t)
@@ -43,6 +50,11 @@
res = cbuilder.cmdexec('')
return int(res.strip())
+# ____________________________________________________________
+
+
+class TestStackless(StacklessTest):
+ gcpolicy = None # Refcounting
def test_stack_depth(self):
def g1():
@@ -290,11 +302,6 @@
class TestStacklessBoehm(TestStackless):
gcpolicy = gc.BoehmGcPolicy
- def setup_class(cls):
- import py
- from pypy.translator.tool.cbuild import check_boehm_presence
- if not check_boehm_presence():
- py.test.skip("Boehm GC not present")
# ____________________________________________________________
Modified: pypy/dist/pypy/translator/c/test/test_tasklets.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_tasklets.py (original)
+++ pypy/dist/pypy/translator/c/test/test_tasklets.py Mon May 8 11:34:39 2006
@@ -8,16 +8,8 @@
# For testing
from pypy.translator.tool import cbuild
-from pypy.translator.c.gc import BoehmGcPolicy
-
-gcpolicy = None
-if cbuild.check_boehm_presence():
- gcpolicy = BoehmGcPolicy
-else:
- # to re-enable this, remove the two characters 'gc' in the
- # declaregcptrtype(rstack.frame_stack_top,...) call in
- # rpython/extfunctable. Doing so breaks translator/stackless/.
- py.test.skip("stackless + refcounting doesn't work any more for now")
+from pypy.translator.c import gc
+from pypy.translator.c.test import test_stackless
# count of loops in tests (set lower to speed up)
loops = 1
@@ -32,34 +24,6 @@
globals = Globals()
globals.count = 0
-def wrap_stackless_function(fn):
- # ensure we have no SomeObject s
- from pypy.annotation.policy import AnnotatorPolicy
- annotatorpolicy = AnnotatorPolicy()
- annotatorpolicy.allow_someobjects = False
-
- from pypy.translator.translator import TranslationContext
- from pypy.translator.c.genc import CStandaloneBuilder
- from pypy.annotation.model import SomeList, SomeString
- from pypy.annotation.listdef import ListDef
- from pypy.translator.backendopt.all import backend_optimizations
-
- def entry_point(argv):
- os.write(1, str(fn()))
- return 0
-
- s_list_of_strings = SomeList(ListDef(None, SomeString()))
- s_list_of_strings.listdef.resize()
- t = TranslationContext()
- t.buildannotator(annotatorpolicy).build_types(entry_point, [s_list_of_strings])
- t.buildrtyper().specialize()
- backend_optimizations(t)
- cbuilder = CStandaloneBuilder(t, entry_point, gcpolicy=gcpolicy)
- cbuilder.stackless = True
- cbuilder.generate_source()
- cbuilder.compile()
- return cbuilder.cmdexec('')
-
# ____________________________________________________________
class ThreadLocals(object):
@@ -230,301 +194,315 @@
# ____________________________________________________________
-def test_simplex():
-
- class Tasklet1(Tasklet):
- def fn(self):
- for ii in range(5):
- globals.count += 1
- schedule()
+class TestTasklets(test_stackless.StacklessTest):
+ gcpolicy = gc.BoehmGcPolicy
+ backendopt = True
+
+ def test_simplex(self):
+
+ class Tasklet1(Tasklet):
+ def fn(self):
+ for ii in range(5):
+ globals.count += 1
+ schedule()
- def f():
- for ii in range(loops):
- Tasklet1().start()
- run()
- return globals.count == loops * 5
+ def f():
+ for ii in range(loops):
+ Tasklet1().start()
+ run()
+ return globals.count == loops * 5
+
+ res = self.wrap_stackless_function(f)
+ print res
+ assert res == 1
+
+ def test_multiple_simple(self):
+
+ class Tasklet1(Tasklet):
+ def fn(self):
+ for ii in range(5):
+ globals.count += 1
+ schedule()
- res = wrap_stackless_function(f)
- print res
- assert res == '1'
+ class Tasklet2(Tasklet):
+ def fn(self):
+ for ii in range(5):
+ globals.count += 1
+ schedule()
+ globals.count += 1
-def test_multiple_simple():
-
- class Tasklet1(Tasklet):
- def fn(self):
- for ii in range(5):
- globals.count += 1
+ class Tasklet3(Tasklet):
+ def fn(self):
schedule()
-
- class Tasklet2(Tasklet):
- def fn(self):
- for ii in range(5):
- globals.count += 1
+ for ii in range(10):
+ globals.count += 1
+ if ii % 2:
+ schedule()
schedule()
- globals.count += 1
- class Tasklet3(Tasklet):
- def fn(self):
- schedule()
- for ii in range(10):
- globals.count += 1
- if ii % 2:
+ def f():
+ for ii in range(loops):
+ Tasklet1().start()
+ Tasklet2().start()
+ Tasklet3().start()
+ run()
+ return globals.count == loops * 25
+
+ res = self.wrap_stackless_function(f)
+ assert res == 1
+
+ def test_schedule_remove(self):
+
+ class Tasklet1(Tasklet):
+ def fn(self):
+ for ii in range(20):
+ if ii < 10:
+ schedule()
+ else:
+ schedule_remove()
+ globals.count += 1
+
+ def f():
+ for ii in range(loops):
+ Tasklet1().start()
+ run()
+ for ii in range(loops):
+ Tasklet1().start()
+ run()
+ return globals.count == loops * 10 * 2
+
+ res = self.wrap_stackless_function(f)
+ assert res == 1
+
+ def test_run_immediately(self):
+ globals.intermediate = 0
+ class Tasklet1(Tasklet):
+ def fn(self):
+ for ii in range(20):
+ globals.count += 1
schedule()
- schedule()
-
- def f():
- for ii in range(loops):
- Tasklet1().start()
- Tasklet2().start()
- Tasklet3().start()
- run()
- return globals.count == loops * 25
-
- res = wrap_stackless_function(f)
- assert res == '1'
-def test_schedule_remove():
+ class RunImmediate(Tasklet):
+ def fn(self):
+ globals.intermediate = globals.count
+ schedule()
- class Tasklet1(Tasklet):
- def fn(self):
- for ii in range(20):
- if ii < 10:
+ class Tasklet2(Tasklet):
+ def fn(self):
+ for ii in range(20):
+ globals.count += 1
+ if ii == 10:
+ RunImmediate().run()
schedule()
- else:
- schedule_remove()
- globals.count += 1
- def f():
- for ii in range(loops):
- Tasklet1().start()
- run()
- for ii in range(loops):
- Tasklet1().start()
- run()
- return globals.count == loops * 10 * 2
+ def f():
+ Tasklet2().start()
+ for ii in range(loops):
+ Tasklet1().start()
+ run()
+ total_expected = (loops + 1) * 20
+ return (globals.intermediate == total_expected / 2 + 1 and
+ globals.count == total_expected)
- res = wrap_stackless_function(f)
- assert res == '1'
+ res = self.wrap_stackless_function(f)
+ assert res == 1
-def test_run_immediately():
- globals.intermediate = 0
- class Tasklet1(Tasklet):
- def fn(self):
- for ii in range(20):
- globals.count += 1
- schedule()
+ def test_channel1(self):
+ ch = Channel()
- class RunImmediate(Tasklet):
- def fn(self):
- globals.intermediate = globals.count
- schedule()
-
- class Tasklet2(Tasklet):
- def fn(self):
- for ii in range(20):
- globals.count += 1
- if ii == 10:
- RunImmediate().run()
- schedule()
+ class Tasklet1(Tasklet):
+ def fn(self):
+ for ii in range(5):
+ ch.send(ii)
+
+ class Tasklet2(Tasklet):
+ def fn(self):
+ #while True: XXX Doesnt annotate
+ for ii in range(6):
+ globals.count += ch.receive()
- def f():
- Tasklet2().start()
- for ii in range(loops):
+ def f():
+ Tasklet2().start()
Tasklet1().start()
- run()
- total_expected = (loops + 1) * 20
- return (globals.intermediate == total_expected / 2 + 1 and
- globals.count == total_expected)
+ run()
+ return (globals.count == 10)
- res = wrap_stackless_function(f)
- assert res == '1'
+ res = self.wrap_stackless_function(f)
+ assert res == 1
-def test_channel1():
- ch = Channel()
-
- class Tasklet1(Tasklet):
- def fn(self):
- for ii in range(5):
- ch.send(ii)
-
- class Tasklet2(Tasklet):
- def fn(self):
- #while True: XXX Doesnt annotate
- for ii in range(6):
- globals.count += ch.receive()
-
- def f():
- Tasklet2().start()
- Tasklet1().start()
- run()
- return (globals.count == 10)
+ def test_channel2(self):
- res = wrap_stackless_function(f)
- assert res == '1'
+ class Tasklet1(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ def fn(self):
+ for ii in range(5):
+ self.ch.send(ii)
+
+ class Tasklet2(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ def fn(self):
+ #while True:XXX Doesnt annotate
+ for ii in range(6):
+ res = self.ch.receive()
+ globals.count += res
+
+ def f():
+ ch = Channel()
+ Tasklet1(ch).start()
+ Tasklet2(ch).start()
+ run()
+ return globals.count == 10
+
+ res = self.wrap_stackless_function(f)
+ assert res == 1
+
+ def test_channel3(self):
+
+ class Tasklet1(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ def fn(self):
+ for ii in range(5):
+ self.ch.send(ii)
+
+ class Tasklet2(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ def fn(self):
+ #while True: XXX Doesnt annotate
+ for ii in range(16):
+ res = self.ch.receive()
+ globals.count += res
+
+ def f():
+ ch = Channel()
+ Tasklet1(ch).start()
+ Tasklet1(ch).start()
+ Tasklet1(ch).start()
+ Tasklet2(ch).start()
+ run()
+ return globals.count == 30
+
+ res = self.wrap_stackless_function(f)
+ assert res == 1
+
+ def test_flexible_channels(self):
+ """ test with something other than int """
+
+ class A(object):
+ def __init__(self, num):
+ self.num = num
+ def getvalue(self):
+ res = self.num
+ self.num *= 2
+ return res
+
+ class Data(object):
+ pass
+
+ class IntData(Data):
+ def __init__(self, i):
+ self.int = i
+
+ class StringData(Data):
+ def __init__(self, s):
+ self.str = s
+
+ class InstanceData(Data):
+ def __init__(self, i):
+ self.instance = i
+
+
+ class Tasklet1(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ def fn(self):
+ for ii in range(5):
+ self.ch.send(IntData(ii))
+
+ class Tasklet2(Tasklet):
+ def __init__(self, ch, strdata):
+ self.ch = ch
+ self.strdata = strdata
+ def fn(self):
+ for ii in range(5):
+ self.ch.send(StringData(self.strdata))
+
+ class Tasklet3(Tasklet):
+ def __init__(self, ch, instance):
+ self.ch = ch
+ self.instance = instance
+ def fn(self):
+ for ii in range(5):
+ self.ch.send(InstanceData(self.instance))
+
+ class Server(Tasklet):
+ def __init__(self, ch):
+ self.ch = ch
+ self.loop = True
+
+ def stop(self):
+ self.loop = False
+
+ def fn(self):
+ while self.loop:
+ data = self.ch.receive()
+ if isinstance(data, IntData):
+ globals.count += data.int
+ elif isinstance(data, StringData):
+ globals.count += len(data.str)
+ elif isinstance(data, InstanceData):
+ globals.count += data.instance.getvalue()
-def test_channel2():
-
- class Tasklet1(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- def fn(self):
- for ii in range(5):
- self.ch.send(ii)
-
- class Tasklet2(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- def fn(self):
- #while True:XXX Doesnt annotate
- for ii in range(6):
- res = self.ch.receive()
- globals.count += res
-
- def f():
ch = Channel()
- Tasklet1(ch).start()
- Tasklet2(ch).start()
- run()
- return globals.count == 10
+ server = Server(ch)
- res = wrap_stackless_function(f)
- assert res == '1'
+ def f():
+ Tasklet1(ch).start()
+ Tasklet2(ch, "abcd").start()
+ Tasklet2(ch, "xxx").start()
+ Tasklet3(ch, A(1)).start()
+ server.start()
+ run()
+ return globals.count == (0+1+2+3+4) + (5*4) + (5*3) + (1+2+4+8+16)
+
+ res = self.wrap_stackless_function(f)
+ assert res == 1
+
+ def test_original_api(self):
+
+ class TaskletAsFunction(Tasklet):
+ def __init__(self, fn):
+ self.redirect_fn = fn
+ def fn(self):
+ self.redirect_fn()
-def test_channel3():
-
- class Tasklet1(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- def fn(self):
- for ii in range(5):
- self.ch.send(ii)
-
- class Tasklet2(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- def fn(self):
- #while True: XXX Doesnt annotate
- for ii in range(16):
- res = self.ch.receive()
- globals.count += res
-
- def f():
- ch = Channel()
- Tasklet1(ch).start()
- Tasklet1(ch).start()
- Tasklet1(ch).start()
- Tasklet2(ch).start()
- run()
- return globals.count == 30
-
- res = wrap_stackless_function(f)
- assert res == '1'
-
-def test_flexible_channels():
- """ test with something other than int """
-
- class A(object):
- def __init__(self, num):
- self.num = num
- def getvalue(self):
- res = self.num
- self.num *= 2
- return res
-
- class Data(object):
- pass
-
- class IntData(Data):
- def __init__(self, i):
- self.int = i
-
- class StringData(Data):
- def __init__(self, s):
- self.str = s
-
- class InstanceData(Data):
- def __init__(self, i):
- self.instance = i
-
-
- class Tasklet1(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- def fn(self):
- for ii in range(5):
- self.ch.send(IntData(ii))
+ def tasklet(fn):
+ return TaskletAsFunction(fn)
- class Tasklet2(Tasklet):
- def __init__(self, ch, strdata):
- self.ch = ch
- self.strdata = strdata
- def fn(self):
+ def simple():
for ii in range(5):
- self.ch.send(StringData(self.strdata))
+ globals.count += 1
+ schedule()
- class Tasklet3(Tasklet):
- def __init__(self, ch, instance):
- self.ch = ch
- self.instance = instance
- def fn(self):
- for ii in range(5):
- self.ch.send(InstanceData(self.instance))
-
- class Server(Tasklet):
- def __init__(self, ch):
- self.ch = ch
- self.loop = True
-
- def stop(self):
- self.loop = False
+ def f():
+ for ii in range(loops):
+ tasklet(simple).start()
+ run()
+ run()
+ return globals.count == loops * 5
- def fn(self):
- while self.loop:
- data = self.ch.receive()
- if isinstance(data, IntData):
- globals.count += data.int
- elif isinstance(data, StringData):
- globals.count += len(data.str)
- elif isinstance(data, InstanceData):
- globals.count += data.instance.getvalue()
-
- ch = Channel()
- server = Server(ch)
+ res = self.wrap_stackless_function(f)
+ assert res == 1
- def f():
- Tasklet1(ch).start()
- Tasklet2(ch, "abcd").start()
- Tasklet2(ch, "xxx").start()
- Tasklet3(ch, A(1)).start()
- server.start()
- run()
- return globals.count == (0+1+2+3+4) + (5*4) + (5*3) + (1+2+4+8+16)
-
- res = wrap_stackless_function(f)
- assert res == '1'
-
-def test_original_api():
-
- class TaskletAsFunction(Tasklet):
- def __init__(self, fn):
- self.redirect_fn = fn
- def fn(self):
- self.redirect_fn()
+# ____________________________________________________________
- def tasklet(fn):
- return TaskletAsFunction(fn)
-
- def simple():
- for ii in range(5):
- globals.count += 1
- schedule()
-
- def f():
- for ii in range(loops):
- tasklet(simple).start()
- run()
- run()
- return globals.count == loops * 5
+class TestTaskletsStacklessTransform(TestTasklets):
- res = wrap_stackless_function(f)
- assert res == '1'
+ def wrap_stackless_function(self, fn):
+ # temporary way of doing this
+ #import py; py.test.skip("in-progress")
+ from pypy.translator.stackless.test import test_transform
+ return test_transform.run_stackless_function(fn)
More information about the Pypy-commit
mailing list