// src/testsuite/test-stepforward.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

/* This executable tests for workingness of the save preferences method,
 * and nothing else. */

#include "config.h"
#include "glibmm/ustring.h"
#include <sgpemv2/key_file.hh>
#include <iostream>



// from here and further until the bottom, all to throw away I suppose

int
main(int argc, char** argv)
{

  using namespace sgpem;
  using Glib::ustring;
  using std::cout;
  using std::endl;

  if ((argc < 4) || (argc % 2) != 0)
  {
    cout << "KeyFile class test program" << endl;
    cout << "Syntax: test-key_file filename key1 value1 [key2 value2...]" << endl;
    cout << "total number of parameters must be odd."
    << endl << endl;
    cout << "This test create a KeyFile object, fill it with all key/value pairs," << endl
    << "write it to the file \"filename\", read it into a new KeyFile object" << endl
    << "and compare all readed values against original values." << endl << endl
    << "If all key/values match the program print \"Test was successful\" and return 0." << endl
    << "If some key/values doesn't match the program print \"Test failed\" and return 1."
    << endl << endl;
    cout << "If there isn't any parameter or bad number of parameters return 2."
    << endl << endl;
    cout << "NOTE: 1) Test fails with invalid filenames or duplicated key in input."
    << endl;
    cout << "      2) File \"filename\" remains in current directory after test execution."
    << endl << endl;
    return 2;
  }

  KeyFile kfile;
  KeyFile kfile_bis;

  // fill KeyFile object
  for (int index = 2; index < argc; index += 2)
  {
    kfile.insert_key_value(Glib::ustring(argv[index]), Glib::ustring(argv[index+1]));
  }

  // write KeyFile object to file
  kfile.file_write(Glib::ustring(argv[1]));

  // read new KeyFile object from file
  kfile_bis.file_read(Glib::ustring(argv[1]));

  // compare writed vs. readed data
  bool ok = true;
  for (int index = 2; ok && index < argc; index += 2)
  {
    Glib::ustring val(argv[index+1]);
    const Glib::ustring* pt = kfile_bis.search_value(Glib::ustring(argv[index]));
    if (!pt || (*pt) != val)
    {
      ok = false;
    }
  }

  // output response...
  if (ok)
  {
    cout << "Test successful" << endl;
    return 0;
  }
  else
  {
    cout << "Test failed" << endl;
    return 1;
  }

  exit(0);
}
