## tests/test_.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. print_title
2. usage
3. get_options

    /***************************************
      $Revision: 1.1.1.1 $

      Example code: Unit test driver template.

      ******************/ /******************
      Modification History:
            ottrey (07/04/1999) Created.
      ******************/ /******************
      Copyright (c) 1999                              RIPE NCC

      All Rights Reserved

      Permission to use, copy, modify, and distribute this software and its
      documentation for any purpose and without fee is hereby granted,
      provided that the above copyright notice appear in all copies and that
      both that copyright notice and this permission notice appear in
      supporting documentation, and that the name of the author not be
      used in advertising or publicity pertaining to distribution of the
      software without specific, written prior permission.

      THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      ***************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>

    /*+ String sizes +*/
    #define STR_S   63
    #define STR_M   255
    #define STR_L   1023
    #define STR_XL  4095
    #define STR_XXL 16383

    #define MAX_TESTS 100

    /*
     * Global Variables
     */
    static char Unit_name[STR_S];      /*+ Name of test. +*/
    int Verbose;                    /*+ Use verbose output. +*/
    int Short;                    /*+ Use short output. +*/
    char *Infile_name;              /*+ Input file to read from. +*/
    int Test[MAX_TESTS];          /*+ Test to execute +*/

    /* print_title() */
    /*++++++++++++++++++++++++++++++++++++++
      Prints a pretty title

      char *text  The text to be displayed in the title.

      More:
      +html+ <PRE>
      +latex+ \begin{verbatim}
      Authors:
            ottrey

      +latex+ \end{verbatim}
      +html+ </PRE>

      ++++++++++++++++++++++++++++++++++++++*/
    static void print_title(char *text) {
    /* [<][>][^][v][top][bottom][index][help] */
      char test_name[STR_M];
      char border[STR_M];
      unsigned int len, i;

      sprintf(test_name, "%s: Unit test %s", Unit_name, text);

      if (Infile_name != NULL) {
        strcat(test_name, " (");
        strcat(test_name, Infile_name);
        strcat(test_name, ")");
      }

      len = strlen(test_name);
      border[0] = '*';
      for (i=1; i < len+3; i++) {
        border[i] = '-';
      }
      border[len+3] = '*';
      border[len+4] = '\0';
      printf("%s\n| %s |\n%s\n", border, test_name, border);

    } /* print_title() */

    /* usage() */
    /*++++++++++++++++++++++++++++++++++++++
      Displays the usage pattern

      More:
      +html+ <PRE>
      +latex+ \begin{verbatim}
      Authors:
            ottrey

      Post-Conditions:
            The user is returned the usage pattern.

      +latex+ \end{verbatim}
      +html+ </PRE>

      ++++++++++++++++++++++++++++++++++++++*/
    static void usage(void)
    /* [<][>][^][v][top][bottom][index][help] */
    {
      fprintf(stderr, "Usage: %s\n", Unit_name);
      fprintf(stderr, " -v \t\tVerbose\n");
      fprintf(stderr, " -s \t\tShort\n");
      fprintf(stderr, " --tests=1,3,4 \tONLY do tests 1, 3 and 4\n");
      fprintf(stderr, " --file=infile \tGet input from `infile'\n");

      exit(1);
    } /* usage() */

    /* get_options() */
    /*++++++++++++++++++++++++++++++++++++++
      Gets the options from the command line.

      More:
      +html+ <PRE>
      +latex+ \begin{verbatim}
      Authors:
            ottrey
      +latex+ \end{verbatim}
      +html+ </PRE>

      ++++++++++++++++++++++++++++++++++++++*/
    static void get_options(int argc, char **argv) {
    /* [<][>][^][v][top][bottom][index][help] */
      int i;
      int test_no;
      char *test_no_str;

      Verbose = 0;
      Short = 0;
      for (i=0; i < MAX_TESTS; i++) {
        Test[i] = 1;
      }

      strcpy(Unit_name, argv[0]);

      for (i=1; i < argc; i++) {
        if (strncmp(argv[i], "-v", 2) == 0) {
          Short = 0;
          Verbose = 1;
        }
        else if (strncmp(argv[i], "-s", 2) == 0) {
          Short = 1;
          Verbose = 0;
        }
        else if (strncmp(argv[i], "--file=", 7) == 0) {
          strtok(argv[i], "=");
          Infile_name = (char *)strtok(NULL, "=");
        }
        else if (strncmp(argv[i], "--tests=", 8) == 0) {
          /* Clear the tests */
          for (test_no=0; test_no < MAX_TESTS; test_no++) {
            Test[test_no] = 0;
          }
          /* Now set these tests */
          strtok(argv[i], "=");
          while ( (test_no_str = (char *)strtok(NULL, ", ")) != NULL) {
            test_no = atoi(test_no_str);
            Test[test_no] = 1;
          }
        }
        else {
          fprintf(stderr, "%s: Invalid option `%s'\n", Unit_name, argv[i]);
          usage();
        }
      }
    } /* get_options() */

/* [<][>][^][v][top][bottom][index][help] */