Author: Carl Friedrich Bolz <cfb...@gmx.de>
Branch: hypothesis-apptest
Changeset: r85126:0485618dc84d
Date: 2016-06-13 15:44 +0200
http://bitbucket.org/pypy/pypy/changeset/0485618dc84d/

Log:    a hypothesis strategy for randomizing the results of we_are_jitted()

diff --git a/rpython/rlib/jit.py b/rpython/rlib/jit.py
--- a/rpython/rlib/jit.py
+++ b/rpython/rlib/jit.py
@@ -325,12 +325,42 @@
         hop.exception_cannot_occur()
         return hop.genop('hint', [v, c_hint], resulttype=v.concretetype)
 
-
 def we_are_jitted():
     """ Considered as true during tracing and blackholing,
     so its consquences are reflected into jitted code """
+    global _hypothesis_data
+    if _hypothesis_data is not None:
+        if _hypothesis_data.data.frozen:
+            # outside of test, reset
+            _hypothesis_data = None
+        else:
+            import hypothesis.strategies as strategies
+            return _hypothesis_data.draw(strategies.booleans())
     return False
 
+def _randomize_we_are_jitted_from(data):
+    global _hypothesis_data
+    _hypothesis_data = data
+    return None
+
+def randomized_we_are_jitted_strategy():
+    """ a Hypothesis strategy to test functions that rely on we_are_jitted().
+    At runtime, we_are_jitted() can either return True of False, in a somewhat
+    hard to predict way. The functionality of the interpreter should never
+    really depend on the value of the returned bool. To test this, hypothesis
+    can be used with this strategy, in the following way:
+
+    @given(randomized_we_are_jitted_strategy())
+    def test_something(_ignored):
+        # in here, we_are_jitted() randomly returns True or False.
+        # the test is run many times to try to make it fail.
+
+    (the implementation, however, is a bit of a hack).
+    """
+    from hypothesis import strategies
+    return strategies.builds(_randomize_we_are_jitted_from, strategies.data())
+
+
 _we_are_jitted = CDefinedIntSymbolic('0 /* we are not jitted here */',
                                      default=0)
 
diff --git a/rpython/rlib/test/test_jit.py b/rpython/rlib/test/test_jit.py
--- a/rpython/rlib/test/test_jit.py
+++ b/rpython/rlib/test/test_jit.py
@@ -5,7 +5,7 @@
 from rpython.rlib.jit import (hint, we_are_jitted, JitDriver, elidable_promote,
     JitHintError, oopspec, isconstant, conditional_call,
     elidable, unroll_safe, dont_look_inside,
-    enter_portal_frame, leave_portal_frame)
+    enter_portal_frame, leave_portal_frame, randomized_we_are_jitted_strategy)
 from rpython.rlib.rarithmetic import r_uint
 from rpython.rtyper.test.tool import BaseRtypingTest
 from rpython.rtyper.lltypesystem import lltype
@@ -115,6 +115,32 @@
         def f():
             pass
 
+def test_randomized_we_are_jitted():
+    from hypothesis import given
+    def good():
+        if we_are_jitted():
+            return 1
+        return 1
+    counter = [0]
+    def bad():
+        if we_are_jitted():
+            return 2
+        return 1
+    @given(randomized_we_are_jitted_strategy())
+    def test_random_good(_):
+        assert good() == 1
+    test_random_good()
+
+    @given(randomized_we_are_jitted_strategy())
+    def test_random_bad(_):
+        assert bad() == 1
+    try:
+        test_random_bad()
+    except AssertionError:
+        pass
+    else:
+        assert 0, "should have failed!"
+
 class TestJIT(BaseRtypingTest):
     def test_hint(self):
         def f():
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to