X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=tests%2Fpdtest;h=158b3e03900bb895fd7fca21e360639f16a5559b;hb=fa62a8f9841955980a5a46e9aaedb62839e098dc;hp=608cd8a464012f3e6bfded10c6c1601f60ad0ca8;hpb=98457aa731ec0e7fe5a1be9f83181354253dc566;p=libsigrokdecode.git diff --git a/tests/pdtest b/tests/pdtest index 608cd8a..158b3e0 100755 --- a/tests/pdtest +++ b/tests/pdtest @@ -1,4 +1,22 @@ -#!/usr/bin/env /usr/bin/python3 +#!/usr/bin/env python3 +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2013 Bert Vermeulen +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . +## import os import sys @@ -6,6 +24,8 @@ from getopt import getopt from tempfile import mkstemp from subprocess import Popen, PIPE from difflib import Differ +from hashlib import md5 +from shutil import copy DEBUG = 0 VERBOSE = False @@ -203,7 +223,7 @@ def get_tests(testnames): return tests -def diff_files(f1, f2): +def diff_text(f1, f2): t1 = open(f1).readlines() t2 = open(f2).readlines() diff = [] @@ -215,6 +235,19 @@ def diff_files(f1, f2): return diff +def compare_binary(f1, f2): + h1 = md5() + h1.update(open(f1, 'rb').read()) + h2 = md5() + h2.update(open(f2, 'rb').read()) + if h1.digest() == h2.digest(): + result = None + else: + result = ["Binary output does not match."] + + return result + + def run_tests(tests, fix=False): errors = 0 results = [] @@ -260,19 +293,22 @@ def run_tests(tests, fix=False): # message on stderr about it results[-1]['error'] = "Unknown error: runtc %d" % p.returncode if 'error' not in results[-1]: - match = os.path.join(decoders_dir, op['pd'], 'test', op['match']) + matchfile = os.path.join(decoders_dir, op['pd'], 'test', op['match']) + DBG("Comparing with %s" % matchfile) try: diff = diff_error = None - if op['type'] == 'annotation': - diff = diff_textfiles(match, outfile) + if op['type'] in ('annotation', 'python'): + diff = diff_text(matchfile, outfile) + elif op['type'] == 'binary': + diff = compare_binary(matchfile, outfile) else: diff = ["Unsupported output type '%s'." % op['type']] except Exception as e: diff_error = e if fix: if diff or diff_error: - copy(outfile, match) - DBG("Wrote %s" % match) + copy(outfile, matchfile) + DBG("Wrote %s" % matchfile) else: if diff: results[-1]['diff'] = diff @@ -371,7 +407,7 @@ if len(sys.argv) == 1: opt_all = opt_run = opt_show = opt_list = opt_fix = False report_dir = None -opts, args = getopt(sys.argv[1:], "dvarslfRS:") +opts, args = getopt(sys.argv[1:], "dvarslfR:S:") for opt, arg in opts: if opt == '-d': DEBUG += 1 @@ -412,7 +448,7 @@ try: if not os.path.isdir(dumps_dir): ERR("Could not find sigrok-dumps repository at %s" % dumps_dir) sys.exit(1) - results, errors = run_tests(testlist) + results, errors = run_tests(testlist, fix=opt_fix) ret = errors elif opt_show: show_tests(testlist)