| 1 | #cxx . . $ `<file>/src/message_formatting.c`/include <cgreen/constraint.h>cxx . . $ `<file>/include/cgreen/constraint.h`/ |
| 2 | #include <cgreen/message_formatting.h>cxx . . $ `<file>/include/cgreen/message_formatting.h`/ |
| 3 | #include <cgreen/string_comparison.h>cxx . . $ `<file>/include/cgreen/string_comparison.h`/ |
| 4 | #include <inttypes.h> |
| 5 | #ifndef __cplusplus |
| 6 | #include <stdbool.h> |
| 7 | #endif |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #ifdef __ANDROID__ |
| 13 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 14 | #endif // #ifdef __ANDROID__ |
| 15 | |
| 16 | #ifdef _MSC_VER |
| 17 | #include "wincompat.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "constraint_internal.h"cxx . . $ `<file>/src/constraint_internal.h`/ |
| 21 | |
| 22 | |
| 23 | // Handling of percent signs |
| 24 | static const char *next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(const char *s) { |
| 25 | return strchrcxx . . $ strchr(2050848944e00d54).(s, '%'); |
| 26 | } |
| 27 | |
| 28 | static size_t count_percent_signscxx . . $ count_percent_signs(751346f8406fd082).(char const *s) |
| 29 | { |
| 30 | size_t count = 0; |
| 31 | char const *p = next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(s); |
| 32 | while (p != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 33 | count++; |
| 34 | p = next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(p+1); |
| 35 | } |
| 36 | return count; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | static char *copy_while_doubling_percent_signscxx . . $ copy_while_doubling_percent_signs(b7b3a83273496486).(char *string, char const *original) |
| 41 | { |
| 42 | char *destination = string; |
| 43 | const char *source = original; |
| 44 | const char *next = next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(source); |
| 45 | for (; next != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; next = next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(next+1)) { |
| 46 | size_t len = next - source + 1; |
| 47 | memcpycxx . . $ memcpy(cc817cb175f282d).(destination, source, len); |
| 48 | destination += len; |
| 49 | *destination++ = '%'; |
| 50 | source = next+1; |
| 51 | } |
| 52 | strcpycxx . . $ strcpy(b7b3a83273496486).(destination, source); |
| 53 | |
| 54 | return string; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static char *double_all_percent_signs_incxx . . $ double_all_percent_signs_in(a8b8e32347182212).(const char *original) |
| 59 | { |
| 60 | size_t percent_count = count_percent_signscxx . . $ count_percent_signs(751346f8406fd082).(original); |
| 61 | char *new_string = (char *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(strlencxx . . $ strlen(751346f8406fd082).(original) + percent_count + 1); |
| 62 | if (new_string == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 63 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 64 | } |
| 65 | copy_while_doubling_percent_signscxx . . $ copy_while_doubling_percent_signs(b7b3a83273496486).(new_string, original); |
| 66 | return new_string; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static int find_index_of_differencecxx . . $ find_index_of_difference(c5040b39c98d43dd).(void *expected, void *actual, size_t size_to_compare) { |
| 71 | char *expectedp = expected; |
| 72 | char *actualp = actual; |
| 73 | |
| 74 | while (size_to_compare--) { |
| 75 | if (*expectedp++ != *actualp++) { |
| 76 | return (int)((void *)actualp - actual)-1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! actual_value_not_necessary_forcxx . . $ actual_value_not_necessary_for(be04999f9e28364e).(Constraintcxx . . $ Constraint# *constraint, const char *actual_string, const char *actual_value_string) { |
| 84 | (void)constraint; // UNUSED! |
| 85 | return strings_are_equalcxx . . $ strings_are_equal(b7a7aa4a824b7595).(actual_string, actual_value_string) || |
| 86 | strings_are_equalcxx . . $ strings_are_equal(b7a7aa4a824b7595).(actual_string, "true") || |
| 87 | strings_are_equalcxx . . $ strings_are_equal(b7a7aa4a824b7595).(actual_string, "false"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! parameters_are_not_valid_forcxx . . $ parameters_are_not_valid_for(908815a3afc9f0a1).(Constraintcxx . . $ Constraint# *constraint, intptr_tcxx . . $ intptr_t# actual) { |
| 92 | char *message = validation_failure_message_forcxx . . $ validation_failure_message_for(61ddab92ee18b24e).(constraint, actual); |
| 93 | |
| 94 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! not_valid = (strlencxx . . $ strlen(751346f8406fd082).(message) > 0); |
| 95 | |
| 96 | freecxx . . $ free(5d22bdacc48458e8).(message); |
| 97 | |
| 98 | return not_valid; |
| 99 | } |
| 100 | |
| 101 | char *validation_failure_message_forcxx . . $ validation_failure_message_for(61ddab92ee18b24e).(Constraintcxx . . $ Constraint# *constraint, intptr_tcxx . . $ intptr_t# actual) { |
| 102 | const char *name_has_incorrect_size_message = |
| 103 | "Wanted to compare contents with [%s],\n" |
| 104 | "\t\tbut [%ld] was given for the comparison size."; |
| 105 | const char *null_used_for_compare_message = |
| 106 | "Wanted to compare contents with [%s],\n" |
| 107 | "\t\tbut NULL was used for the pointer we wanted to compare to.\n" |
| 108 | "\t\tIf you want to explicitly check for null, use the is_null constraint instead."; |
| 109 | const char *null_used_for_actual_message = |
| 110 | "Wanted to compare contents of [%s] but it had a value of NULL.\n" |
| 111 | "\t\tIf you want to explicitly check for null, use the is_null constraint instead."; |
| 112 | |
| 113 | |
| 114 | |
| 115 | size_t message_size = strlencxx . . $ strlen(751346f8406fd082).(name_has_incorrect_size_message) + |
| 116 | strlencxx . . $ strlen(751346f8406fd082).(null_used_for_compare_message) + |
| 117 | strlencxx . . $ strlen(751346f8406fd082).(null_used_for_actual_message) + |
| 118 | 512; // just in case |
| 119 | |
| 120 | char *message = (char *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(message_size); |
| 121 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(message, 0, message_size); |
| 122 | |
| 123 | // (void)function; // UNUSED! |
| 124 | // if (function != NULL && strlen(function) > 0) { |
| 125 | // snprintf(message, message_size - 1, "\tIn mocked function [%s]:\n", function); |
| 126 | // } else { |
| 127 | // snprintf(message, message_size - 1, "\tIn assertion:\n"); |
| 128 | // } |
| 129 | |
| 130 | if (is_content_comparingcxx . . $ is_content_comparing(1ba69099f960dda3).(constraint)) { |
| 131 | const char *as_string; |
| 132 | if (constraint->parameter_namecxx . . $ Constraint_#parameter_name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 133 | as_string = constraint->parameter_namecxx . . $ Constraint_#parameter_name.; |
| 134 | } else { |
| 135 | as_string = constraint->expected_value_namecxx . . $ Constraint_#expected_value_name.; |
| 136 | } |
| 137 | |
| 138 | if ((long signed)constraint->size_of_expected_valuecxx . . $ Constraint_#size_of_expected_value. <= 0) { |
| 139 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 140 | name_has_incorrect_size_message, |
| 141 | as_string, |
| 142 | (long signed)constraint->size_of_expected_valuecxx . . $ Constraint_#size_of_expected_value.); |
| 143 | |
| 144 | return message; |
| 145 | } |
| 146 | |
| 147 | if ((void *)actual == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 148 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 149 | null_used_for_compare_message, |
| 150 | as_string); |
| 151 | |
| 152 | return message; |
| 153 | } |
| 154 | |
| 155 | if (constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..pointer_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#pointer_value. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 156 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 157 | null_used_for_actual_message, |
| 158 | as_string); |
| 159 | |
| 160 | return message; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return message; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! is_equal_to_string_constraintcxx . . $ is_equal_to_string_constraint(b7da63ddd1fb537e).(Constraintcxx . . $ Constraint# *constraint) { |
| 169 | return strstrcxx . . $ strstr(a713fe7b9a57cf96).(constraint->namecxx . . $ Constraint_#name., "not ") == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || strstrcxx . . $ strstr(a713fe7b9a57cf96).(constraint->namecxx . . $ Constraint_#name., "equal ") == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 170 | } |
| 171 | |
| 172 | /* Formats for printing actual and expected values */ |
| 173 | static const char actual_value_string_formatcxx . . $ actual_value_string_format.[] = "\n\t\tactual value:\t\t\t[\"%s\"]"; |
| 174 | static const char expected_value_string_formatcxx . . $ expected_value_string_format.[] = "[%s]"; |
| 175 | static const char constraint_as_string_formatcxx . . $ constraint_as_string_format.[] = "Expected [%s] to [%s]"; |
| 176 | static const char at_offsetcxx . . $ at_offset.[] = "\n\t\tat offset:\t\t\t[%d]"; |
| 177 | static const char expected_contentcxx . . $ expected_content.[] = "\n\t\t\tactual value:\t\t[0x%02x]\n\t\t\texpected value:\t\t[0x%02x]"; |
| 178 | |
| 179 | |
| 180 | static void format_actual_string_valuecxx . . $ format_actual_string_value(6d3357e79d281725).(intptr_tcxx . . $ intptr_t# actual_value, char *message, size_t message_size) { |
| 181 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 182 | actual_value_string_formatcxx . . $ actual_value_string_format., |
| 183 | (const char *)actual_value); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void format_expected_string_valuecxx . . $ format_expected_string_value(f563ee03875540b8).(Constraintcxx . . $ Constraint# *constraint, char *message, size_t message_size) { |
| 188 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 189 | constraint->expected_value_messagecxx . . $ Constraint_#expected_value_message., |
| 190 | constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..string_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#string_value.); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | char *failure_message_forcxx . . $ failure_message_for(12dea272953de187).(Constraintcxx . . $ Constraint# *constraint, const char *actual_string, intptr_tcxx . . $ intptr_t# actual_value) { |
| 195 | char actual_int_value_string[32]; |
| 196 | const char *actual_value_as_string; |
| 197 | char *message; |
| 198 | size_t message_size = strlencxx . . $ strlen(751346f8406fd082).(constraint_as_string_formatcxx . . $ constraint_as_string_format.) + |
| 199 | strlencxx . . $ strlen(751346f8406fd082).(expected_value_string_formatcxx . . $ expected_value_string_format.) + |
| 200 | strlencxx . . $ strlen(751346f8406fd082).(actual_value_string_formatcxx . . $ actual_value_string_format.) + |
| 201 | strlencxx . . $ strlen(751346f8406fd082).(at_offsetcxx . . $ at_offset.) + |
| 202 | strlencxx . . $ strlen(751346f8406fd082).(constraint->actual_value_messagecxx . . $ Constraint_#actual_value_message.) + |
| 203 | strlencxx . . $ strlen(751346f8406fd082).(constraint->expected_value_messagecxx . . $ Constraint_#expected_value_message.) + |
| 204 | strlencxx . . $ strlen(751346f8406fd082).(constraint->expected_value_namecxx . . $ Constraint_#expected_value_name.) + |
| 205 | strlencxx . . $ strlen(751346f8406fd082).(constraint->namecxx . . $ Constraint_#name.) + |
| 206 | strlencxx . . $ strlen(751346f8406fd082).(actual_string) + |
| 207 | 512; // just in case |
| 208 | |
| 209 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(actual_int_value_string, sizeof(actual_int_value_string) - 1, "%" PRIdPTRcxx . . $ `<external>/15933738808807975373/inttypes.h:165:10`!, actual_value); |
| 210 | |
| 211 | if (values_are_strings_incxx . . $ values_are_strings_in(1ba69099f960dda3).(constraint)) { |
| 212 | message_size += strlencxx . . $ strlen(751346f8406fd082).(constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..string_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#string_value.); |
| 213 | if (actual_value != (intptr_tcxx . . $ intptr_t#)NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 214 | message_size += strlencxx . . $ strlen(751346f8406fd082).((char *)actual_value); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | message = (char *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(message_size); |
| 219 | |
| 220 | /* if the actual value expression contains '%' we want it to survive the final expansion with |
| 221 | arguments that happens in assert_true() */ |
| 222 | actual_value_as_string = double_all_percent_signs_incxx . . $ double_all_percent_signs_in(a8b8e32347182212).(actual_string); |
| 223 | |
| 224 | /* expand the constraint with the actual value in string format... */ |
| 225 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message, message_size - 1, |
| 226 | constraint_as_string_formatcxx . . $ constraint_as_string_format., |
| 227 | actual_value_as_string, |
| 228 | constraint->namecxx . . $ Constraint_#name.); |
| 229 | |
| 230 | freecxx . . $ free(5d22bdacc48458e8).((void*)actual_value_as_string); |
| 231 | |
| 232 | if (no_expected_value_incxx . . $ no_expected_value_in(1ba69099f960dda3).(constraint)) { |
| 233 | return message; |
| 234 | } else |
| 235 | strcatcxx . . $ strcat(b7b3a83273496486).(message, " "); |
| 236 | |
| 237 | /* expand the expected value string for all assertions that have one... */ |
| 238 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 239 | expected_value_string_formatcxx . . $ expected_value_string_format., |
| 240 | constraint->expected_value_namecxx . . $ Constraint_#expected_value_name.); |
| 241 | |
| 242 | if (actual_value_not_necessary_forcxx . . $ actual_value_not_necessary_for(be04999f9e28364e).(constraint, actual_string, actual_int_value_string)) { |
| 243 | /* when the actual string and the actual value are the same, don't print both of them */ |
| 244 | /* also, don't print "0" for false and "1" for true */ |
| 245 | /* also, don't print expected/actual for contents constraints since that is useless */ |
| 246 | return message; |
| 247 | } |
| 248 | |
| 249 | /* for string constraints, print out the strings encountered and not their pointer values */ |
| 250 | if (values_are_strings_incxx . . $ values_are_strings_in(1ba69099f960dda3).(constraint)) { |
| 251 | format_actual_string_valuecxx . . $ format_actual_string_value(6d3357e79d281725).(actual_value, message, message_size); |
| 252 | if (is_equal_to_string_constraintcxx . . $ is_equal_to_string_constraint(b7da63ddd1fb537e).(constraint)) { |
| 253 | strcatcxx . . $ strcat(b7b3a83273496486).(message, "\n"); |
| 254 | format_expected_string_valuecxx . . $ format_expected_string_value(f563ee03875540b8).(constraint, message, message_size); |
| 255 | } |
| 256 | /* The final string may have percent characters, so, since it is |
| 257 | later used in a (v)printf, we have to double them |
| 258 | */ |
| 259 | if (next_percent_signcxx . . $ next_percent_sign(fb58d2e09a2a1ce6).(message) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 260 | char *message_with_doubled_percent_signs = double_all_percent_signs_incxx . . $ double_all_percent_signs_in(a8b8e32347182212).(message); |
| 261 | freecxx . . $ free(5d22bdacc48458e8).(message); |
| 262 | message = message_with_doubled_percent_signs; |
| 263 | } |
| 264 | return message; |
| 265 | } |
| 266 | |
| 267 | /* show difference for contents as a position */ |
| 268 | if (is_content_comparingcxx . . $ is_content_comparing(1ba69099f960dda3).(constraint)) { |
| 269 | int difference_index = find_index_of_differencecxx . . $ find_index_of_difference(c5040b39c98d43dd).(constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..pointer_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#pointer_value., |
| 270 | (void *)actual_value, |
| 271 | constraint->size_of_expected_valuecxx . . $ Constraint_#size_of_expected_value.); |
| 272 | if (difference_index != -1) { |
| 273 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 274 | at_offsetcxx . . $ at_offset., |
| 275 | difference_index); |
| 276 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 277 | expected_contentcxx . . $ expected_content., |
| 278 | ((char *)actual_value)[difference_index], |
| 279 | ((char *)constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..pointer_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#pointer_value.)[difference_index]); |
| 280 | } |
| 281 | return message; |
| 282 | } |
| 283 | |
| 284 | /* add the actual value */ |
| 285 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 286 | constraint->actual_value_messagecxx . . $ Constraint_#actual_value_message., |
| 287 | actual_value); |
| 288 | |
| 289 | /* add the expected value */ |
| 290 | if (strstrcxx . . $ strstr(a713fe7b9a57cf96).(constraint->namecxx . . $ Constraint_#name., "not ") == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 291 | strcatcxx . . $ strcat(b7b3a83273496486).(message, "\n"); |
| 292 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(message + strlencxx . . $ strlen(751346f8406fd082).(message), message_size - strlencxx . . $ strlen(751346f8406fd082).(message) - 1, |
| 293 | constraint->expected_value_messagecxx . . $ Constraint_#expected_value_message., |
| 294 | constraint->expected_valuecxx . . $ Constraint_#expected_value..valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#value..string_valuecxx . . $ $anonymous_type_b16cac6eed758fe0_1#$anonymous_type_2#string_value.); |
| 295 | } |
| 296 | |
| 297 | return message; |
| 298 | } |