From 0eee5052a4be89ec5a299be1bf88e6bc40b4ac06 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 6 Jun 2022 23:01:41 -0500 Subject: [PATCH] Update: Get all f_conversion unit tests working. Add missing mocks. --- level_0/f_conversion/data/build/settings-mocks | 1 + level_0/f_conversion/data/build/testfile | 4 +- .../f_conversion/tests/unit/c/mock-conversion.c | 15 ++++++-- .../f_conversion/tests/unit/c/mock-conversion.h | 3 +- .../unit/c/test-conversion-number_signed_print.c | 45 +++++++++++++++++++--- .../unit/c/test-conversion-number_signed_print.h | 14 +++++++ .../unit/c/test-conversion-number_unsigned_print.c | 45 +++++++++++++++++++--- .../unit/c/test-conversion-number_unsigned_print.h | 14 +++++++ .../f_conversion/tests/unit/c/test-conversion.c | 24 +++++++----- 9 files changed, 136 insertions(+), 29 deletions(-) diff --git a/level_0/f_conversion/data/build/settings-mocks b/level_0/f_conversion/data/build/settings-mocks index dee400e..ef0117c 100644 --- a/level_0/f_conversion/data/build/settings-mocks +++ b/level_0/f_conversion/data/build/settings-mocks @@ -58,4 +58,5 @@ flags-coverage --coverage -fprofile-abs-path -fprofile-dir=build/coverage/ flags_library -fPIC # Inject mocks. +flags -Wl,--wrap=ferror_unlocked flags -Wl,--wrap=fwrite_unlocked diff --git a/level_0/f_conversion/data/build/testfile b/level_0/f_conversion/data/build/testfile index b1b8535..91efc11 100644 --- a/level_0/f_conversion/data/build/testfile +++ b/level_0/f_conversion/data/build/testfile @@ -17,9 +17,7 @@ settings: #define CMOCKA_XML_FILE ./out.xml main: - # Disable mocks until mocking can be fixed for this project. - #build settings-mocks - build settings individual test + build settings-mocks individual test build settings-tests individual test operate ld_library_path diff --git a/level_0/f_conversion/tests/unit/c/mock-conversion.c b/level_0/f_conversion/tests/unit/c/mock-conversion.c index a29fd68..91603c2 100644 --- a/level_0/f_conversion/tests/unit/c/mock-conversion.c +++ b/level_0/f_conversion/tests/unit/c/mock-conversion.c @@ -4,7 +4,12 @@ extern "C" { #endif -size_t __wrap_fwrite_unlocked(const void * const ptr, size_t size, size_t n, FILE *stream) { +int __wrap_ferror_unlocked(FILE *stream) { + + return mock_type(int); +} + +size_t __wrap_fwrite_unlocked(const void * const ptr, size_t size, size_t nmemb, FILE *stream) { const bool failure = mock_type(bool); @@ -12,9 +17,13 @@ size_t __wrap_fwrite_unlocked(const void * const ptr, size_t size, size_t n, FIL return mock_type(int); } - check_expected(ptr); + const bool check_ptr = mock_type(bool); + + if (check_ptr) { + check_expected(ptr); + } - return n; + return nmemb; } #ifdef __cplusplus diff --git a/level_0/f_conversion/tests/unit/c/mock-conversion.h b/level_0/f_conversion/tests/unit/c/mock-conversion.h index dead3e6..985d0dd 100644 --- a/level_0/f_conversion/tests/unit/c/mock-conversion.h +++ b/level_0/f_conversion/tests/unit/c/mock-conversion.h @@ -28,7 +28,8 @@ extern "C" { const static int mock_errno_generic = 32767; -extern size_t __wrap_fwrite_unlocked(const void * const ptr, size_t size, size_t n, FILE *stream); +extern int __wrap_ferror_unlocked(FILE *stream); +extern size_t __wrap_fwrite_unlocked(const void * const ptr, size_t size, size_t nmemb, FILE *stream); #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.c b/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.c index db89218..417a8c3 100644 --- a/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.c +++ b/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.c @@ -12,10 +12,11 @@ void test__f_conversion_number_signed_print__fails(void **state) { will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_signed_print(1, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } @@ -26,24 +27,26 @@ void test__f_conversion_number_signed_print__fails_for_prepend(void **state) { will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_signed_print(1, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } void test__f_conversion_number_signed_print__fails_for_zero(void **state) { { - const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_signed_print(0, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } @@ -70,13 +73,43 @@ void test__f_conversion_number_signed_print__parameter_checking(void **state) { void test__f_conversion_number_signed_print__works(void **state) { { - const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); + + will_return(__wrap_fwrite_unlocked, false); + will_return(__wrap_fwrite_unlocked, sizeof(f_char_t)); + will_return(__wrap_ferror_unlocked, 0); + + expect_string(__wrap_fwrite_unlocked, ptr, "1"); const f_status_t status = f_conversion_number_signed_print(1, data, F_type_output_d); + assert_int_equal(status, F_none); + } +} + +void test__f_conversion_number_signed_print__works_for_zero(void **state) { + + { + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); + will_return(__wrap_fwrite_unlocked, false); + will_return(__wrap_fwrite_unlocked, true); + will_return(__wrap_ferror_unlocked, false); - expect_string(__wrap_fwrite_unlocked, ptr, "1"); + expect_string(__wrap_fwrite_unlocked, ptr, "0"); + + const f_status_t status = f_conversion_number_signed_print(0, data, F_type_output_d); + + assert_int_equal(status, F_none); + } +} + +void test__f_conversion_number_signed_print__works_for_zero_with_width_zero(void **state) { + + { + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + + const f_status_t status = f_conversion_number_signed_print(0, data, F_type_output_d); assert_int_equal(status, F_none); } diff --git a/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.h b/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.h index 2dacb4d..2d94574 100644 --- a/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.h +++ b/level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.h @@ -45,4 +45,18 @@ extern void test__f_conversion_number_signed_print__parameter_checking(void **st */ extern void test__f_conversion_number_signed_print__works(void **state); +/** + * Test that function works for 0. + * + * @see f_conversion_number_signed_print() + */ +extern void test__f_conversion_number_signed_print__works_for_zero(void **state); + +/** + * Test that function works for 0 when width is 0. + * + * @see f_conversion_number_signed_print() + */ +extern void test__f_conversion_number_signed_print__works_for_zero_with_width_zero(void **state); + #endif // _TEST__F_conversion_number_signed_print_h diff --git a/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.c b/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.c index a9df510..8d2ba3d 100644 --- a/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.c +++ b/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.c @@ -12,10 +12,11 @@ void test__f_conversion_number_unsigned_print__fails(void **state) { will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_unsigned_print(1, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } @@ -26,24 +27,26 @@ void test__f_conversion_number_unsigned_print__fails_for_prepend(void **state) { will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_unsigned_print(1, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } void test__f_conversion_number_unsigned_print__fails_for_zero(void **state) { { - const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); will_return(__wrap_fwrite_unlocked, true); will_return(__wrap_fwrite_unlocked, 0); + will_return(__wrap_ferror_unlocked, true); const f_status_t status = f_conversion_number_unsigned_print(0, data, F_type_output_d); - assert_int_equal(F_status_set_fine(status), F_output); + assert_int_equal(status, F_status_set_error(F_output)); } } @@ -70,13 +73,43 @@ void test__f_conversion_number_unsigned_print__parameter_checking(void **state) void test__f_conversion_number_unsigned_print__works(void **state) { { - const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); + + will_return(__wrap_fwrite_unlocked, false); + will_return(__wrap_fwrite_unlocked, true); + will_return(__wrap_ferror_unlocked, false); + + expect_string(__wrap_fwrite_unlocked, ptr, "1"); const f_status_t status = f_conversion_number_unsigned_print(1, data, F_type_output_d); + assert_int_equal(status, F_none); + } +} + +void test__f_conversion_number_unsigned_print__works_for_zero(void **state) { + + { + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 1); + will_return(__wrap_fwrite_unlocked, false); + will_return(__wrap_fwrite_unlocked, true); + will_return(__wrap_ferror_unlocked, false); - expect_string(__wrap_fwrite_unlocked, ptr, "1"); + expect_string(__wrap_fwrite_unlocked, ptr, "0"); + + const f_status_t status = f_conversion_number_unsigned_print(0, data, F_type_output_d); + + assert_int_equal(status, F_none); + } +} + +void test__f_conversion_number_unsigned_print__works_for_zero_with_width_zero(void **state) { + + { + const f_conversion_data_t data = macro_f_conversion_data_t_initialize(10, 0, 0); + + const f_status_t status = f_conversion_number_unsigned_print(0, data, F_type_output_d); assert_int_equal(status, F_none); } diff --git a/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.h b/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.h index 65c6e1a..02e5393 100644 --- a/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.h +++ b/level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.h @@ -45,4 +45,18 @@ extern void test__f_conversion_number_unsigned_print__parameter_checking(void ** */ extern void test__f_conversion_number_unsigned_print__works(void **state); +/** + * Test that function works for 0. + * + * @see f_conversion_number_unsigned_print() + */ +extern void test__f_conversion_number_unsigned_print__works_for_zero(void **state); + +/** + * Test that function works for 0 when width is 0. + * + * @see f_conversion_number_unsigned_print() + */ +extern void test__f_conversion_number_unsigned_print__works_for_zero_with_width_zero(void **state); + #endif // _TEST__F_conversion_number_unsigned_print_h diff --git a/level_0/f_conversion/tests/unit/c/test-conversion.c b/level_0/f_conversion/tests/unit/c/test-conversion.c index 9edfce2..706d170 100644 --- a/level_0/f_conversion/tests/unit/c/test-conversion.c +++ b/level_0/f_conversion/tests/unit/c/test-conversion.c @@ -45,21 +45,25 @@ int main(void) { cmocka_unit_test(test__f_conversion_character_to_octal__fails), cmocka_unit_test(test__f_conversion_character_to_octal__works), - // Currently failing for uknown reasons, it appears __wrap_fwrite_unlocked() is somehow not being wrapped properly. - //cmocka_unit_test(test__f_conversion_number_signed_print__fails), - //cmocka_unit_test(test__f_conversion_number_signed_print__fails_for_prepend), - //cmocka_unit_test(test__f_conversion_number_signed_print__fails_for_zero), - //cmocka_unit_test(test__f_conversion_number_signed_print__works), + cmocka_unit_test(test__f_conversion_number_signed_print__fails), + cmocka_unit_test(test__f_conversion_number_signed_print__fails_for_prepend), + cmocka_unit_test(test__f_conversion_number_signed_print__fails_for_zero), + + cmocka_unit_test(test__f_conversion_number_signed_print__works), + cmocka_unit_test(test__f_conversion_number_signed_print__works_for_zero), + cmocka_unit_test(test__f_conversion_number_signed_print__works_for_zero_with_width_zero), cmocka_unit_test(test__f_conversion_number_signed_to_string__works), cmocka_unit_test(test__f_conversion_number_signed_to_string__works_for_prepend), cmocka_unit_test(test__f_conversion_number_signed_to_string__works_for_zero), - // // Currently failing for uknown reasons, it appears __wrap_fwrite_unlocked() is somehow not being wrapped properly. - //cmocka_unit_test(test__f_conversion_number_unsigned_print__fails), - //cmocka_unit_test(test__f_conversion_number_unsigned_print__fails_for_prepend), - //cmocka_unit_test(test__f_conversion_number_unsigned_print__fails_for_zero), - //cmocka_unit_test(test__f_conversion_number_unsigned_print__works), + cmocka_unit_test(test__f_conversion_number_unsigned_print__fails), + cmocka_unit_test(test__f_conversion_number_unsigned_print__fails_for_prepend), + cmocka_unit_test(test__f_conversion_number_unsigned_print__fails_for_zero), + + cmocka_unit_test(test__f_conversion_number_unsigned_print__works), + cmocka_unit_test(test__f_conversion_number_unsigned_print__works_for_zero), + cmocka_unit_test(test__f_conversion_number_unsigned_print__works_for_zero_with_width_zero), cmocka_unit_test(test__f_conversion_number_unsigned_to_string__works), cmocka_unit_test(test__f_conversion_number_unsigned_to_string__works_for_prepend), -- 1.8.3.1