From: Kevin Day Date: Sun, 28 Jan 2024 06:28:56 +0000 (-0600) Subject: Feature: Add f_console_parameter_reset() function for resetting the console parameters. X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=e59c89da80d6d2869b68b2ca81f9baae2016cecc;p=fll Feature: Add f_console_parameter_reset() function for resetting the console parameters. --- diff --git a/level_0/f_console/c/console.c b/level_0/f_console/c/console.c index 141426c..f57ea47 100644 --- a/level_0/f_console/c/console.c +++ b/level_0/f_console/c/console.c @@ -673,6 +673,42 @@ extern "C" { } #endif // _di_f_console_parameter_process_ +#ifndef _di_f_console_parameter_reset_ + f_status_t f_console_parameter_reset(f_console_parameters_t * const parameters) { + #ifndef _di_level_0_parameter_checking_ + if (!parameters) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + f_number_unsigned_t i = 0; + + for (; i < parameters->arguments.used; ++i) { + parameters->arguments.array[i].used = 0; + } // for + + for (i = 0; i < parameters->used; ++i) { + + memset(parameters->array[i].locations.array, 0, parameters->array[i].locations.used); + memset(parameters->array[i].locations_sub.array, 0, parameters->array[i].locations_sub.used); + memset(parameters->array[i].values.array, 0, parameters->array[i].values.used); + + parameters->array[i].flag &= ~f_console_flag_disable_e; + parameters->array[i].result = f_console_result_none_e; + parameters->array[i].location = 0; + parameters->array[i].location_sub = 0; + parameters->array[i].locations.used = 0; + parameters->array[i].locations_sub.used = 0; + parameters->array[i].values.used = 0; + } // for + + memset(parameters->remaining.array, 0, parameters->remaining.used); + + parameters->arguments.used = 0; + parameters->remaining.used = 0; + + return F_okay; + } +#endif // _di_f_console_parameter_reset_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_0/f_console/c/console.h b/level_0/f_console/c/console.h index 4aed088..b1fbba9 100644 --- a/level_0/f_console/c/console.h +++ b/level_0/f_console/c/console.h @@ -185,6 +185,33 @@ extern "C" { extern void f_console_parameter_process(const f_console_arguments_t arguments, f_console_parameters_t * const parameters, f_state_t * const state, void * const data); #endif // _di_f_console_parameter_process_ + +/** + * Reset all parameter properties. + * + * This only resets values using the "used" counts just in case any static data is provided rather than resetting via the "size" counts. + * This does not perform any de-allocation. + * This does not alter the "size" counts. + * This does not alter the parameters.used. + * This does not alter the following individual parameter properties in the parameters array: + * - match_short. + * - match_long. + * - match_simple. + * - values_total. + * This only alters "state" flags in parameters[].flag. + * + * @param parameters + * The console parameters to reset. + * + * @return + * F_okay on success. + * + * @see memset() + */ +#ifndef _di_f_console_parameter_reset_ + extern f_status_t f_console_parameter_reset(f_console_parameters_t * const parameters); +#endif // _di_f_console_parameter_reset_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_0/f_console/data/build/settings-tests b/level_0/f_console/data/build/settings-tests index 71c6db4..8c1b333 100644 --- a/level_0/f_console/data/build/settings-tests +++ b/level_0/f_console/data/build/settings-tests @@ -25,7 +25,14 @@ build_language c build_libraries -lc -lcmocka build_libraries-individual -lf_memory -lf_string -lf_type_array -lf_utf -lf_console -build_sources_program test-console-environment_process.c test-console-identify.c test-console-parameter_prioritize_left.c test-console-parameter_prioritize_right.c test-console-parameter_process.c test-console-parameters_delete.c test-console-parameters_destroy.c test-console.c +build_sources_program test-console-environment_process.c +build_sources_program test-console-identify.c +build_sources_program test-console-parameter_prioritize_left.c +build_sources_program test-console-parameter_prioritize_right.c +build_sources_program test-console-parameter_process.c +build_sources_program test-console-parameter_reset.c +build_sources_program test-console-parameters_delete.c test-console-parameters_destroy.c +build_sources_program test-console.c build_script no build_shared yes diff --git a/level_0/f_console/tests/unit/c/test-console-parameter_reset.c b/level_0/f_console/tests/unit/c/test-console-parameter_reset.c new file mode 100644 index 0000000..a98284c --- /dev/null +++ b/level_0/f_console/tests/unit/c/test-console-parameter_reset.c @@ -0,0 +1,124 @@ +#include "test-console.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_console_parameter_reset__parameter_checking(void **state) { + + { + const f_status_t status = f_console_parameter_reset(0); + + assert_int_equal(status, F_status_set_error(F_parameter)); + } +} + +void test__f_console_parameter_reset__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", + "+fast", + "-staff", + "-mMxx", + "mixed 1", + "mixed 2, value 1", + "mixed 2, value 2", + 0, + }; + + const f_string_static_t s_f = macro_f_string_static_t_initialize_1("f", 0, 1); + const f_string_static_t s_s = macro_f_string_static_t_initialize_1("s", 0, 1); + const f_string_static_t s_t = macro_f_string_static_t_initialize_1("t", 0, 1); + const f_string_static_t s_F = macro_f_string_static_t_initialize_1("F", 0, 1); + const f_string_static_t s_N = macro_f_string_static_t_initialize_1("N", 0, 1); + const f_string_static_t s_m = macro_f_string_static_t_initialize_1("m", 0, 1); + const f_string_static_t s_M = macro_f_string_static_t_initialize_1("M", 0, 1); + const f_string_static_t s_x = macro_f_string_static_t_initialize_1("x", 0, 1); + const f_string_static_t s_second = macro_f_string_static_t_initialize_1("second", 0, 6); + const f_string_static_t s_third = macro_f_string_static_t_initialize_1("third", 0, 5); + const f_string_static_t s_fifth = macro_f_string_static_t_initialize_1("fifth", 0, 5); + const f_string_static_t s_sixth = macro_f_string_static_t_initialize_1("sixth", 0, 5); + const f_string_static_t s_seventh = macro_f_string_static_t_initialize_1("seventh", 0, 7); + const f_string_static_t s_not_found = macro_f_string_static_t_initialize_1("not_found", 0, 9); + const f_string_static_t s_mixed_1 = macro_f_string_static_t_initialize_1("mixed_1", 0, 7); + const f_string_static_t s_mixed_2 = macro_f_string_static_t_initialize_1("mixed_2", 0, 7); + const f_string_static_t s_mixed_3 = macro_f_string_static_t_initialize_1("mixed_3", 0, 7); + const f_string_static_t s_other = macro_f_string_static_t_initialize_1("other", 0, 5); + + const f_console_arguments_t arguments = macro_f_console_arguments_t_initialize_1(24, argv, 0); + const uint8_t parameters_total = 12; + + f_console_parameter_t parameter[] = { + macro_f_console_parameter_t_initialize_2(s_f, f_string_empty_s, f_string_empty_s, 1, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_s, s_second, f_string_empty_s, 1, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_t, s_third, f_string_empty_s, 1, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_F, f_string_empty_s, f_string_empty_s, 0, f_console_flag_inverse_e), + macro_f_console_parameter_t_initialize_2(f_string_empty_s, s_fifth, f_string_empty_s, 1, f_console_flag_inverse_e), + macro_f_console_parameter_t_initialize_2(f_string_empty_s, s_sixth, f_string_empty_s, 1, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(f_string_empty_s, s_seventh, f_string_empty_s, 2, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_N, s_not_found, f_string_empty_s, 0, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_m, s_mixed_1, f_string_empty_s, 1, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_M, s_mixed_2, f_string_empty_s, 2, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(s_x, s_mixed_3, f_string_empty_s, 0, f_console_flag_normal_e), + macro_f_console_parameter_t_initialize_2(f_string_empty_s, f_string_empty_s, s_other, 0, f_console_flag_simple_e), + }; + + f_console_parameters_t parameters = macro_f_console_parameters_t_initialize_1(parameter, parameters_total, 0); + + { + { + f_state_t state = f_state_t_initialize; + f_console_parameter_process(arguments, ¶meters, &state, 0); + + assert_int_equal(state.status, F_okay); + } + + parameter[0].flag |= f_console_flag_disable_e; + + { + const f_status_t status = f_console_parameter_reset(¶meters); + + assert_int_equal(status, F_okay); + } + + for (f_number_unsigned_t i = 0; i < parameters.arguments.size; ++i) { + assert_int_equal(parameters.arguments.array[i].used, 0); + } // for + + for (uint8_t i = 0; i < parameters_total; ++i) { + + assert_int_equal(parameter[i].flag & f_console_flag_disable_e, 0); + assert_int_equal(parameter[i].result, f_console_result_none_e); + assert_int_equal(parameter[i].locations.used, 0); + assert_int_equal(parameter[i].locations_sub.used, 0); + assert_int_equal(parameter[i].values.used, 0); + assert_int_equal(parameter[i].location, 0); + assert_int_equal(parameter[i].location_sub, 0); + } // for + + assert_int_equal(parameters.remaining.used, 0); + } + + f_console_parameters_delete(¶meters); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_console/tests/unit/c/test-console-parameter_reset.h b/level_0/f_console/tests/unit/c/test-console-parameter_reset.h new file mode 100644 index 0000000..4f54d5c --- /dev/null +++ b/level_0/f_console/tests/unit/c/test-console-parameter_reset.h @@ -0,0 +1,29 @@ +/** + * FLL - Level 0 + * + * Project: Console + * API Version: 0.7 + * Licenses: lgpl-2.1-or-later + * + * Test the console project. + */ +#ifndef _TEST__F_console_parameter_reset_h +#define _TEST__F_console_parameter_reset_h + +// f_console_parameter_reset() only returns parameter failures. + +/** + * Test that parameter checking works as expected. + * + * @see f_console_parameter_reset() + */ +extern void test__f_console_parameter_reset__parameter_checking(void **state); + +/** + * Test that function works. + * + * @see f_console_parameter_reset() + */ +extern void test__f_console_parameter_reset__works(void **state); + +#endif // _TEST__F_console_parameter_reset_h diff --git a/level_0/f_console/tests/unit/c/test-console.c b/level_0/f_console/tests/unit/c/test-console.c index fe9a67a..1919b3a 100644 --- a/level_0/f_console/tests/unit/c/test-console.c +++ b/level_0/f_console/tests/unit/c/test-console.c @@ -38,6 +38,8 @@ int main(void) { cmocka_unit_test(test__f_console_parameter_process__only_remaining), cmocka_unit_test(test__f_console_parameter_process__works), + cmocka_unit_test(test__f_console_parameter_reset__works), + cmocka_unit_test(test__f_console_parameters_delete__frees_memory), cmocka_unit_test(test__f_console_parameters_destroy__frees_memory), @@ -48,6 +50,7 @@ int main(void) { 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), + cmocka_unit_test(test__f_console_parameter_reset__parameter_checking), cmocka_unit_test(test__f_console_parameters_delete__parameter_checking), cmocka_unit_test(test__f_console_parameters_destroy__parameter_checking), #endif // _di_level_0_parameter_checking_ diff --git a/level_0/f_console/tests/unit/c/test-console.h b/level_0/f_console/tests/unit/c/test-console.h index 563fd6d..59759d8 100644 --- a/level_0/f_console/tests/unit/c/test-console.h +++ b/level_0/f_console/tests/unit/c/test-console.h @@ -28,6 +28,7 @@ #include "test-console-parameter_prioritize_left.h" #include "test-console-parameter_prioritize_right.h" #include "test-console-parameter_process.h" +#include "test-console-parameter_reset.h" #include "test-console-parameters_delete.h" #include "test-console-parameters_destroy.h"