OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [libiberty/] [testsuite/] [test-expandargv.c] - Diff between revs 38 and 156

Only display areas with differences | Details | Blame | View Log

Rev 38 Rev 156
/* expandargv test program,
/* expandargv test program,
   Copyright (C) 2006 Free Software Foundation, Inc.
   Copyright (C) 2006 Free Software Foundation, Inc.
   Written by Carlos O'Donell <carlos@codesourcery.com>
   Written by Carlos O'Donell <carlos@codesourcery.com>
 
 
   This file is part of the libiberty library, which is part of GCC.
   This file is part of the libiberty library, which is part of GCC.
 
 
   This file is free software; you can redistribute it and/or modify
   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   In addition to the permissions in the GNU General Public License, the
   In addition to the permissions in the GNU General Public License, the
   Free Software Foundation gives you unlimited permission to link the
   Free Software Foundation gives you unlimited permission to link the
   compiled version of this file into combinations with other programs,
   compiled version of this file into combinations with other programs,
   and to distribute those combinations without any restriction coming
   and to distribute those combinations without any restriction coming
   from the use of this file.  (The General Public License restrictions
   from the use of this file.  (The General Public License restrictions
   do apply in other respects; for example, they cover modification of
   do apply in other respects; for example, they cover modification of
   the file, and distribution when not linked into a combined
   the file, and distribution when not linked into a combined
   executable.)
   executable.)
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
*/
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config.h"
#endif
#endif
#include "libiberty.h"
#include "libiberty.h"
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
#include <errno.h>
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.h>
#endif
#endif
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#endif
#endif
 
 
#ifndef EXIT_SUCCESS
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#define EXIT_SUCCESS 0
#endif
#endif
 
 
#ifndef EXIT_FAILURE
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#define EXIT_FAILURE 1
#endif
#endif
 
 
static void fatal_error (int, const char *, int) ATTRIBUTE_NORETURN;
static void fatal_error (int, const char *, int) ATTRIBUTE_NORETURN;
void writeout_test (int, const char *);
void writeout_test (int, const char *);
void run_replaces (char *);
void run_replaces (char *);
void hook_char_replace (char *, size_t, char, char);
void hook_char_replace (char *, size_t, char, char);
int run_tests (const char **);
int run_tests (const char **);
void erase_test (int);
void erase_test (int);
 
 
/* Test input data, argv before, and argv after:
/* Test input data, argv before, and argv after:
 
 
   The \n is an important part of test_data since expandargv
   The \n is an important part of test_data since expandargv
   may have to work in environments where \n is translated
   may have to work in environments where \n is translated
   as \r\n. Thus \n is included in the test data for the file.
   as \r\n. Thus \n is included in the test data for the file.
 
 
   We use \b to indicate that the test data is the null character.
   We use \b to indicate that the test data is the null character.
   This is because we use \0 normally to represent the end of the
   This is because we use \0 normally to represent the end of the
   file data, so we need something else for this. */
   file data, so we need something else for this. */
 
 
#define FILENAME_PATTERN "test-expandargv-%d.lst"
#define FILENAME_PATTERN "test-expandargv-%d.lst"
#define ARGV0 "test-expandargv"
#define ARGV0 "test-expandargv"
 
 
const char *test_data[] = {
const char *test_data[] = {
  /* Test 0 - Check for expansion with \r\n */
  /* Test 0 - Check for expansion with \r\n */
  "a\r\nb",     /* Test 0 data */
  "a\r\nb",     /* Test 0 data */
  ARGV0,
  ARGV0,
  "@test-expandargv-0.lst",
  "@test-expandargv-0.lst",
  0, /* End of argv[] before expansion */
  0, /* End of argv[] before expansion */
  ARGV0,
  ARGV0,
  "a",
  "a",
  "b",
  "b",
  0, /* End of argv[] after expansion */
  0, /* End of argv[] after expansion */
 
 
  /* Test 1 - Check for expansion with \n */
  /* Test 1 - Check for expansion with \n */
  "a\nb",       /* Test 1 data */
  "a\nb",       /* Test 1 data */
  ARGV0,
  ARGV0,
  "@test-expandargv-1.lst",
  "@test-expandargv-1.lst",
  0,
  0,
  ARGV0,
  ARGV0,
  "a",
  "a",
  "b",
  "b",
  0,
  0,
 
 
  /* Test 2 - Check for expansion with \0 */
  /* Test 2 - Check for expansion with \0 */
  "a\bb",       /* Test 2 data */
  "a\bb",       /* Test 2 data */
  ARGV0,
  ARGV0,
  "@test-expandargv-2.lst",
  "@test-expandargv-2.lst",
  0,
  0,
  ARGV0,
  ARGV0,
  "a",
  "a",
  0,
  0,
 
 
  /* Test 3 - Check for expansion with only \0 */
  /* Test 3 - Check for expansion with only \0 */
  "\b",         /* Test 3 data */
  "\b",         /* Test 3 data */
  ARGV0,
  ARGV0,
  "@test-expandargv-3.lst",
  "@test-expandargv-3.lst",
  0,
  0,
  ARGV0,
  ARGV0,
  0,
  0,
 
 
  0 /* Test done marker, don't remove. */
  0 /* Test done marker, don't remove. */
};
};
 
 
/* Print a fatal error and exit.  LINE is the line number where we
/* Print a fatal error and exit.  LINE is the line number where we
   detected the error, ERRMSG is the error message to print, and ERR
   detected the error, ERRMSG is the error message to print, and ERR
   is 0 or an errno value to print.  */
   is 0 or an errno value to print.  */
 
 
static void
static void
fatal_error (int line, const char *errmsg, int err)
fatal_error (int line, const char *errmsg, int err)
{
{
  fprintf (stderr, "test-expandargv:%d: %s", line, errmsg);
  fprintf (stderr, "test-expandargv:%d: %s", line, errmsg);
  if (errno != 0)
  if (errno != 0)
    fprintf (stderr, ": %s", xstrerror (err));
    fprintf (stderr, ": %s", xstrerror (err));
  fprintf (stderr, "\n");
  fprintf (stderr, "\n");
  exit (EXIT_FAILURE);
  exit (EXIT_FAILURE);
}
}
 
 
/* hook_char_replace:
/* hook_char_replace:
     Replace 'replacethis' with 'withthis' */
     Replace 'replacethis' with 'withthis' */
 
 
void
void
hook_char_replace (char *string, size_t len, char replacethis, char withthis)
hook_char_replace (char *string, size_t len, char replacethis, char withthis)
{
{
  int i = 0;
  int i = 0;
  for (i = 0; i < len; i++)
  for (i = 0; i < len; i++)
    if (string[i] == replacethis)
    if (string[i] == replacethis)
      string[i] = withthis;
      string[i] = withthis;
}
}
 
 
/* run_replaces:
/* run_replaces:
     Hook here all the character for character replaces.
     Hook here all the character for character replaces.
     Be warned that expanding the string or contracting the string
     Be warned that expanding the string or contracting the string
     should be handled with care. */
     should be handled with care. */
 
 
void
void
run_replaces (char * string)
run_replaces (char * string)
{
{
  /* Store original string size */
  /* Store original string size */
  size_t len = strlen (string);
  size_t len = strlen (string);
  hook_char_replace (string, len, '\b', '\0');
  hook_char_replace (string, len, '\b', '\0');
}
}
 
 
/* write_test:
/* write_test:
   Write test datafile */
   Write test datafile */
 
 
void
void
writeout_test (int test, const char * test_data)
writeout_test (int test, const char * test_data)
{
{
  char filename[256];
  char filename[256];
  FILE *fd;
  FILE *fd;
  size_t len;
  size_t len;
  char * parse;
  char * parse;
 
 
  /* Unique filename per test */
  /* Unique filename per test */
  sprintf (filename, FILENAME_PATTERN, test);
  sprintf (filename, FILENAME_PATTERN, test);
  fd = fopen (filename, "w");
  fd = fopen (filename, "w");
  if (fd == NULL)
  if (fd == NULL)
    fatal_error (__LINE__, "Failed to create test file.", errno);
    fatal_error (__LINE__, "Failed to create test file.", errno);
 
 
  /* Generate RW copy of data for replaces */
  /* Generate RW copy of data for replaces */
  len = strlen (test_data);
  len = strlen (test_data);
  parse = malloc (sizeof (char) * (len + 1));
  parse = malloc (sizeof (char) * (len + 1));
  if (parse == NULL)
  if (parse == NULL)
    fatal_error (__LINE__, "Failed to malloc parse.", errno);
    fatal_error (__LINE__, "Failed to malloc parse.", errno);
 
 
  memcpy (parse, test_data, sizeof (char) * len);
  memcpy (parse, test_data, sizeof (char) * len);
  /* Run all possible replaces */
  /* Run all possible replaces */
  run_replaces (parse);
  run_replaces (parse);
 
 
  fwrite (parse, len, sizeof (char), fd);
  fwrite (parse, len, sizeof (char), fd);
  free (parse);
  free (parse);
  fclose (fd);
  fclose (fd);
}
}
 
 
/* erase_test:
/* erase_test:
     Erase the test file */
     Erase the test file */
 
 
void
void
erase_test (int test)
erase_test (int test)
{
{
  char filename[256];
  char filename[256];
  sprintf (filename, FILENAME_PATTERN, test);
  sprintf (filename, FILENAME_PATTERN, test);
  if (unlink (filename) != 0)
  if (unlink (filename) != 0)
    fatal_error (__LINE__, "Failed to erase test file.", errno);
    fatal_error (__LINE__, "Failed to erase test file.", errno);
}
}
 
 
 
 
/* run_tests:
/* run_tests:
    Run expandargv
    Run expandargv
    Compare argv before and after.
    Compare argv before and after.
    Return number of fails */
    Return number of fails */
 
 
int
int
run_tests (const char **test_data)
run_tests (const char **test_data)
{
{
  int argc_after, argc_before;
  int argc_after, argc_before;
  char ** argv_before, ** argv_after;
  char ** argv_before, ** argv_after;
  int i, j, k, fails, failed;
  int i, j, k, fails, failed;
 
 
  i = j = fails = 0;
  i = j = fails = 0;
  /* Loop over all the tests */
  /* Loop over all the tests */
  while (test_data[j])
  while (test_data[j])
    {
    {
      /* Write test data */
      /* Write test data */
      writeout_test (i, test_data[j++]);
      writeout_test (i, test_data[j++]);
      /* Copy argv before */
      /* Copy argv before */
      argv_before = dupargv ((char **) &test_data[j]);
      argv_before = dupargv ((char **) &test_data[j]);
 
 
      /* Count argc before/after */
      /* Count argc before/after */
      argc_before = 0;
      argc_before = 0;
      argc_after = 0;
      argc_after = 0;
      while (test_data[j + argc_before])
      while (test_data[j + argc_before])
        argc_before++;
        argc_before++;
      j += argc_before + 1; /* Skip null */
      j += argc_before + 1; /* Skip null */
      while (test_data[j + argc_after])
      while (test_data[j + argc_after])
        argc_after++;
        argc_after++;
 
 
      /* Copy argv after */
      /* Copy argv after */
      argv_after = dupargv ((char **) &test_data[j]);
      argv_after = dupargv ((char **) &test_data[j]);
 
 
      /* Run all possible replaces */
      /* Run all possible replaces */
      for (k = 0; k < argc_before; k++)
      for (k = 0; k < argc_before; k++)
        run_replaces (argv_before[k]);
        run_replaces (argv_before[k]);
      for (k = 0; k < argc_after; k++)
      for (k = 0; k < argc_after; k++)
        run_replaces (argv_after[k]);
        run_replaces (argv_after[k]);
 
 
      /* Run test: Expand arguments */
      /* Run test: Expand arguments */
      expandargv (&argc_before, &argv_before);
      expandargv (&argc_before, &argv_before);
 
 
      failed = 0;
      failed = 0;
      /* Compare size first */
      /* Compare size first */
      if (argc_before != argc_after)
      if (argc_before != argc_after)
        {
        {
          printf ("FAIL: test-expandargv-%d. Number of arguments don't match.\n", i);
          printf ("FAIL: test-expandargv-%d. Number of arguments don't match.\n", i);
          failed++;
          failed++;
        }
        }
      /* Compare each of the argv's ... */
      /* Compare each of the argv's ... */
      else
      else
        for (k = 0; k < argc_after; k++)
        for (k = 0; k < argc_after; k++)
          if (strncmp (argv_before[k], argv_after[k], strlen(argv_after[k])) != 0)
          if (strncmp (argv_before[k], argv_after[k], strlen(argv_after[k])) != 0)
            {
            {
              printf ("FAIL: test-expandargv-%d. Arguments don't match.\n", i);
              printf ("FAIL: test-expandargv-%d. Arguments don't match.\n", i);
              failed++;
              failed++;
            }
            }
 
 
      if (!failed)
      if (!failed)
        printf ("PASS: test-expandargv-%d.\n", i);
        printf ("PASS: test-expandargv-%d.\n", i);
      else
      else
        fails++;
        fails++;
 
 
      freeargv (argv_before);
      freeargv (argv_before);
      freeargv (argv_after);
      freeargv (argv_after);
      /* Advance to next test */
      /* Advance to next test */
      j += argc_after + 1;
      j += argc_after + 1;
      /* Erase test file */
      /* Erase test file */
      erase_test (i);
      erase_test (i);
      i++;
      i++;
    }
    }
  return fails;
  return fails;
}
}
 
 
/* main:
/* main:
    Run tests.
    Run tests.
    Check result and exit with appropriate code. */
    Check result and exit with appropriate code. */
 
 
int
int
main(int argc, char **argv)
main(int argc, char **argv)
{
{
  int fails;
  int fails;
  /* Repeat for all the tests:
  /* Repeat for all the tests:
     - Parse data array and write into file.
     - Parse data array and write into file.
       - Run replace hooks before writing to file.
       - Run replace hooks before writing to file.
     - Parse data array and build argv before/after.
     - Parse data array and build argv before/after.
       - Run replace hooks on argv before/after
       - Run replace hooks on argv before/after
     - Run expandargv.
     - Run expandargv.
     - Compare output of expandargv argv to after argv.
     - Compare output of expandargv argv to after argv.
       - If they compare the same then test passes
       - If they compare the same then test passes
         else the test fails.
         else the test fails.
     - Erase test file. */
     - Erase test file. */
 
 
  fails = run_tests (test_data);
  fails = run_tests (test_data);
  if (!fails)
  if (!fails)
    exit (EXIT_SUCCESS);
    exit (EXIT_SUCCESS);
  else
  else
    exit (EXIT_FAILURE);
    exit (EXIT_FAILURE);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.