source: icGREP/icgrep-devel/QA/character_deletion/character_deletion_test.py @ 5864

Last change on this file since 5864 was 5848, checked in by xwa163, 21 months ago
  1. Fix bug of SwizzledDeleteByPEXTKernel
  2. Add character_deletion pipeline and character_deletion_test to make sure SwizzledDeleteByPEXTKernel related logic work correctly
File size: 4.8 KB
Line 
1import sys, subprocess, optparse, os, string, random
2import codecs
3import xml.etree.ElementTree as ET
4
5global options
6global character_deletion_test_file_name
7global character_deletion_program_under_test
8failure_count = 0
9
10def generateRandomTestFile(name, length):
11    content = generate_random_str(length)
12    generate_test_file(name, content)
13
14
15def make_data_files(deletion_test_xml):
16    tree = ET.parse(deletion_test_xml)
17    root = tree.getroot()
18    for child in root:
19        if child.tag == 'testcase':
20            if "name" in child.attrib:
21                generate_test_file(child.attrib["name"], child.text)
22        elif child.tag == 'random-testcase':
23            if "length" in child.attrib and "name" in child.attrib:
24                generateRandomTestFile(child.attrib["name"], int(child.attrib["length"]))
25
26
27def run_test(test_name, random_deletion, character_to_be_deleted):
28    global options
29    global failure_count
30
31    test_file = os.path.join(options.datafile_dir, test_name)
32    inf = codecs.open(test_file, encoding='utf-8', mode='r')
33    content = inf.read()
34    inf.close()
35
36    target_character = None
37    if random_deletion:
38        target_character = random.choice(string.ascii_letters)
39    else:
40        target_character = character_to_be_deleted
41
42    expected_output = content.replace(target_character, "")
43
44    character_deletion_cmd = "%s %s %s" % (character_deletion_program_under_test, target_character, test_file)
45    if options.verbose:
46        print("Doing: " + character_deletion_cmd)
47    try:
48        deletion_out = subprocess.check_output(character_deletion_cmd.encode('utf-8'), cwd=options.exec_dir, shell=True)
49    except subprocess.CalledProcessError as e:
50        deletion_out = e.output
51
52    if expected_output != deletion_out:
53        print("Test failure: datafile {%s}, deletion character {%s}" % (test_name, target_character))
54        failure_count += 1
55    else:
56        if options.verbose:
57            print("Test success: datafile {%s}, deletion character {%s}" % (test_name, target_character))
58
59
60def run_tests(base64test_xml):
61    tree = ET.parse(base64test_xml)
62    root = tree.getroot()
63    for child in root:
64
65        if "name" in child.attrib:
66            test_name = child.attrib["name"]
67            random_deletion = True
68            character_to_be_deleted = None
69            if 'random-deletion' in child.attrib:
70                random_deletion = child.attrib['random-deletion'] == 'true'
71            if 'character' in child.attrib:
72                character_to_be_deleted = child.attrib['character']
73            run_test(test_name, random_deletion, character_to_be_deleted)
74
75    if failure_count > 0:
76        exit(1)
77
78
79def generate_test_file(name, content):
80    if not content or len(content) == 0:
81        return
82    outfpath = os.path.join(options.datafile_dir, name)
83    outf = codecs.open(outfpath, encoding='utf-8', mode='w')
84    outf.write(content)
85    outf.close()
86
87
88def generate_random_str(length):
89    outputStr = ""
90    for i in range(length):
91        # outputStr += random.choice(string.printable)
92        outputStr += random.choice(string.ascii_letters + "\n")
93    return outputStr
94
95
96if __name__ == '__main__':
97    global options
98    global character_deletion_test_file_name
99    global character_deletion_program_under_test
100
101    QA_dir = os.path.dirname(sys.argv[0])
102    option_parser = optparse.OptionParser(usage='python %prog [options] <character_deletion_executable>', version='1.0')
103    option_parser.add_option('-d', '--datafile_dir',
104                             dest='datafile_dir', type='string', default='testfiles',
105                             help='directory for test files.')
106    option_parser.add_option('-t', '--testcases',
107                             dest='testcases', type='string', default='character_deletion_test.xml',
108                             help='character_deletion test case file (XML format).')
109    option_parser.add_option('-e', '--exec_dir',
110                             dest='exec_dir', type='string', default='.',
111                             help='executable directory')
112    option_parser.add_option('-v', '--verbose',
113                             dest='verbose', action='store_true', default=False,
114                             help='verbose output: show successful tests')
115    options, args = option_parser.parse_args(sys.argv[1:])
116
117    if len(args) != 1:
118        option_parser.print_usage()
119        sys.exit(1)
120
121    if not os.path.exists(options.datafile_dir):
122        os.mkdir(options.datafile_dir)
123
124    if not os.path.isdir(options.datafile_dir):
125        print("Cannot use %s as working test file directory.\n" % options.datafile_dir)
126        sys.exit(1)
127
128    character_deletion_program_under_test = args[0]
129    character_deletion_test_file_name = os.path.join(QA_dir, options.testcases)
130    make_data_files(character_deletion_test_file_name)
131    run_tests(character_deletion_test_file_name)
Note: See TracBrowser for help on using the repository browser.