-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
167 lines (141 loc) · 5.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* This file is part of Sort-Algo-Benchmark.
*
* Sort-Algo-Benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sort-Algo-Benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sort-Algo-Benchmark. If not, see
* <https://www.gnu.org/licenses/>.
*/
/**
* @file main.c
* @author Paul Coral
* @brief A simple program to test speed of sorting algorithm
* @date 2020-11-01
*
* @copyright Copyright (c) 2020
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "algo_interface/algo_interface.h"
#include "utils/algo_dir_utils.h"
#include "utils/algo_error.h"
#include "utils/rand_array.h"
// ==================================================================
#define OPTION_LIST "-l"
#define OPTION_SHOW_AFTER "-sa"
#define OPTION_SHOW_BEFORE "-sb"
#define SUCCESS_EXIT_MESSAGE "\nFINISHED, bye ;)\n"
typedef struct {
char list;
char show_before;
char show_after;
size_t array_size;
} algo_args_t;
algo_error_t parse_input(const int argc, char **argv, algo_interface_t *inter, algo_args_t *options);
void list_algos(void);
// ==================================================================
#define PRINT_RA(A, O, BA, P) \
do { \
if (O.BA) { \
puts("\n\n" P " : "); \
rand_array_print(A); \
} \
} while (0)
#define PRINT_BEFORE(A, O) PRINT_RA(A, O, show_before, "Before")
#define PRINT_AFTER(A, O) PRINT_RA(A, O, show_after, "After")
// ---------------------------------------
int main(int argc, char **argv) {
algo_error_t error = SUCCESS;
algo_interface_t algo_to_use = NULL;
algo_args_t options;
if ((error = parse_input(argc, argv, &algo_to_use, &options)) != SUCCESS) {
algo_error_print(stderr, error, NULL);
return EXIT_FAILURE;
}
if (algo_to_use == NULL) {
return EXIT_SUCCESS;
}
// get random int array
rand_array_t array;
if ((error = rand_array_init(options.array_size, &array)) != SUCCESS) {
algo_error_print(stderr, error, NULL);
return EXIT_FAILURE;
}
printf("Starting sort...");
PRINT_BEFORE(array, options);
clock_t t1;
clock_t t2;
t1 = clock();
algo_to_use(array);
t2 = clock();
PRINT_AFTER(array, options);
puts("Sorting finished !!!\n");
rand_array_print_is_sorted(array);
const double time_elapsed = ((double)(t2 - t1)) / CLOCKS_PER_SEC;
printf("\n\nSorting performed in %.3lf sec.\n\n", time_elapsed);
rand_array_free(&array);
puts(SUCCESS_EXIT_MESSAGE);
return EXIT_SUCCESS;
}
// ==================================================================
#define COMP_ARGS(X, Y) (strcmp(argv[(X)], (Y)) == 0)
/**
* @brief Parse the input arguments, give pointer to sorting function and fill
* options struct
*
* @param argc The number of arguement, the argc of main
* @param argv The list of arguement, the argv of main
* @param inter The function pointer to sorting function, interface between
* shared library and the program
* @param options A pointer to struct options, which will be initialized and
* filled by this function
* @return algo_error_t An error code
*/
algo_error_t parse_input(const int argc, char **argv, algo_interface_t *inter, algo_args_t *options) {
if (argv == NULL || options == NULL) {
return INV_ARG;
}
// when `-l` argument is given
if ((argc == 2) && (strcmp(OPTION_LIST, argv[1])) == 0) {
list_algos();
options->list = 1;
return SUCCESS;
}
if (argc < 3) {
printf("Usage : %s <algorithm index> <size of array (non-zero)> "
"[OPTIONS...] \n",
argv[0]);
printf("Usage : %s %s to show list of available algorithm \n", argv[0], OPTION_LIST);
printf("OPTION : %s to show list after sort\n", OPTION_SHOW_AFTER);
printf("OPTION : %s to show list before sort\n", OPTION_SHOW_BEFORE);
return SUCCESS;
}
memset(options, 0, sizeof(algo_args_t));
const size_t index = atol(argv[1]);
if ((options->array_size = atol(argv[2])) == 0) {
fprintf(stderr, "Error array size can't be zero");
return INV_ARG;
}
for (size_t i = 3; i < (argc); i++) {
if (COMP_ARGS(i, OPTION_SHOW_AFTER)) {
options->show_after = 1;
} else if (COMP_ARGS(i, OPTION_SHOW_BEFORE)) {
options->show_before = 1;
} else {
printf("Warning : unrecognized options : %s\n", argv[i]);
}
}
return select_algo(index, inter);
}