From: Kevin Day Date: Mon, 17 Jan 2022 17:49:35 +0000 (-0600) Subject: Update: Add f_console unit tests. X-Git-Tag: 0.5.8~96 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=5444d6723650925040ae4a8f31da0a725b62b2fa;p=fll Update: Add f_console unit tests. I did not handle all possible permutations for the f_console_parameter_process() tests. I instead went with a good enough for now approach. --- diff --git a/level_0/f_console/data/build/dependencies-tests b/level_0/f_console/data/build/dependencies-tests new file mode 100644 index 0000000..dea3179 --- /dev/null +++ b/level_0/f_console/data/build/dependencies-tests @@ -0,0 +1,3 @@ +# fss-0001 + +cmocka 1.* diff --git a/level_0/f_console/data/build/settings-tests b/level_0/f_console/data/build/settings-tests new file mode 100644 index 0000000..495df17 --- /dev/null +++ b/level_0/f_console/data/build/settings-tests @@ -0,0 +1,45 @@ +# fss-0001 + +project_name test-f_console + +version_major 0 +version_minor 5 +version_micro 8 +version_file major +version_target major + +modes individual level monolithic +modes_default individual + +build_compiler gcc +build_indexer ar +build_indexer_arguments rcs +build_language c +build_libraries -lc -lcmocka +build_libraries-individual -lf_memory -lf_string -lf_type_array -lf_utf -lf_console +build_libraries-level -lfll_0 +build_libraries-monolithic -lfll +build_sources_program test-console-identify.c test-console-parameter_prioritize_left.c test-console-parameter_prioritize_right.c test-console-parameter_process.c test-console.c +build_script no +build_shared yes +build_static no + +path_headers tests/c +path_headers_preserve no +path_sources tests/c +path_standard no + +search_exclusive yes +search_shared yes +search_static yes + +defines -Ibuild/includes +defines_static -Lbuild/libraries/static +defines_shared -Lbuild/libraries/shared + +flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-logical-op-parentheses -Wno-parentheses +flags_program -fPIE +flags_program_shared +flags_program_static +flags_shared +flags_static diff --git a/level_0/f_console/data/build/testfile b/level_0/f_console/data/build/testfile new file mode 100644 index 0000000..ed22f9c --- /dev/null +++ b/level_0/f_console/data/build/testfile @@ -0,0 +1,45 @@ +# fss-0005 iki-0002 + +settings: + load_build yes + fail exit + + environment LD_LIBRARY_PATH + +main: + build settings + build settings-tests + + operate ld_library_path + + if exists build/programs/shared/test-f_console + shell build/programs/shared/test-f_console + + if exists build/programs/static/test-f_console + shell build/programs/static/test-f_console + + if not exists build/programs/shared/test-f_console + and not exists build/programs/static/test-f_console + operate not_created + +not_created: + print + print context:"error"Failed to test due to being unable to find either a shared or static test binary to perform tests. context:"error" + + exit failure + +ld_library_path: + if defined environment LD_LIBRARY_PATH + and defined parameter work + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:"work:value"libraries/shared:define:"LD_LIBRARY_PATH"' + + else + if defined environment LD_LIBRARY_PATH + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:define:"LD_LIBRARY_PATH"' + + else + if defined parameter work + define LD_LIBRARY_PATH 'build/libraries/shared:parameter:"work:value"libraries/shared' + + else + define LD_LIBRARY_PATH build/libraries/shared diff --git a/level_0/f_console/tests/c/test-console-identify.c b/level_0/f_console/tests/c/test-console-identify.c new file mode 100644 index 0000000..0109ece --- /dev/null +++ b/level_0/f_console/tests/c/test-console-identify.c @@ -0,0 +1,100 @@ +#include "test-console.h" +#include "test-console-identify.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_console_identify__parameter_checking(void **state) { + + const f_string_t input = f_string_t_initialize; + + { + const f_status_t status = f_console_identify(input, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_console_identify__works(void **state) { + + f_console_id_t id = f_console_id_t_initialize; + + { + const f_string_t input = f_string_t_initialize; + + const f_status_t status = f_console_identify(input, &id); + + assert_int_equal(status, F_data_not); + assert_int_equal(id, f_console_none_e); + } + + { + const f_status_t status = f_console_identify("input", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_none_e); + } + + { + const f_status_t status = f_console_identify("-", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_empty_short_enable_e); + } + + { + const f_status_t status = f_console_identify("+", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_empty_short_disable_e); + } + + { + const f_status_t status = f_console_identify("-i", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_short_enable_e); + } + + { + const f_status_t status = f_console_identify("+i", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_short_disable_e); + } + + { + const f_status_t status = f_console_identify("--input", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_long_enable_e); + } + + { + const f_status_t status = f_console_identify("++input", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_long_disable_e); + } + + { + const f_status_t status = f_console_identify("--", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_empty_long_enable_e); + } + + { + const f_status_t status = f_console_identify("++", &id); + + assert_int_equal(status, F_none); + assert_int_equal(id, f_console_empty_long_disable_e); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/c/test-console-identify.h b/level_0/f_console/tests/c/test-console-identify.h new file mode 100644 index 0000000..7472bb6 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-identify.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Console + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_identify_ +#define _TEST__F_console_identify_ + +// f_console_identify() doesn't return failure. + +/** + * Test that parameter checking works as expected. + * + * @see f_console_identify() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_console_identify__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_console_identify() + */ +extern void test__f_console_identify__works(void **state); + +#endif // _TEST__F_console_identify_ diff --git a/level_0/f_console/tests/c/test-console-parameter_prioritize_left.c b/level_0/f_console/tests/c/test-console-parameter_prioritize_left.c new file mode 100644 index 0000000..b5d9af1 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_prioritize_left.c @@ -0,0 +1,111 @@ +#include "test-console.h" +#include "test-console-parameter_prioritize_left.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_console_parameter_prioritize_left__fails(void **state) { + + f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + f_console_parameter_id_t decision = f_console_parameter_id_t_initialize; + + { + f_console_parameter_id_t ids_array[1] = { 20 }; + choices.id = ids_array; + choices.used = 1; + + f_console_parameter_t parameter = f_console_parameter_t_initialize; + f_console_parameter_t parameters_array[1] = { parameter }; + parameters.parameter = parameters_array; + parameters.used = 1; + + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, &decision); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } +} + +#ifndef _di_level_0_parameter_checking_ + void test__f_console_parameter_prioritize_left__parameter_checking(void **state) { + + const f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + + { + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + f_console_parameter_id_t ids_array[1] = { f_console_parameter_id_t_initialize }; + choices.id = ids_array; + + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_console_parameter_prioritize_left__works(void **state) { + + f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + f_console_parameter_id_t decision = f_console_parameter_id_t_initialize; + + { + f_console_parameter_id_t ids_array[1] = { 0 }; + choices.id = ids_array; + + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, &decision); + + assert_int_equal(status, F_data_not); + } + + { + f_console_parameter_id_t ids_array[1] = { 0 }; + choices.id = ids_array; + choices.used = 1; + + f_console_parameter_t parameter = f_console_parameter_t_initialize; + f_console_parameter_t parameters_array[1] = { parameter }; + parameters.parameter = parameters_array; + parameters.used = 1; + + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, &decision); + + assert_int_equal(status, F_data_not); + } + + { + f_console_parameter_id_t ids_array[2] = { 0, 1 }; + choices.id = ids_array; + choices.used = 2; + + f_console_parameter_t parameter_1 = f_console_parameter_t_initialize; + f_console_parameter_t parameter_2 = f_console_parameter_t_initialize; + + parameter_1.result = f_console_result_found_e; + parameter_1.location = 5; + parameter_1.location_sub = 6; + parameter_2.result = f_console_result_found_e; + parameter_2.location = 3; + parameter_2.location_sub = 4; + + f_console_parameter_t parameters_array[2] = { parameter_1, parameter_2 }; + parameters.parameter = parameters_array; + parameters.used = 2; + + const f_status_t status = f_console_parameter_prioritize_left(parameters, choices, &decision); + + assert_int_equal(status, F_none); + assert_int_equal(decision, 1); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/c/test-console-parameter_prioritize_left.h b/level_0/f_console/tests/c/test-console-parameter_prioritize_left.h new file mode 100644 index 0000000..46b9085 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_prioritize_left.h @@ -0,0 +1,36 @@ +/** + * FLL - Level 0 + * + * Project: Console + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_parameter_prioritize_left_ +#define _TEST__F_console_parameter_prioritize_left_ + +/** + * Test that function fails. + * + * @see f_console_parameter_prioritize_left() + */ +extern void test__f_console_parameter_prioritize_left__fails(void **state); + +/** + * Test that parameter checking works as expected. + * + * @see f_console_parameter_prioritize_left() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_console_parameter_prioritize_left__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_console_parameter_prioritize_left() + */ +extern void test__f_console_parameter_prioritize_left__works(void **state); + +#endif // _TEST__F_console_parameter_prioritize_left_ diff --git a/level_0/f_console/tests/c/test-console-parameter_prioritize_right.c b/level_0/f_console/tests/c/test-console-parameter_prioritize_right.c new file mode 100644 index 0000000..eb80b46 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_prioritize_right.c @@ -0,0 +1,111 @@ +#include "test-console.h" +#include "test-console-parameter_prioritize_right.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_console_parameter_prioritize_right__fails(void **state) { + + f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + f_console_parameter_id_t decision = f_console_parameter_id_t_initialize; + + { + f_console_parameter_id_t ids_array[1] = { 20 }; + choices.id = ids_array; + choices.used = 1; + + f_console_parameter_t parameter = f_console_parameter_t_initialize; + f_console_parameter_t parameters_array[1] = { parameter }; + parameters.parameter = parameters_array; + parameters.used = 1; + + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, &decision); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } +} + +#ifndef _di_level_0_parameter_checking_ + void test__f_console_parameter_prioritize_right__parameter_checking(void **state) { + + const f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + + { + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + + { + f_console_parameter_id_t ids_array[1] = { f_console_parameter_id_t_initialize }; + choices.id = ids_array; + + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_console_parameter_prioritize_right__works(void **state) { + + f_console_parameters_t parameters = f_console_parameters_t_initialize; + f_console_parameter_ids_t choices = f_console_parameter_ids_t_initialize; + f_console_parameter_id_t decision = f_console_parameter_id_t_initialize; + + { + f_console_parameter_id_t ids_array[1] = { 0 }; + choices.id = ids_array; + + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, &decision); + + assert_int_equal(status, F_data_not); + } + + { + f_console_parameter_id_t ids_array[1] = { 0 }; + choices.id = ids_array; + choices.used = 1; + + f_console_parameter_t parameter = f_console_parameter_t_initialize; + f_console_parameter_t parameters_array[1] = { parameter }; + parameters.parameter = parameters_array; + parameters.used = 1; + + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, &decision); + + assert_int_equal(status, F_data_not); + } + + { + f_console_parameter_id_t ids_array[2] = { 0, 1 }; + choices.id = ids_array; + choices.used = 2; + + f_console_parameter_t parameter_1 = f_console_parameter_t_initialize; + f_console_parameter_t parameter_2 = f_console_parameter_t_initialize; + + parameter_1.result = f_console_result_found_e; + parameter_1.location = 5; + parameter_1.location_sub = 6; + parameter_2.result = f_console_result_found_e; + parameter_2.location = 3; + parameter_2.location_sub = 4; + + f_console_parameter_t parameters_array[2] = { parameter_1, parameter_2 }; + parameters.parameter = parameters_array; + parameters.used = 2; + + const f_status_t status = f_console_parameter_prioritize_right(parameters, choices, &decision); + + assert_int_equal(status, F_none); + assert_int_equal(decision, 0); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/c/test-console-parameter_prioritize_right.h b/level_0/f_console/tests/c/test-console-parameter_prioritize_right.h new file mode 100644 index 0000000..36c2d9b --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_prioritize_right.h @@ -0,0 +1,36 @@ +/** + * FLL - Level 0 + * + * Project: Console + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_parameter_prioritize_right_ +#define _TEST__F_console_parameter_prioritize_right_ + +/** + * Test that function fails. + * + * @see f_console_parameter_prioritize_right() + */ +extern void test__f_console_parameter_prioritize_right__fails(void **state); + +/** + * Test that parameter checking works as expected. + * + * @see f_console_parameter_prioritize_right() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_console_parameter_prioritize_right__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_console_parameter_prioritize_right() + */ +extern void test__f_console_parameter_prioritize_right__works(void **state); + +#endif // _TEST__F_console_parameter_prioritize_right_ diff --git a/level_0/f_console/tests/c/test-console-parameter_process.c b/level_0/f_console/tests/c/test-console-parameter_process.c new file mode 100644 index 0000000..30edde0 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_process.c @@ -0,0 +1,332 @@ +#include "test-console.h" +#include "test-console-parameter_process.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_console_parameter_process__no_arguments(void **state) { + + const f_string_t argv[] = { + "program", + 0, + }; + + const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize(1, argv); + + f_console_parameter_t parameter[] = { + macro_f_console_parameter_t_initialize("f", 0, 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("s", "second", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("t", "third", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("F", 0, 0, 0, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "fifth", 0, 1, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "sixth", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, "seventh", 0, 2, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("N", "not_found", 0, 0, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, 0, "other", 0, f_console_type_other_e), + }; + + f_console_parameters_t parameters = macro_f_console_parameters_t_initialize(parameter, 9); + f_array_lengths_t remaining = f_array_lengths_t_initialize; + + { + const f_status_t status = f_console_parameter_process(arguments, parameters, &remaining); + + assert_int_equal(status, F_none); + + assert_int_equal(parameter[0].result, f_console_result_none_e); + assert_int_equal(parameter[0].locations.used, 0); + assert_int_equal(parameter[0].values.used, 0); + assert_int_equal(parameter[0].location, 0); + assert_int_equal(parameter[0].location_sub, 0); + + assert_int_equal(parameter[1].result, f_console_result_none_e); + assert_int_equal(parameter[1].locations.used, 0); + assert_int_equal(parameter[1].values.used, 0); + assert_int_equal(parameter[1].location, 0); + assert_int_equal(parameter[1].location_sub, 0); + + assert_int_equal(parameter[2].result, f_console_result_none_e); + assert_int_equal(parameter[2].locations.used, 0); + assert_int_equal(parameter[2].values.used, 0); + assert_int_equal(parameter[2].location, 0); + assert_int_equal(parameter[2].location_sub, 0); + + assert_int_equal(parameter[3].result, f_console_result_none_e); + assert_int_equal(parameter[3].locations.used, 0); + assert_int_equal(parameter[3].values.used, 0); + assert_int_equal(parameter[3].location, 0); + assert_int_equal(parameter[3].location_sub, 0); + + assert_int_equal(parameter[4].result, f_console_result_none_e); + assert_int_equal(parameter[4].locations.used, 0); + assert_int_equal(parameter[4].values.used, 0); + assert_int_equal(parameter[4].location, 0); + assert_int_equal(parameter[4].location_sub, 0); + + assert_int_equal(parameter[5].result, f_console_result_none_e); + assert_int_equal(parameter[5].locations.used, 0); + assert_int_equal(parameter[5].values.used, 0); + assert_int_equal(parameter[5].location, 0); + assert_int_equal(parameter[5].location_sub, 0); + + assert_int_equal(parameter[6].result, f_console_result_none_e); + assert_int_equal(parameter[6].locations.used, 0); + assert_int_equal(parameter[6].values.used, 0); + assert_int_equal(parameter[6].location, 0); + assert_int_equal(parameter[6].location_sub, 0); + + assert_int_equal(parameter[7].result, f_console_result_none_e); + assert_int_equal(parameter[7].locations.used, 0); + assert_int_equal(parameter[7].values.used, 0); + assert_int_equal(parameter[7].location, 0); + assert_int_equal(parameter[7].location_sub, 0); + + assert_int_equal(parameter[8].result, f_console_result_none_e); + assert_int_equal(parameter[8].locations.used, 0); + assert_int_equal(parameter[8].values.used, 0); + assert_int_equal(parameter[8].location, 0); + assert_int_equal(parameter[8].location_sub, 0); + + assert_int_equal(remaining.used, 0); + } + + f_type_array_lengths_resize(0, &remaining); +} + +void test__f_console_parameter_process__only_remaining(void **state) { + + const f_string_t argv[] = { + "program", + "anything", + "--", + "can go here", + "+h", + "--help", + "../`~!@#$%^&*()_-+={[}]:;\"'<,>./?", + 0, + }; + + const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize(7, argv); + + f_console_parameter_t parameter[] = { + macro_f_console_parameter_t_initialize("f", 0, 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("s", "second", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("t", "third", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("F", 0, 0, 0, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "fifth", 0, 1, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "sixth", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, "seventh", 0, 2, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("N", "not_found", 0, 0, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, 0, "other", 0, f_console_type_other_e), + }; + + f_console_parameters_t parameters = macro_f_console_parameters_t_initialize(parameter, 9); + f_array_lengths_t remaining = f_array_lengths_t_initialize; + + { + const f_status_t status = f_console_parameter_process(arguments, parameters, &remaining); + + assert_int_equal(status, F_none); + + assert_int_equal(parameter[0].result, f_console_result_none_e); + assert_int_equal(parameter[0].locations.used, 0); + assert_int_equal(parameter[0].values.used, 0); + assert_int_equal(parameter[0].location, 0); + assert_int_equal(parameter[0].location_sub, 0); + + assert_int_equal(parameter[1].result, f_console_result_none_e); + assert_int_equal(parameter[1].locations.used, 0); + assert_int_equal(parameter[1].values.used, 0); + assert_int_equal(parameter[1].location, 0); + assert_int_equal(parameter[1].location_sub, 0); + + assert_int_equal(parameter[2].result, f_console_result_none_e); + assert_int_equal(parameter[2].locations.used, 0); + assert_int_equal(parameter[2].values.used, 0); + assert_int_equal(parameter[2].location, 0); + assert_int_equal(parameter[2].location_sub, 0); + + assert_int_equal(parameter[3].result, f_console_result_none_e); + assert_int_equal(parameter[3].locations.used, 0); + assert_int_equal(parameter[3].values.used, 0); + assert_int_equal(parameter[3].location, 0); + assert_int_equal(parameter[3].location_sub, 0); + + assert_int_equal(parameter[4].result, f_console_result_none_e); + assert_int_equal(parameter[4].locations.used, 0); + assert_int_equal(parameter[4].values.used, 0); + assert_int_equal(parameter[4].location, 0); + assert_int_equal(parameter[4].location_sub, 0); + + assert_int_equal(parameter[5].result, f_console_result_none_e); + assert_int_equal(parameter[5].locations.used, 0); + assert_int_equal(parameter[5].values.used, 0); + assert_int_equal(parameter[5].location, 0); + assert_int_equal(parameter[5].location_sub, 0); + + assert_int_equal(parameter[6].result, f_console_result_none_e); + assert_int_equal(parameter[6].locations.used, 0); + assert_int_equal(parameter[6].values.used, 0); + assert_int_equal(parameter[6].location, 0); + assert_int_equal(parameter[6].location_sub, 0); + + assert_int_equal(parameter[7].result, f_console_result_none_e); + assert_int_equal(parameter[7].locations.used, 0); + assert_int_equal(parameter[7].values.used, 0); + assert_int_equal(parameter[7].location, 0); + assert_int_equal(parameter[7].location_sub, 0); + + assert_int_equal(parameter[8].result, f_console_result_none_e); + assert_int_equal(parameter[8].locations.used, 0); + assert_int_equal(parameter[8].values.used, 0); + assert_int_equal(parameter[8].location, 0); + assert_int_equal(parameter[8].location_sub, 0); + + assert_int_equal(remaining.used, 6); + } + + f_type_array_lengths_resize(0, &remaining); +} + +#ifndef _di_level_0_parameter_checking_ + void test__f_console_parameter_process__parameter_checking(void **state) { + + const f_console_arguments_t arguments = f_console_arguments_t_initialize; + const f_console_parameters_t parameters = f_console_parameters_t_initialize; + + { + const f_status_t status = f_console_parameter_process(arguments, parameters, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_console_parameter_process__works(void **state) { + + const f_string_t argv[] = { + "program", + "-f", + "first", + "-st", + "second", + "third", + "other", + "+F", + "++fifth", + "fifth", + "--sixth", + "sixth", + "--seventh", + "seventh", + "7", + "other", + "free", + "-4", + 0, + }; + + const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize(18, argv); + + f_console_parameter_t parameter[] = { + macro_f_console_parameter_t_initialize("f", 0, 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("s", "second", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("t", "third", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("F", 0, 0, 0, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "fifth", 0, 1, f_console_type_inverse_e), + macro_f_console_parameter_t_initialize(0, "sixth", 0, 1, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, "seventh", 0, 2, f_console_type_normal_e), + macro_f_console_parameter_t_initialize("N", "not_found", 0, 0, f_console_type_normal_e), + macro_f_console_parameter_t_initialize(0, 0, "other", 0, f_console_type_other_e), + }; + + f_console_parameters_t parameters = macro_f_console_parameters_t_initialize(parameter, 9); + f_array_lengths_t remaining = f_array_lengths_t_initialize; + + { + const f_status_t status = f_console_parameter_process(arguments, parameters, &remaining); + + assert_int_equal(status, F_none); + + assert_int_equal(parameter[0].result, f_console_result_additional_e); + assert_int_equal(parameter[0].locations.used, 1); + assert_int_equal(parameter[0].values.used, 1); + assert_int_equal(parameter[0].location, 1); + assert_int_equal(parameter[0].location_sub, 1); + assert_int_equal(parameter[0].locations.array[0], 1); + assert_string_equal(argv[parameter[0].values.array[0]], "first"); + + assert_int_equal(parameter[1].result, f_console_result_additional_e); + assert_int_equal(parameter[1].locations.used, 1); + assert_int_equal(parameter[1].values.used, 1); + assert_int_equal(parameter[1].location, 3); + assert_int_equal(parameter[1].location_sub, 1); + assert_int_equal(parameter[1].locations.array[0], 3); + assert_string_equal(argv[parameter[1].values.array[0]], "second"); + + assert_int_equal(parameter[2].result, f_console_result_additional_e); + assert_int_equal(parameter[2].locations.used, 1); + assert_int_equal(parameter[2].values.used, 1); + assert_int_equal(parameter[2].location, 3); + assert_int_equal(parameter[2].location_sub, 2); + assert_int_equal(parameter[2].locations.array[0], 3); + assert_string_equal(argv[parameter[2].values.array[0]], "third"); + + assert_int_equal(parameter[3].result, f_console_result_found_e); + assert_int_equal(parameter[3].locations.used, 1); + assert_int_equal(parameter[3].values.used, 0); + assert_int_equal(parameter[3].location, 7); + assert_int_equal(parameter[3].location_sub, 1); + assert_int_equal(parameter[3].locations.array[0], 7); + + assert_int_equal(parameter[4].result, f_console_result_additional_e); + assert_int_equal(parameter[4].locations.used, 1); + assert_int_equal(parameter[4].values.used, 1); + assert_int_equal(parameter[4].location, 8); + assert_int_equal(parameter[4].location_sub, 0); + assert_int_equal(parameter[4].locations.array[0], 8); + assert_string_equal(argv[parameter[4].values.array[0]], "fifth"); + + assert_int_equal(parameter[5].result, f_console_result_additional_e); + assert_int_equal(parameter[5].locations.used, 1); + assert_int_equal(parameter[5].values.used, 1); + assert_int_equal(parameter[5].location, 10); + assert_int_equal(parameter[5].location_sub, 0); + assert_int_equal(parameter[5].locations.array[0], 10); + assert_string_equal(argv[parameter[5].values.array[0]], "sixth"); + + assert_int_equal(parameter[6].result, f_console_result_additional_e); + assert_int_equal(parameter[6].locations.used, 1); + assert_int_equal(parameter[6].values.used, 2); + assert_int_equal(parameter[6].location, 12); + assert_int_equal(parameter[6].location_sub, 0); + assert_int_equal(parameter[6].locations.array[0], 12); + assert_string_equal(argv[parameter[6].values.array[0]], "seventh"); + assert_string_equal(argv[parameter[6].values.array[1]], "7"); + + assert_int_equal(parameter[7].result, f_console_result_none_e); + assert_int_equal(parameter[7].locations.used, 0); + assert_int_equal(parameter[7].values.used, 0); + assert_int_equal(parameter[7].location, 0); + assert_int_equal(parameter[7].location_sub, 0); + + assert_int_equal(parameter[8].result, f_console_result_found_e); + assert_int_equal(parameter[8].locations.used, 2); + assert_int_equal(parameter[8].values.used, 0); + assert_int_equal(parameter[8].location, 15); + assert_int_equal(parameter[8].location_sub, 0); + assert_int_equal(parameter[8].locations.array[0], 6); + assert_int_equal(parameter[8].locations.array[1], 15); + + assert_int_equal(remaining.used, 2); + assert_string_equal(argv[remaining.array[0]], "free"); + assert_string_equal(argv[remaining.array[1]], "-4"); + } + + f_type_array_lengths_resize(0, &remaining); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/c/test-console-parameter_process.h b/level_0/f_console/tests/c/test-console-parameter_process.h new file mode 100644 index 0000000..cb04c45 --- /dev/null +++ b/level_0/f_console/tests/c/test-console-parameter_process.h @@ -0,0 +1,45 @@ +/** + * FLL - Level 0 + * + * Project: Console + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_parameter_process_ +#define _TEST__F_console_parameter_process_ + +// f_console_parameter_process() only returns memory failures. + +/** + * Test that function works with no (argv) arguments (but does have program name). + * + * @see f_console_parameter_process() + */ +extern void test__f_console_parameter_process__no_arguments(void **state); + +/** + * Test that function works with all (argv) arguments do not match any defined parameter. + * + * @see f_console_parameter_process() + */ +extern void test__f_console_parameter_process__only_remaining(void **state); + +/** + * Test that parameter checking works as expected. + * + * @see f_console_parameter_process() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_console_parameter_process__parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_console_parameter_process() + */ +extern void test__f_console_parameter_process__works(void **state); + +#endif // _TEST__F_console_parameter_process_ diff --git a/level_0/f_console/tests/c/test-console.c b/level_0/f_console/tests/c/test-console.c new file mode 100644 index 0000000..a554c69 --- /dev/null +++ b/level_0/f_console/tests/c/test-console.c @@ -0,0 +1,50 @@ +#include "test-console.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int setup(void **state) { + + return 0; +} + +int setdown(void **state) { + + errno = 0; + + return 0; +} + +int main(void) { + + const struct CMUnitTest tests[] = { + + // f_console_identify() doesn't return failure. + cmocka_unit_test(test__f_console_identify__works), + + cmocka_unit_test(test__f_console_parameter_prioritize_left__fails), + cmocka_unit_test(test__f_console_parameter_prioritize_left__works), + + cmocka_unit_test(test__f_console_parameter_prioritize_right__fails), + cmocka_unit_test(test__f_console_parameter_prioritize_right__works), + + // f_console_parameter_process() only returns memory failures. + cmocka_unit_test(test__f_console_parameter_process__no_arguments), + cmocka_unit_test(test__f_console_parameter_process__only_remaining), + cmocka_unit_test(test__f_console_parameter_process__works), + + #ifndef _di_level_0_parameter_checking_ + cmocka_unit_test(test__f_console_identify__parameter_checking), + cmocka_unit_test(test__f_console_parameter_prioritize_left__parameter_checking), + cmocka_unit_test(test__f_console_parameter_prioritize_right__parameter_checking), + cmocka_unit_test(test__f_console_parameter_process__parameter_checking), + #endif // _di_level_0_parameter_checking_ + }; + + return cmocka_run_group_tests(tests, setup, setdown); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/c/test-console.h b/level_0/f_console/tests/c/test-console.h new file mode 100644 index 0000000..e3a8a6c --- /dev/null +++ b/level_0/f_console/tests/c/test-console.h @@ -0,0 +1,72 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_h +#define _TEST__F_console_h + +// libc includes. +#include +#include +#include +#include + +// cmocka includes. +#include + +// fll-0 includes. +#include + +// test includes. +#include "test-console-identify.h" +#include "test-console-parameter_prioritize_left.h" +#include "test-console-parameter_prioritize_right.h" +#include "test-console-parameter_process.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Perform any setup operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setup(void **state); + +/** + * Peform any setdown operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setdown(void **state); + +/** + * Run all tests. + * + * @return + * The final result of the tests. + * + * @see cmocka_run_group_tests() + * @see cmocka_unit_test() + */ +extern int main(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _TEST__F_console_h