-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
96 lines (79 loc) · 2.2 KB
/
test.c
File metadata and controls
96 lines (79 loc) · 2.2 KB
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
/**
* Zdrojový soubor test.c
* Obsahuje logiku pro automatické testování.
*
* OBSAH V TOMTO SOUBROU NEUPRAVUJTE!
*/
#include "test.h"
#include "types.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int test_example_success(int argc, char **argv) {
print_args(argv, argc);
return 0;
}
int test_example_failure(int argc, char **argv) {
print_args(argv, argc);
return 1;
}
const char *test_names[] = {
"test_example_success",
"test_example_failure",
};
int (*tests[])(int, char**) = {
&test_example_success,
&test_example_failure,
};
#define TEST_COUNT (sizeof(tests) / sizeof(*tests))
int run_test_by_name(const char *test_name, int argc, char **argv) {
if (test_name != NULL) {
for (size_t testId = 0; testId < TEST_COUNT; testId++)
{
if (strcmp(test_names[testId], test_name) == 0) {
return tests[testId](argc, argv);
}
}
}
fprintf(stderr, "could not find test '%s'\n", test_name);
fprintf(stderr, "supported tets:\n");
for (size_t testId = 0; testId < TEST_COUNT; testId++)
{
fprintf(stderr, " - %s\n", test_names[testId]);
}
return TEST_ERR_NOT_FOUND;
}
#pragma region Base support methods for testing purposes
void __print_array(FILE *target, int *array, int length) {
fprintf(target, "[");
for (int i = 0; i < length - 1; i++)
{
fprintf(target, "%2d, ", array[i]);
}
if (length > 0) {
fprintf(target, "%2d", array[length - 1]);
}
fprintf(target, "]");
}
int __load_array(int **array) {
int __length;
scanf(" load %d items: ", &__length);
*array = (int *) malloc(__length * sizeof(int));
if (*array == NULL) exit(TEST_ERR_SYSTEM_FAILURE);
for (int i = 0; i < __length; i++) {
if (scanf("%d", (*array) + i) != 1) {
free(*array);
fprintf(stderr, "failed reading value for index %d\n", i);
exit(TEST_ERR_WRONG_INVOCATION);
}
}
fprintf(stderr, "loaded: "); __print_array(stderr, *array, __length); fprintf(stderr, "\n");
return __length;
}
#pragma endregion
#ifdef TEST_BUILD
int main(int argc, char **argv) {
const char *test_name = getenv("TEST_NAME");
return run_test_by_name(test_name, argc, argv);
}
#endif