def expand(infile, outfile, clib_root="~/clib/"): deps = {} file_contents = {} unique_files = [canonical_path(sys.argv[1])] dependency_re = re.compile("//\s*{{{(.*)}}}\s*") inx = 0 while inx < len(unique_files): fpath = unique_files[inx] inx += 1 with open(fpath) as f: lines = f.readlines() deps[fpath] = [] file_contents[fpath] = lines for line in lines: match = dependency_re.match(line) if match: dep_fpath = canonical_path(clib_root + match.group(1).strip()) deps[fpath].append(dep_fpath) if dep_fpath not in unique_files: unique_files.append(dep_fpath) write_order = [] while len(write_order) < len(unique_files): appended = False for fpath in unique_files: if fpath in write_order: continue can_write = True for dep in deps[fpath]: if dep not in write_order: can_write = False if can_write: write_order.append(fpath) appended = True if not appended: print "Encountered a cyclic dependency!" exit(1) with open(outfile, 'w') as outf: for fpath in write_order: outf.writelines(file_contents[fpath]) outf.write("\n")
def run_tests(infile, clib_root): infile = canonical_path(infile) dirname = os.path.dirname(infile) basename = os.path.basename(infile) expanded_file = dirname + "/." + basename expand(infile, expanded_file, clib_root) build_and_run(expanded_file)
def run_tests(infile, clib_root): if os.path.isdir(infile): infile = os.path.normpath(infile) test_name = os.path.basename(infile) infile += "/" + test_name + ".cpp" infile = canonical_path(infile) dirname = os.path.dirname(infile) basename = os.path.basename(infile) expanded_file = dirname + "/." + basename expand(infile, expanded_file, clib_root) build_and_run(expanded_file)
def build_and_run(filepath): filepath = canonical_path(filepath) binary_path = filepath[:-4] bold("Compiling " + filepath + " ...") if build_with_compiler_warnings(filepath, binary_path): err("Compilation failed") if build_with_instrumentation(filepath, binary_path): err("Compilation failed") dirname = os.path.dirname(filepath) test_dir = dirname + "/tests" sample_input = re.compile("sample.*[.]in") sample_files = [canonical_path(test_dir + "/" + f, "")[:-3] for f in os.listdir(test_dir) if sample_input.match(f)] failed = [] summary = "" first_line_has_multiple_tokens = False bold("\nRunning samples...") for sample in sorted(sample_files): first_line = open("{}.in".format(sample), 'r').read().split("\n")[0] if (len(first_line.strip().split(" ")) > 1): first_line_has_multiple_tokens = True if run("{} < {}.in > {}.my".format(binary_path, sample, sample)): err("Run-time error on " + sample) print "\nExpected output on {}:".format(sample) has_output = True try: out = open("{}.out".format(sample), 'r').read() print out except IOError: print "File \'{}.out\' not found\n".format(sample) has_output = False my = open("{}.my".format(sample), 'r').read() print "Your output on {}:".format(sample) print my if not has_output: warn(sample + " failed (missing \'{}.out\')".format(sample)) failed.append(sample) summary += icons.MAYBE continue proc = subprocess.Popen("diff {}.out {}.my".format(sample, sample),\ shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o,e = proc.communicate() if o.decode('ascii') == "": bold(sample + " passed") summary += icons.PASS else: proc = subprocess.Popen("diff -b {}.out {}.my".format(sample, sample),\ shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o,e = proc.communicate() if o.decode('ascii') == "": bold(sample + " passed (ignored whitespace)") summary += icons.PASS else: my_output = open("{}.my".format(sample)).readlines() my_tokens = [token for line in my_output for token in line.split()] exp_output = open("{}.out".format(sample)).readlines() exp_tokens = [token for line in exp_output for token in line.split()] match = True if len(my_output) != len(exp_output): match = False else: for i in xrange(0, len(my_output)): if my_output[i] == exp_output[i]: continue try: my_val = float( my_output[i]) exp_val = float(exp_output[i]) if abs(my_val - exp_val) / max(1, abs(exp_val)) > 1e-9: raise ValueError except ValueError: match = False break if match: bold(sample + " passed (ignored whitespace, float tolerance 1e-9)") summary += icons.PASS else: warn(sample + " failed") failed.append(sample) summary += icons.FAIL if failed: print print summary + ": FAILED samples " + str(failed) elif not sample_files: print warn("No sample inputs found in {}".format(test_dir)) else: print print summary + colors.BOLD + ": ALL SAMPLES OK!" + colors.ENDC if first_line_has_multiple_tokens: warn("First line of input has multiple tokens. Check their order carefully.")