Ignore:
Timestamp:
Feb 19, 2018, 6:24:03 PM (19 months ago)
Author:
xwa163
Message:

Use random test file generator for character_deposit testing

Location:
icGREP/icgrep-devel/QA/character_deposit
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/QA/character_deposit/character_deposit_test.py

    r5873 r5875  
    33import codecs
    44import shutil
     5import xml.etree.ElementTree as ET
     6import testfile_generator
    57
    68global options
     
    911failure_count = 0
    1012
    11 def run_tests(test_dir, output_dir, python_output_dir):
     13def generate_test_file(name, content):
     14    if not content or len(content) == 0:
     15        return
     16    outfpath = os.path.join(options.datafile_dir, name)
     17    outf = codecs.open(outfpath, encoding='utf-8', mode='w')
     18    outf.write(content)
     19    outf.close()
     20
     21
     22def generateRandomTestFile(name, length, target_character):
     23    content = testfile_generator.generate_text(length, target_character)
     24    generate_test_file(name, content)
     25
     26
     27def make_data_files(deposit_test_xml):
     28    tree = ET.parse(deposit_test_xml)
     29    root = tree.getroot()
     30    for child in root:
     31        if child.tag == 'random-testcase':
     32            if "length" in child.attrib and "name" in child.attrib and "target-character" in child.attrib:
     33                generateRandomTestFile(child.attrib["name"], int(child.attrib["length"]), child.attrib['target-character'])
     34
     35
     36def run_tests(testfile_name, test_dir, output_dir, python_output_dir):
    1237    global options
    1338    global failure_count
    1439
    15     test_files = os.listdir(test_dir)
    16     for test_file in test_files:
    17         if not test_file.endswith('.txt'):
    18             continue
    19         output_file = "out_" + test_file
     40    tree = ET.parse(testfile_name)
     41    root = tree.getroot()
     42    for child in root:
     43        if child.tag == 'random-testcase':
     44            if "length" in child.attrib and "name" in child.attrib and "target-character" in child.attrib:
     45                test_file = child.attrib["name"]
     46                target_character = child.attrib['target-character']
    2047
    21         test_file_full_path = os.path.join(test_dir, test_file)
    22         output_file_full_path = os.path.join(output_dir, output_file)
    23         python_output_file_full_path = os.path.join(python_output_dir, output_file)
     48                output_file = "out_" + test_file
    2449
    25         character_deposit_cmd = "%s b -buffer-segments=2 %s %s" % (lz4d_program_under_test, test_file_full_path, output_file_full_path)
     50                test_file_full_path = os.path.join(test_dir, test_file)
     51                output_file_full_path = os.path.join(output_dir, output_file)
     52                python_output_file_full_path = os.path.join(python_output_dir, output_file)
    2653
    27         if options.verbose:
    28             print("Doing: " + character_deposit_cmd)
     54                character_deposit_cmd = "%s b -buffer-segments=2 %s %s" % (
     55                lz4d_program_under_test, test_file_full_path, output_file_full_path)
    2956
    30         try:
    31             character_deposit_out = subprocess.check_output(character_deposit_cmd.encode('utf-8'), cwd=options.exec_dir, shell=True)
    32         except subprocess.CalledProcessError as e:
    33             character_deposit_out = e.output
     57                if options.verbose:
     58                    print("Doing: " + character_deposit_cmd)
    3459
    35         character_deposit.handle_deposit(test_file_full_path, python_output_file_full_path, 'b')
     60                try:
     61                    character_deposit_out = subprocess.check_output(character_deposit_cmd.encode('utf-8'),
     62                                                                    cwd=options.exec_dir, shell=True)
     63                except subprocess.CalledProcessError as e:
     64                    character_deposit_out = e.output
    3665
    37         with codecs.open(output_file_full_path, mode='rb') as inf:
    38             output_content = inf.read()
    39         with codecs.open(python_output_file_full_path, mode='rb') as inf:
    40             expected_output_content = inf.read()
     66                character_deposit.handle_deposit(test_file_full_path, python_output_file_full_path, 'b')
    4167
    42         if output_content != expected_output_content:
    43             print("Test failure: datafile {%s}" % test_file)
    44             failure_count += 1
    45         else:
    46             if options.verbose:
    47                 print("Test success: datafile {%s}" % test_file)
     68                with codecs.open(output_file_full_path, mode='rb') as inf:
     69                    output_content = inf.read()
     70                with codecs.open(python_output_file_full_path, mode='rb') as inf:
     71                    expected_output_content = inf.read()
     72
     73                if output_content != expected_output_content:
     74                    print("Test failure: datafile {%s}" % test_file)
     75                    failure_count += 1
     76                else:
     77                    if options.verbose:
     78                        print("Test success: datafile {%s}" % test_file)
    4879
    4980    if failure_count > 0:
     
    6192                             dest='datafile_dir', type='string', default='testfiles',
    6293                             help='directory for test files.')
     94    option_parser.add_option('-t', '--testcases',
     95                             dest='testcases', type='string', default='character_deposit_test.xml',
     96                             help='character_deposit_test test case file (XML format).')
    6397    option_parser.add_option('-e', '--exec_dir',
    6498                             dest='exec_dir', type='string', default='.',
     
    87121    os.mkdir(options.output_dir)
    88122
    89 
    90123    if os.path.exists(options.python_output_dir):
    91124        shutil.rmtree(options.python_output_dir)
    92125    os.mkdir(options.python_output_dir)
    93126
     127    test_dir = os.path.join(QA_dir, options.datafile_dir)
     128    if os.path.exists(options.datafile_dir):
     129        shutil.rmtree(options.datafile_dir)
     130    os.mkdir(options.datafile_dir)
     131
    94132    lz4d_program_under_test = args[0]
    95133
    96     test_dir = os.path.join(QA_dir, options.datafile_dir)
     134    character_deposit_test_file_name = os.path.join(QA_dir, options.testcases)
     135    make_data_files(character_deposit_test_file_name)
     136
    97137    output_dir = os.path.join(QA_dir, options.output_dir)
    98138    python_output_dir = os.path.join(QA_dir, options.python_output_dir)
    99     run_tests(test_dir, output_dir, python_output_dir)
     139    run_tests(character_deposit_test_file_name, test_dir, output_dir, python_output_dir)
    100140
  • icGREP/icgrep-devel/QA/character_deposit/testfile_generator.py

    r5873 r5875  
    33
    44
    5 def generate_text(num_of_group):
     5def generate_text(num_of_group, target_character):
     6    string_set = string.ascii_letters.replace(target_character, '')
     7
    68    output_str = ''
    79    for i in range(num_of_group):
    810        group_size = random.randrange(1, 7)
    911        for j in range(group_size):
    10             output_str += random.choice(string.ascii_uppercase)
     12            output_str += random.choice(string_set)
    1113        for j in range(group_size):
    12             output_str += 'b'
     14            output_str += target_character
    1315
    1416    return output_str
    1517
    16 
    1718if __name__ == '__main__':
    18     t = generate_text(20000)
     19    t = generate_text(20000, 'b')
    1920    print(t)
Note: See TracChangeset for help on using the changeset viewer.