Blame support/support_test_compare_string.c

Packit Service f9020f
/* Check two strings for equality.
Packit Service f9020f
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service f9020f
   This file is part of the GNU C Library.
Packit Service f9020f
Packit Service f9020f
   The GNU C Library is free software; you can redistribute it and/or
Packit Service f9020f
   modify it under the terms of the GNU Lesser General Public
Packit Service f9020f
   License as published by the Free Software Foundation; either
Packit Service f9020f
   version 2.1 of the License, or (at your option) any later version.
Packit Service f9020f
Packit Service f9020f
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service f9020f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service f9020f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service f9020f
   Lesser General Public License for more details.
Packit Service f9020f
Packit Service f9020f
   You should have received a copy of the GNU Lesser General Public
Packit Service f9020f
   License along with the GNU C Library; if not, see
Packit Service f9020f
   <http://www.gnu.org/licenses/>.  */
Packit Service f9020f
Packit Service f9020f
#include <stdio.h>
Packit Service f9020f
#include <stdlib.h>
Packit Service f9020f
#include <string.h>
Packit Service f9020f
#include <support/check.h>
Packit Service f9020f
#include <support/support.h>
Packit Service f9020f
#include <support/xmemstream.h>
Packit Service f9020f
Packit Service f9020f
static void
Packit Service f9020f
report_length (const char *what, const char *str, size_t length)
Packit Service f9020f
{
Packit Service f9020f
  if (str == NULL)
Packit Service f9020f
    printf ("  %s string: NULL\n", what);
Packit Service f9020f
  else
Packit Service f9020f
    printf ("  %s string: %zu bytes\n", what, length);
Packit Service f9020f
}
Packit Service f9020f
Packit Service f9020f
static void
Packit Service f9020f
report_string (const char *what, const unsigned char *blob,
Packit Service f9020f
               size_t length, const char *expr)
Packit Service f9020f
{
Packit Service f9020f
  if (length > 0)
Packit Service f9020f
    {
Packit Service f9020f
      printf ("  %s (evaluated from %s):\n", what, expr);
Packit Service f9020f
      char *quoted = support_quote_blob (blob, length);
Packit Service f9020f
      printf ("      \"%s\"\n", quoted);
Packit Service f9020f
      free (quoted);
Packit Service f9020f
Packit Service f9020f
      fputs ("     ", stdout);
Packit Service f9020f
      for (size_t i = 0; i < length; ++i)
Packit Service f9020f
        printf (" %02X", blob[i]);
Packit Service f9020f
      putc ('\n', stdout);
Packit Service f9020f
    }
Packit Service f9020f
}
Packit Service f9020f
Packit Service f9020f
static size_t
Packit Service f9020f
string_length_or_zero (const char *str)
Packit Service f9020f
{
Packit Service f9020f
  if (str == NULL)
Packit Service f9020f
    return 0;
Packit Service f9020f
  else
Packit Service f9020f
    return strlen (str);
Packit Service f9020f
}
Packit Service f9020f
Packit Service f9020f
void
Packit Service f9020f
support_test_compare_string (const char *left, const char *right,
Packit Service f9020f
                             const char *file, int line,
Packit Service f9020f
                             const char *left_expr, const char *right_expr)
Packit Service f9020f
{
Packit Service f9020f
  /* Two null pointers are accepted.  */
Packit Service f9020f
  if (left == NULL && right == NULL)
Packit Service f9020f
    return;
Packit Service f9020f
Packit Service f9020f
  size_t left_length = string_length_or_zero (left);
Packit Service f9020f
  size_t right_length = string_length_or_zero (right);
Packit Service f9020f
Packit Service f9020f
  if (left_length != right_length || left == NULL || right == NULL
Packit Service f9020f
      || memcmp (left, right, left_length) != 0)
Packit Service f9020f
    {
Packit Service f9020f
      support_record_failure ();
Packit Service f9020f
      printf ("%s:%d: error: blob comparison failed\n", file, line);
Packit Service f9020f
      if (left_length == right_length && right != NULL && left != NULL)
Packit Service f9020f
        printf ("  string length: %zu bytes\n", left_length);
Packit Service f9020f
      else
Packit Service f9020f
        {
Packit Service f9020f
          report_length ("left", left, left_length);
Packit Service f9020f
          report_length ("right", right, right_length);
Packit Service f9020f
        }
Packit Service f9020f
      report_string ("left", (const unsigned char *) left,
Packit Service f9020f
                     left_length, left_expr);
Packit Service f9020f
      report_string ("right", (const unsigned char *) right,
Packit Service f9020f
                     right_length, right_expr);
Packit Service f9020f
    }
Packit Service f9020f
}