i2s: Update wav output (PD now emits 32bit samples).
[sigrok-test.git] / decoder / pdtest
index fa72934fc87e4197a7c7bd43d32ce2a701e600a6..e3509b967176730588183ac9c8388675c96bdb36 100755 (executable)
@@ -55,17 +55,17 @@ def ERR(msg):
 def usage(msg=None):
     if msg:
         print(msg.strip() + '\n')
-    print("""Usage: testpd [-dvarslR] [test, ...]
+    print("""Usage: testpd [-dvalsrfcR] [<test1> <test2> ...]
   -d  Turn on debugging
   -v  Verbose
   -a  All tests
-  -l  List all tests
+  -l  List test(s)
   -s  Show test(s)
   -r  Run test(s)
-  -f  Fix failed test(s)
+  -f  Fix failed test(s) / create initial output for new test(s)
   -c  Report decoder code coverage
   -R <directory>  Save test reports to <directory>
-  <test>  Protocol decoder name ("i2c") and optionally test name ("i2c/icc")""")
+  <test>  Protocol decoder name ("i2c") and optionally test name ("i2c/rtc")""")
     sys.exit()
 
 
@@ -113,6 +113,7 @@ def parse_testfile(path, pd, tc, op_type, op_class):
                     'name': f.pop(0),
                     'channels': [],
                     'options': [],
+                    'initial_pins': [],
                 }
                 while len(f):
                     if len(f) == 1:
@@ -131,6 +132,12 @@ def parse_testfile(path, pd, tc, op_type, op_class):
                         pd_spec['channels'].append([opt, val])
                     elif a == 'option':
                         pd_spec['options'].append([opt, val])
+                    elif a == 'initial_pin':
+                        try:
+                            val = int(val)
+                        except:
+                            raise E_syntax
+                        pd_spec['initial_pins'].append([opt, val])
                     else:
                         raise E_syntax
                 tclist[-1]['pdlist'].append(pd_spec)
@@ -319,6 +326,8 @@ def run_tests(tests, fix=False):
                         args.extend(['-p', "%s=%d" % (label, channel)])
                     for option, value in spd['options']:
                         args.extend(['-o', "%s=%s" % (option, value)])
+                    for label, initial_pin in spd['initial_pins']:
+                        args.extend(['-N', "%s=%d" % (label, initial_pin)])
                 args.extend(['-i', os.path.join(dumps_dir, tc['input'])])
                 for op in tc['output']:
                     name = "%s/%s/%s" % (pd, tc['name'], op['type'])
@@ -327,7 +336,7 @@ def run_tests(tests, fix=False):
                         opargs[-1] += ":%s" % op['class']
                         name += "/%s" % op['class']
                     if VERBOSE:
-                        dots = '.' * (60 - len(name) - 2)
+                        dots = '.' * (77 - len(name) - 2)
                         INFO("%s %s " % (name, dots), end='')
                     results.append({
                         'testcase': name,
@@ -383,6 +392,7 @@ def run_tests(tests, fix=False):
                         if re.match(reg, results[-1]['error']):
                             # found it, not an error
                             results[-1].pop('error')
+                            errors -= 1
                     if VERBOSE:
                         if 'diff' in results[-1]:
                             INFO("Output mismatch")
@@ -435,6 +445,17 @@ def run_tests(tests, fix=False):
 
     return results, errors
 
+def get_run_tests_error_diff_counts(results):
+    """Get error and diff counters from run_tests() results."""
+    errs = 0
+    diffs = 0
+    for result in results:
+        if 'error' in result:
+            errs += 1
+        if 'diff' in result:
+            diffs += 1
+    return errs, diffs
+
 
 def gen_report(result):
     out = []
@@ -473,7 +494,9 @@ def show_tests(tests):
                     for label, channel in pd['channels']:
                         print("    Channel %s=%d" % (label, channel))
                     for option, value in pd['options']:
-                        print("    Option %s=%d" % (option, value))
+                        print("    Option %s=%s" % (option, value))
+                    for label, initial_pin in pd['initial_pins']:
+                        print("    Initial pin %s=%d" % (label, initial_pin))
                 if 'stack' in tc:
                     print("  Stack: %s" % ' '.join(tc['stack']))
                 print("  Input: %s" % tc['input'])
@@ -512,7 +535,10 @@ if len(sys.argv) == 1:
 
 opt_all = opt_run = opt_show = opt_list = opt_fix = opt_coverage = False
 report_dir = None
-opts, args = getopt(sys.argv[1:], "dvarslfcR:S:")
+try:
+    opts, args = getopt(sys.argv[1:], "dvarslfcR:S:")
+except Exception as e:
+    usage('error while parsing command line arguments: {}'.format(e))
 for opt, arg in opts:
     if opt == '-d':
         DEBUG += 1
@@ -546,7 +572,7 @@ ret = 0
 try:
     if args:
         testlist = get_tests(args)
-    elif opt_all:
+    elif opt_all or opt_list:
         testlist = get_tests(os.listdir(tests_dir))
     else:
         usage("Specify either -a or tests.")
@@ -556,7 +582,12 @@ try:
             ERR("Could not find sigrok-dumps repository at %s" % dumps_dir)
             sys.exit(1)
         results, errors = run_tests(testlist, fix=opt_fix)
-        ret = errors
+        ret = 0
+        errs, diffs = get_run_tests_error_diff_counts(results)
+        if errs:
+            ret = 1
+        elif diffs:
+            ret = 2
     elif opt_show:
         show_tests(testlist)
     elif opt_list:
@@ -571,4 +602,3 @@ except Exception as e:
         raise
 
 sys.exit(ret)
-