def mk_plain_test(finput, prefix, t):
    name = sizes_alias[prefix] + "_" + str(t)
    dir_name = PLAIN_TESTS + "/" + name
    rm_and_mkdir(dir_name)
    os.system("cp " + finput + " " + dir_name + "/")
    amber_test_generation.generate_amber_test(finput, dir_name + "/" + name)
    metal_test_generation.generate_metal_test(finput, dir_name + "/" + name)
def mk_chunked_test(finput, prefix, t):
    name = sizes_alias[prefix] + "_" + str(t)
    dir_name = CHUNKED_TESTS + "/" + name
    rm_and_mkdir(dir_name)
    chunk_cfg = Configuration(timeout=2000,
                              workgroups=65532,
                              threads_per_workgroup=1,
                              saturation_level=2,
                              subgroup=0)
    os.system("cp " + finput + " " + dir_name + "/")
    amber_test_generation.generate_amber_test(finput, dir_name + "/" + name,
                                              chunk_cfg)
    metal_test_generation.generate_metal_test(finput, dir_name + "/" + name,
                                              chunk_cfg)
def run_amber_test(input_dir, output_dir, each_cfg_option, amber_build_path,
                   amber_build_flags, num_iter, android):
    simple_test_results = []
    verbose_test_results = []
    all_test_results = []

    # prepare to name each file according to the level of saturation being done and subgroup status
    saturation_level = each_cfg_option.get_saturation_level()
    subgroup_setting = each_cfg_option.get_subgroup_setting()
    threads_per_wg = each_cfg_option.get_threads_per_workgroup()
    if saturation_level == 0:
        if subgroup_setting == 0:
            output_file_name_extension = "no_saturation_same_subgroup_" + str(
                threads_per_wg) + "_threads_per_wg"
        elif subgroup_setting == 1:
            output_file_name_extension = "no_saturation_diff_subgroup_" + str(
                threads_per_wg) + "_threads_per_wg"
    elif saturation_level == 1:
        output_file_name_extension = "round_robin"
    elif saturation_level == 2:
        output_file_name_extension = "chunking"
    else:
        output_file_name_extension = "_" + str(
            saturation_level) + "level_saturation"

    # iterate over all files in the input directory, create a .amber file for each .txt file, and run the amber file
    for file_name in os.listdir(input_dir):
        if file_name.endswith('.txt'):
            # input file name shouldn't end with .amber as the amber_test_generation.py script will add the extension
            output_file_name = file_name[:-4] + "_txt_" + output_file_name_extension
            input_file_name = os.path.join(input_dir, file_name)
            log_print("generating amber test for: " + file_name + " in " +
                      output_dir)
            # create the amber file associated with input_file_name
            amber_test_generation.generate_amber_test(input_file_name,
                                                      output_file_name,
                                                      each_cfg_option)

            output_file_name = output_file_name + ".amber"

            # generate command to run the amber test for a specified iteration count and append results to a temp file
            run__test = "timeout -k 1 5 " + amber_build_path + output_file_name + amber_build_flags + ">> temp_results.txt"
            if android:
                # push test file on the device
                os.system("adb push " + output_file_name + " /data/local/tmp/")
                # prepare the specific run command to run amber on-device
                run__test = "timeout -k 1 5 adb shell 'cd /data/local/tmp ; ./amber_ndk " + amber_build_flags + " " + os.path.basename(
                    output_file_name) + "' >> temp_results.txt"
            for i in range(int(num_iter)):
                log_print("running test: " + output_file_name)
                log_print(run__test)
                os.system(run__test)

            # analyze the results of the temporary file to determine whether the test passed (P) or failed (F)
            with open('temp_results.txt', 'r') as file:
                results = file.read().split("\n")
                test_iteration = file_name[:-4]
                failure_count = 0
                pass_count = 0

                # count the number of failures, if any, and update both simple and verbose results accordingly
                for current_line in results:
                    if "1 fail" in current_line:
                        failure_count = failure_count + 1
                    elif "1 pass" in current_line:
                        pass_count = pass_count + 1

                # if there were no failures, indicate a "P" in both sets of tables
                if failure_count == 0:
                    log_print("P")
                    temp_item = [test_iteration, "P"]
                    simple_test_results.append(temp_item)
                    verbose_test_results.append(temp_item)

                # if there is at least one failure, update simple table with "F" and verbose table with fraction of "F"
                else:
                    log_print("F")
                    fract = "F (" + str(failure_count) + "/" + str(
                        num_iter) + ")"
                    temp_item_verbose = [test_iteration, fract]
                    temp_item_simple = [test_iteration, "F"]
                    simple_test_results.append(temp_item_simple)
                    verbose_test_results.append(temp_item_verbose)

            os.system("rm -f temp_results.txt")
            # create a directory of the amber test scripts generated at the specified output directory
            log_print("")
            move_amber_test_file = "mv " + output_file_name + " " + output_dir
            os.system(move_amber_test_file)

    os.system("rm -f temp_results.txt")
    all_test_results.append(simple_test_results)
    all_test_results.append(verbose_test_results)

    return all_test_results