#define DBG 0
#define ARG 0
#include "frmwrk/inc/main.h"
/* ---------------------------------------------------------------------
   (c) ED 2004
   Project      : CLIB - Flexible STRing
   Function     : Flexible string manager
   Module       : FSTR
   File         : tfstr.c
   Created      : 03-12-2004
   Modified     : 03-12-2004
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   0.0 03-12-2004 Created

   --------------------------------------------------------------------- */
#ifdef __cplusplus
#error This is not C++. Please use a C compiler.
#endif

#include "ed/inc/fstr.h"
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"

/* private macro definitions =========================================== */

#define ID "FSTR"
#define VER "0.0"

/* private constants =================================================== */

/* private types ======================================================= */

/* private structures ================================================== */

/* private functions =================================================== */

/* ---------------------------------------------------------------------
   test_str ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
static int test_str (fstr_s * fs)
{
   typedef struct
   {
      char const *s_in;
      char const *s_out;
   }
   test_s;

   int ret = EXIT_FAILURE;
   char const *s_out;

   static test_s const a_test[] =
   {
      {"", ""},
      {"a", "a"},
      {"b", "ab"},
      {"c", "abc"},
      {"d", "abcd"},
      {"e", "abcde"},
      {"f", "abcdef"},
      {"g", "abcdefg"},
      {"h", "abcdefgh"},
      {"i", "abcdefghi"},
   };

   size_t i;

   for (i = 0; i < NELEM (a_test); i++)
   {
      test_s const *t = a_test + i;

      fstr_cat (fs, t->s_in);

      s_out = fstr_str (fs);

      if (strcmp (s_out, t->s_out) != 0)
      {
         printf ("error at line #%u\n", i + 1);
         break;
      }

      printf ("len = %u ", (unsigned) fstr_len (fs));
      fstr_dbg (fs);
   }

   if (i == NELEM (a_test))
   {
      puts ("test_str()\n"
            "P A S S E D\n");
      ret = EXIT_SUCCESS;
   }
   return ret;
}

/* ---------------------------------------------------------------------
   test_car ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
static int test_car (fstr_s * fs)
{
   typedef struct
   {
      enum
      {
         CLR, ADD, DEL
      }
      cde;
      int c_in;
      char const *s_out;
   }
   test_s;

   int ret = EXIT_FAILURE;
   char const *s_out;

   static test_s const a_test[] =
   {
      {CLR, 'a', ""},
      {ADD, 'a', "a"},
      {ADD, 'b', "ab"},
      {ADD, 'c', "abc"},
      {ADD, 'd', "abcd"},
      {ADD, 'e', "abcde"},
      {ADD, 'f', "abcdef"},
      {ADD, 'g', "abcdefg"},
      {ADD, 'h', "abcdefgh"},
      {ADD, 'i', "abcdefghi"},
      {DEL, -1, "abcdefgh"},
   };

   size_t i;


   for (i = 0; i < NELEM (a_test); i++)
   {
      test_s const *t = a_test + i;

      switch (t->cde)
      {

      case CLR:
         fstr_clr (fs);
         break;

      case ADD:
         fstr_add (fs, t->c_in);
         break;

      case DEL:
         fstr_del (fs);
         break;

      default:
         ASSERT (0);;
      }


      s_out = fstr_str (fs);

      if (strcmp (s_out, t->s_out) != 0)
      {
         printf ("error at line #%u\n", i + 1);
         break;
      }

      printf ("len = %u ", (unsigned) fstr_len (fs));
      fstr_dbg (fs);
   }

   if (i == NELEM (a_test))
   {
      puts ("test_car()\n"
            "P A S S E D\n");
      ret = EXIT_SUCCESS;
   }
   return ret;
}

/* ---------------------------------------------------------------------
   test ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
static int test (void)
{
   int ret = EXIT_SUCCESS;

   {
      fstr_s *fs = fstr_create (2);

      if (fs != NULL)
      {
         fstr_clr (fs);

         ret = test_str (fs);

         if (ret == EXIT_SUCCESS)
         {
            test_car (fs);

            {
               char *s_out = fstr_remove (fs);

#if 1
               fstr_dbg (fs);

#endif
               printf ("s_out = '%s'\n", s_out);

               FREE (s_out);
            }
         }
         fstr_delete (fs), fs = NULL;
      }
   }
   return ret;
}

/* ---------------------------------------------------------------------
   test1 ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
static int test1 (void)
{
   int ret = EXIT_SUCCESS;

   static char const *as[] =
   {"Configuration", "Maintenance"};

   int i = 0;

   fstr_s *fs = fstr_create (8);

   if (fs != NULL)
   {
      char s[8];

      sprintf (s, "%u - ", (unsigned) i + 1);

      fstr_cat (fs, s);
      fstr_dbg (fs);
      printf ("'%s'\n", fstr_str (fs));

      fstr_cat (fs, as[i]);
      fstr_dbg (fs);
      printf ("'%s'\n", fstr_str (fs));

      fstr_delete (fs), fs = NULL;
   }

   return ret;
}

/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   main ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
int main (void)
{
   int ret = EXIT_SUCCESS;

/* identification test */
   printf ("Testing: %s %s\n", fstr_sid (), fstr_sver ());

#if 1
   ret = test ();
#endif

#if 0
   ret = test1 ();
#endif

   return ret;
}

/* ---------------------------------------------------------------------
   Generated by NEW (c) ED 2.4
   Powered by C-code generator (c) ED 2003 1.0
   --------------------------------------------------------------------- */
