├── cmds ├── .gitignore ├── cmds2 ├── srcs ├── lib │ ├── error.log │ ├── ft_free_arr.c │ ├── Makefile │ ├── execute_push_swap_thread.c │ ├── memtest.c │ ├── permutation_utils.c │ ├── init_exaustive_test_table.c │ ├── build_cmdstr.c │ ├── mersenne_twister_algorithm.c │ ├── init_memtest_table.c │ ├── execution_utils.c │ ├── compile_commands.json │ ├── error_management_tests.c │ └── logging_utils.c ├── mandatory │ ├── error_management.c │ ├── test500.c │ ├── memory_tests.c │ ├── test5.c │ ├── test100.c │ ├── identity_test.c │ └── exaustive_test.c ├── bonus │ ├── error_management_b.c │ ├── build_bonus_command.c │ ├── main.c │ └── execute_bonus.c ├── compile_commands.json └── include │ └── averager.h ├── checker_linux ├── .gdb_history ├── analyse_log.sh ├── compile_commands.json ├── Makefile ├── README.md └── LICENSE.md /cmds: -------------------------------------------------------------------------------- 1 | sa 2 | as 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .cache/ 2 | -------------------------------------------------------------------------------- /cmds2: -------------------------------------------------------------------------------- 1 | sa 2 | pa 3 | -------------------------------------------------------------------------------- /srcs/lib/error.log: -------------------------------------------------------------------------------- 1 | valgrind: ./checker: No such file or directory 2 | -------------------------------------------------------------------------------- /checker_linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vinni-Cedraz/push_swap_averager/HEAD/checker_linux -------------------------------------------------------------------------------- /srcs/lib/ft_free_arr.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void ft_free_arr(char **arr, void **aux) { 4 | while (arr && *arr) free(*arr++); 5 | free(aux); 6 | } 7 | 8 | void ft_free_arr_size(void **arr, uint size) { 9 | uint i; 10 | 11 | if (arr == NULL) return; 12 | i = 0; 13 | while (i < size) { 14 | free(arr[i]); 15 | i++; 16 | } 17 | free(arr); 18 | } 19 | -------------------------------------------------------------------------------- /srcs/mandatory/error_management.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | int main(void) { 4 | static void (*test[])() = { 5 | no_args, non_numeric1, non_numeric2, non_numeric3, 6 | non_numeric4, non_numeric5, non_numeric6, non_numeric_empty_string, 7 | max_int_overf, duplicate_sorted, duplicate_arguments, NULL 8 | }; 9 | exec_each_function_in(test, "./push_swap"); 10 | } 11 | -------------------------------------------------------------------------------- /srcs/lib/Makefile: -------------------------------------------------------------------------------- 1 | NAME = lib.a 2 | AR = ar 3 | ARFLAGS = -rcs 4 | CFLAGS = -w -O3 5 | INC = ../include/ 6 | OBJS_DIR = objs/ 7 | 8 | SRCS = $(wildcard *.c) 9 | OBJS = $(patsubst %.c,$(OBJS_DIR)%.o,$(SRCS)) 10 | 11 | all: $(NAME) 12 | 13 | $(NAME): $(OBJS) 14 | $(AR) $(ARFLAGS) $(NAME) $(OBJS) 15 | 16 | $(OBJS_DIR)%.o: %.c | $(OBJS_DIR) 17 | $(CC) $(CFLAGS) -I$(INC) -c $< -o $@ 18 | 19 | $(OBJS_DIR): 20 | @mkdir -p $(OBJS_DIR) 21 | 22 | clean: 23 | @rm -rf $(OBJS_DIR) 24 | 25 | fclean: clean 26 | @rm -f $(NAME) 27 | 28 | re: fclean all 29 | -------------------------------------------------------------------------------- /.gdb_history: -------------------------------------------------------------------------------- 1 | start 2 | n 3 | n 4 | s 5 | start 6 | n 7 | ls 8 | start 9 | n 10 | n 11 | n 12 | s 13 | n 14 | s 15 | n 16 | n 17 | n 18 | n 19 | n 20 | n 21 | n 22 | n 23 | start 24 | n 25 | n 26 | n 27 | s 28 | n 29 | s 30 | n 31 | n 32 | n 33 | n 34 | n 35 | n 36 | q 37 | start 38 | n 39 | n 40 | s 41 | n 42 | s 43 | n 44 | disp line 45 | n 46 | n 47 | disp line 48 | s 49 | n 50 | n 51 | n 52 | n 53 | n 54 | disp out_str 55 | n 56 | start 57 | n 58 | n 59 | n 60 | s 61 | n 62 | s 63 | n 64 | q 65 | start 66 | n 67 | n 68 | n 69 | n 70 | start 71 | start 72 | n 73 | n 74 | n 75 | s 76 | n 77 | n 78 | n 79 | start 80 | b fprintf_ok_ko 81 | c 82 | n 83 | n 84 | q 85 | start 86 | s execute_push_swap_thread 87 | start 88 | b execute_push_swap_thread 89 | c 90 | n 91 | n 92 | c 93 | n 94 | -------------------------------------------------------------------------------- /srcs/lib/execute_push_swap_thread.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void *execute_push_swap_thread(void *arg) { 4 | t_uargs *this_task = (t_uargs *)arg; 5 | char cmd[BIG_CMD_LEN]; 6 | char buffer[BUF_LEN]; 7 | FILE *output; 8 | 9 | int idx = this_task->thread_idx; 10 | FILE *fp = fopen(this_task->tmp_file, "a"); 11 | while (this_task->table[idx]) { 12 | this_task->build_cmd_string(cmd, this_task->size, idx, this_task->table); 13 | execute_cmd(cmd, buffer, output); 14 | print_array_to_file(fp, idx, this_task->size, this_task->table); 15 | this_task->fprintf_result_to_file(buffer, fp, &this_task->error); 16 | handle_err(this_task->table, this_task->size, idx, buffer); 17 | idx++; 18 | } 19 | 20 | fclose(fp); 21 | pthread_exit(NULL); 22 | return NULL; 23 | } 24 | -------------------------------------------------------------------------------- /srcs/bonus/error_management_b.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void b_invalid_action(char *program_name) { 4 | char cmd[CMD_LEN]; 5 | sprintf(cmd, "(cat cmds | valgrind --leak-check=full --show-leak-kinds=all -q %s %d %d %d %d %d %d) 2>error.log", 6 | program_name, 11, 10, 12, 13, 14, 15); 7 | printf(CYAN "invalid_action ------ " DEF_COLOR); 8 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 9 | system("rm -f error.log"); 10 | } 11 | 12 | void b_whitespaced_action(char *program_name) { 13 | char cmd[CMD_LEN]; 14 | sprintf(cmd, "(cat cmds2 | valgrind --leak-check=full --show-leak-kinds=all -q %s %d %d %d %d %d %d) 2>error.log", 15 | program_name, 11, 10, 12, 13, 14, 15); 16 | printf(CYAN "whitespaced_action -- " DEF_COLOR); 17 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 18 | system("rm -f error.log"); 19 | } 20 | -------------------------------------------------------------------------------- /srcs/bonus/build_bonus_command.c: -------------------------------------------------------------------------------- 1 | 2 | #include "../include/averager.h" 3 | 4 | void build_bonus_string(char cmd[], t_action size_and_idx, int **table) { 5 | char buffer[5]; 6 | strcpy(cmd, "(valgrind -q ./push_swap"); 7 | for (int j = 0; j < size_and_idx.arr_size; j++) { 8 | sprintf(buffer, " %d", table[size_and_idx.tab_idx][j]); 9 | strcat(cmd, buffer); 10 | } 11 | strcat(cmd, " | ./checker_linux"); 12 | for (int j = 0; j < size_and_idx.arr_size; j++) { 13 | sprintf(buffer, " %d", table[size_and_idx.tab_idx][j]); 14 | strcat(cmd, buffer); 15 | } 16 | strcat(cmd, ") 2>&1"); 17 | } 18 | 19 | void build_bonus_reference_string(char cmd[], t_action size_and_idx, int **table) { 20 | char buffer[5]; 21 | strcpy(cmd, "./push_swap"); 22 | for (int j = 0; j < size_and_idx.arr_size; j++) { 23 | sprintf(buffer, " %d", table[size_and_idx.tab_idx][j]); 24 | strcat(cmd, buffer); 25 | } 26 | strcat(cmd, " | ./checker_linux"); 27 | for (int j = 0; j < size_and_idx.arr_size; j++) { 28 | sprintf(buffer, " %d", table[size_and_idx.tab_idx][j]); 29 | strcat(cmd, buffer); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /srcs/lib/memtest.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void process_operations_test_output(FILE *output, int **table, t_action action, char *buffer); 4 | static void process_sorting_test_output(FILE *output, int **table, t_action action, char *buffer); 5 | 6 | void exec_memtest(int **table, t_action action) { 7 | char buffer[BUF_LEN]; 8 | char cmd[HUGE_CMD_LEN]; 9 | static void (*process_test_output[])(FILE *, int **, t_action, char *) = { 10 | process_sorting_test_output, 11 | process_operations_test_output, 12 | }; 13 | while (table[action.tab_idx]) { 14 | action.build_cmd(cmd, action.arr_size, action.tab_idx, table); 15 | FILE *output = popen(cmd, "r"); 16 | fgets(buffer, 100, output); 17 | process_test_output[action.is_sorting_test](output, table, action, buffer); 18 | pclose(output); 19 | action.tab_idx++; 20 | } 21 | } 22 | 23 | static inline void process_operations_test_output(FILE *output, int **table, t_action action, char *buffer) { 24 | log_cmd_and_output_3(table, action.arr_size, action.tab_idx, buffer); 25 | } 26 | 27 | static inline void process_sorting_test_output(FILE *output, int **table, t_action action, char *buffer) { 28 | log_cmd_and_output(table, action.arr_size, action.tab_idx, buffer); 29 | fprintf_ok_ko(buffer, output, NULL); 30 | } 31 | -------------------------------------------------------------------------------- /srcs/lib/permutation_utils.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static int is_reverse_sorted(int *arr, int last_index) { 4 | for (int i = 0; i < last_index; i++) 5 | if (arr[i] < arr[i + 1]) 6 | return 0; 7 | return 1; 8 | } 9 | 10 | int *next_permutation(int *arr, int last_index) { 11 | if (is_reverse_sorted(arr, last_index)) 12 | return NULL; 13 | int i = last_index; 14 | while (arr[i - 1] >= arr[i]) 15 | i--; 16 | int j = last_index; 17 | while (arr[j] <= arr[i - 1]) 18 | j--; 19 | 20 | int temp = arr[i - 1]; 21 | arr[i - 1] = arr[j]; 22 | arr[j] = temp; 23 | 24 | j = last_index; 25 | while (i < j) { 26 | temp = arr[i]; 27 | arr[i] = arr[j]; 28 | arr[j] = temp; 29 | i++, j--; 30 | } 31 | return arr; 32 | } 33 | 34 | uint *seq_except(int exclude) { 35 | int j = 0; 36 | int i = 0; 37 | uint *arr = malloc(99 * sizeof(uint)); 38 | for (; ++i != 101;) 39 | if (i != exclude) 40 | arr[j++] = i; 41 | return arr; 42 | } 43 | 44 | int is_repeated100(uint **table, uint *tmp_arr, int count) { 45 | int i = -1; 46 | while (++i <= count) { 47 | if (!table[i]) 48 | continue; 49 | if (!memcmp(table[i], tmp_arr, 70 * sizeof(uint))) 50 | return 1; 51 | } 52 | return 0; 53 | } 54 | 55 | int is_repeated500(uint **table, uint *tmp_arr, int count) { 56 | int i = -1; 57 | while (++i <= count) { 58 | if (!table[i]) 59 | continue; 60 | if (!memcmp(table[i], tmp_arr, 250 * sizeof(uint))) 61 | return 1; 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /srcs/lib/init_exaustive_test_table.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | #define START_IDX_TEST20 0 4 | #define END_IDX_TEST20 160 5 | #define START_IDX_TEST100 160 6 | #define END_IDX_TEST100 321 7 | #define TEST20 20 8 | #define TEST100 100 9 | 10 | static void print_table(int **table, int numRows, int arr_size) { 11 | for (int i = 0; i < numRows; i++) { 12 | if (table[i] != NULL) { 13 | printf(BLUE "Table[%d]: " DEF_COLOR, i); 14 | for (int j = 0; j < arr_size; j++) { 15 | printf("%d ", table[i][j]); 16 | } 17 | printf("\n"); 18 | } else { 19 | printf(RED "Table[%d]: NULL\n" DEF_COLOR, i); 20 | } 21 | } 22 | } 23 | 24 | int **init_table_exaustive_tests(void) { 25 | int **table = calloc(sizeof(int *), 1000); 26 | int count; 27 | 28 | count = 0; 29 | srand(time(NULL) ^ (getpid() << 16)); 30 | init_exaustive(&count, table, rand(), START_IDX_TEST20, END_IDX_TEST20, 31 | TEST20); 32 | init_exaustive(&count, table, rand(), START_IDX_TEST100, END_IDX_TEST100, 33 | TEST100); 34 | return table; 35 | } 36 | 37 | void init_exaustive(int *count, int **table, int rand, int i_start, int i_end, 38 | int arr_size) { 39 | while (*count != i_start) 40 | (*count)++; 41 | int *arr = malloc(sizeof(int) * arr_size); 42 | for (int i = 0; i < arr_size; i++) 43 | arr[i] = i; 44 | while (*count < i_end) { 45 | if (*count && (*count % 20) == 0) 46 | table[*count] = NULL; 47 | else { 48 | shuffle_array((uint *)arr, arr_size, rand); 49 | table[*count] = calloc(sizeof(int), arr_size); 50 | for (int i = 0; i < arr_size; i++) 51 | table[*count][i] = arr[i]; 52 | } 53 | (*count)++; 54 | } 55 | free(arr); 56 | } 57 | -------------------------------------------------------------------------------- /srcs/bonus/main.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void log_bonus_tests_header(void); 4 | static void log_bonus_tests_footer(void); 5 | static t_args *args_for_execute_bonus(t_args*); 6 | 7 | int main(void) { 8 | t_args *args = malloc(sizeof(t_args)); 9 | args->table = init_table_memtests_sizes(); 10 | static void (*error_management[])(char *) = { 11 | no_args, non_numeric_empty_string, non_numeric1, non_numeric2, non_numeric3, 12 | non_numeric4, non_numeric5, non_numeric6, max_int_overf, duplicate_sorted, 13 | b_invalid_action, duplicate_arguments, b_whitespaced_action, NULL 14 | }; 15 | log_bonus_tests_header(); 16 | exec_each_function_in(error_management, "./checker"); 17 | for (int idx = 0; idx < 6; idx++) execute_bonus(args_for_execute_bonus(args)); 18 | log_bonus_tests_footer(); 19 | ft_free_arr_size((void **)args->table, 100); 20 | free(args); 21 | } 22 | 23 | static inline t_args *args_for_execute_bonus(t_args *args) { 24 | static int call_counter = -1; 25 | const static t_action action[] = { 26 | (t_action){.arr_size = 5, .tab_idx = 7}, 27 | (t_action){.arr_size = 10, .tab_idx = 11}, 28 | (t_action){.arr_size = 15, .tab_idx = 15}, 29 | (t_action){.arr_size = 20, .tab_idx = 19}, 30 | (t_action){.arr_size = 100, .tab_idx = 23}, 31 | (t_action){.arr_size = 500, .tab_idx = 27} 32 | }; 33 | args->sizes_and_idx = action[++call_counter]; 34 | return args; 35 | } 36 | 37 | static void log_bonus_tests_header(void) { 38 | dprintf(1, HRED "\n\nMAKE SURE YOU COMPILED EVERYTHING WITH THE -O3 FLAG\n\n" DEF_COLOR); 39 | sleep(2); 40 | printf(HRED "\nrunning tests with valgrind on quiet mode (-q flag)\n" DEF_COLOR); 41 | } 42 | 43 | static void log_bonus_tests_footer(void) { 44 | printf("\n\n\n"); 45 | printf(CYAN "If you didnt see any valgrind messages it means no memory leaks were found in your program" DEF_COLOR); 46 | printf("\n\n\n\n"); 47 | } 48 | -------------------------------------------------------------------------------- /srcs/bonus/execute_bonus.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void print_message(char *reference, char *buffer, t_args *args); 4 | static void print_ok_ko(char *reference, char *buffer); 5 | 6 | void execute_bonus(void *args_void) { 7 | t_args *args = (t_args *)args_void; 8 | char command[HUGE_CMD_LEN]; 9 | char reference_cmd[HUGE_CMD_LEN]; 10 | char buffer[BUF_LEN]; 11 | char reference[BUF_LEN]; 12 | FILE *output; 13 | FILE *reference_output; 14 | int idx = args->sizes_and_idx.tab_idx - 1; 15 | 16 | while (args->table[++idx]) { 17 | build_bonus_string(command, args->sizes_and_idx, args->table); 18 | build_bonus_reference_string(reference_cmd, args->sizes_and_idx, args->table); 19 | execute_cmd(command, buffer, output); 20 | execute_cmd(reference_cmd, reference, reference_output); 21 | print_message(reference, buffer, args); 22 | } 23 | } 24 | 25 | static inline void print_message(char *reference, char *buffer, t_args *args) { 26 | t_action size_and_idx = args->sizes_and_idx; 27 | printf(HBLUE "size(%d):" DEF_COLOR "{ ", size_and_idx.arr_size); 28 | for (int j = 0; j < size_and_idx.arr_size; j++) printf("%d ", args->table[size_and_idx.tab_idx][j]); 29 | printf("}\n"); 30 | print_ok_ko(reference, buffer); 31 | } 32 | 33 | static inline void print_ok_ko(char *reference, char *buffer) { 34 | if (!strncmp(reference, "OK", 2)) 35 | printf(CYAN "official checker: " GREEN "OK\n" DEF_COLOR); 36 | else if (strncmp(reference, "KO", 2)) 37 | printf(CYAN "official checker: " RED "KO\n" DEF_COLOR); 38 | if (!strncmp(buffer, "OK", 2)) 39 | printf(HCYAN " your checker: " HGREEN "OK\n" DEF_COLOR); 40 | else if (!strncmp(buffer, "KO", 2)) 41 | printf(HCYAN " your checker: " HRED "KO\n" DEF_COLOR); 42 | if (strcmp(buffer, reference) != 0) { 43 | printf(HRED "ERROR\n" DEF_COLOR); 44 | exit(1); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /srcs/lib/build_cmdstr.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void build_averager_test_cmd_string(char command[], int num_args, int idx, int **table) { 4 | int j; 5 | char buffer[5]; 6 | strcpy(command, "(./push_swap"); 7 | for (j = 0; j < num_args; j++) { 8 | sprintf(buffer, " %d", table[idx][j]); 9 | strcat(command, buffer); 10 | } 11 | strcat(command, " | wc -l) 2>&1"); 12 | } 13 | 14 | void build_exaustive_checker_test_cmd_string(char *command, int num_args, int i, int **table) { 15 | int j; 16 | char buffer[5]; 17 | strcpy(command, "(./push_swap"); 18 | for (j = 0; j < num_args; j++) { 19 | sprintf(buffer, " %d", table[i][j]); 20 | strcat(command, buffer); 21 | } 22 | strcat(command, " | ./checker_linux"); 23 | for (j = 0; j < num_args; j++) { 24 | sprintf(buffer, " %d", table[i][j]); 25 | strcat(command, buffer); 26 | } 27 | strcat(command, ") 2>&1"); 28 | } 29 | 30 | void build_size3_sorting_cmdstr(char memtest[], int num_args, int idx, int **table) { 31 | int j; 32 | char buffer[5]; 33 | strcpy(memtest, "(valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap"); 34 | for (j = 0; j < num_args; j++) { 35 | sprintf(buffer, " %d", table[idx][j]); 36 | strcat(memtest, buffer); 37 | } 38 | strcat(memtest, " | wc -l) 2>&1"); 39 | } 40 | 41 | void build_memtest_cmdstring(char memtest[], int num_args, int idx, int **table) { 42 | int j; 43 | char buffer[5]; 44 | strcpy(memtest, "(valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap"); 45 | for (j = 0; j < num_args; j++) { 46 | sprintf(buffer, " %d", table[idx][j]); 47 | strcat(memtest, buffer); 48 | } 49 | strcat(memtest, " | ./checker_linux"); 50 | for (j = 0; j < num_args; j++) { 51 | sprintf(buffer, " %d", table[idx][j]); 52 | strcat(memtest, buffer); 53 | } 54 | strcat(memtest, ") 2>&1"); 55 | } 56 | -------------------------------------------------------------------------------- /srcs/mandatory/test500.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static uint **init_permutation_table(void) { 4 | int count = 0; 5 | uint *tmp_arr = malloc(sizeof(uint *) * 500); 6 | uint **table = calloc(sizeof(uint *), 481); 7 | for (int i = 0; i < 500; i++) 8 | tmp_arr[i] = i; 9 | srand(time(NULL) ^ (getpid() << 16)); 10 | shuffle_array(tmp_arr, 500, rand()); 11 | 12 | while (count < 480) { 13 | if (count && (count % 60) == 0) 14 | table[count] = NULL; 15 | else if (!is_repeated500(table, tmp_arr, count)) { 16 | table[count] = calloc(sizeof(uint), 500); 17 | memcpy(table[count], tmp_arr, 500 * sizeof(uint)); 18 | } 19 | shuffle_array(tmp_arr, 500, rand()); 20 | count++; 21 | } 22 | 23 | free(tmp_arr); 24 | return table; 25 | } 26 | 27 | static void init_this_task(t_uargs *this_task, int **table, int task_size) { 28 | static int call_counter = -1; 29 | static const int thread_idxs[] = {0, 61, 121, 181, 241, 301, 361, 421}; 30 | static const char *tmp_files[] = {"tmp1", "tmp2", "tmp3", "tmp4", "tmp5", "tmp6", "tmp7", "tmp8"}; 31 | this_task->table = table; 32 | this_task->size = task_size; 33 | this_task->tmp_file = tmp_files[++call_counter]; 34 | this_task->thread_idx = thread_idxs[call_counter]; 35 | this_task->build_cmd_string = build_averager_test_cmd_string; 36 | this_task->fprintf_result_to_file = fprintf_nb_of_op; 37 | } 38 | 39 | int main(void) { 40 | static t_uargs this_task[8]; 41 | pthread_t pthread[8]; 42 | int **table = init_permutation_table(); 43 | printf(WHITE "\nTESTS FOR SIZE 500\n" DEF_COLOR); 44 | for (int i = 0; i < 8; i++) { 45 | init_this_task(&this_task[i], table, 500); 46 | pthread_create(&pthread[i], NULL, execute_push_swap_thread, &this_task[i]); 47 | } 48 | for (int i = 0; i < 8; i++) 49 | pthread_join(pthread[i], NULL); 50 | ft_free_arr_size((void **)this_task->table, 481); 51 | } 52 | -------------------------------------------------------------------------------- /srcs/mandatory/memory_tests.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void execute_memtests(void *args_void); 4 | static void log_memtests_header(void); 5 | static t_action get_sizes_and_action(void); 6 | 7 | int main(void) { 8 | t_args *args; 9 | 10 | args = malloc(sizeof(t_args)); 11 | args->table = init_table_memtests_sizes(); 12 | execute_memtests(args); 13 | ft_free_arr_size((void **)args->table, 1000); 14 | free(args); 15 | } 16 | 17 | static void execute_memtests(void *args_void) { 18 | t_args *args = (t_args *)args_void; 19 | int **table = args->table; 20 | 21 | log_memtests_header(); 22 | for (int exec_eight_times = 0; exec_eight_times < 8; exec_eight_times++) 23 | exec_memtest(table, get_sizes_and_action()); 24 | dprintf(1, MEMORY_TEST_FOOTER); 25 | } 26 | 27 | static t_action get_sizes_and_action(void) { 28 | static short call_counter; 29 | const static t_action action[9] = { 30 | {.arr_size = -1, .tab_idx = -1, .build_cmd = NULL}, 31 | {.is_sorting_test = TRUE, .arr_size = 3, .tab_idx = 1, .build_cmd = &build_size3_sorting_cmdstr}, 32 | {.is_sorting_test = FALSE, .arr_size = 3, .tab_idx = 1, .build_cmd = &build_memtest_cmdstring}, 33 | {.is_sorting_test = FALSE, .arr_size = 5, .tab_idx = 7, .build_cmd = &build_memtest_cmdstring}, 34 | {.is_sorting_test = FALSE, .arr_size = 10, .tab_idx = 11, .build_cmd = &build_memtest_cmdstring}, 35 | {.is_sorting_test = FALSE, .arr_size = 15, .tab_idx = 15, .build_cmd = &build_memtest_cmdstring}, 36 | {.is_sorting_test = FALSE, .arr_size = 20, .tab_idx = 19, .build_cmd = &build_memtest_cmdstring}, 37 | {.is_sorting_test = FALSE, .arr_size = 100, .tab_idx = 23, .build_cmd = &build_memtest_cmdstring}, 38 | {.is_sorting_test = FALSE, .arr_size = 500, .tab_idx = 27, .build_cmd = &build_memtest_cmdstring}, 39 | }; 40 | return (action[++call_counter]); 41 | } 42 | 43 | static void log_memtests_header(void) { 44 | dprintf(1, MEMORY_TEST_HEADER); 45 | sleep(3); 46 | dprintf(STDOUT_FILENO, MEMORY_TEST_MESSAGE); 47 | } 48 | -------------------------------------------------------------------------------- /srcs/lib/mersenne_twister_algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../include/averager.h" 6 | 7 | static void mt_init(struct MT *m, uint seed) { 8 | int i; 9 | 10 | m->state[0] = seed; 11 | for (i = 1; i < MT_LEN; i++) { 12 | m->state[i] = 13 | 1812433253 * (m->state[i - 1] ^ (m->state[i - 1] >> 30)) + i; 14 | } 15 | m->next = MT_LEN; 16 | } 17 | 18 | static uint mt_generate(struct MT *m) { 19 | uint y; 20 | static uint mag[2] = {0x0, 0x9908b0df}; 21 | int i = m->next; 22 | 23 | if (i >= MT_LEN) { 24 | int k; 25 | 26 | if (i == MT_LEN + 1) mt_init(m, 5489); 27 | 28 | for (k = 0; k < MT_LEN - MT_IA; k++) { 29 | y = (m->state[k] & 0x80000000) | (m->state[k + 1] & 0x7fffffff); 30 | m->state[k] = m->state[k + MT_IA] ^ (y >> 1) ^ mag[y & 0x1]; 31 | } 32 | for (; k < MT_LEN - 1; k++) { 33 | y = (m->state[k] & 0x80000000) | (m->state[k + 1] & 0x7fffffff); 34 | m->state[k] = 35 | m->state[k + (MT_IA - MT_LEN)] ^ (y >> 1) ^ mag[y & 0x1]; 36 | } 37 | y = (m->state[MT_LEN - 1] & 0x80000000) | (m->state[0] & 0x7fffffff); 38 | m->state[MT_LEN - 1] = m->state[MT_IA - 1] ^ (y >> 1) ^ mag[y & 0x1]; 39 | 40 | i = 0; 41 | } 42 | 43 | y = m->state[i]; 44 | y ^= (y >> 11); 45 | y ^= (y << 7) & 0x9d2c5680; 46 | y ^= (y << 15) & 0xefc60000; 47 | y ^= (y >> 18); 48 | 49 | m->next = i + 1; 50 | return y; 51 | } 52 | 53 | void shuffle_array(uint *arr, int len, uint seed) { 54 | struct MT mt; 55 | int i, j; 56 | uint tmp; 57 | 58 | mt_init(&mt, seed); 59 | 60 | for (i = len - 1; i > 0; i--) { 61 | j = mt_generate(&mt) % (i + 1); 62 | tmp = arr[j]; 63 | arr[j] = arr[i]; 64 | arr[i] = tmp; 65 | } 66 | } 67 | 68 | // 69 | // int main() { 70 | // int i; 71 | // uint arr[500]; 72 | // 73 | // for (i = 1; i < 500; i++) arr[i] = i; 74 | // 75 | // shuffle_array(arr, 500); 76 | // 77 | // for (i = 0; i < 500; i++) printf("%u ", arr[i]); 78 | // } 79 | -------------------------------------------------------------------------------- /srcs/lib/init_memtest_table.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void init_memtest_table(int *index, int **table, int rand, int arr_size); 4 | 5 | static void print_table(int **table, int numRows, int arr_size) { 6 | for (int i = 0; i < numRows; i++) { 7 | if (table[i] != NULL) { 8 | printf(BLUE "Table[%d]: " DEF_COLOR, i); 9 | for (int j = 0; j < arr_size; j++) { 10 | printf("%d ", table[i][j]); 11 | } 12 | printf("\n"); 13 | } else { 14 | printf(RED "Table[%d]: NULL\n" DEF_COLOR, i); 15 | } 16 | } 17 | } 18 | 19 | int **init_table_memtests_sizes(void) { 20 | int **table = calloc(sizeof(int *), 1000); 21 | srand(time(NULL) ^ (getpid() << 16)); 22 | int index = 1; 23 | init_memtest_table(&index, table, rand(), 3); 24 | init_memtest_table(&index, table, rand(), 5); 25 | init_memtest_table(&index, table, rand(), 10); 26 | init_memtest_table(&index, table, rand(), 15); 27 | init_memtest_table(&index, table, rand(), 20); 28 | init_memtest_table(&index, table, rand(), 100); 29 | init_memtest_table(&index, table, rand(), 500); 30 | return (table); 31 | } 32 | 33 | static void init_memtest_table(int *index, int **table, int rand, int arr_size) { 34 | int *arr = malloc(sizeof(int) * arr_size); 35 | for (int i = 1; i < arr_size + 1; i++) 36 | arr[i - 1] = i; 37 | if (3 == arr_size) { 38 | int *going_on; 39 | while ((going_on = next_permutation(arr, 2))) { 40 | table[*index] = calloc(sizeof(int), 3); 41 | for (int i = 0; i < 3; i++) 42 | table[*index][i] = arr[i]; 43 | (*index)++; 44 | } 45 | (*index)++; 46 | free(arr); 47 | } else { 48 | for (int three = 0; three < 3; three++) { 49 | shuffle_array((uint *)arr, arr_size, rand); 50 | table[*index] = calloc(sizeof(int), arr_size); 51 | for (int i = 0; i < arr_size; i++) 52 | table[*index][i] = arr[i]; 53 | (*index)++; 54 | } 55 | (*index)++; 56 | free(arr); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /srcs/mandatory/test5.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static int **init_permutation_table(void) { 4 | int count = 0; 5 | int *arr = malloc(sizeof(int) * 5); 6 | int **table = calloc(sizeof(int *), 128); 7 | int *is_still_going_on; 8 | arr[0] = -1, arr[1] = 0, arr[2] = 1, arr[3] = 2, arr[4] = 3; 9 | 10 | do { 11 | if ((is_still_going_on = next_permutation(arr, 4))) { 12 | if (count && (count % 24) == 0) { 13 | table[count] = NULL; 14 | count++; 15 | } 16 | table[count] = malloc(sizeof(int) * 5); 17 | for (int i = 0; i < 5; i++) 18 | table[count][i] = arr[i]; 19 | count++; 20 | } 21 | } while (is_still_going_on); 22 | 23 | free(arr); 24 | return table; 25 | } 26 | 27 | static void print_averager_header(void) { 28 | printf(WHITE "Now here comes...\n\n" DEF_COLOR); 29 | printf(HGREEN "< < THE AVERAGER > >\n\n" DEF_COLOR); 30 | printf(CYAN "It'll test the number of operations\n\n" DEF_COLOR); 31 | sleep(3); 32 | printf(WHITE "TESTS FOR SIZE 5\n" DEF_COLOR); 33 | } 34 | 35 | static void init_this_task(t_uargs *this_task, int **table, int task_size) { 36 | static int call_counter = -1; 37 | const int thread_idxs[5] = {0, 25, 49, 73, 97}; 38 | const char *tmp_files[5] = {"tmp1", "tmp2", "tmp3", "tmp4", "tmp5"}; 39 | this_task->table = table; 40 | this_task->size = task_size; 41 | this_task->tmp_file = tmp_files[++call_counter]; 42 | this_task->thread_idx = thread_idxs[call_counter]; 43 | this_task->build_cmd_string = build_averager_test_cmd_string; 44 | this_task->fprintf_result_to_file = fprintf_nb_of_op; 45 | } 46 | 47 | 48 | int main(void) { 49 | pthread_t pthread[5]; 50 | static t_args this_task[5]; 51 | int **table = init_permutation_table(); 52 | for (int i = 0; i < 5; i++) { 53 | init_this_task(&this_task[i], table, 5); 54 | pthread_create(&pthread[i], NULL, execute_push_swap_thread, &this_task[i]); 55 | } 56 | for (int i = 0; i < 5; i++) 57 | pthread_join(pthread[i], NULL); 58 | ft_free_arr((char **)this_task->table, (void **)this_task->table); 59 | } 60 | -------------------------------------------------------------------------------- /srcs/mandatory/test100.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static uint **init_permutation_table(void) { 4 | int count = 0; 5 | int j = 1; 6 | uint *tmp_arr; 7 | uint **table = calloc(sizeof(uint *), 1300); 8 | srand(time(NULL) ^ (getpid() << 16)); 9 | 10 | for (int i = 1; i <= 100; i++) { 11 | tmp_arr = seq_except(i); 12 | for (int k = 1; k <= 12; k++) { 13 | if (count && (count % 143) == 0) { 14 | if (count >= 1300) 15 | break; 16 | table[count] = NULL; 17 | count++; 18 | } 19 | if (count >= 1300) 20 | break; 21 | table[count] = calloc(sizeof(uint), 100); 22 | table[count][0] = i; 23 | shuffle_array(tmp_arr, 99, rand()); 24 | if (is_repeated100(table, tmp_arr, count)) { 25 | k--; 26 | free(table[count]); 27 | continue; 28 | } 29 | int k = 0; 30 | for (j = 1; j < 100; j++) { 31 | table[count][j] = tmp_arr[k]; 32 | k++; 33 | } 34 | count++; 35 | } 36 | free(tmp_arr); 37 | } 38 | return table; 39 | } 40 | 41 | static void init_this_task(t_uargs *this_task, int **table, int task_size) { 42 | static int call_counter = -1; 43 | const int thread_idxs[8] = {0, 144, 288, 432, 576, 732, 888, 1044}; 44 | const char *tmp_files[] = {"tmp1", "tmp2", "tmp3", "tmp4", "tmp5", "tmp6", "tmp7", "tmp8"}; 45 | this_task->table = table; 46 | this_task->size = task_size; 47 | this_task->tmp_file = tmp_files[++call_counter]; 48 | this_task->thread_idx = thread_idxs[call_counter]; 49 | this_task->build_cmd_string = build_averager_test_cmd_string; 50 | this_task->fprintf_result_to_file = fprintf_nb_of_op; 51 | } 52 | 53 | int main(void) { 54 | pthread_t pthread[8]; 55 | static t_uargs this_task[8]; 56 | uint **table = init_permutation_table(); 57 | printf(WHITE "\nTESTS FOR SIZE 100\n" DEF_COLOR); 58 | for (int i = 0; i < 8; i++) { 59 | init_this_task(&this_task[i], table, 100); 60 | pthread_create(&pthread[i], NULL, execute_push_swap_thread, &this_task[i]); 61 | } 62 | for (int i = 0; i < 8; i++) 63 | pthread_join(pthread[i], NULL); 64 | ft_free_arr((char **)this_task->table, (void **)this_task->table); 65 | } 66 | -------------------------------------------------------------------------------- /analyse_log.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #Colors 4 | HRED="\033[1;31m" 5 | GREEN="\033[0;32m" 6 | HGREEN="\033[0;92m" 7 | DEF_COLOR="\033[0;39m" 8 | CYAN="\033[0;36m" 9 | HCYAN="\033[1;36m" 10 | 11 | # take the directory with the logfiles as an argument: 12 | log_file=$1 13 | 14 | LIMIT_5=12 15 | LIMIT_100=1500 16 | LIMIT_500=11500 17 | 18 | list_size=0 19 | 20 | dir="log_files/" 21 | 22 | if [[ "$log_file" == "test5.log" || "$log_file" == $dir"test5.log" ]]; then 23 | list_size=5 24 | elif [[ "$log_file" == "test100.log" || "$log_file" == $dir"test100.log" ]]; then 25 | list_size=100 26 | elif [[ "$log_file" == "test500.log" || "$log_file" == $dir"test500.log" ]]; then 27 | list_size=500 28 | fi 29 | 30 | # get all non-zero values of the number of operations into an array 31 | arr=($(grep -v 'number of operations: 0' $log_file | grep -o 'number of operations: [0-9]*' | awk '{print $NF}')) 32 | 33 | # calculate the total number of operations and the total number of lines 34 | total_ops=0 35 | total_lines=0 36 | for ops in "${arr[@]}"; do 37 | # Check if ops is a number 38 | if [[ $ops =~ ^[0-9]+$ ]]; then 39 | ((total_ops += ops)) 40 | ((total_lines++)) 41 | else 42 | cat $log_file | grep "Error" 43 | exit 1 44 | fi 45 | done 46 | 47 | # calculate the average number of operations 48 | average=$((total_ops / total_lines)) 49 | 50 | # calculate the lowest and highest number of operations 51 | sorted=($(echo "${arr[@]}" | tr ' ' '\n' | sort -n)) 52 | lowest=${sorted[0]} 53 | highest=${sorted[-1]} 54 | 55 | COLOR=$CYAN 56 | if [ "$list_size" -eq 5 -a "$highest" -gt "$LIMIT_5" ] || [ "$list_size" -eq 100 -a "$highest" -gt "$LIMIT_100" ] || \ 57 | [ "$list_size" -eq 500 -a "$highest" -gt "$LIMIT_500" ]; then 58 | COLOR=$HRED 59 | printf "\n$COLOR ERROR: YOU ARE DOING TOO MANY MOVEMENTS SOMEWHERE, CHECK YOUR WORST CASE AND LOG FILES\n" 60 | fi 61 | 62 | # print the lines with the lowest, highest, and average number of operations 63 | printf "$GREEN< best case arrays >\n$DEF_COLOR" 64 | cat $log_file | grep "number of operations: $lowest" | head -n 2 65 | 66 | printf "$COLOR< worst case arrays >\n$DEF_COLOR" 67 | string=$(cat $log_file | grep "number of operations: $highest" | head -n 2 | awk '{gsub(ENVIRON["CYAN"], ENVIRON["COLOR"]); print}') 68 | printf "$string\n"$DEF_COLOR 69 | 70 | # print the lowest, highest, and average number of operations 71 | if [[ "$list_size" -ge 100 ]]; 72 | then printf "$HGREEN< AVERAGE > $CYAN average case: $average$DEF_COLOR\n\n\n" 73 | fi 74 | -------------------------------------------------------------------------------- /compile_commands.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 4 | "arguments": [ 5 | "cc", 6 | "-w", 7 | "-pthread", 8 | "-g3", 9 | "-Isrcs/include/", 10 | "-c", 11 | "srcs/mandatory/error_management.c", 12 | "-o", 13 | "objs/error_management.o" 14 | ], 15 | "file": "srcs/mandatory/error_management.c" 16 | }, 17 | { 18 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 19 | "arguments": [ 20 | "cc", 21 | "-w", 22 | "-pthread", 23 | "-g3", 24 | "-Isrcs/include/", 25 | "-c", 26 | "srcs/mandatory/exaustive_test.c", 27 | "-o", 28 | "objs/exaustive_test.o" 29 | ], 30 | "file": "srcs/mandatory/exaustive_test.c" 31 | }, 32 | { 33 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 34 | "arguments": [ 35 | "cc", 36 | "-w", 37 | "-pthread", 38 | "-g3", 39 | "-Isrcs/include/", 40 | "-c", 41 | "srcs/mandatory/identity_test.c", 42 | "-o", 43 | "objs/identity_test.o" 44 | ], 45 | "file": "srcs/mandatory/identity_test.c" 46 | }, 47 | { 48 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 49 | "arguments": [ 50 | "cc", 51 | "-w", 52 | "-pthread", 53 | "-g3", 54 | "-Isrcs/include/", 55 | "-c", 56 | "srcs/mandatory/memory_tests.c", 57 | "-o", 58 | "objs/memory_tests.o" 59 | ], 60 | "file": "srcs/mandatory/memory_tests.c" 61 | }, 62 | { 63 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 64 | "arguments": [ 65 | "cc", 66 | "-w", 67 | "-pthread", 68 | "-g3", 69 | "-Isrcs/include/", 70 | "-c", 71 | "srcs/mandatory/test100.c", 72 | "-o", 73 | "objs/test100.o" 74 | ], 75 | "file": "srcs/mandatory/test100.c" 76 | }, 77 | { 78 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 79 | "arguments": [ 80 | "cc", 81 | "-w", 82 | "-pthread", 83 | "-g3", 84 | "-Isrcs/include/", 85 | "-c", 86 | "srcs/mandatory/test500.c", 87 | "-o", 88 | "objs/test500.o" 89 | ], 90 | "file": "srcs/mandatory/test500.c" 91 | }, 92 | { 93 | "directory": "/home/vcedraz-/push_swap/push_swap_averager", 94 | "arguments": [ 95 | "cc", 96 | "-w", 97 | "-pthread", 98 | "-g3", 99 | "-Isrcs/include/", 100 | "-c", 101 | "srcs/mandatory/test5.c", 102 | "-o", 103 | "objs/test5.o" 104 | ], 105 | "file": "srcs/mandatory/test5.c" 106 | } 107 | ] 108 | -------------------------------------------------------------------------------- /srcs/lib/execution_utils.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void exec_each_function_in(void (*array_of_function_ptrs[])(char *str), char *str) { 4 | while (*array_of_function_ptrs) 5 | (*array_of_function_ptrs++)(str); 6 | } 7 | 8 | char *execute_cmd(char cmd[], char buffer[], FILE *output) { 9 | output = popen(cmd, "r"); 10 | char *out_str = fgets(buffer, BUF_LEN, output); 11 | pclose(output); 12 | return out_str; 13 | } 14 | 15 | void open_process_and_exec_cmd_there(FILE **fp, char *cmd, bool close) { 16 | *fp = popen(cmd, "r"); 17 | if (*fp == NULL) { 18 | perror("popen"); 19 | exit(errno); 20 | } 21 | if (close) 22 | pclose(*fp); 23 | } 24 | 25 | void trim_linebreak(char *str) { 26 | int len = 0; 27 | if (str) 28 | len = strlen(str); 29 | if (len > 0 && str[len - 1] == '\n') 30 | str[len - 1] = '\0'; 31 | } 32 | 33 | static void analyse_error_log_file(char line[500], char cmd[500], bool bonus) { 34 | ssize_t read; 35 | FILE *error_log = fopen("error.log", "r"); 36 | if (error_log == NULL) { 37 | perror("Error opening file"); 38 | exit(errno); 39 | } 40 | fseek(error_log, 0, SEEK_END); 41 | if (ftell(error_log) == 0) { 42 | log_error(LOG_FILE, line, cmd); 43 | return; 44 | } 45 | rewind(error_log); 46 | if ((read = fread(line, sizeof(char), 500, error_log)) != -1) { 47 | if (!strcmp(line, "Error\n")) { 48 | printf(GREEN"OK"DEF_COLOR); 49 | bonus ? printf("\n") : printf(" <---- %s\n", cmd); 50 | } else if (NULL != strstr(line, "egmentation")) { 51 | printf(RED "KO" DEF_COLOR " <---- SEGFAULT\n"); 52 | exit(1); 53 | } else if (NULL != strstr(line, "memcheck")) { 54 | printf(RED "KO" DEF_COLOR " <---- MEMORY LEAK on %s\n", cmd); 55 | } else 56 | log_error(LOG_FILE, line, cmd); 57 | } 58 | fclose(error_log); 59 | } 60 | 61 | void exec_test_and_analyse_output(char *cmd, bool empty_expected, bool bonus) { 62 | FILE *fp; 63 | char line[CMD_LEN] = {0}; 64 | switch (empty_expected) { 65 | case CMD_OUTPUT: 66 | open_process_and_exec_cmd_there(&fp, cmd, DONT_CLOSE_PROCESS_AFTER); 67 | analyse_cmd_output(line, fp, cmd, bonus); 68 | pclose(fp); 69 | break; 70 | case LOG_FILE: 71 | open_process_and_exec_cmd_there(&fp, cmd, CLOSE_PROCESS_AFTER); 72 | analyse_error_log_file(line, cmd, bonus); 73 | break; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /srcs/lib/compile_commands.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 4 | "arguments": [ 5 | "cc", 6 | "-w", 7 | "-O3", 8 | "-I../include/", 9 | "-c", 10 | "ft_free_arr.c", 11 | "-o", 12 | "objs/ft_free_arr.o" 13 | ], 14 | "file": "ft_free_arr.c" 15 | }, 16 | { 17 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 18 | "arguments": [ 19 | "cc", 20 | "-w", 21 | "-O3", 22 | "-I../include/", 23 | "-c", 24 | "build_cmdstr500.c", 25 | "-o", 26 | "objs/build_cmdstr500.o" 27 | ], 28 | "file": "build_cmdstr500.c" 29 | }, 30 | { 31 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 32 | "arguments": [ 33 | "cc", 34 | "-w", 35 | "-O3", 36 | "-I../include/", 37 | "-c", 38 | "init_table2.c", 39 | "-o", 40 | "objs/init_table2.o" 41 | ], 42 | "file": "init_table2.c" 43 | }, 44 | { 45 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 46 | "arguments": [ 47 | "cc", 48 | "-w", 49 | "-O3", 50 | "-I../include/", 51 | "-c", 52 | "init_table.c", 53 | "-o", 54 | "objs/init_table.o" 55 | ], 56 | "file": "init_table.c" 57 | }, 58 | { 59 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 60 | "arguments": [ 61 | "cc", 62 | "-w", 63 | "-O3", 64 | "-I../include/", 65 | "-c", 66 | "mersenne_twister_algorithm.c", 67 | "-o", 68 | "objs/mersenne_twister_algorithm.o" 69 | ], 70 | "file": "mersenne_twister_algorithm.c" 71 | }, 72 | { 73 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 74 | "arguments": [ 75 | "cc", 76 | "-w", 77 | "-O3", 78 | "-I../include/", 79 | "-c", 80 | "build_cmdstr.c", 81 | "-o", 82 | "objs/build_cmdstr.o" 83 | ], 84 | "file": "build_cmdstr.c" 85 | }, 86 | { 87 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 88 | "arguments": [ 89 | "cc", 90 | "-w", 91 | "-O3", 92 | "-I../include/", 93 | "-c", 94 | "memtest.c", 95 | "-o", 96 | "objs/memtest.o" 97 | ], 98 | "file": "memtest.c" 99 | }, 100 | { 101 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 102 | "arguments": [ 103 | "cc", 104 | "-w", 105 | "-O3", 106 | "-I../include/", 107 | "-c", 108 | "exaustive_test20.c", 109 | "-o", 110 | "objs/exaustive_test20.o" 111 | ], 112 | "file": "exaustive_test20.c" 113 | }, 114 | { 115 | "directory": "/home/myuser/push_swap/push_swap_averager/srcs/lib", 116 | "arguments": [ 117 | "cc", 118 | "-w", 119 | "-O3", 120 | "-I../include/", 121 | "-c", 122 | "utils.c", 123 | "-o", 124 | "objs/utils.o" 125 | ], 126 | "file": "utils.c" 127 | } 128 | ] 129 | -------------------------------------------------------------------------------- /srcs/mandatory/identity_test.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void analyse_error_log_file(char line[CMD_LEN], char cmd[CMD_LEN]); 4 | static void five_hundred_sorted_elements(); 5 | static void hundred_sorted_elements(); 6 | static void fifty_sorted_elements(); 7 | static void nine_sorted_elements(); 8 | static void four_sorted_elements(); 9 | static void single_sorted_element(); 10 | static void exec_cmd_and_analyse_output(); 11 | 12 | int main(void) { 13 | char output[CMD_LEN]; 14 | static void (*tests[])(char[]) = { 15 | single_sorted_element, four_sorted_elements, nine_sorted_elements, 16 | fifty_sorted_elements, hundred_sorted_elements, five_hundred_sorted_elements, NULL 17 | }; 18 | exec_each_function_in(tests, "./push_swap"); 19 | } 20 | 21 | static void single_sorted_element() { 22 | FILE *fp; 23 | char cmd[CMD_LEN]; 24 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap %d 2>&1", 42); 25 | printf(CYAN "single_sorted_element ------ " DEF_COLOR); 26 | exec_cmd_and_analyse_output(cmd); 27 | } 28 | 29 | static void four_sorted_elements() { 30 | FILE *fp; 31 | char cmd[CMD_LEN]; 32 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap %d %d %d %d 2>&1", 0, 1, 2, 3); 33 | printf(CYAN "four_sorted_elements ------- " DEF_COLOR); 34 | exec_cmd_and_analyse_output(cmd); 35 | } 36 | 37 | static void nine_sorted_elements() { 38 | char cmd[CMD_LEN]; 39 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap %d %d %d %d %d %d %d %d %d 2>&1", 40 | 0, 1, 2, 3, 4, 5, 6, 7, 8); 41 | printf(CYAN "nine_sorted_elements ------- " DEF_COLOR); 42 | exec_cmd_and_analyse_output(cmd); 43 | } 44 | 45 | static void fifty_sorted_elements() { 46 | int i; 47 | char buffer[5]; 48 | char cmd[MEDIUM_CMD_LEN]; 49 | strcpy(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap "); 50 | for (i = 0; i < 50; i++) { 51 | sprintf(buffer, " %d", i); 52 | strcat(cmd, buffer); 53 | } 54 | strcat(cmd, " 2>&1"); 55 | printf(CYAN "fifty_sorted_elements ------ " DEF_COLOR); 56 | exec_cmd_and_analyse_output(cmd); 57 | } 58 | 59 | static void hundred_sorted_elements() { 60 | int i; 61 | char buffer[5]; 62 | char cmd[MEDIUM_CMD_LEN]; 63 | strcpy(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap "); 64 | for (i = 0; i < 100; i++) { 65 | sprintf(buffer, " %d", i); 66 | strcat(cmd, buffer); 67 | } 68 | strcat(cmd, " 2>&1"); 69 | printf(CYAN "hundred_sorted_elements ---- " DEF_COLOR); 70 | exec_cmd_and_analyse_output(cmd); 71 | } 72 | 73 | static void five_hundred_sorted_elements() { 74 | int i; 75 | char buffer[5]; 76 | char cmd[BIG_CMD_LEN]; 77 | strcpy(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q ./push_swap "); 78 | for (i = 0; i < 500; i++) { 79 | sprintf(buffer, " %d", i); 80 | strcat(cmd, buffer); 81 | } 82 | strcat(cmd, " 2>&1"); 83 | printf(CYAN "five_hundred_sorted_elements " DEF_COLOR); 84 | exec_cmd_and_analyse_output(cmd); 85 | } 86 | 87 | static void exec_cmd_and_analyse_output(char *cmd) { 88 | FILE *fp; 89 | char line[CMD_LEN]; 90 | open_process_and_exec_cmd_there(&fp, cmd, DONT_CLOSE_PROCESS_AFTER); 91 | analyse_cmd_output(line, fp, cmd, !BONUS); 92 | pclose(fp); 93 | return; 94 | } 95 | -------------------------------------------------------------------------------- /srcs/compile_commands.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "directory": "/home/myuser/push_swap/push_swap_averager", 4 | "arguments": [ 5 | "cc", 6 | "-w", 7 | "-pthread", 8 | "-O3", 9 | "-g3", 10 | "-Isrcs/include/", 11 | "-c", 12 | "srcs/mandatory/test100.c", 13 | "-o", 14 | "objs/test100.o" 15 | ], 16 | "file": "srcs/mandatory/test100.c" 17 | }, 18 | { 19 | "directory": "/home/myuser/push_swap/push_swap_averager", 20 | "arguments": [ 21 | "cc", 22 | "-w", 23 | "-pthread", 24 | "-O3", 25 | "-g3", 26 | "-Isrcs/include/", 27 | "-c", 28 | "srcs/mandatory/exaustive_test100.c", 29 | "-o", 30 | "objs/exaustive_test100.o" 31 | ], 32 | "file": "srcs/mandatory/exaustive_test100.c" 33 | }, 34 | { 35 | "directory": "/home/myuser/push_swap/push_swap_averager", 36 | "arguments": [ 37 | "cc", 38 | "-w", 39 | "-pthread", 40 | "-O3", 41 | "-g3", 42 | "-Isrcs/include/", 43 | "-c", 44 | "srcs/mandatory/test5.c", 45 | "-o", 46 | "objs/test5.o" 47 | ], 48 | "file": "srcs/mandatory/test5.c" 49 | }, 50 | { 51 | "directory": "/home/myuser/push_swap/push_swap_averager", 52 | "arguments": [ 53 | "cc", 54 | "-w", 55 | "-pthread", 56 | "-O3", 57 | "-g3", 58 | "-Isrcs/include/", 59 | "-c", 60 | "srcs/mandatory/identity_test.c", 61 | "-o", 62 | "objs/identity_test.o" 63 | ], 64 | "file": "srcs/mandatory/identity_test.c" 65 | }, 66 | { 67 | "directory": "/home/myuser/push_swap/push_swap_averager", 68 | "arguments": [ 69 | "cc", 70 | "-w", 71 | "-pthread", 72 | "-O3", 73 | "-g3", 74 | "-Isrcs/include/", 75 | "-c", 76 | "srcs/mandatory/memory_tests.c", 77 | "-o", 78 | "objs/memory_tests.o" 79 | ], 80 | "file": "srcs/mandatory/memory_tests.c" 81 | }, 82 | { 83 | "directory": "/home/myuser/push_swap/push_swap_averager", 84 | "arguments": [ 85 | "cc", 86 | "-w", 87 | "-pthread", 88 | "-O3", 89 | "-g3", 90 | "-Isrcs/include/", 91 | "-c", 92 | "srcs/mandatory/test500.c", 93 | "-o", 94 | "objs/test500.o" 95 | ], 96 | "file": "srcs/mandatory/test500.c" 97 | }, 98 | { 99 | "directory": "/home/myuser/push_swap/push_swap_averager", 100 | "arguments": [ 101 | "cc", 102 | "-w", 103 | "-pthread", 104 | "-O3", 105 | "-g3", 106 | "-Isrcs/include/", 107 | "-c", 108 | "srcs/mandatory/error_management.c", 109 | "-o", 110 | "objs/error_management.o" 111 | ], 112 | "file": "srcs/mandatory/error_management.c" 113 | }, 114 | { 115 | "directory": "/home/myuser/push_swap/push_swap_averager", 116 | "arguments": [ 117 | "cc", 118 | "-w", 119 | "-pthread", 120 | "-O3", 121 | "-Isrcs/include/", 122 | "-c", 123 | "srcs/bonus/error_management_b.c", 124 | "-o", 125 | "bobjs/error_management_b.o" 126 | ], 127 | "file": "srcs/bonus/error_management_b.c" 128 | }, 129 | { 130 | "directory": "/home/myuser/push_swap/push_swap_averager", 131 | "arguments": [ 132 | "cc", 133 | "-w", 134 | "-pthread", 135 | "-O3", 136 | "-Isrcs/include/", 137 | "-c", 138 | "srcs/bonus/build_bonus_command.c", 139 | "-o", 140 | "bobjs/build_bonus_command.o" 141 | ], 142 | "file": "srcs/bonus/build_bonus_command.c" 143 | }, 144 | { 145 | "directory": "/home/myuser/push_swap/push_swap_averager", 146 | "arguments": [ 147 | "cc", 148 | "-w", 149 | "-pthread", 150 | "-O3", 151 | "-Isrcs/include/", 152 | "-c", 153 | "srcs/bonus/main.c", 154 | "-o", 155 | "bobjs/main.o" 156 | ], 157 | "file": "srcs/bonus/main.c" 158 | }, 159 | { 160 | "directory": "/home/myuser/push_swap/push_swap_averager", 161 | "arguments": [ 162 | "cc", 163 | "-w", 164 | "-pthread", 165 | "-O3", 166 | "-Isrcs/include/", 167 | "-c", 168 | "srcs/bonus/execute_bonus.c", 169 | "-o", 170 | "bobjs/execute_bonus.o" 171 | ], 172 | "file": "srcs/bonus/execute_bonus.c" 173 | } 174 | ] 175 | -------------------------------------------------------------------------------- /srcs/mandatory/exaustive_test.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | static void exaustive_test20(t_args *args_void); 4 | static void exaustive_test100(t_args *args_void); 5 | static void exaustive20_footer(t_args this_task[8]); 6 | static void exaustive100_footer(t_args this_task[8]); 7 | static void init_this_task20(t_args *this_task, int **table, int task_size); 8 | static void init_this_task100(t_args *this_task, int **table, int task_size); 9 | 10 | int main(void) { 11 | t_args *args = malloc(sizeof(t_args)); 12 | args->table = init_table_exaustive_tests(); 13 | 14 | dprintf(1, EXAUSTIVE_TEST20_HEADER); 15 | exaustive_test20(args); 16 | exaustive_test100(args); 17 | ft_free_arr_size((void **)args->table, 1000); 18 | free(args); 19 | } 20 | 21 | static void exaustive_test20(t_args *args_void) { 22 | pthread_t pthread[8]; 23 | static t_args this_task[8]; 24 | for (int i = 0; i < 8; i++) { 25 | init_this_task20(&this_task[i], args_void->table, 20); 26 | pthread_create(&pthread[i], 0, execute_push_swap_thread, &this_task[i]); 27 | } 28 | for (int i = 0; i < 8; i++) 29 | pthread_join(pthread[i], NULL); 30 | create_unified_log_file20(); 31 | exaustive20_footer(this_task); 32 | } 33 | 34 | static void init_this_task20(t_args *this_task, int **table, int task_size) { 35 | static int call_counter = -1; 36 | const int thread_idxs[8] = {0, 21, 41, 61, 81, 101, 121, 141}; 37 | const char *tmp_files[] = {"tmp1", "tmp2", "tmp3", "tmp4", "tmp5", "tmp6", "tmp7", "tmp8"}; 38 | this_task->error = 0; 39 | this_task->table = table; 40 | this_task->size = task_size; 41 | this_task->tmp_file = tmp_files[++call_counter]; 42 | this_task->thread_idx = thread_idxs[call_counter]; 43 | this_task->build_cmdstr = build_exaustive_checker_test_cmd_string; 44 | this_task->fprintf_result_to_file = fprintf_ok_ko; 45 | } 46 | 47 | static void exaustive20_footer(t_args this_task[8]) { 48 | bool error = 0; 49 | for (int i = 0; i < 8; i++) { 50 | error = this_task[i].error; 51 | if (error) 52 | break; 53 | } 54 | if (1 == error) { 55 | dprintf(1, 56 | RED "One or more sorting tests " HRED "FAILED!!!\n" YELLOW 57 | "to see the details run: " DEF_COLOR 58 | "cat push_swap_averager/log_files/exaustive20.log\n\n\n"); 59 | exit(1); 60 | } else 61 | dprintf(1, GREEN 62 | "160 lists of 20 elements were sorted correctly\n" DEF_COLOR); 63 | } 64 | 65 | static void exaustive_test100(t_args *args_void) { 66 | pthread_t pthread[8]; 67 | static t_args this_task[8]; 68 | for (int i = 0; i < 8; i++) { 69 | init_this_task100(&this_task[i], args_void->table, 100); 70 | pthread_create(&pthread[i], 0, execute_push_swap_thread, &this_task[i]); 71 | } 72 | for (int i = 0; i < 8; i++) 73 | pthread_join(pthread[i], NULL); 74 | create_unified_log_file100(); 75 | exaustive100_footer(this_task); 76 | } 77 | 78 | static void init_this_task100(t_args *this_task, int **table, int task_size) { 79 | static int call_counter = -1; 80 | const int thread_idxs[8] = {161, 181, 201, 221, 241, 261, 281, 301}; 81 | const char *tmp_files[] = {"tmp1", "tmp2", "tmp3", "tmp4", "tmp5", "tmp6", "tmp7", "tmp8"}; 82 | this_task->error = 0; 83 | this_task->table = table; 84 | this_task->size = task_size; 85 | this_task->tmp_file = tmp_files[++call_counter]; 86 | this_task->thread_idx = thread_idxs[call_counter]; 87 | this_task->build_cmdstr = build_exaustive_checker_test_cmd_string; 88 | this_task->fprintf_result_to_file = fprintf_ok_ko; 89 | } 90 | 91 | static void exaustive100_footer(t_args this_task[8]) { 92 | bool error = 0; 93 | for (int i = 0; i < 8; i++) { 94 | error = this_task[i].error; 95 | if (error) 96 | break; 97 | } 98 | if (1 == error) { 99 | dprintf(1, 100 | RED "One or more sorting tests " HRED "FAILED!!!\n" YELLOW 101 | "to see the details run: " DEF_COLOR 102 | "cat push_swap_averager/log_files/exaustive100.log\n\n\n"); 103 | exit(1); 104 | } else 105 | dprintf(1, GREEN 106 | "160 lists of 100 elements were sorted correctly\n" DEF_COLOR); 107 | } 108 | -------------------------------------------------------------------------------- /srcs/lib/error_management_tests.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void no_args(char *program_name) { 4 | char cmd[CMD_LEN]; 5 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" " -q %s 2>&1", program_name); 6 | printf(CYAN "no_args ------------- " DEF_COLOR); 7 | exec_test_and_analyse_output(cmd, CMD_OUTPUT, !BONUS); 8 | system("rm -f error.log"); 9 | } 10 | 11 | void non_numeric1(char *program_name) { 12 | char cmd[CMD_LEN]; 13 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 14 | " -q %s %d %d %d %s 2>error.log", 15 | program_name, 3, 2, 1, "9a"); 16 | printf(CYAN "non_numeric 1 ------- " DEF_COLOR); 17 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 18 | system("rm -f error.log"); 19 | } 20 | 21 | void non_numeric2(char *program_name) { 22 | char cmd[CMD_LEN]; 23 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 24 | " -q %s %d %d %d %s 2>error.log", 25 | program_name, 3, 2, 1, "a"); 26 | printf(CYAN "non_numeric 2 ------- " DEF_COLOR); 27 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 28 | system("rm -f error.log"); 29 | } 30 | 31 | void non_numeric3(char *program_name) { 32 | char cmd[CMD_LEN]; 33 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 34 | " -q %s %d %d %d %s 2>error.log", 35 | program_name, 3, 2, 1, "12 a 2"); 36 | printf(CYAN "non_numeric 3 ------- " DEF_COLOR); 37 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 38 | system("rm -f error.log"); 39 | } 40 | 41 | void non_numeric4(char *program_name) { 42 | char cmd[CMD_LEN]; 43 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 44 | " -q %s %d %d %d %s 2>error.log", 45 | program_name, 3, 2, 1, "12 - 2"); 46 | printf(CYAN "non_numeric 4 ------- " DEF_COLOR); 47 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 48 | system("rm -f error.log"); 49 | } 50 | 51 | void non_numeric5(char *program_name) { 52 | char cmd[CMD_LEN]; 53 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 54 | " -q %s %d %d %d %s 2>error.log", 55 | program_name, 3, 2, 1, "12 --2"); 56 | printf(CYAN "non_numeric 5 ------- " DEF_COLOR); 57 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 58 | system("rm -f error.log"); 59 | } 60 | 61 | void non_numeric6(char *program_name) { 62 | char cmd[CMD_LEN]; 63 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 64 | " -q %s %d %d %d %s 2>error.log", 65 | program_name, 3, 2, 1, "12 ++2"); 66 | printf(CYAN "non_numeric 6 ------- " DEF_COLOR); 67 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 68 | system("rm -f error.log"); 69 | } 70 | 71 | void non_numeric_empty_string(char *program_name) { 72 | char cmd[CMD_LEN]; 73 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all -q %s \"%s\" 2>error.log", program_name, ""); 74 | printf(CYAN "empty_string -------- " DEF_COLOR); 75 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 76 | system("rm -f error.log"); 77 | } 78 | 79 | void max_int_overf(char *program_name) { 80 | char cmd[CMD_LEN]; 81 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 82 | " -q %s %d %d %d %lu 2>error.log", 83 | program_name, 35, 24, 21, 21474836498); 84 | printf(CYAN "max_int_overf ------- " DEF_COLOR); 85 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 86 | system("rm -f error.log"); 87 | } 88 | 89 | void duplicate_arguments(char *program_name) { 90 | char cmd[CMD_LEN]; 91 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 92 | " -q %s %d %d %d 2>error.log", 93 | program_name, 1, 2, 1); 94 | printf(CYAN "duplicate_arguments - " DEF_COLOR); 95 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 96 | system("rm -f error.log"); 97 | } 98 | 99 | void duplicate_sorted(char *program_name) { 100 | char cmd[CMD_LEN]; 101 | sprintf(cmd, "valgrind --leak-check=full --show-leak-kinds=all" 102 | " -q %s %d %d %d %d %d %d 2>error.log", 103 | program_name, 10, 11, 12, 13, 14, 14); 104 | printf(CYAN "duplicate_sorted ---- " DEF_COLOR); 105 | exec_test_and_analyse_output(cmd, LOG_FILE, !BONUS); 106 | system("rm -f error.log"); 107 | } 108 | -------------------------------------------------------------------------------- /srcs/lib/logging_utils.c: -------------------------------------------------------------------------------- 1 | #include "../include/averager.h" 2 | 3 | void fprintf_ok_ko(char *out_str, FILE *fp, bool *GLOBAL) { 4 | if (!strncmp("OK", out_str, 2) && GLOBAL) { 5 | fprintf(fp, " checker_linux:" GREEN "%s" DEF_COLOR, out_str); 6 | } else if (strncmp("OK", out_str, 2) && GLOBAL) { 7 | (*GLOBAL) = 1; 8 | fprintf(fp, " checker_linux:" HRED "%s" DEF_COLOR, out_str); 9 | } else if (!strncmp("OK", out_str, 2) && !GLOBAL) { 10 | dprintf(1, GREEN " %s" DEF_COLOR, out_str); 11 | } else if (strncmp("OK", out_str, 2) && !GLOBAL) { 12 | dprintf(1, HRED " %s" DEF_COLOR, out_str); 13 | } 14 | } 15 | 16 | void fprintf_nb_of_op(char *out_str, FILE *fp, bool *error) { 17 | (void)error; 18 | fprintf(fp, CYAN "number of operations: %s" DEF_COLOR, out_str); 19 | } 20 | 21 | void create_unified_log_file20(void) { 22 | system("cat tmp8 >> tmp7"); 23 | system("cat tmp7 >> tmp6"); 24 | system("cat tmp6 >> tmp5"); 25 | system("cat tmp5 >> tmp4"); 26 | system("cat tmp4 >> tmp3"); 27 | system("cat tmp3 >> tmp2"); 28 | system("cat tmp2 >> tmp1"); 29 | system("cat tmp1 > exaustive20.log"); 30 | system("mv exaustive20.log log_files/"); 31 | system("rm tmp*"); 32 | } 33 | 34 | void create_unified_log_file100(void) { 35 | system("cat tmp8 >> tmp7"); 36 | system("cat tmp7 >> tmp6"); 37 | system("cat tmp6 >> tmp5"); 38 | system("cat tmp5 >> tmp4"); 39 | system("cat tmp4 >> tmp3"); 40 | system("cat tmp3 >> tmp2"); 41 | system("cat tmp2 >> tmp1"); 42 | system("cat tmp1 > exaustive100.log"); 43 | system("mv exaustive100.log log_files/"); 44 | system("rm tmp*"); 45 | } 46 | 47 | static void log_err_to_stdout(int size, int **table, int i, bool segf) { 48 | if (segf) { 49 | dprintf(1, HRED "\nSegfault occurred with the test:" DEF_COLOR " ./push_swap " DEF_COLOR); 50 | for (int j = 0; j < size; j++) 51 | dprintf(1, "%d ", table[i][j]); 52 | dprintf(1, "\n\n\n"); 53 | } else { 54 | dprintf(1, HRED "\nMemory error occurred with the test:" DEF_COLOR " ./push_swap " DEF_COLOR); 55 | for (int j = 0; j < size; j++) 56 | dprintf(1, "%d ", table[i][j]); 57 | dprintf(1, "\n\n\n"); 58 | } 59 | exit(1); 60 | } 61 | 62 | int handle_err(int **table, int size, int i, char *buf) { 63 | if (!strncmp("Segmentation", buf, 5) || !strncmp("segmentation", buf, 5)) { 64 | bool segf = 1; 65 | log_err_to_stdout(size, table, i, segf); 66 | } else if (!strncmp("==", buf, 2)) { 67 | bool segf = 0; 68 | log_err_to_stdout(size, table, i, segf); 69 | } 70 | } 71 | 72 | void log_cmd_and_output(int **table, int size, int i, char *buf) { 73 | dprintf(1, HBLUE "(SIZE %d):" DEF_COLOR " ./push_swap ", size); 74 | for (int j = 0; j < size; j++) 75 | dprintf(1, "%d ", table[i][j]); 76 | handle_err(table, size, i, buf); 77 | dprintf(1, YELLOW "\nchecker_linux: ", DEF_COLOR); 78 | } 79 | 80 | void log_cmd_and_output_3(int **table, int size, int i, char *buf) { 81 | dprintf(1, BLUE "arr[%d]:" DEF_COLOR " ./push_swap ", i); 82 | for (int j = 0; j < 3; j++) 83 | dprintf(1, "%d ", table[i][j]); 84 | dprintf(1, CYAN " Operations: " DEF_COLOR WHITE "%s" DEF_COLOR, buf); 85 | if (atoi(buf) > 2) { 86 | dprintf(1, HRED "ERROR: " RED "exceeded the limit of operations (2)\n" DEF_COLOR); 87 | } 88 | handle_err(table, 3, i, buf); 89 | } 90 | 91 | void log_error(bool empty_expected, char *out_str, char *cmd) { 92 | trim_linebreak(out_str); 93 | printf(cmd); 94 | switch (empty_expected) { 95 | case TRUE: 96 | printf(HRED "\n KO " DEF_COLOR); 97 | printf(YELLOW"-> Expected nothing either on stderr nor on stdout (fd 1 or fd 2)"); 98 | printf(" But found this:"DEF_COLOR"\n\"%s\""YELLOW" instead\n\n" DEF_COLOR, out_str); 99 | break; 100 | case FALSE: 101 | printf(HRED "\n KO " DEF_COLOR); 102 | printf(YELLOW"-> Expected the string \"Error\\n\" on the stderr (fd 2)"); 103 | printf(" But found this:\n"DEF_COLOR"\"%s\""YELLOW" instead\n\n" DEF_COLOR, out_str); 104 | break; 105 | } 106 | } 107 | 108 | void print_array_to_file(FILE *fp, int idx, int arr_size, uint **table) { 109 | fprintf(fp, HBLUE "arr[%d]: " DEF_COLOR " ./push_swap ", idx); 110 | for (int j = 0; j < arr_size; j++) 111 | fprintf(fp, "%d ", table[idx][j]); 112 | } 113 | 114 | void analyse_cmd_output(char line[500], FILE *fp, char cmd[500], bool bonus) { 115 | char *out_str = fgets(line, 100, fp); 116 | switch (NULL != out_str) { 117 | case TRUE: 118 | if (strstr("egmentation", out_str)) 119 | printf(RED "KO" DEF_COLOR " <---- SEGFAULT on %s\n", cmd), exit(1); 120 | else if (strstr("memcheck", out_str)) 121 | printf(RED "KO" DEF_COLOR " <---- MEMORY LEAK on %s\n", cmd); 122 | break; 123 | case FALSE: 124 | printf(GREEN "OK" DEF_COLOR); 125 | bonus ? printf("\n") : printf(" <---- %s\n", cmd); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | #Colors 3 | RED = \033[0;31m 4 | HRED = \033[1;31m 5 | HGREEN = \033[1;32m 6 | HYELLOW = \033[1;33m 7 | HBLUE = \033[1;34m 8 | DEF_COLOR = \033[0m 9 | CYAN = \033[0;36m 10 | 11 | # Define flags 12 | CFLAGS = -w -pthread -O3 13 | INCLUDES = -Isrcs/include/ 14 | BLIB = blib.a 15 | 16 | # Define the source and object file directories 17 | VPATH = srcs/mandatory/ 18 | BSRCSDIR = srcs/bonus/ 19 | ODIR = objs/ 20 | BOBJSDIR = bobjs/ 21 | LIB = srcs/lib/lib.a 22 | 23 | # Define the source and object files 24 | SRCS = $(wildcard $(VPATH)*.c) 25 | BSRCS = $(wildcard $(BSRCSDIR)*.c) 26 | OBJS = $(patsubst $(VPATH)%.c, $(ODIR)%.o, $(SRCS)) 27 | BOBJS = $(patsubst $(BSRCSDIR)%.c, $(BOBJSDIR)%.o, $(BSRCS)) 28 | 29 | all: pre_all 30 | @make --no-print-directory pre_all 31 | @make --no-print-directory run 32 | 33 | pre_all: lib $(OBJS) 34 | @rm -f error.log 35 | @make --no-print-directory execs 36 | @if [[ -f ../Makefile ]]; then \ 37 | make --no-print-directory -C ..; \ 38 | else \ 39 | printf "\n$(HRED)ERROR: RTFM: The push_swap_averager must be placed inside of your push_swap project root directory\n$(DEF_COLOR)\n"; \ 40 | printf "\n$(HBLUE)ERROR: RTFM: The push_swap_averager must be placed inside of your push_swap project root directory\n$(DEF_COLOR)\n"; \ 41 | printf "\n$(HYELLOW)ERROR: RTFM: The push_swap_averager must be placed inside of your push_swap project root directory\n$(DEF_COLOR)\n"; \ 42 | fi 43 | @make --no-print-directory -C .. 44 | @cp -f ../push_swap . 45 | 46 | lib: 47 | @make --no-print-directory -C srcs/lib 48 | 49 | execs: $(OBJS) 50 | @$(foreach file,$(OBJS), \ 51 | if [ $(file) -nt $(patsubst $(ODIR)%.o,%,$(file)) ] || [ ! -f $(patsubst $(ODIR)%.o,%,$(file)) ] || [ $(LIB) -nt $(file) ]; \ 52 | then \ 53 | printf "Compiling $(file) -> $(HGREEN)$(patsubst $(ODIR)%.o,%,$(file))\n$(DEF_COLOR)"; \ 54 | $(CC) $(CFLAGS) $(INCLUDES) $(file) $(LIB) -o $(patsubst $(ODIR)%.o,%,$(file)); \ 55 | fi; \ 56 | ) 57 | 58 | $(ODIR)%.o: %.c 59 | mkdir -p $(ODIR) 60 | $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ 61 | 62 | bonus: $(BOBJS) lib 63 | @make --no-print-directory -C .. 64 | @make --no-print-directory -C .. bonus 65 | @cp -f ../push_swap . 66 | @cp -f ../checker . 67 | @make --no-print-directory bexec 68 | ./basic_bonus 69 | 70 | bexec: $(BLIB) 71 | @$(CC) $(CFLAGS) $(INCLUDES) $(BLIB) $(LIB) -o basic_bonus 72 | @printf "$(CC) $(CFLAGS) $(INCLUDES) $(BLIB) $(LIB) -o $(HGREEN)basic_bonus$(DEF_COLOR)\n\n"; 73 | 74 | $(BOBJSDIR)%.o: $(BSRCSDIR)%.c 75 | mkdir -p $(BOBJSDIR) 76 | $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ 77 | 78 | $(BLIB): $(BOBJS) 79 | ar rcs $(BLIB) $? 80 | 81 | run: 82 | @printf "$(CYAN)running tests with valgrind on quiet mode $(DEF_COLOR)\n\n"; 83 | ./error_management 84 | @printf "\n\n"; 85 | ./identity_test 86 | @rm -f tmp* 87 | @rm -rf log_files 88 | @mkdir -p log_files 89 | @./memory_tests 90 | @./exaustive_test 91 | ./test5 92 | @cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test5.log && rm tmp* 93 | @./analyse_log.sh test5.log 94 | @mv test5.log ./log_files 95 | ./test100 96 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test100.log && rm tmp* 97 | @./analyse_log.sh test100.log 98 | @mv test100.log ./log_files 99 | ./test500 100 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test500.log && rm tmp* 101 | @./analyse_log.sh test500.log 102 | @mv test500.log ./log_files 103 | 104 | averager: pre_all 105 | @mkdir -p log_files 106 | ./test5 107 | @cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test5.log && rm tmp* 108 | @./analyse_log.sh test5.log 109 | @mv test5.log ./log_files 110 | ./test100 111 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test100.log && rm tmp* 112 | @./analyse_log.sh test100.log 113 | @mv test100.log ./log_files 114 | ./test500 115 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test500.log && rm tmp* 116 | @./analyse_log.sh test500.log 117 | @mv test500.log ./log_files 118 | 119 | test500: pre_all 120 | @mkdir -p log_files 121 | @printf "$(CYAN)running test500 with valgrind on quiet mode $(DEF_COLOR)\n\n"; 122 | ./test500 123 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test500.log && rm tmp* 124 | @./analyse_log.sh test500.log 125 | @mv test500.log ./log_files 126 | 127 | test100: pre_all 128 | @mkdir -p log_files 129 | @printf "$(CYAN)running test100 with valgrind on quiet mode $(DEF_COLOR)\n\n"; 130 | ./test100 131 | @cat tmp8 >> tmp7 && cat tmp7 >> tmp6 && cat tmp6 >> tmp5 && cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test100.log && rm tmp* 132 | @./analyse_log.sh test100.log 133 | @mv test100.log ./log_files 134 | 135 | test5: pre_all 136 | @mkdir -p log_files 137 | @printf "$(CYAN)running test5 with valgrind on quiet mode $(DEF_COLOR)\n\n"; 138 | ./test5 139 | @cat tmp5 >> tmp4 && cat tmp4 >> tmp3 && cat tmp3 >> tmp2 && cat tmp2 >> tmp1 && cat tmp1 > test5.log && rm tmp* 140 | @./analyse_log.sh test5.log 141 | @mv test5.log ./log_files 142 | 143 | clean: 144 | @rm -rf objs 145 | @rm -rf bobjs 146 | @rm -f tmp* 147 | @make --no-print-directory -C srcs/lib clean 148 | @rm -f $(BLIB) 149 | 150 | fclean: clean 151 | @rm -rf ./log_files 152 | @rm -f test* push_swap checker memory_tests basic_bonus error_management identity_test exaustive* error.log 153 | @make --no-print-directory -C srcs/lib fclean 154 | 155 | re: fclean all 156 | -------------------------------------------------------------------------------- /srcs/include/averager.h: -------------------------------------------------------------------------------- 1 | #ifndef AVERAGER_H 2 | #define AVERAGER_H 3 | 4 | // SYSTEM HEADERS 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // COLORS 17 | #define GREEN "\033[0;32m" 18 | #define HGREEN "\033[1;32m" 19 | #define DEF_COLOR "\033[0;39m" 20 | #define CYAN "\033[0;36m" 21 | #define HCYAN "\033[1;36m" 22 | #define WHITE "\033[1;97m" 23 | #define MAGENTA "\033[0;35m" 24 | #define YELLOW "\033[0;33m" 25 | #define BLUE "\033[0;34m" 26 | #define HBLUE "\033[1;34m" 27 | #define HRED "\033[1;31m" 28 | #define RED "\033[0;91m" 29 | 30 | // LENGTHS 31 | #define MT_LEN 624 32 | #define MT_IA 397 33 | #define BUF_LEN 100 34 | #define CMD_LEN 500 35 | #define MEDIUM_CMD_LEN 1500 36 | #define BIG_CMD_LEN 3000 37 | #define HUGE_CMD_LEN 10000 38 | 39 | #define CMD_OUTPUT 1 40 | #define LOG_FILE 0 41 | #define CLOSE_PROCESS_AFTER 1 42 | #define DONT_CLOSE_PROCESS_AFTER 0 43 | #define TRUE 1 44 | #define FALSE 0 45 | #define BONUS 1 46 | 47 | #define MEMORY_TEST_HEADER \ 48 | HRED "\n\nMAKE SURE YOU COMPILED EVERYTHING WITH THE -O3 " \ 49 | "FLAG\n\n\n" DEF_COLOR 50 | 51 | #define MEMORY_TEST_MESSAGE \ 52 | WHITE "\nMEMORY AND SORTING TESTS\n" DEF_COLOR CYAN " with valgrind on quiet mode (-q flag) " \ 53 | "\n\n\n" DEF_COLOR 54 | 55 | #define MEMORY_TEST_FOOTER \ 56 | CYAN "\n\n\nIf you didnt see any valgrind messages it means no memory " \ 57 | "leaks were found in your program\n\n\n\n" DEF_COLOR 58 | 59 | #define EXAUSTIVE_TEST20_HEADER \ 60 | HCYAN "Now, we will check sorting correctness with several different " \ 61 | "permutations...\n" DEF_COLOR 62 | 63 | // USER DEFINED TYPES 64 | typedef unsigned int uint; 65 | typedef void(t_build_cmdstr)(char[], int, int, int **); 66 | typedef void(t_fprintf_result_to_file)(char[], FILE *, bool *); 67 | 68 | typedef struct MT { 69 | uint state[MT_LEN]; 70 | int next; 71 | } t_MT; 72 | 73 | typedef struct s_uargs { 74 | uint **table; 75 | char *tmp_file; 76 | int thread_idx; 77 | t_build_cmdstr *build_cmd_string; 78 | t_fprintf_result_to_file *fprintf_result_to_file; 79 | bool error; 80 | int size; 81 | } t_uargs; 82 | 83 | typedef struct s_action { 84 | int arr_size; 85 | int tab_idx; 86 | bool is_sorting_test; 87 | t_build_cmdstr *build_cmd; 88 | } t_action; 89 | 90 | typedef struct s_args { 91 | int **table; 92 | char *tmp_file; 93 | int thread_idx; 94 | t_build_cmdstr *build_cmdstr; 95 | t_fprintf_result_to_file *fprintf_result_to_file; 96 | bool error; 97 | int size; 98 | t_action sizes_and_idx; 99 | } t_args; 100 | 101 | // FUNCTION PROTOTYPES 102 | void fprintf_nb_of_op(char *out_str, FILE *fp, bool *error); 103 | int is_repeated100(uint **table, uint *tmp_arr, int count); 104 | int is_repeated500(uint **table, uint *tmp_arr, int count); 105 | int *next_permutation(int *arr, int last_index); 106 | uint *seq_except(int exclude); 107 | int *next_permutation(int *arr, int last_index); 108 | void ft_free_arr(char **arr, void **aux); 109 | void ft_free_arr_size(void **arr, uint size); 110 | void shuffle_array(uint *arr, int len, uint seed); 111 | void build_bonus_string(char cmd[], t_action size_and_idx, int **table); 112 | void build_bonus_reference_string(char cmd[], t_action size_and_idx, int **table); 113 | int **init_table_memtests_sizes(void); 114 | void fprintf_ok_ko(char *out_str, FILE *fp, bool *GLOBAL); 115 | void create_unified_log_file20(void); 116 | void create_unified_log_file100(void); 117 | int handle_err(int **table, int size, int i, char *buffer); 118 | void log_error(bool empty_expected, char *out_str, char *cmd); 119 | void log_cmd_and_output(int **table, int size, int i, char *buf); 120 | void log_cmd_and_output_3(int **table, int size, int i, char *buf); 121 | void build_memtest_cmdstring(char memtest[], int i, int num_args, int **table); 122 | void build_size3_sorting_cmdstr(char memtest[], int num_args, int idx, int **table); 123 | void exec_memtest(int **table, t_action sizes_and_action); 124 | void init_exaustive(int *count, int **table, int rand, int i_start, int i_end, int arr_size); 125 | void bonus_log_error(bool empty_expected, char *out_str); 126 | void open_process_and_exec_cmd_there(FILE **fp, char *cmd, bool close); 127 | void build_exaustive_checker_test_cmd_string(char command[], int num_args, int idx, int **table); 128 | void build_averager_test_cmd_string(char command[], int num_args, int idx, int **table); 129 | void trim_linebreak(char *str); 130 | void max_int_overf(char *program_name); 131 | void duplicate_sorted(char *program_name); 132 | void duplicate_arg(char *program_name); 133 | void no_args(char *program_name); 134 | void empty_string(char *program_name); 135 | void b_invalid_action(char *program_name); 136 | void b_whitespaced_action(char *program_name); 137 | void non_numeric1(char *program_name); 138 | void non_numeric2(char *program_name); 139 | void non_numeric3(char *program_name); 140 | void non_numeric4(char *program_name); 141 | void non_numeric5(char *program_name); 142 | void non_numeric6(char *program_name); 143 | void non_numeric_empty_string(char *program_name); 144 | void duplicate_arguments(char *program_name); 145 | int **init_table_exaustive_tests(void); 146 | void *execute_push_swap_thread(void *args_void); 147 | void print_array_to_file(FILE *fp, int idx, int arr_size, uint **table); 148 | char *execute_cmd(char cmd[], char buffer[], FILE *output); 149 | void exec_each_function_in(void (*array_of_function_ptrs[])(char *str), char *str); 150 | void analyse_cmd_output(char line[500], FILE *fp, char cmd[500], bool bonus); 151 | void exec_test_and_analyse_output(char *cmd, bool empty_expected, bool bonus); 152 | void execute_bonus(void *args_void); 153 | 154 | #endif 155 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # push_swap_averager 2 | 3 | This tester is called push_swap_averager because it is accurate and detailed in calculating the best case, worst case and the average case of your push_swap. However, it also tests sorting correctness, error handling and memory safety (with Valgrind) of both the mandatory and bonus projects. 4 | 5 | ## Basic Usage TL;DR (too long didn't read) 6 | 7 | #### Make sure your push_swap makefiles are compiling with the O3 flag! 8 | $(CC) -O3 -Wall -Wex.... etc 9 | #### Clone the tester inside of your push_swap directory: 10 | git clone https://github.com/Vinni-Cedraz/push_swap_averager 11 | #### And this is how to use it from here: 12 | make -C push_swap_averager 13 | #### And 14 | make -C push_swap_averager bonus 15 | 16 | ###### You could also cd into the tester's directory and run just ```make``` and ```make bonus``` from there 17 | 18 | ## Table of Contents 19 | 20 | - [The Why](#Why) 21 | - [Installation](#installation) 22 | - [Usage](#usage) 23 | - [How I did it](#Development) 24 | - [Trivia](#trivia) 25 | 26 | ## Why 27 | 28 | This tester is designed to try out a very broad range of combinations for the 29 | 5, 100, and 500 array sizes that your `push_swap` program is supposed to sort. 30 | 31 | As you probably know by now, during your `push_swap` evaluation, your evaluator 32 | will be asked to run your program a few times with random arrays of specific sizes 33 | and then rank your project according to the results he finds in his tests. 34 | 35 | There is a myth going around in the 42 community that says the evaluator is 36 | asked by the ruler to take the average of the tests he has run, but that is not 37 | what the ruler asks. The ruler only requires him to run "a couple of tests with 38 | several permutations" and check it against the scores.] 39 | 40 | This means that if your average case is not safely below the number of 41 | operations allowed to get the grade, you'll have a very annoying chance that 42 | the evaluator will incur precisely into a case where you're slightly above the 43 | limit and you won't get the highest grade. 44 | 45 | This could make or break your bonus project. Imagine 46 | how insufficient it is to measure 5 cases out of 100 factorial: 47 | 48 | It would be 5 samples out of: 49 | 50 | 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 51 | 52 | For an array of size 500, the number is even more obscene: 53 | 54 | 1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 55 | 56 | This is **why** the `push_swap-averager` was born: to give you a broader perspective 57 | on your push_swap performance before you submit it for evaluation. And it does 58 | that not only by taking precise averages but also by giving you the worst cases 59 | it found so that if you want to be extremely safe, you can guide yourself by 60 | the worst case instead. 61 | 62 | ## Installation 63 | Just clone it inside of your push_swap directory: 64 | 65 | `git clone https://github.com/Vinni-Cedraz/pushswap-averager.git` 66 | 67 | ## Usage 68 | ### basic usage 69 | 70 | *First: it is **CRUCIAL** that you set your push_swap Makefiles to compile everything with the -O3 flag!* 71 | 72 | Then, just do this from your push_swap root directory and your program will be 73 | compiled and the tests will be run: 74 | 75 | `make -C push_swap-averager` 76 | 77 | The first tests will be error_management and identity_test which are based on 78 | the first items in the guide for the evaluation of the project 79 | 80 | The second test will be the basic_test, it tests if you are sorting the 81 | arrays and accuses any memory leaks. It will do so with: 82 | - all unsorted arrays of size 3 (checking the limit of 2 operations for size 3) 83 | - one unsorted array of each size 5, 10, 15, 20, 50, 100, 500 and 1000 84 | Then the tests for the averages will start. 85 | 86 | If you passed on all of those and did the bonus part: 87 | - Bonus 88 | 89 | `make -C push_swap_averager bonus` 90 | 91 | Error management is going to be the first test. Not only incorrect arguments to 92 | the checker will be tested but also incorrect input such as " pa ". Then the 93 | bonus rule will test the correctness of your checker against the official 94 | checker_linux for "OK" cases and will accuse any memory leaks it finds. 95 | 96 | ### advanced features 97 | 98 | - You can open the log files containing all array permutations that were tested 99 | for each size and the number of operations push_swap took to sort it. You can 100 | also use grep to figure out which and how many arrays scored a given number, 101 | for example: 102 | 103 | `cat push_swap_averager/log_files/test100.log | grep "operations: 600"` 104 | 105 | - You can run parts of the test separately: 106 | 107 | `make -C push_swap_averager test100` 108 | 109 | `make -C push_swap_averager test500` 110 | 111 | `make -C push_swap_averager averager` 112 | 113 | ## Development 114 | 115 | Since the number of permutations and the computation can be intense, I decided 116 | to take a multithreaded approach. I chose 8 to be the number of threads since 117 | the 42 iMacs have 4 cores and support 2 threads per core, this way, true 118 | parallelism can be achieved. I even tested subdividing the tasks into more than 119 | 8 threads but the performance did not improve and even started to go down. So I 120 | stuck with 8 threads. 121 | 122 | For this, I only had to use two functions from the C library `pthread.h`: 123 | `pthread_create` and `pthread_join`. I use a micro-managed subdivision of tasks to 124 | avoid race conditions instead of mutexes, which would complexify and slow down 125 | things in this context. 126 | 127 | To test thousands of permutations in a semi-random pattern that would be as 128 | representative as possible, I have divided the total number of permutations 129 | of an N-sized array into N groups. Then, I pick up 12 random permutations 130 | from each of these N groups to create the sample set of permutations that will 131 | be tested as input for your `push_swap`. These permutations were initially 132 | generated by a very simple implementation of the Fisher-Yates algorithm. 133 | 134 | This means that for the 100 size, the `push_swap-averager` will be 135 | executing your push_swap 1,200 times. It is still a small number compared to 136 | the total. But because of the way the combinations are selected, the result is 137 | a very consistent measurement of your average case and it can be finished 138 | within 2 seconds on the 4 cores / 2 threads per core iMacs we have at the 42 139 | school. 140 | 141 | As it was, if I ran the `push_swap-averager` 30 times in a row, I would see a 142 | slightly different result each time so I had to calculate the difference 143 | between the highest and the lowest of the 30 averages to see check if the test 144 | is reliable enough. 145 | 146 | For example, out of 30 tests, my `push_swap` program ranked averages between 147 | 547 and 647. This is a "margin of error" of 100 operations, so I had to increase 148 | the number of tests (up to 15,000) to get to an acceptable margin of 10. It was 149 | taking 30 seconds now instead of 2, so I thought it was ok. 150 | 151 | However, when I applied the same Fisher-Yates algorithm to randomize the 500 152 | elements array, I realized the result (the measurement of the average) was not 153 | consistent enough unless the test took 5 to 10 minutes, and that is too much time. 154 | 155 | Then I've done some research on the basics of Pseudorandomness in computer 156 | science and decided to implement the Mersenne-Twister algorithm to improve both 157 | the consistency of the average-case calculations and the execution speed of the 158 | program. 159 | 160 | The Mersenne Twister algorithm is a well-known and widely used algorithm for 161 | generating high-quality pseudo-random numbers. It has been extensively tested 162 | and shown to be very efficient and effective at generating well-distributed 163 | permutations. It is commonly used in scientific simulations and other 164 | applications that require high-quality random number generation. That is why I 165 | chose it. You can find its implementation in the source file called 166 | `mersenne_twister_algorithm.c`. 167 | 168 | Now, with more well-distributed permutations the program takes 2 seconds to 169 | calculate the average of the size 100 with a margin of error of 4 operations. 170 | That is an improvement of 2400% in accuracy, without adding any extra time to the 171 | program execution. 172 | 173 | ## Trivia 174 | - The longest English word with all its letters in alphabetical order is 175 | "aegilops," which is a type of plant. 176 | - Scientists have discovered a set of snow crystals that may be the first 177 | matching pair ever found, challenging the common belief that no two snowflakes 178 | are alike. The crystals were collected on a glass plate during a research 179 | flight over Wisconsin in 1986 and were found to have almost identical growth 180 | histories. The discovery raises the possibility that more identical pairs of 181 | snow crystals could be found in the future. 182 | - The traditional method of shuffling playing cards, known as the riffle 183 | shuffle, is not truly random. A study published in the Proceedings of the 184 | National Academy of Sciences found that if someone knows the initial order of 185 | the cards, they can predict the outcome of a riffle shuffle with high 186 | accuracy. 187 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | GNU GENERAL PUBLIC LICENSE 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The GNU General Public License is a free, copyleft license for 12 | software and other kinds of work. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | the GNU General Public License is intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. We, the Free Software Foundation, use the 19 | GNU General Public License for most of our software; it applies also to 20 | any other work released this way by its authors. You can apply it to 21 | your programs, too. 22 | 23 | When we speak of free software, we are referring to freedom, not 24 | price. Our General Public Licenses are designed to make sure that you 25 | have the freedom to distribute copies of free software (and charge for 26 | them if you wish), that you receive source code or can get it if you 27 | want it, and that you can change the software or use pieces of it in new 28 | free programs, and that you know you can do these things. 29 | 30 | To protect your rights, we need to prevent others from denying you 31 | these rights or asking you to surrender the rights. Therefore, you have 32 | certain responsibilities if you distribute copies of the software, or if 33 | you modify it: responsibilities to respect the freedom of others. 34 | 35 | For example, if you distribute copies of such a program, whether 36 | gratis or for a fee, you must pass on to the recipients the same 37 | freedoms that you received. You must make sure that they, too, receive 38 | or can get the source code. And you must show them these terms so they 39 | know their rights. 40 | 41 | Developers that use the GNU GPL protect your rights with two steps: 42 | (1) assert copyright on the software, and (2) offer you this License 43 | giving you legal permission to copy, distribute and/or modify it. 44 | 45 | For the developers' and authors' protection, the GPL clearly explains 46 | that there is no warranty for this free software. For both users' and 47 | authors' sake, the GPL requires that modified versions be marked as changed so 48 | that their problems will not be attributed erroneously to authors of previous 49 | versions. 50 | 51 | Some devices are designed to deny users access to install or run 52 | modified versions of the software inside them, although the manufacturer 53 | can do so. This is fundamentally incompatible with the aim of 54 | protecting users' freedom to change the software. The systematic 55 | pattern of such abuse occurs in the area of products for individuals to 56 | use, which is precisely where it is most unacceptable. Therefore, we 57 | have designed this version of the GPL to prohibit the practice for those 58 | products. If such problems arise substantially in other domains, we 59 | stand ready to extend this provision to those domains in future versions 60 | of the GPL, as needed to protect the freedom of users. 61 | 62 | Finally, every program is threatened constantly by software patents. 63 | States should not allow patents to restrict the development and use of 64 | software on general-purpose computers, but in those that do, we wish to 65 | avoid the special danger that patents applied to a free program could 66 | make it effectively proprietary. To prevent this, the GPL assures that 67 | patents cannot be used to render the program non-free. 68 | 69 | The precise terms and conditions for copying, distribution and 70 | modification follow. 71 | 72 | TERMS AND CONDITIONS 73 | 74 | 0. Definitions. 75 | 76 | "This License" refers to version 3 of the GNU General Public License. 77 | 78 | "Copyright" also means copyright-like laws that apply to other kinds of 79 | works, such as semiconductor masks. 80 | 81 | "The Program" refers to any copyrightable work licensed under this 82 | License. Each licensee is addressed as "you". "Licensees" and 83 | "recipients" may be individuals or organizations. 84 | 85 | To "modify" a work means to copy from or adapt all or part of the work 86 | in a fashion requiring copyright permission, other than the making of an 87 | exact copy. The resulting work is called a "modified version" of the 88 | earlier work or a work "based on" the earlier work. 89 | 90 | A "covered work" means either the unmodified Program or a work based 91 | on the Program. 92 | 93 | To "propagate" a work means to do anything with it that, without 94 | permission, would make you directly or secondarily liable for 95 | infringement under applicable copyright law, except executing it on a 96 | computer or modifying a private copy. Propagation includes copying, 97 | distribution (with or without modification), making available to the 98 | public, and in some countries other activities as well. 99 | 100 | To "convey" a work means any kind of propagation that enables other 101 | parties to make or receive copies. Mere interaction with a user through 102 | a computer network, with no transfer of a copy, is not conveying. 103 | 104 | An interactive user interface displays "Appropriate Legal Notices" 105 | to the extent that it includes a convenient and prominently visible 106 | feature that (1) displays an appropriate copyright notice, and (2) 107 | tells the user that there is no warranty for the work (except to the 108 | extent that warranties are provided), that licensees may convey the 109 | work under this License, and how to view a copy of this License. If 110 | the interface presents a list of user commands or options, such as a 111 | menu, a prominent item in the list meets this criterion. 112 | 113 | 1. Source Code. 114 | 115 | The "source code" for a work means the preferred form of the work 116 | for making modifications to it. "Object code" means any non-source 117 | form of a work. 118 | 119 | A "Standard Interface" means an interface that either is an official 120 | standard defined by a recognized standards body, or, in the case of 121 | interfaces specified for a particular programming language, one that 122 | is widely used among developers working in that language. 123 | 124 | The "System Libraries" of an executable work include anything, other 125 | than the work as a whole, that (a) is included in the normal form of 126 | packaging a Major Component, but which is not part of that Major 127 | Component, and (b) serves only to enable use of the work with that 128 | Major Component, or to implement a Standard Interface for which an 129 | implementation is available to the public in source code form. A 130 | "Major Component", in this context, means a major essential component 131 | (kernel, window system, and so on) of the specific operating system 132 | (if any) on which the executable work runs, or a compiler used to 133 | produce the work, or an object code interpreter used to run it. 134 | 135 | The "Corresponding Source" for a work in object code form means all 136 | the source code needed to generate, install, and (for an executable 137 | work) run the object code and to modify the work, including scripts to 138 | control those activities. However, it does not include the work's 139 | System Libraries, or general-purpose tools or generally available free 140 | programs which are used unmodified in performing those activities but 141 | which are not part of the work. For example, Corresponding Source 142 | includes interface definition files associated with source files for 143 | the work, and the source code for shared libraries and dynamically 144 | linked subprograms that the work is specifically designed to require, 145 | such as by intimate data communication or control flow between those 146 | subprograms and other parts of the work. 147 | 148 | The Corresponding Source need not include anything that users 149 | can regenerate automatically from other parts of the Corresponding 150 | Source. 151 | 152 | The Corresponding Source for a work in source code form is that 153 | same work. 154 | 155 | 2. Basic Permissions. 156 | 157 | All rights granted under this License are granted for the term of 158 | copyright on the Program, and are irrevocable provided the stated 159 | conditions are met. This License explicitly affirms your unlimited 160 | permission to run the unmodified Program. The output from running a 161 | covered work is covered by this License only if the output, given its 162 | content, constitutes a covered work. This License acknowledges your 163 | rights of fair use or other equivalent, as provided by copyright law. 164 | 165 | You may make, run and propagate covered works that you do not 166 | convey, without conditions so long as your license otherwise remains 167 | in force. You may convey covered works to others for the sole purpose 168 | of having them make modifications exclusively for you, or provide you 169 | with facilities for running those works, provided that you comply with 170 | the terms of this License in conveying all material for which you do 171 | not control copyright. Those thus making or running the covered works 172 | for you must do so exclusively on your behalf, under your direction 173 | and control, on terms that prohibit them from making any copies of 174 | your copyrighted material outside their relationship with you. 175 | 176 | Conveying under any other circumstances is permitted solely under 177 | the conditions stated below. Sublicensing is not allowed; section 10 178 | makes it unnecessary. 179 | 180 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 181 | 182 | No covered work shall be deemed part of an effective technological 183 | measure under any applicable law fulfilling obligations under article 184 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 185 | similar laws prohibiting or restricting circumvention of such 186 | measures. 187 | 188 | When you convey a covered work, you waive any legal power to forbid 189 | circumvention of technological measures to the extent such circumvention 190 | is effected by exercising rights under this License with respect to 191 | the covered work, and you disclaim any intention to limit operation or 192 | modification of the work as a means of enforcing, against the work's 193 | users, your or third parties' legal rights to forbid circumvention of 194 | technological measures. 195 | 196 | 4. Conveying Verbatim Copies. 197 | 198 | You may convey verbatim copies of the Program's source code as you 199 | receive it, in any medium, provided that you conspicuously and 200 | appropriately publish on each copy an appropriate copyright notice; 201 | keep intact all notices stating that this License and any 202 | non-permissive terms added in accord with section 7 apply to the code; 203 | keep intact all notices of the absence of any warranty; and give all 204 | recipients a copy of this License along with the Program. 205 | 206 | You may charge any price or no price for each copy that you convey, 207 | and you may offer support or warranty protection for a fee. 208 | 209 | 5. Conveying Modified Source Versions. 210 | 211 | You may convey a work based on the Program, or the modifications to 212 | produce it from the Program, in the form of source code under the 213 | terms of section 4, provided that you also meet all of these conditions: 214 | 215 | a) The work must carry prominent notices stating that you modified 216 | it, and giving a relevant date. 217 | 218 | b) The work must carry prominent notices stating that it is 219 | released under this License and any conditions added under section 220 | 7. This requirement modifies the requirement in section 4 to 221 | "keep intact all notices". 222 | 223 | c) You must license the entire work, as a whole, under this 224 | License to anyone who comes into possession of a copy. This 225 | License will therefore apply, along with any applicable section 7 226 | additional terms, to the whole of the work, and all its parts, 227 | regardless of how they are packaged. This License gives no 228 | permission to license the work in any other way, but it does not 229 | invalidate such permission if you have separately received it. 230 | 231 | d) If the work has interactive user interfaces, each must display 232 | Appropriate Legal Notices; however, if the Program has interactive 233 | interfaces that do not display Appropriate Legal Notices, your 234 | work need not make them do so. 235 | 236 | A compilation of a covered work with other separate and independent 237 | works, which are not by their nature extensions of the covered work, 238 | and which are not combined with it such as to form a larger program, 239 | in or on a volume of a storage or distribution medium, is called an 240 | "aggregate" if the compilation and its resulting copyright are not 241 | used to limit the access or legal rights of the compilation's users 242 | beyond what the individual works permit. Inclusion of a covered work 243 | in an aggregate does not cause this License to apply to the other 244 | parts of the aggregate. 245 | 246 | 6. Conveying Non-Source Forms. 247 | 248 | You may convey a covered work in object code form under the terms 249 | of sections 4 and 5, provided that you also convey the 250 | machine-readable Corresponding Source under the terms of this License, 251 | in one of these ways: 252 | 253 | a) Convey the object code in, or embodied in, a physical product 254 | (including a physical distribution medium), accompanied by the 255 | Corresponding Source fixed on a durable physical medium 256 | customarily used for software interchange. 257 | 258 | b) Convey the object code in, or embodied in, a physical product 259 | (including a physical distribution medium), accompanied by a 260 | written offer, valid for at least three years and valid for as 261 | long as you offer spare parts or customer support for that product 262 | model, to give anyone who possesses the object code either (1) a 263 | copy of the Corresponding Source for all the software in the 264 | product that is covered by this License, on a durable physical 265 | medium customarily used for software interchange, for a price no 266 | more than your reasonable cost of physically performing this 267 | conveying of source, or (2) access to copy the 268 | Corresponding Source from a network server at no charge. 269 | 270 | c) Convey individual copies of the object code with a copy of the 271 | written offer to provide the Corresponding Source. This 272 | alternative is allowed only occasionally and noncommercially, and 273 | only if you received the object code with such an offer, in accord 274 | with subsection 6b. 275 | 276 | d) Convey the object code by offering access from a designated 277 | place (gratis or for a charge), and offer equivalent access to the 278 | Corresponding Source in the same way through the same place at no 279 | further charge. You need not require recipients to copy the 280 | Corresponding Source along with the object code. If the place to 281 | copy the object code is a network server, the Corresponding Source 282 | may be on a different server (operated by you or a third party) 283 | that supports equivalent copying facilities, provided you maintain 284 | clear directions next to the object code saying where to find the 285 | Corresponding Source. Regardless of what server hosts the 286 | Corresponding Source, you remain obligated to ensure that it is 287 | available for as long as needed to satisfy these requirements. 288 | 289 | e) Convey the object code using peer-to-peer transmission, provided 290 | you inform other peers where the object code and Corresponding 291 | Source of the work are being offered to the general public at no 292 | charge under subsection 6d. 293 | 294 | A separable portion of the object code, whose source code is excluded 295 | from the Corresponding Source as a System Library, need not be 296 | included in conveying the object code work. 297 | 298 | A "User Product" is either (1) a "consumer product", which means any 299 | tangible personal property which is normally used for personal, family, 300 | or household purposes, or (2) anything designed or sold for incorporation 301 | into a dwelling. In determining whether a product is a consumer product, 302 | doubtful cases shall be resolved in favor of coverage. For a particular 303 | product received by a particular user, "normally used" refers to a 304 | typical or common use of that class of product, regardless of the status 305 | of the particular user or of the way in which the particular user 306 | actually uses, or expects or is expected to use, the product. A product 307 | is a consumer product regardless of whether the product has substantial 308 | commercial, industrial or non-consumer uses, unless such uses represent 309 | the only significant mode of use of the product. 310 | 311 | "Installation Information" for a User Product means any methods, 312 | procedures, authorization keys, or other information required to install 313 | and execute modified versions of a covered work in that User Product from 314 | a modified version of its Corresponding Source. The information must 315 | suffice to ensure that the continued functioning of the modified object 316 | code is in no case prevented or interfered with solely because 317 | modification has been made. 318 | 319 | If you convey an object code work under this section in, or with, or 320 | specifically for use in, a User Product, and the conveying occurs as 321 | part of a transaction in which the right of possession and use of the 322 | User Product is transferred to the recipient in perpetuity or for a 323 | fixed term (regardless of how the transaction is characterized), the 324 | Corresponding Source conveyed under this section must be accompanied 325 | by the Installation Information. But this requirement does not apply 326 | if neither you nor any third party retains the ability to install 327 | modified object code on the User Product (for example, the work has 328 | been installed in ROM). 329 | 330 | The requirement to provide Installation Information does not include a 331 | requirement to continue to provide support service, warranty, or updates 332 | for a work that has been modified or installed by the recipient, or for 333 | the User Product in which it has been modified or installed. Access to a 334 | network may be denied when the modification itself materially and 335 | adversely affects the operation of the network or violates the rules and 336 | protocols for communication across the network. 337 | 338 | Corresponding Source conveyed, and Installation Information provided, 339 | in accord with this section must be in a format that is publicly 340 | documented (and with an implementation available to the public in 341 | source code form), and must require no special password or key for 342 | unpacking, reading or copying. 343 | 344 | 7. Additional Terms. 345 | 346 | "Additional permissions" are terms that supplement the terms of this 347 | License by making exceptions from one or more of its conditions. 348 | Additional permissions that are applicable to the entire Program shall 349 | be treated as though they were included in this License, to the extent 350 | that they are valid under applicable law. If additional permissions 351 | apply only to part of the Program, that part may be used separately 352 | under those permissions, but the entire Program remains governed by 353 | this License without regard to the additional permissions. 354 | 355 | When you convey a copy of a covered work, you may at your option 356 | remove any additional permissions from that copy, or from any part of 357 | it. (Additional permissions may be written to require their own 358 | removal in certain cases when you modify the work.) You may place 359 | additional permissions on material, added by you to a covered work, 360 | for which you have or can give appropriate copyright permission. 361 | 362 | Notwithstanding any other provision of this License, for material you 363 | add to a covered work, you may (if authorized by the copyright holders of 364 | that material) supplement the terms of this License with terms: 365 | 366 | a) Disclaiming warranty or limiting liability differently from the 367 | terms of sections 15 and 16 of this License; or 368 | 369 | b) Requiring preservation of specified reasonable legal notices or 370 | author attributions in that material or in the Appropriate Legal 371 | Notices displayed by works containing it; or 372 | 373 | c) Prohibiting misrepresentation of the origin of that material, or 374 | requiring that modified versions of such material be marked in 375 | reasonable ways as different from the original version; or 376 | 377 | d) Limiting the use for publicity purposes of names of licensors or 378 | authors of the material; or 379 | 380 | e) Declining to grant rights under trademark law for use of some 381 | trade names, trademarks, or service marks; or 382 | 383 | f) Requiring indemnification of licensors and authors of that 384 | material by anyone who conveys the material (or modified versions of 385 | it) with contractual assumptions of liability to the recipient, for 386 | any liability that these contractual assumptions directly impose on 387 | those licensors and authors. 388 | 389 | All other non-permissive additional terms are considered "further 390 | restrictions" within the meaning of section 10. If the Program as you 391 | received it, or any part of it, contains a notice stating that it is 392 | governed by this License along with a term that is a further 393 | restriction, you may remove that term. If a license document contains 394 | a further restriction but permits relicensing or conveying under this 395 | License, you may add to a covered work material governed by the terms 396 | of that license document, provided that the further restriction does 397 | not survive such relicensing or conveying. 398 | 399 | If you add terms to a covered work in accord with this section, you 400 | must place, in the relevant source files, a statement of the 401 | additional terms that apply to those files, or a notice indicating 402 | where to find the applicable terms. 403 | 404 | Additional terms, permissive or non-permissive, may be stated in the 405 | form of a separately written license, or stated as exceptions; 406 | the above requirements apply either way. 407 | 408 | 8. Termination. 409 | 410 | You may not propagate or modify a covered work except as expressly 411 | provided under this License. Any attempt otherwise to propagate or 412 | modify it is void, and will automatically terminate your rights under 413 | this License (including any patent licenses granted under the third 414 | paragraph of section 11). 415 | 416 | However, if you cease all violation of this License, then your 417 | license from a particular copyright holder is reinstated (a) 418 | provisionally, unless and until the copyright holder explicitly and 419 | finally terminates your license, and (b) permanently, if the copyright 420 | holder fails to notify you of the violation by some reasonable means 421 | prior to 60 days after the cessation. 422 | 423 | Moreover, your license from a particular copyright holder is 424 | reinstated permanently if the copyright holder notifies you of the 425 | violation by some reasonable means, this is the first time you have 426 | received notice of violation of this License (for any work) from that 427 | copyright holder, and you cure the violation prior to 30 days after 428 | your receipt of the notice. 429 | 430 | Termination of your rights under this section does not terminate the 431 | licenses of parties who have received copies or rights from you under 432 | this License. If your rights have been terminated and not permanently 433 | reinstated, you do not qualify to receive new licenses for the same 434 | material under section 10. 435 | 436 | 9. Acceptance Not Required for Having Copies. 437 | 438 | You are not required to accept this License in order to receive or 439 | run a copy of the Program. Ancillary propagation of a covered work 440 | occurring solely as a consequence of using peer-to-peer transmission 441 | to receive a copy likewise does not require acceptance. However, 442 | nothing other than this License grants you permission to propagate or 443 | modify any covered work. These actions infringe copyright if you do 444 | not accept this License. Therefore, by modifying or propagating a 445 | covered work, you indicate your acceptance of this License to do so. 446 | 447 | 10. Automatic Licensing of Downstream Recipients. 448 | 449 | Each time you convey a covered work, the recipient automatically 450 | receives a license from the original licensors, to run, modify and 451 | propagate that work, subject to this License. You are not responsible 452 | for enforcing compliance by third parties with this License. 453 | 454 | An "entity transaction" is a transaction transferring control of an 455 | organization, or substantially all assets of one, or subdividing an 456 | organization, or merging organizations. If propagation of a covered 457 | work results from an entity transaction, each party to that 458 | transaction who receives a copy of the work also receives whatever 459 | licenses to the work the party's predecessor in interest had or could 460 | give under the previous paragraph, plus a right to possession of the 461 | Corresponding Source of the work from the predecessor in interest, if 462 | the predecessor has it or can get it with reasonable efforts. 463 | 464 | You may not impose any further restrictions on the exercise of the 465 | rights granted or affirmed under this License. For example, you may 466 | not impose a license fee, royalty, or other charge for exercise of 467 | rights granted under this License, and you may not initiate litigation 468 | (including a cross-claim or counterclaim in a lawsuit) alleging that 469 | any patent claim is infringed by making, using, selling, offering for 470 | sale, or importing the Program or any portion of it. 471 | 472 | 11. Patents. 473 | 474 | A "contributor" is a copyright holder who authorizes use under this 475 | License of the Program or a work on which the Program is based. The 476 | work thus licensed is called the contributor's "contributor version". 477 | 478 | A contributor's "essential patent claims" are all patent claims 479 | owned or controlled by the contributor, whether already acquired or 480 | hereafter acquired, that would be infringed by some manner, permitted 481 | by this License, of making, using, or selling its contributor version, 482 | but do not include claims that would be infringed only as a 483 | consequence of further modification of the contributor version. For 484 | purposes of this definition, "control" includes the right to grant 485 | patent sublicenses in a manner consistent with the requirements of 486 | this License. 487 | 488 | Each contributor grants you a non-exclusive, worldwide, royalty-free 489 | patent license under the contributor's essential patent claims, to 490 | make, use, sell, offer for sale, import and otherwise run, modify and 491 | propagate the contents of its contributor version. 492 | 493 | In the following three paragraphs, a "patent license" is any express 494 | agreement or commitment, however denominated, not to enforce a patent 495 | (such as an express permission to practice a patent or covenant not to 496 | sue for patent infringement). To "grant" such a patent license to a 497 | party means to make such an agreement or commitment not to enforce a 498 | patent against the party. 499 | 500 | If you convey a covered work, knowingly relying on a patent license, 501 | and the Corresponding Source of the work is not available for anyone 502 | to copy, free of charge and under the terms of this License, through a 503 | publicly available network server or other readily accessible means, 504 | then you must either (1) cause the Corresponding Source to be so 505 | available, or (2) arrange to deprive yourself of the benefit of the 506 | patent license for this particular work, or (3) arrange, in a manner 507 | consistent with the requirements of this License, to extend the patent 508 | license to downstream recipients. "Knowingly relying" means you have 509 | actual knowledge that, but for the patent license, your conveying the 510 | covered work in a country, or your recipient's use of the covered work 511 | in a country, would infringe one or more identifiable patents in that 512 | country that you have reason to believe are valid. 513 | 514 | If, pursuant to or in connection with a single transaction or 515 | arrangement, you convey, or propagate by procuring conveyance of, a 516 | covered work, and grant a patent license to some of the parties 517 | receiving the covered work authorizing them to use, propagate, modify 518 | or convey a specific copy of the covered work, then the patent license 519 | you grant is automatically extended to all recipients of the covered 520 | work and works based on it. 521 | 522 | A patent license is "discriminatory" if it does not include within 523 | the scope of its coverage, prohibits the exercise of, or is 524 | conditioned on the non-exercise of one or more of the rights that are 525 | specifically granted under this License. You may not convey a covered 526 | work if you are a party to an arrangement with a third party that is 527 | in the business of distributing software, under which you make payment 528 | to the third party based on the extent of your activity of conveying 529 | the work, and under which the third party grants, to any of the 530 | parties who would receive the covered work from you, a discriminatory 531 | patent license (a) in connection with copies of the covered work 532 | conveyed by you (or copies made from those copies), or (b) primarily 533 | for and in connection with specific products or compilations that 534 | contain the covered work, unless you entered into that arrangement, 535 | or that patent license was granted, prior to 28 March 2007. 536 | 537 | Nothing in this License shall be construed as excluding or limiting 538 | any implied license or other defenses to infringement that may 539 | otherwise be available to you under applicable patent law. 540 | 541 | 12. No Surrender of Others' Freedom. 542 | 543 | If conditions are imposed on you (whether by court order, agreement or 544 | otherwise) that contradict the conditions of this License, they do not 545 | excuse you from the conditions of this License. If you cannot convey a 546 | covered work so as to satisfy simultaneously your obligations under this 547 | License and any other pertinent obligations, then as a consequence you may 548 | not convey it at all. For example, if you agree to terms that obligate you 549 | to collect a royalty for further conveying from those to whom you convey 550 | the Program, the only way you could satisfy both those terms and this 551 | License would be to refrain entirely from conveying the Program. 552 | 553 | 13. Use with the GNU Affero General Public License. 554 | 555 | Notwithstanding any other provision of this License, you have 556 | permission to link or combine any covered work with a work licensed 557 | under version 3 of the GNU Affero General Public License into a single 558 | combined work, and to convey the resulting work. The terms of this 559 | License will continue to apply to the part which is the covered work, 560 | but the special requirements of the GNU Affero General Public License, 561 | section 13, concerning interaction through a network will apply to the 562 | combination as such. 563 | 564 | 14. Revised Versions of this License. 565 | 566 | The Free Software Foundation may publish revised and/or new versions of 567 | the GNU General Public License from time to time. Such new versions will 568 | be similar in spirit to the present version, but may differ in detail to 569 | address new problems or concerns. 570 | 571 | Each version is given a distinguishing version number. If the 572 | Program specifies that a certain numbered version of the GNU General 573 | Public License "or any later version" applies to it, you have the 574 | option of following the terms and conditions either of that numbered 575 | version or of any later version published by the Free Software 576 | Foundation. If the Program does not specify a version number of the 577 | GNU General Public License, you may choose any version ever published 578 | by the Free Software Foundation. 579 | 580 | If the Program specifies that a proxy can decide which future 581 | versions of the GNU General Public License can be used, that proxy's 582 | public statement of acceptance of a version permanently authorizes you 583 | to choose that version for the Program. 584 | 585 | Later license versions may give you additional or different 586 | permissions. However, no additional obligations are imposed on any 587 | author or copyright holder as a result of your choosing to follow a 588 | later version. 589 | 590 | 15. Disclaimer of Warranty. 591 | 592 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 593 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 594 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 595 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 596 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 597 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 598 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 599 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 600 | 601 | 16. Limitation of Liability. 602 | 603 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 604 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 605 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 606 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 607 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 608 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 609 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 610 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 611 | SUCH DAMAGES. 612 | 613 | 17. Interpretation of Sections 15 and 16. 614 | 615 | If the disclaimer of warranty and limitation of liability provided 616 | above cannot be given local legal effect according to their terms, 617 | reviewing courts shall apply local law that most closely approximates 618 | an absolute waiver of all civil liability in connection with the 619 | Program, unless a warranty or assumption of liability accompanies a 620 | copy of the Program in return for a fee. 621 | 622 | END OF TERMS AND CONDITIONS 623 | 624 | How to Apply These Terms to Your New Programs 625 | 626 | If you develop a new program, and you want it to be of the greatest 627 | possible use to the public, the best way to achieve this is to make it 628 | free software which everyone can redistribute and change under these terms. 629 | 630 | To do so, attach the following notices to the program. It is safest 631 | to attach them to the start of each source file to most effectively 632 | state the exclusion of warranty; and each file should have at least 633 | the "copyright" line and a pointer to where the full notice is found. 634 | 635 | 636 | Copyright (C) 637 | 638 | This program is free software: you can redistribute it and/or modify 639 | it under the terms of the GNU General Public License as published by 640 | the Free Software Foundation, either version 3 of the License, or 641 | (at your option) any later version. 642 | 643 | This program is distributed in the hope that it will be useful, 644 | but WITHOUT ANY WARRANTY; without even the implied warranty of 645 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 646 | GNU General Public License for more details. 647 | 648 | You should have received a copy of the GNU General Public License 649 | along with this program. If not, see . 650 | 651 | Also, add information on how to contact you by electronic and paper mail. 652 | 653 | If the program does terminal interaction, make it output a short 654 | notice like this when it starts in an interactive mode: 655 | 656 | Copyright (C) 657 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 658 | This is free software, and you are welcome to redistribute it 659 | under certain conditions; type `show c' for details. 660 | 661 | The hypothetical commands `show w' and `show c' should show the appropriate 662 | parts of the General Public License. Of course, your program's commands 663 | might be different; for a GUI interface, you would use an "about box". 664 | 665 | You should also get your employer (if you work as a programmer) or school, 666 | if any, to sign a "copyright disclaimer" for the program, if necessary. 667 | For more information on this, and how to apply and follow the GNU GPL, see 668 | . 669 | 670 | The GNU General Public License does not permit incorporating your program 671 | into proprietary programs. If your program is a subroutine library, you 672 | may consider it more useful to permit linking proprietary applications with 673 | the library. If this is what you want to do, use the GNU Lesser General 674 | Public License instead of this License. But first, please read 675 | . 676 | --------------------------------------------------------------------------------