Blame support/support_test_compare_string.c

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