Author: Richard Plangger <[email protected]>
Branch: gcstress-hypothesis
Changeset: r82768:5ae35a1cd368
Date: 2016-03-03 10:52 +0100
http://bitbucket.org/pypy/pypy/changeset/5ae35a1cd368/

Log:    creating a list of byte codes using hypothesis. the state along each
        instruction is passed using shared

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -76,3 +76,4 @@
 ^.git/
 ^release/
 ^rpython/_cache$
+^.hypothesis/
diff --git a/rpython/jit/backend/llsupport/tl/interp.py 
b/rpython/jit/backend/llsupport/tl/interp.py
--- a/rpython/jit/backend/llsupport/tl/interp.py
+++ b/rpython/jit/backend/llsupport/tl/interp.py
@@ -15,6 +15,10 @@
         assert isinstance(w_lst, W_ListObject)
         return space.wrap(self.items + w_lst.items)
 
+    def is_of_type(self, type):
+        """ NOT_RPYTHON """
+        return type in (LIST_TYP,)
+
 class W_IntObject(W_Root):
     def __init__(self, value):
         self.value = value
@@ -26,6 +30,12 @@
     def concat(self, space, w_obj):
         raise NotImplementedError("cannot concat int with object")
 
+    def is_of_type(self, type):
+        """ NOT_RPYTHON """
+        return type in (code.INT_TYP,code.IDX_TYP,
+                        code.COND_TYP,code.SHORT_TYP,
+                        code.BYTE_TYP)
+
 class W_StrObject(W_Root):
     def __init__(self, value):
         self.value = value
@@ -34,6 +44,10 @@
         assert isinstance(w_str, W_StrObject)
         return space.wrap(self.value + w_str.value)
 
+    def is_of_type(self, type):
+        """ NOT_RPYTHON """
+        return type in (code.STR_TYP,)
+
 class Space(object):
     @specialize.argtype(1)
     def wrap(self, val):
diff --git a/rpython/jit/backend/llsupport/tl/stack.py 
b/rpython/jit/backend/llsupport/tl/stack.py
--- a/rpython/jit/backend/llsupport/tl/stack.py
+++ b/rpython/jit/backend/llsupport/tl/stack.py
@@ -3,6 +3,14 @@
 class Stack(object):
     _virtualizable_ = ['stackpos', 'stack[*]']
 
+    @staticmethod
+    def from_items(space, elems):
+        s = Stack(len(elems))
+        for elem in elems:
+            s.append(space.wrap(elem))
+        return s
+
+
     def __init__(self, size):
         self = hint(self, access_directly=True, fresh_virtualizable=True)
         self.stack = [None] * size
diff --git a/rpython/jit/backend/llsupport/tl/test/code_strategies.py 
b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
--- a/rpython/jit/backend/llsupport/tl/test/code_strategies.py
+++ b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
@@ -28,21 +28,33 @@
 
 STD_SPACE = interp.Space()
 
-@composite
-def runtime_stack(draw, clazz):
-    strats = [get_strategy_for(t) for t in clazz._stack_types]
-    stack_obj = stack.Stack(len(strats))
-    for i,strat in enumerate(strats):
-        if clazz._stack_types[i] == IDX_TYP:
-            # it is only valid to access a list with a valid index!
-            w_list = stack_obj.peek(i-1)
-            l = len(w_list.items)
-            assume(l > 0)
-            integrals = st.integers(min_value=0, max_value=l-1)
-            stack_obj.append(STD_SPACE.wrap(draw(integrals)))
-            continue
-        stack_obj.append(STD_SPACE.wrap(draw(strat)))
-    return stack_obj
+#@composite
+#def runtime_stack(draw, clazz):
+#    strats = [get_strategy_for(t) for t in clazz._stack_types]
+#    stack_obj = stack.Stack(len(strats))
+#    for i,strat in enumerate(strats):
+#        if clazz._stack_types[i] == IDX_TYP:
+#            # it is only valid to access a list with a valid index!
+#            w_list = stack_obj.peek(i-1)
+#            l = len(w_list.items)
+#            assume(l > 0)
+#            integrals = st.integers(min_value=0, max_value=l-1)
+#            stack_obj.append(STD_SPACE.wrap(draw(integrals)))
+#            continue
+#        stack_obj.append(STD_SPACE.wrap(draw(strat)))
+#    return stack_obj
+
+@defines_strategy
+def stack_entry(types=all_types):
+    return st.sampled_from([get_strategy_for(t) for t in types])
+
+@defines_strategy
+def runtime_stack(min_size=0, average_size=5, max_size=4096,
+          types=all_types):
+    stack_entries = st.lists(stack_entry(all_types), min_size,
+                             average_size, max_size)
+    return stack_entries.map(lambda elems: \
+                                stack.Stack.from_items(STD_SPACE, elems))
 
 def byte_code_classes():
     for name, clazz in code.__dict__.items():
@@ -55,21 +67,30 @@
             return clazz
     return None
 
+
+@defines_strategy
+def bytecode_class(stack):
+    def filter_using_stack(bytecode_class):
+        required_types = bytecode_class.requires_stack
+        if len(required_types) < stack.size():
+            return False
+        j = len(required_types)-1
+        for i in range(stack.size()):
+            item = stack.peek(i)
+            if not item.is_of_type(required_types[j]):
+                return False
+            j -= 1
+            if j < 0:
+                break
+        return True
+    return st.sampled_from(byte_code_classes()).filter(filter_using_stack)
+
 @composite
-def single_bytecode(draw,
-        clazzes=st.sampled_from(byte_code_classes()),
-        integrals=st.integers(), texts=st.text()):
-    clazz = draw(clazzes)
+def bytecode(draw, max_stack_size=4096):
+    # get a stack that is the same for one test run
+    rs = runtime_stack(max_size=max_stack_size)
+    stack = draw(st.shared(rs, 'stack'))
+    clazz = draw(bytecode_class(stack))
     inst = clazz.create_from(draw, get_strategy_for)
     bytecode, consts = code.Context().transform([inst])
-    _stack = draw(runtime_stack(clazz))
-    return bytecode, consts, _stack
-
-@composite
-def bytecode_block(draw,
-        clazzes=st.sampled_from(byte_code_classes()),
-        integrals=st.integers(), texts=st.text()):
-    clazz = draw(clazzes)
-    inst = clazz.create_from(draw, get_strategy_for)
-    bytecode, consts = code.Context().transform([inst])
-    return bytecode, consts
+    return bytecode, consts, stack
diff --git a/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py 
b/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py
--- a/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py
+++ b/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py
@@ -1,5 +1,6 @@
 import py
 from hypothesis import given
+from hypothesis.strategies import lists
 from rpython.jit.backend.llsupport.tl import code, interp
 from rpython.jit.backend.llsupport.tl.stack import Stack
 from rpython.jit.backend.llsupport.tl.test import code_strategies as st
@@ -22,7 +23,7 @@
         assert c.get_short(3) == 1
 
 class TestInterp(object):
-    @given(st.single_bytecode())
+    @given(st.bytecode())
     def test_consume_stack(self, args):
         bytecode, consts, stack = args
         space = interp.Space()
@@ -31,9 +32,9 @@
         clazz = st.get_byte_code_class(ord(bytecode[0]))
         assert stack.size() == len(clazz._return_on_stack_types)
 
-    @given(st.bytecode_block())
+    @given(lists(st.bytecode(max_stack_size=0)))
     def test_execute_bytecode_block(self, args):
-        bytecode, consts = args
+        bytecode, consts, _ = args
         space = interp.Space()
         stack = Stack(16)
         pc = 0
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to