source: icGREP/icgrep-devel/QA/abc_testgen.py @ 4120

Last change on this file since 4120 was 4094, checked in by cameron, 5 years ago

ab*c test generation and execution.

File size: 2.7 KB
Line 
1#
2# abc_testgen.py
3#
4# Generate grep test cases for ab*c pattern to segment
5# handling support (long lines, b* crossing segment boundaries).
6#
7from random import randint
8import os
9#
10#
11output_home = 'abc'
12
13#
14# Generate a line that matches ab*c with a given offset from
15# the line beginning, a given length of the match and a given
16# total line length (including new line).
17#
18def generate_line_to_match(offset, match_length, line_length):
19  return "%sa%sc%s\n" % (('_' * offset), ("b" * (match_length - 2)), ('_' * (line_length - (offset + match_length) - 1)))
20
21#
22# Generate lines that do not match ab*c of total length lgth.
23#
24def generate_non_match_lines(lgth):
25  lgth_remain = lgth
26  linedata = ""
27  while lgth_remain > 0:
28    this_line_lgth = randint(1, lgth_remain)
29    if this_line_lgth == 1: linedata += "\n"
30    else: linedata += "a%s\n" % ("b" * (this_line_lgth - 2))
31    lgth_remain -= this_line_lgth
32  return linedata
33
34def generate_case(line_pos, match_pos, match_length, line_length, extra):
35  file_name = "ab{%i}c@%i.txt" % (match_length - 2, line_pos+match_pos)
36  line_to_match = generate_line_to_match(match_pos-line_pos, match_length, line_length)
37  f1 = file(output_home + "/ExpectedOutput/" + file_name, "w")
38  f1.write(line_to_match)
39  f1.close()
40  f2 = file(output_home + "/TestFiles/" + file_name, "w")
41  f2.write(generate_non_match_lines(line_pos))
42  f2.write(line_to_match)
43  f2.write(generate_non_match_lines(extra))
44  f2.close()
45
46def main():
47  if os.path.exists(output_home + ".bak" ):
48    shutil.rmtree(output_home + ".bak")
49  if os.path.exists( output_home ):
50    shutil.move(output_home, output_home + ".bak")
51  os.mkdir(output_home)
52  os.mkdir(output_home + "/TestFiles/")
53  os.mkdir(output_home + "/ExpectedOutput/")
54
55  for block_size in [64, 128, 1024]:
56     for segment_blocks in [2,3,5,11,15,19,25,30,31,32,43]:
57        segment_base = block_size * segment_blocks
58        for segments_in_match in range(4):
59          additional_blocks = randint(0, segment_blocks - 1)
60          match_blocks = segments_in_match * segment_blocks + additional_blocks
61          match_extra = randint(0, block_size - 1)
62          match_length = match_blocks * block_size + match_extra
63          min_segment_offset = segment_base - (additional_blocks * block_size + match_extra)   
64          max_segment_offset = segment_base - match_extra
65          match_abs_pos = randint(min_segment_offset, max_segment_offset) + randint(0, 10) * segment_base
66          line_abs_pos = match_abs_pos - randint(1, match_abs_pos)
67          line_length = (match_abs_pos - line_abs_pos) + match_length + randint(1, segment_base)
68          extra = randint(0, segment_base * 4)
69          generate_case(line_abs_pos, match_abs_pos, match_length, line_length, extra)
70
71if __name__ == "__main__":
72  main()
73
74
75 
Note: See TracBrowser for help on using the repository browser.