1/* Check two strings for equality.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <wchar.h>
23#include <support/check.h>
24#include <support/support.h>
25#include <support/xmemstream.h>
26
27static void
28report_length (const char *what, const CHAR *str, size_t length)
29{
30 if (str == NULL)
31 printf (format: " %s string: NULL\n", what);
32 else
33 printf (format: " %s string: %zu %s\n", what, length,
34 WIDE ? "wide characters" : "bytes");
35}
36
37static void
38report_string (const char *what, const UCHAR *blob,
39 size_t length, const char *expr)
40{
41 if (length > 0)
42 {
43 printf (format: " %s (evaluated from %s):\n", what, expr);
44 char *quoted = SUPPORT_QUOTE_BLOB (blob, length);
45 printf (format: " %s\"%s\"\n", LPREFIX, quoted);
46 free (ptr: quoted);
47
48 fputs (s: " ", stdout);
49 for (size_t i = 0; i < length; ++i)
50 printf (format: " %02X", (unsigned int) blob[i]);
51 putc (c: '\n', stdout);
52 }
53}
54
55static size_t
56string_length_or_zero (const CHAR *str)
57{
58 if (str == NULL)
59 return 0;
60 else
61 return STRLEN (s: str);
62}
63
64void
65SUPPORT_TEST_COMPARE_STRING (const CHAR *left, const CHAR *right,
66 const char *file, int line,
67 const char *left_expr, const char *right_expr)
68{
69 /* Two null pointers are accepted. */
70 if (left == NULL && right == NULL)
71 return;
72
73 size_t left_length = string_length_or_zero (str: left);
74 size_t right_length = string_length_or_zero (str: right);
75
76 if (left_length != right_length || left == NULL || right == NULL
77 || MEMCMP (s1: left, s2: right, n: left_length) != 0)
78 {
79 support_record_failure ();
80 printf (format: "%s:%d: error: string comparison failed\n", file, line);
81 if (left_length == right_length && right != NULL && left != NULL)
82 printf (format: " string length: %zu %s\n", left_length,
83 WIDE ? "wide characters" : "bytes");
84 else
85 {
86 report_length (what: "left", str: left, length: left_length);
87 report_length (what: "right", str: right, length: right_length);
88 }
89 report_string (what: "left", blob: (const UCHAR *) left,
90 length: left_length, expr: left_expr);
91 report_string (what: "right", blob: (const UCHAR *) right,
92 length: right_length, expr: right_expr);
93 }
94}
95

source code of glibc/support/support_test_compare_string_main.c