add support for filtering tests using flags
[openpower-isa.git] / src / openpower / test / common.py
index 538609d3089b1d81cfdcbb1fa09718620868c314..f3c2962ed1864b10dae48f06a8098a69215719d8 100644 (file)
@@ -4,7 +4,7 @@ Bugreports:
 """
 
 from contextlib import contextmanager
-import inspect
+import sys
 import functools
 import types
 import os
@@ -12,7 +12,7 @@ import os
 from openpower.decoder.power_enums import XER_bits, CryIn, spr_dict
 from openpower.util import LogKind, log, \
     fast_reg_to_spr, slow_reg_to_spr  # HACK!
-from openpower.consts import XERRegsEnum
+from openpower.consts import XERRegsEnum, DEFAULT_MSR
 
 
 # TODO: make this a util routine (somewhere)
@@ -39,7 +39,7 @@ def _id(obj):
     return obj
 
 
-def skip_case(reason):
+def skip_case(reason, *, __condition=True):
     """
     Unconditionally skip a test case.
 
@@ -54,13 +54,19 @@ def skip_case(reason):
 
     For use with TestAccumulatorBase
     """
+    if not callable(__condition):
+        def __condition(ta, *, __retval=bool(__condition)):
+            return __retval
     def decorator(item):
         assert not isinstance(item, type), \
             "can't use skip_case to decorate types"
 
         @functools.wraps(item)
-        def wrapper(*args, **kwargs):
-            raise SkipCase(reason)
+        def wrapper(self, *args, **kwargs):
+            if __condition(self):
+                raise SkipCase(reason)
+            else:
+                return item(self, *args, **kwargs)
         return wrapper
     if isinstance(reason, types.FunctionType):
         item = reason
@@ -78,18 +84,53 @@ def skip_case_if(condition, reason):
         def case_abc(self):
             ...
 
+    Or:
+        # ta is the TestAccumulatorBase instance
+        @skip_case_if(lambda ta: ta.has_case_abc(), "my reason for skipping")
+        def case_abc(self):
+            ...
+
+    For use with TestAccumulatorBase
+    """
+    return skip_case(reason, __condition=condition)
+
+
+def skip_case_if_flag(flag_name):
+    """
+    Skip a test case if `flag_name in TestAccumulatorBase.flags`.
+
+    Use like:
+        @skip_if_flag("foo")
+        def case_not_on_foo(self):
+            ...
+
+    For use with TestAccumulatorBase
+    """
+    return skip_case_if(lambda ta: flag_name in ta.flags,
+                        flag_name + " is in flags")
+
+
+def skip_case_if_not_flag(flag_name):
+    """
+    Skip a test case if `flag_name not in TestAccumulatorBase.flags`.
+
+    Use like:
+        @skip_if_not_flag("foo")
+        def case_only_on_foo(self):
+            ...
+
     For use with TestAccumulatorBase
     """
-    if condition:
-        return skip_case(reason)
-    return _id
+    return skip_case_if(lambda ta: flag_name not in ta.flags,
+                        flag_name + " isn't in flags")
 
 
 class TestAccumulatorBase:
     __test__ = False  # pytest should ignore this class
 
-    def __init__(self):
+    def __init__(self, flags=()):
         self.__subtest_args = {}
+        self.flags = frozenset(flags)
 
         self.test_data = []
         # automatically identifies anything starting with "case_" and
@@ -116,18 +157,20 @@ class TestAccumulatorBase:
             self.__subtest_args = old_subtest_args
 
     def add_case(self, prog, initial_regs=None, initial_sprs=None,
-                 initial_cr=0, initial_msr=0,
+                 initial_cr=0, initial_msr=DEFAULT_MSR,
                  initial_mem=None,
                  initial_svstate=0,
                  expected=None,
                  stop_at_pc=None,
+                 fpregs=None,
+                 initial_fpscr=None,
                  src_loc_at=0):
 
+        c = sys._getframe(1 + src_loc_at).f_code
         # name of caller of this function
-        test_name = inspect.stack()[1 + src_loc_at][3]
+        test_name = c.co_name
         # name of file containing test case
-        test_file = os.path.splitext(os.path.basename(
-                                    inspect.stack()[1][1]))[0]
+        test_file = os.path.splitext(os.path.basename(c.co_filename))[0]
         tc = TestCase(prog, test_name,
                       regs=initial_regs, sprs=initial_sprs, cr=initial_cr,
                       msr=initial_msr,
@@ -136,7 +179,9 @@ class TestAccumulatorBase:
                       expected=expected,
                       stop_at_pc=stop_at_pc,
                       test_file=test_file,
-                      subtest_args=self.__subtest_args.copy())
+                      subtest_args=self.__subtest_args.copy(),
+                      fpregs=fpregs,
+                      initial_fpscr=initial_fpscr)
 
         self.test_data.append(tc)
 
@@ -152,7 +197,9 @@ class TestCase:
                  expected=None,
                  stop_at_pc=None,
                  test_file=None,
-                 subtest_args=None):
+                 subtest_args=None,
+                 fpregs=None,
+                 initial_fpscr=None):
 
         self.program = program
         self.name = name
@@ -163,7 +210,10 @@ class TestCase:
             sprs = {}
         if mem is None:
             mem = {}
+        if fpregs is None:
+            fpregs = [0] * 32
         self.regs = regs
+        self.fpregs = fpregs
         self.sprs = sprs
         self.cr = cr
         self.mem = mem
@@ -175,6 +225,9 @@ class TestCase:
         self.stop_at_pc = stop_at_pc # hard-stop address (do not attempt to run)
         self.test_file = test_file
         self.subtest_args = {} if subtest_args is None else dict(subtest_args)
+        if initial_fpscr is None:
+            initial_fpscr = 0
+        self.initial_fpscr = initial_fpscr
 
 
 class ALUHelpers: