#ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include #define ARGV_COUNT_MAX 100 #define BUFFER_SIZE 4096 static void test( const char *cmdline, size_t argv_count_max, size_t buffer_size, bool expected_result, const char *expected_error_msg, size_t expected_argc, const char *const *const expected_argv ); int main() { {% for case in cases %} { {% if not case.error %} const char *const expected_argv[{{ escape_int(len(case.result) or 1) }}] = { {% if len(case.result) %} {% for item in case.result %} {{ escape_str(item) }}, {% endfor %} {% else %} NULL, {% endif %} }; {% endif %} test( {{ escape_str(case.cmdline) }}, {{ escape_int(case.argv_count_max or 0) }}, {{ escape_int(case.buffer_size or 0) }}, {% if not case.error %} true, "", {{ escape_int(len(case.result)) }}, expected_argv {% else %} false, {{ escape_str(case.error) }}, 0, NULL {% endif %} ); } {% endfor %} return 0; } void test( const char *const cmdline, size_t argv_count_max, size_t buffer_size, const bool expected_result, const char *const expected_error_msg, size_t expected_argc, const char *const *const expected_argv ) { if (argv_count_max == 0) argv_count_max = ARGV_COUNT_MAX; if (buffer_size == 0) buffer_size = BUFFER_SIZE; char *error_msg = malloc(KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); size_t argc = 1234; char **const argv = malloc(sizeof(char*) * argv_count_max); char *const buffer = malloc(buffer_size); assert(error_msg); assert(argv); assert(buffer); memset(error_msg, 'x', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); memset(argv, 'x', sizeof(char*) * argv_count_max); memset(buffer, 'x', buffer_size); assert( kernaux_cmdline( cmdline, error_msg, &argc, argv, buffer, argv_count_max, buffer_size ) == !!expected_result ); assert(strcmp(error_msg, expected_error_msg) == 0); assert(argc == expected_argc); if (expected_argv) { for (size_t index = 0; index < argc; ++index) { assert(strcmp(argv[index], expected_argv[index]) == 0); } } for (size_t index = argc; index < argv_count_max; ++index) { assert(argv[index] == NULL); } free(error_msg); free(argv); free(buffer); }