]> Kevux Git Server - fll/commitdiff
Update: Get all f_conversion unit tests working.
authorKevin Day <thekevinday@gmail.com>
Tue, 7 Jun 2022 04:01:41 +0000 (23:01 -0500)
committerKevin Day <thekevinday@gmail.com>
Tue, 7 Jun 2022 04:01:41 +0000 (23:01 -0500)
Add missing mocks.

level_0/f_conversion/data/build/settings-mocks
level_0/f_conversion/data/build/testfile
level_0/f_conversion/tests/unit/c/mock-conversion.c
level_0/f_conversion/tests/unit/c/mock-conversion.h
level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.c
level_0/f_conversion/tests/unit/c/test-conversion-number_signed_print.h
level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.c
level_0/f_conversion/tests/unit/c/test-conversion-number_unsigned_print.h
level_0/f_conversion/tests/unit/c/test-conversion.c

index dee400ebafc7834a33e4b88e7c6e961d7caf753d..ef0117c128541112998ecf49a9583658a18310f8 100644 (file)
@@ -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
index b1b8535e2b8cd52afda89634e04d624b0987a422..91efc1100d3eb2c32139f802056abf07b0c63069 100644 (file)
@@ -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
index a29fd680770e280cce9a874bf8929de490c5b1d8..91603c29b853d85905f4dfd7a50a8cee58026a62 100644 (file)
@@ -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
index dead3e6411e2d2d4587901bd1abf07f9fbd228e9..985d0dd1665324f3811f20ac3503c7877f3fee00 100644 (file)
@@ -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"
index db89218af09e66e82a5a99737e2d55e6c0390d12..417a8c371facf7f8cc43e01d556e5faa5163a882 100644 (file)
@@ -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);
   }
index 2dacb4d5d9e77fb1a1ed479c137fced2d5a87215..2d945749dcf5e7a95131f883ffa291ca3201b92c 100644 (file)
@@ -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
index a9df510058f96d83b2d666e44f5fecd8f9ef802c..8d2ba3d0021bd9bb9508921712c27d0759fd9a2b 100644 (file)
@@ -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);
   }
index 65c6e1a97aabf9b0ec4b98015488ead179cc5805..02e53935dd0ed41f22717d97905e9a5c80571801 100644 (file)
@@ -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
index 9edfce22341e020be9d60ef9ecc91f5e56148cc6..706d17015a0f9ed95792e43159039ce11b486b2b 100644 (file)
@@ -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),