This commit is contained in:
Claudio Maggioni 2023-12-09 12:43:16 +01:00
parent 521e317a39
commit abc54f2324
4 changed files with 23 additions and 319 deletions

View file

@ -198,14 +198,11 @@ def get_test_case_source(f_name: str, test_case: Params, i: int, indent: int):
output = invoke(f_name, test_case)
if type(output) == str:
output = f"'{output}'"
return f"""{space}def test_{f_name_orig}_{i}(self):
{space}{single_indent}assert {call_statement(f_name_orig, test_case)} == {output}"""
{space}{single_indent}assert {call_statement(f_name_orig, test_case)} == {repr(output)}"""
def get_test_class(f_name: str, case: set[Params]) -> str:
def get_test_class(f_name: str, cases: set[Params]) -> str:
f_name_orig = BranchTransformer.to_original_name(f_name)
test_class = (f"from unittest import TestCase\n\nfrom {module_of[f_name]} import {f_name_orig}\n\n\n"

View file

@ -14,7 +14,7 @@ CXPROB = 0.5
TOURNSIZE = 3
NPOP = 300
NGEN = 200
REPS = 10
REPS = 1
to_test: str = ""
@ -40,14 +40,8 @@ def generate(f_name: str):
toolbox = base.Toolbox()
toolbox.register("attr_test_case", get_test_case_generator(to_test, instrument.functions[to_test]))
toolbox.register("individual",
tools.initIterate,
creator.Individual,
lambda: toolbox.attr_test_case())
toolbox.register("population",
tools.initRepeat,
list,
toolbox.individual)
toolbox.register("individual", tools.initIterate, creator.Individual, lambda: toolbox.attr_test_case())
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", compute_fitness)
def mate(tc1, tc2):
@ -77,6 +71,8 @@ def generate(f_name: str):
coverage.append(cov)
print(coverage)
return set(list(instrument.archive_true_branches.values()) +
list(instrument.archive_false_branches.values()))
def compute_fitness(individual: list) -> tuple[float]:
@ -87,12 +83,17 @@ def compute_fitness(individual: list) -> tuple[float]:
instrument.distances_true = {}
instrument.distances_false = {}
# the archive_true_branches and archive_false_branches are reset after
# each generation. This is intentional as they are used to archive branches that
# have already been covered, and their presence increases the fitness value of
# test cases that would re-cover them
# Run the function under test
try:
out = instrument.invoke(to_test, x)
except AssertionError:
print(to_test, x, "=", "[FAILS] fitness = 10000")
return 10000,
print(to_test, x, "=", "[FAILS] fitness = 100.0")
return 100.0,
fitness = 0.0
@ -103,13 +104,14 @@ def compute_fitness(individual: list) -> tuple[float]:
instrument.archive_true_branches[branch] = x
if branch not in instrument.archive_true_branches:
fitness += normalize(instrument.distances_true[branch])
for branch in range(range_start, range_end):
if branch in instrument.distances_false:
elif branch in instrument.distances_false:
if instrument.distances_false[branch] == 0 and branch not in instrument.archive_false_branches:
instrument.archive_false_branches[branch] = x
if branch not in instrument.archive_false_branches:
fitness += normalize(instrument.distances_false[branch])
else:
fitness += 1.0
print(to_test, x, "=", out, "fitness =", fitness)
return fitness,
@ -117,9 +119,8 @@ def compute_fitness(individual: list) -> tuple[float]:
def main():
instrument.load_benchmark(save_instrumented=False) # instrument all files in benchmark
f_name = "railencrypt_instrumented"
generate(f_name)
cases = generate(f_name)
with open(os.path.join(OUT_DIR, f_name + ".py"), "w") as f:
cases = get_test_cases(f_name, instrument.functions[f_name], 100)
f.write(get_test_class(f_name, cases))

View file

@ -211,10 +211,7 @@ def load_benchmark(save_instrumented=True):
def call_statement(f_name: str, f_args: Params) -> str:
arg_list: list[str] = []
for k, v in f_args.items():
if type(v) == str:
arg_list.append(f"{k}='{v}'") # quote strings
else:
arg_list.append(f"{k}={v}")
arg_list.append(f"{k}={repr(v)}") # quote strings
return f"{f_name}({', '.join(arg_list)})"

View file

@ -5,301 +5,10 @@ from benchmark.railfence_cipher import railencrypt
class Test_railencrypt(TestCase):
def test_railencrypt_1(self):
assert railencrypt(st='FKglwgJ(p', k=840) == 'FKglwgJ(p'
assert railencrypt(st='6u#%', k=3) == '6u%#'
def test_railencrypt_2(self):
assert railencrypt(st='gvj', k=379) == 'gvj'
assert railencrypt(st='6uD*z(', k=3) == '6zu*(D'
def test_railencrypt_3(self):
assert railencrypt(st='', k=755) == ''
def test_railencrypt_4(self):
assert railencrypt(st='9pS', k=577) == '9pS'
def test_railencrypt_5(self):
assert railencrypt(st='`j4z_:S', k=169) == '`j4z_:S'
def test_railencrypt_6(self):
assert railencrypt(st='? }qW^W', k=892) == '? }qW^W'
def test_railencrypt_7(self):
assert railencrypt(st='ns(ob!Pb', k=389) == 'ns(ob!Pb'
def test_railencrypt_8(self):
assert railencrypt(st='r$&=(', k=427) == 'r$&=('
def test_railencrypt_9(self):
assert railencrypt(st='2A/L~[%4', k=339) == '2A/L~[%4'
def test_railencrypt_10(self):
assert railencrypt(st='M>'', k=210) == 'M>''
def test_railencrypt_11(self):
assert railencrypt(st='=5]'146#', k=143) == '=5]'146#'
def test_railencrypt_12(self):
assert railencrypt(st='c?g!b', k=675) == 'c?g!b'
def test_railencrypt_13(self):
assert railencrypt(st='U"i-', k=547) == 'U"i-'
def test_railencrypt_14(self):
assert railencrypt(st='+e[', k=328) == '+e['
def test_railencrypt_15(self):
assert railencrypt(st='@ICWq0.[D', k=944) == '@ICWq0.[D'
def test_railencrypt_16(self):
assert railencrypt(st='~a}By', k=141) == '~a}By'
def test_railencrypt_17(self):
assert railencrypt(st='', k=574) == ''
def test_railencrypt_18(self):
assert railencrypt(st='#7oR+_$*', k=457) == '#7oR+_$*'
def test_railencrypt_19(self):
assert railencrypt(st='/c', k=900) == '/c'
def test_railencrypt_20(self):
assert railencrypt(st='r', k=492) == 'r'
def test_railencrypt_21(self):
assert railencrypt(st='vh]lR', k=242) == 'vh]lR'
def test_railencrypt_22(self):
assert railencrypt(st='**hH2', k=270) == '**hH2'
def test_railencrypt_23(self):
assert railencrypt(st=')>On.', k=70) == ')>On.'
def test_railencrypt_24(self):
assert railencrypt(st='djQ*5r_Uk', k=420) == 'djQ*5r_Uk'
def test_railencrypt_25(self):
assert railencrypt(st='{+xw', k=405) == '{+xw'
def test_railencrypt_26(self):
assert railencrypt(st='RGvx~r', k=976) == 'RGvx~r'
def test_railencrypt_27(self):
assert railencrypt(st='<$', k=154) == '<$'
def test_railencrypt_28(self):
assert railencrypt(st=')>On.', k=221) == ')>On.'
def test_railencrypt_29(self):
assert railencrypt(st='IH!=Y`l*', k=885) == 'IH!=Y`l*'
def test_railencrypt_30(self):
assert railencrypt(st='w::L', k=641) == 'w::L'
def test_railencrypt_31(self):
assert railencrypt(st=')>On.', k=885) == ')>On.'
def test_railencrypt_32(self):
assert railencrypt(st='!CIXRTgh', k=754) == '!CIXRTgh'
def test_railencrypt_33(self):
assert railencrypt(st='?NGYChStM', k=850) == '?NGYChStM'
def test_railencrypt_34(self):
assert railencrypt(st=' X$qR?', k=389) == ' X$qR?'
def test_railencrypt_35(self):
assert railencrypt(st='dA$PGkO', k=453) == 'dA$PGkO'
def test_railencrypt_36(self):
assert railencrypt(st=',gN', k=663) == ',gN'
def test_railencrypt_37(self):
assert railencrypt(st='', k=885) == ''
def test_railencrypt_38(self):
assert railencrypt(st='3kDF>S%/', k=816) == '3kDF>S%/'
def test_railencrypt_39(self):
assert railencrypt(st='h{I<H[%4', k=468) == 'h{I<H[%4'
def test_railencrypt_40(self):
assert railencrypt(st='Ac#@F', k=335) == 'Ac#@F'
def test_railencrypt_41(self):
assert railencrypt(st='pR9.T\F#', k=950) == 'pR9.T\F#'
def test_railencrypt_42(self):
assert railencrypt(st='dA$PGkO', k=898) == 'dA$PGkO'
def test_railencrypt_43(self):
assert railencrypt(st='MX[JC2*', k=146) == 'MX[JC2*'
def test_railencrypt_44(self):
assert railencrypt(st='kwVesxWq', k=126) == 'kwVesxWq'
def test_railencrypt_45(self):
assert railencrypt(st='So!D5q<s', k=687) == 'So!D5q<s'
def test_railencrypt_46(self):
assert railencrypt(st='BgG', k=452) == 'BgG'
def test_railencrypt_47(self):
assert railencrypt(st='#6U@a', k=986) == '#6U@a'
def test_railencrypt_48(self):
assert railencrypt(st='-O HB', k=853) == '-O HB'
def test_railencrypt_49(self):
assert railencrypt(st='tGr_xE_<', k=31) == 'tGr_xE_<'
def test_railencrypt_50(self):
assert railencrypt(st='', k=750) == ''
def test_railencrypt_51(self):
assert railencrypt(st='%;A <}gy?', k=968) == '%;A <}gy?'
def test_railencrypt_52(self):
assert railencrypt(st='=', k=577) == '='
def test_railencrypt_53(self):
assert railencrypt(st='!q)3r7i_', k=370) == '!q)3r7i_'
def test_railencrypt_54(self):
assert railencrypt(st='y"X', k=359) == 'y"X'
def test_railencrypt_55(self):
assert railencrypt(st='=os/W!ss', k=176) == '=os/W!ss'
def test_railencrypt_56(self):
assert railencrypt(st='-G;'k', k=652) == '-G;'k'
def test_railencrypt_57(self):
assert railencrypt(st='tP&LZgJ(p', k=753) == 'tP&LZgJ(p'
def test_railencrypt_58(self):
assert railencrypt(st='J3kaFe2c', k=94) == 'J3kaFe2c'
def test_railencrypt_59(self):
assert railencrypt(st='M"9&HYe', k=691) == 'M"9&HYe'
def test_railencrypt_60(self):
assert railencrypt(st='OF', k=697) == 'OF'
def test_railencrypt_61(self):
assert railencrypt(st='H@', k=918) == 'H@'
def test_railencrypt_62(self):
assert railencrypt(st='', k=880) == ''
def test_railencrypt_63(self):
assert railencrypt(st='3kDF>S%/', k=160) == '3kDF>S%/'
def test_railencrypt_64(self):
assert railencrypt(st='1A!>S', k=242) == '1A!>S'
def test_railencrypt_65(self):
assert railencrypt(st='zZ|;(j', k=159) == 'zZ|;(j'
def test_railencrypt_66(self):
assert railencrypt(st='T:', k=887) == 'T:'
def test_railencrypt_67(self):
assert railencrypt(st='tP&LZI', k=753) == 'tP&LZI'
def test_railencrypt_68(self):
assert railencrypt(st='', k=541) == ''
def test_railencrypt_69(self):
assert railencrypt(st='8p+S~< \', k=705) == '8p+S~< \'
def test_railencrypt_70(self):
assert railencrypt(st='!3V0]Es', k=502) == '!3V0]Es'
def test_railencrypt_71(self):
assert railencrypt(st='', k=255) == ''
def test_railencrypt_72(self):
assert railencrypt(st='z-woX#S*', k=664) == 'z-woX#S*'
def test_railencrypt_73(self):
assert railencrypt(st='b]Eb', k=721) == 'b]Eb'
def test_railencrypt_74(self):
assert railencrypt(st='`82?%]G=k', k=89) == '`82?%]G=k'
def test_railencrypt_75(self):
assert railencrypt(st='', k=533) == ''
def test_railencrypt_76(self):
assert railencrypt(st='Kd*', k=125) == 'Kd*'
def test_railencrypt_77(self):
assert railencrypt(st='gB}[Y!', k=137) == 'gB}[Y!'
def test_railencrypt_78(self):
assert railencrypt(st='G\uHOS', k=745) == 'G\uHOS'
def test_railencrypt_79(self):
assert railencrypt(st='5H>Eac', k=588) == '5H>Eac'
def test_railencrypt_80(self):
assert railencrypt(st='', k=506) == ''
def test_railencrypt_81(self):
assert railencrypt(st='2A/L~q', k=339) == '2A/L~q'
def test_railencrypt_82(self):
assert railencrypt(st='=D?*', k=548) == '=D?*'
def test_railencrypt_83(self):
assert railencrypt(st='wvG6lC=YR', k=201) == 'wvG6lC=YR'
def test_railencrypt_84(self):
assert railencrypt(st='!&=,S3H', k=662) == '!&=,S3H'
def test_railencrypt_85(self):
assert railencrypt(st='0LO\', k=948) == '0LO\'
def test_railencrypt_86(self):
assert railencrypt(st='?', k=276) == '?'
def test_railencrypt_87(self):
assert railencrypt(st=':W', k=741) == ':W'
def test_railencrypt_88(self):
assert railencrypt(st=';:H(XyM%(', k=816) == ';:H(XyM%('
def test_railencrypt_89(self):
assert railencrypt(st='UvOW='', k=330) == 'UvOW=''
def test_railencrypt_90(self):
assert railencrypt(st='^82?%]G=k', k=5) == '^k8=2G?]%'
def test_railencrypt_91(self):
assert railencrypt(st='rh', k=985) == 'rh'
def test_railencrypt_92(self):
assert railencrypt(st='o$NfGib0x', k=437) == 'o$NfGib0x'
def test_railencrypt_93(self):
assert railencrypt(st='{', k=661) == '{'
def test_railencrypt_94(self):
assert railencrypt(st='BgOT!e/E', k=452) == 'BgOT!e/E'
def test_railencrypt_95(self):
assert railencrypt(st='n4:j', k=62) == 'n4:j'
def test_railencrypt_96(self):
assert railencrypt(st='', k=772) == ''
def test_railencrypt_97(self):
assert railencrypt(st='U}I~pr7', k=291) == 'U}I~pr7'
def test_railencrypt_98(self):
assert railencrypt(st='}Bx&KQ[i{', k=51) == '}Bx&KQ[i{'
def test_railencrypt_99(self):
assert railencrypt(st='?Ij', k=361) == '?Ij'
def test_railencrypt_100(self):
assert railencrypt(st='\El#o*a;}', k=422) == '\El#o*a;}'
assert railencrypt(st='(', k=307) == '('