/*
   <p>Test harness for the numconv and numdisp functions.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include <stdlib.h>
#include "environ.h"

#ifdef OS9
#include <strings.h>
#else
#include <string.h>
#endif

int showall = FALSE;

void test(char *s, int n, char *u, char *e)
{
   int   r;
   char  *rs;
   
   r = numconv(s);
   if ( r != n || showall)
      printf("numconv: '%s' --> %d, expected %d\n", s, r, n);

   rs = numdisp(r, u);
   if (strcmp(rs, e) != 0 || showall)
      printf("numdisp: %d --> '%s', expected '%s'\n", r, rs, e);
}

main(int argc, char **argv)
{
   if (argc > 1)
   {
      if (strcmp(argv[1], "-s") == 0)
         showall = TRUE;
      else
      {
         printf("Syntax:   testnumconv -s\n");
         printf("Function: Regression tests for numconv() and numdisp()\n");
         printf("          Default is to show only unexpected results.\n");
         printf("Options:  -s  show all test results.\n");
         exit(0);
      }
   }

   if (showall)
      printf("No output expected from correct operation\n");

   test("", 0, "", "0");
   test("42", 42, "", "42");
   test("2048", 2048, "dec", "2048");
   test("2kb", 2048, "K", "2Kb");
   test("4KB", 4096, "KB", "4Kb");
   test("2m", 2097152, "mb", "2Mb");
   test("4MB", 4194304, "M", "4Mb");
   test("$FFFE", 65534, "$", "$FFFE");
   test("$1b", 27, "$", "$1B");
   test("$0c", 12, "$", "$C");
   test("$0c", 12, "$2", "$0C");
   test("$100", 256, "$4", "$0100");
   test("$0003", 3, "$4", "$0003");
   test("0xFFFE", 65534, "0x", "0XFFFE");
   test("0x1b", 27, "0x", "0X1B");
   test("0x0c", 12, "0x", "0XC");
   test("0x0c", 12, "0x2", "0X0C");
   test("0x100", 256, "0x4", "0X0100");
   test("0x0003", 3, "0x4", "0X0003");
   test("0", 0, "x", "0");
   test("0xFFFE", 65534, "x", "FFFE");
   test("0x1b", 27, "x", "1B");
   test("0x0c", 12, "x", "C");
   test("0x100", 256, "x", "100");
   test("0x0003", 3, "x", "3");
   printf("Test ended\n");
   return(0);
}
