/* Siconos is a program dedicated to modeling, simulation and control
 * of non smooth dynamical systems.
 *
 * Copyright 2021 INRIA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>           // for printf
#include <string.h>          // for strcmp
#include "lcp_cst.h"         // for SICONOS_LCP_AVI_CAOFERRIS, SICONOS_LCP_BARD
#include "lcp_test_utils.h"  // for lcp_test_function
#include "test_utils.h"      // for TestCase, build_test_collection_generic
#include "SiconosConfig.h"   // for HAVE_GAMS_C_API // IWYU pragma: keep

// Set TEST_LCP_COLLECTION_XX value (XX = 1,2,3,4 or 5, depending on cmake test setup).
#define @PROBLEM_COLLECTION@ 1

int main(void)
{
  // Remind that data_collection depends on current PROBLEM_COLLECTION value.
  const char ** _data_collection = data_collection();
  int n_data = 0;
  while (strcmp(_data_collection[n_data], "---") !=0)
    n_data++;

  // Set solvers list for each collection of data.
#if defined(TEST_LCP_COLLECTION_1) || defined(TEST_LCP_COLLECTION_2)
  // same solvers for data collections 1 and 2
  int solvers[] = {SICONOS_LCP_LEMKE, SICONOS_LCP_CPG, SICONOS_LCP_PGS,
                   SICONOS_LCP_RPGS, SICONOS_LCP_LATIN, SICONOS_LCP_LATIN_W,
                   SICONOS_LCP_AVI_CAOFERRIS, SICONOS_LCP_NEWTONMIN,
                   SICONOS_LCP_NEWTON_FB_FBLSA, SICONOS_LCP_NEWTON_MIN_FBLSA,
                   SICONOS_LCP_BARD, SICONOS_LCP_MURTY, SICONOS_LCP_PIVOT,
                   SICONOS_LCP_PIVOT_LUMOD, SICONOS_LCP_PATHSEARCH};
#elif TEST_LCP_COLLECTION_3
  int solvers[] = {SICONOS_LCP_NSGS_SBM};
#elif TEST_LCP_COLLECTION_4
  int solvers[] = {SICONOS_LCP_ENUM, SICONOS_LCP_LEMKE};
#elif TEST_LCP_COLLECTION_5
  int solvers[] =
    {SICONOS_LCP_PIVOT
#ifdef HAVE_PATHFERRIS
     , SICONOS_LCP_PATH
#endif
#ifdef HAVE_GAMS_C_API
     , SICONOS_LCP_GAMS
#endif
    };
#endif

  int n_solvers = (int)(sizeof(solvers) / sizeof(solvers[0]));

  printf("Test %i data files with %i solvers.\n", n_data, n_solvers);

  TestCase * collection = build_test_collection_generic(n_data, _data_collection, n_solvers, solvers);

#ifdef TEST_LCP_COLLECTION_1
  // some solvers are expected to fail ...
  collection[28].will_fail = 1;  // Latin, lcp_exp_murty.dat
  collection[29].will_fail = 1;  // Latin, lcp_exp_murty2.dat
  collection[34].will_fail = 1;  // Latin_w, lcp_exp_murty.dat
  collection[35].will_fail = 1;  // Latin_w, lcp_exp_murty2.dat
  collection[78].will_fail = 1;  // Pivot based method with BLU updates using LUMOD, on ./data/lcp_mmc.dat
  // some others are unstable
  collection[10].will_fail = 2;
  collection[27].will_fail = 2;
  collection[33].will_fail = 2;
  collection[40].will_fail = 2;
  collection[42].will_fail = 2;
#elif TEST_LCP_COLLECTION_2
  collection[3].will_fail = 1;
  collection[7].will_fail = 1;
  collection[9].will_fail = 1;
  collection[11].will_fail = 1;
  collection[12].will_fail = 1;
  collection[14].will_fail = 1;
  collection[16].will_fail = 1;
  collection[17].will_fail = 1;
  collection[18].will_fail = 1;
  collection[19].will_fail = 1;
  collection[23].will_fail = 1;
  collection[24].will_fail = 1;
  collection[28].will_fail = 1;
  collection[30].will_fail = 1;
  collection[31].will_fail = 1;
  collection[32].will_fail = 1;
  collection[33].will_fail = 1;
  collection[35].will_fail = 1;
  collection[37].will_fail = 1;
  collection[38].will_fail = 1;
  collection[39].will_fail = 1;
  collection[40].will_fail = 1;
  collection[45].will_fail = 1;
  collection[49].will_fail = 1;
  collection[50].will_fail = 1;
  collection[51].will_fail = 1;
  collection[53].will_fail = 1;
  collection[54].will_fail = 1;
  collection[55].will_fail = 1;
  collection[57].will_fail = 1;
  collection[70].will_fail = 1;
  collection[72].will_fail = 1;
  collection[73].will_fail = 1;
  collection[74].will_fail = 1;
  collection[75].will_fail = 1;
  collection[79].will_fail = 1;
  collection[80].will_fail = 1;
  collection[81].will_fail = 1;
  collection[82].will_fail = 1;
  collection[87].will_fail = 1;
  collection[94].will_fail = 1;
  collection[101].will_fail = 1;
#elif TEST_LCP_COLLECTION_4
  collection[1].will_fail = 1;
  collection[4].will_fail = 1;
#elif TEST_LCP_COLLECTION_5
  collection[0].will_fail = 1;
#endif

  
  int number_of_tests = n_data * n_solvers;
  int out = run_test_collection(collection, number_of_tests, lcp_test_function);
  free_test_collection(collection, number_of_tests);

  
  return out;
}
