ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.02k stars 969 forks source link

TEST_PRINTF(): printing 64-bit hex numbers or pointers #693

Closed cmachida closed 11 months ago

cmachida commented 1 year ago

Hi there,

Thank you for the Unity project!!

We have a UNITY_SUPPORT_64 system and noticed that %llu works fine, but %llx and %p still print out 32 bits. Can this patch be applied, or something better be applied to address this issue?

Thanks, Curtis Machida

$ git diff
diff --git a/src/unity.c b/src/unity.c
index 8c946eb..d845850 100644
--- a/src/unity.c
+++ b/src/unity.c
@@ -2031,15 +2031,25 @@ static void UnityPrintFVA(const char* format, va_list va)
                                 UNITY_EXTRACT_ARG(UNITY_UINT, number, length_mod, va, unsigned int);
                                 UNITY_OUTPUT_CHAR('0');
                                 UNITY_OUTPUT_CHAR('x');
-                                UnityPrintNumberHex(number, 8);
+                                if (length_mod == UNITY_LENGTH_MODIFIER_LONG_LONG)
+                                    UnityPrintNumberHex(number, 16);
+                                else
+                                    UnityPrintNumberHex(number, 8);
                                 break;
                             }
                         case 'p':
                             {
-                                const unsigned int number = va_arg(va, unsigned int);
+                                UNITY_UINT number;
+                                char nibbles_to_print = 8;
+                                if (UNITY_POINTER_WIDTH == 64)
+                                {
+                                    length_mod = UNITY_LENGTH_MODIFIER_LONG_LONG;
+                                    nibbles_to_print = 16;
+                                }
+                                UNITY_EXTRACT_ARG(UNITY_UINT, number, length_mod, va, unsigned int);
                                 UNITY_OUTPUT_CHAR('0');
                                 UNITY_OUTPUT_CHAR('x');
-                                UnityPrintNumberHex((UNITY_UINT)number, 8);
+                                UnityPrintNumberHex((UNITY_UINT)number, nibbles_to_print);
                                 break;
                             }
                         case 'c':

References: https://github.com/ThrowTheSwitch/Unity/blob/master/docs/UnityConfigurationGuide.md

AJIOB commented 1 year ago

@cmachida, your changes look good!

Feel free to open the pull request