/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"ned_testing_.h"

void
ned_testing_init (char *module, char* runmode, char *tests, struct TEST *pTest)
{
  int i, id, qty;
  char *pretest0 = NULL;
  char **pretestlist = NULL;
  char **testlist = NULL;
  char **testdeps = NULL;
  char **testids = NULL;
  int testqty;
  char* NNN = ut_alloc_1d_char (10);

  if (!strcmp (module, "-T"))
    ned_testing_list_t (&pretest0, &pretestlist, &testlist, &testdeps, &qty);
  else if (!strcmp (module, "-M"))
    ned_testing_list_m (&pretest0, &pretestlist, &testlist, &testdeps, &qty);
  else if (!strcmp (module, "-V"))
    ned_testing_list_v (&pretest0, &pretestlist, &testlist, &testdeps, &qty);
  else
    abort ();

  if (!strcmp (runmode, "fast"))
    sprintf (NNN, "%d", 2);
  else if (!strcmp (runmode, "normal"))
    sprintf (NNN, "%d", 32);
  else if (!strcmp (runmode, "extensive"))
    sprintf (NNN, "%d", 512);
  else if (!strcmp (runmode, "paranoiac"))
    sprintf (NNN, "%d", 8192);

  ut_string_fnrs (pretest0, "NNN", NNN, INT_MAX);
  for (i = 0; i < qty; i++)
  {
    ut_string_fnrs (pretestlist[i], "NNN", NNN, INT_MAX);
    ut_string_fnrs (testlist[i], "NNN", NNN, INT_MAX);
  }

  if (pretest0)
  {
    (*pTest).ModPreCom = ut_alloc_1d_char (strlen (pretest0) + 1);
    strcpy ((*pTest).ModPreCom, pretest0);
  }

  if (strstr (tests, "last"))
  {
    char* laststring = ut_alloc_1d_char (10);
    sprintf (laststring, "%d", qty);
    ut_string_fnrs (tests, "last", laststring, 1);
    ut_free_1d_char (laststring);
  }

  else if (!strcmp (tests, "all"))
  {
    (*pTest).Qty = qty;
    (*pTest).Id = ut_alloc_1d_int ((*pTest).Qty + 1);
    (*pTest).Com = ut_alloc_1d_pchar ((*pTest).Qty + 1);
    (*pTest).PreCom = ut_alloc_1d_pchar ((*pTest).Qty + 1);
    (*pTest).ComDep = ut_alloc_1d_pchar ((*pTest).Qty + 1);

    for (i = 0; i < qty; i++)
    {
      (*pTest).Id[i + 1] = i + 1;
      (*pTest).Com[i + 1] = ut_alloc_1d_char (strlen (testlist[i]) + 1);
      strcpy ((*pTest).Com[i + 1], testlist[i]);
      if (pretestlist[i])
      {
	(*pTest).PreCom[i + 1] =
	  ut_alloc_1d_char (strlen (pretestlist[i]) + 1);
	strcpy ((*pTest).PreCom[i + 1], pretestlist[i]);
      }
      if (testdeps[i])
      {
	(*pTest).ComDep[i + 1] =
	  ut_alloc_1d_char (strlen (testdeps[i]) + 1);
	strcpy ((*pTest).ComDep[i + 1], testdeps[i]);
      }
    }
  }

  else
  {
    ut_string_separate (tests, NEUT_SEP_NODEP, &testids, &testqty);
    (*pTest).Qty = testqty;
    (*pTest).Id = ut_alloc_1d_int ((*pTest).Qty + 1);
    (*pTest).Com = ut_alloc_1d_pchar ((*pTest).Qty + 1);
    (*pTest).PreCom = ut_alloc_1d_pchar ((*pTest).Qty + 1);
    (*pTest).ComDep = ut_alloc_1d_pchar ((*pTest).Qty + 1);

    for (i = 0; i < testqty; i++)
    {
      sscanf (testids[i], "%d", &id);
      (*pTest).Id[i + 1] = id;
      (*pTest).Com[i + 1] = ut_alloc_1d_char (strlen (testlist[id - 1]) + 1);
      strcpy ((*pTest).Com[i + 1], testlist[id - 1]);
      if (pretestlist[id - 1])
      {
	(*pTest).PreCom[i + 1] =
	  ut_alloc_1d_char (strlen (pretestlist[id - 1]) + 1);
	strcpy ((*pTest).PreCom[i + 1], pretestlist[id - 1]);
      }
      if (testdeps[id - 1])
      {
	(*pTest).ComDep[i + 1] =
	  ut_alloc_1d_char (strlen (testdeps[id - 1]) + 1);
	strcpy ((*pTest).ComDep[i + 1], testdeps[id - 1]);
      }
    }
    ut_free_2d_char (testids, testqty);
  }

  ut_free_2d_char (testlist, qty);
  ut_free_2d_char (testdeps, qty);
  ut_free_2d_char (pretestlist, qty);
  ut_free_1d_char (pretest0);
  ut_free_1d_char (NNN);

  return;
}
