/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */

#include <cxxtest/TestSuite.h>

#include "csvfilereader.hh"

class CSVFileReader_test: public CxxTest::TestSuite
{
private:
  std::string test_file_name ;

public:

  CSVFileReader_test(void)
  {
    test_file_name = "/tmp/CSVFileReader_test_file.csv" ;
  }


  ~CSVFileReader_test(void)
  {
    TestUtil::remove_file(test_file_name) ;
  }


  static CSVFileReader_test* createSuite(void)
  {
    return new CSVFileReader_test() ;
  }


  static void destroySuite(CSVFileReader_test *suite)
  {
    delete suite ;
  }


  void test_csvfilereader(void)
  {
    const unsigned int nlines = 6 ;

    std::ofstream output_file(test_file_name.c_str()) ;
    assert(output_file) ;
    for (unsigned int i = 0 ; i < nlines ; ++i)
      output_file << "  \n    c,string,42,3.1416\n   \n" ;
    output_file.close() ;

    CSVFileReader *csvfilereader ;
    TS_ASSERT_THROWS_NOTHING(
      csvfilereader = new CSVFileReader(test_file_name, ',')) ;

    for (unsigned int i = 0 ; i < nlines ; ++i)
      {
        TS_ASSERT(csvfilereader->next_line()) ;

        char c ;
        TS_ASSERT(csvfilereader->read_field(c)) ;
        TS_ASSERT_EQUALS(c, 'c') ;

        std::string s ;
        TS_ASSERT(csvfilereader->read_field(s)) ;
        TS_ASSERT_EQUALS(s, "string") ;

        uint64_t ui ;
        TS_ASSERT(csvfilereader->read_field(ui)) ;
        TS_ASSERT_EQUALS(ui, 42u) ;

        double f ;
        TS_ASSERT(csvfilereader->read_field(f)) ;
        TS_ASSERT_EQUALS(f, 3.1416) ;

        TS_ASSERT(! csvfilereader->read_field(f)) ;
        TS_ASSERT(! csvfilereader->eof()) ;
      }

    TS_ASSERT(! csvfilereader->next_line()) ;
    TS_ASSERT(csvfilereader->eof()) ;

    delete csvfilereader ;
  }


  void test_request(void)
  {
    const unsigned int nlines = 10 ;

    std::ofstream output_file(test_file_name.c_str()) ;
    assert(output_file) ;
    for (unsigned int i = 0 ; i < nlines ; ++i)
      output_file
          << "aa:bb:cc:dd:ee:ff;1265120910725;0;0;0;0;"
          << "11:22:33:44:55:01;-58;11:22:33:44:55:03;-50;"
          << "11:22:33:44:55:02;-42;11:22:33:44:55:01;-55;"
          << "11:22:33:44:55:02;-37\n" ;
    output_file.close() ;

    CSVFileReader *csvfilereader ;
    TS_ASSERT_THROWS_NOTHING(
      csvfilereader = new CSVFileReader(test_file_name)) ;

    for (unsigned int i = 0 ; i < nlines ; ++i)
      {
        TS_ASSERT(csvfilereader->next_line()) ;

        std::string mac ;
        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "aa:bb:cc:dd:ee:ff") ;

        uint64_t timestamp ;
        TS_ASSERT(csvfilereader->read_field(timestamp)) ;
        TS_ASSERT_EQUALS(timestamp, 1265120910725ull) ;

        float position ;
        TS_ASSERT(csvfilereader->read_field(position)) ;
        TS_ASSERT_EQUALS(position, 0) ;

        TS_ASSERT(csvfilereader->read_field(position)) ;
        TS_ASSERT_EQUALS(position, 0) ;

        TS_ASSERT(csvfilereader->read_field(position)) ;
        TS_ASSERT_EQUALS(position, 0) ;

        int direction ;
        TS_ASSERT(csvfilereader->read_field(direction)) ;
        TS_ASSERT_EQUALS(direction, 0) ;

        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "11:22:33:44:55:01") ;

        int ss ;
        TS_ASSERT(csvfilereader->read_field(ss)) ;
        TS_ASSERT_EQUALS(ss, -58) ;

        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "11:22:33:44:55:03") ;

        TS_ASSERT(csvfilereader->read_field(ss)) ;
        TS_ASSERT_EQUALS(ss, -50) ;

        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "11:22:33:44:55:02") ;

        TS_ASSERT(csvfilereader->read_field(ss)) ;
        TS_ASSERT_EQUALS(ss, -42) ;

        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "11:22:33:44:55:01") ;

        TS_ASSERT(csvfilereader->read_field(ss)) ;
        TS_ASSERT_EQUALS(ss, -55) ;

        TS_ASSERT(csvfilereader->read_field(mac)) ;
        TS_ASSERT_EQUALS(mac, "11:22:33:44:55:02") ;

        TS_ASSERT(csvfilereader->read_field(ss)) ;
        TS_ASSERT_EQUALS(ss, -37) ;

        TS_ASSERT(! csvfilereader->read_field(mac)) ;
        TS_ASSERT(! csvfilereader->eof()) ;
      }

    TS_ASSERT(! csvfilereader->next_line()) ;
    TS_ASSERT(csvfilereader->eof()) ;

    delete csvfilereader ;
  }

} ;
