/*
 * 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 "request.hh"
#include "mobile.hh"
#include "timestamp.hh"

class Request_test: public CxxTest::TestSuite
{
public:

  void test_constructors(void)
  {
    // We can't test the default constructor with two different objects
    // like we usually do because the reception time is initialised by
    // the Request constructor.

    // Copy constructor
    std::unordered_map<std::string, Measurement> measurements ;
    Measurement meas1 ;
    measurements["AA:BB:CC:DD:EE:FF"] = meas1 ;
    Timestamp current_time ;
    current_time.now() ;
    Mobile mob1 ;
    Request r3(&mob1, current_time, measurements) ;
    Request r4(r3) ;
    TS_ASSERT_EQUALS(r3, r4) ;
  }


  void test_accessors(void)
  {
    // Simple read accessors
    Timestamp current_time ;
    current_time.now() ;
    CapturePoint ap1(Point3D(1,2,3), "192.168.0.1", "AA:BB:CC:DD:EE:FF") ;
    Measurement meas1(&ap1) ;
    std::unordered_map<std::string, Measurement> measurements ;
    measurements["AA:BB:CC:DD:EE:FF"] = meas1 ;
    Mobile mob1 ;
    Request r1(&mob1, current_time, measurements) ;
    TS_ASSERT_EQUALS(r1.get_mobile(), &mob1) ;
    TS_ASSERT_EQUALS(r1.get_time_sent(), current_time) ;
    TS_ASSERT_EQUALS(r1.get_measurements(), measurements) ;

    // Write & read accessors
    r1.set_type(OWL_REQUEST_GENERATED) ;
    TS_ASSERT_EQUALS(r1.get_type(), OWL_REQUEST_GENERATED) ;

    r1.set_nb_packets(42) ;
    TS_ASSERT_EQUALS(r1.get_nb_packets(), 42) ;

    Mobile mob2 ;
    r1.set_mobile(&mob2) ;
    TS_ASSERT_EQUALS(r1.get_mobile(), &mob2) ;

    current_time = static_cast<uint64_t>(current_time) + 10 ;
    r1.set_time_sent(current_time) ;
    TS_ASSERT_EQUALS(r1.get_time_sent(), current_time) ;

    current_time.now() ;
    r1.received_now() ;
    TS_ASSERT_DELTA(static_cast<uint64_t>(r1.get_time_received()),
                    static_cast<uint64_t>(current_time),
                    100) ;

    CapturePoint ap2(Point3D(3,2,1), "192.168.0.2", "AA:BB:CC:DD:EE:02") ;
    Measurement meas2(&ap2) ;
    measurements["AA:BB:CC:DD:EE:02"] = meas2 ;
    r1.set_measurements(measurements) ;
    TS_ASSERT_EQUALS(r1.get_measurements(), measurements) ;

    // clear()
    r1.clear() ;
    Request r2 ;
    r2.clear() ;
    TS_ASSERT_EQUALS(r1, r2) ;
  }


  /*
   * Operations:
   * We don't test similarity() because it's just a wrapper around
   * PosUtil functions.
   */


  void test_operators(void)
  {
    // ==
    Timestamp current_time ;
    current_time.now() ;
    std::unordered_map<std::string, Measurement> measurements ;
    Measurement meas1 ;
    measurements["AA:BB:CC:DD:EE:00"] = meas1 ;
    Request r1(current_time, measurements) ;
    Request r2(r1) ;
    TS_ASSERT_EQUALS(r1, r2) ;

    Mobile mob1 ;
    Request r3(&mob1, current_time, measurements) ;
    Request r4(r3) ;
    TS_ASSERT_EQUALS(r3, r4) ;

    // !=
    Measurement meas2 ;
    measurements["AA:BB:CC:DD:EE:01"] = meas2 ;
    Request r5(&mob1, current_time, measurements) ;
    TS_ASSERT(r4 != r5) ;

    // =
    r2 = r3 ;
    TS_ASSERT_EQUALS(r2, r3) ;

    // bool
    TS_ASSERT(r5) ;
    Request r6 ;
    TS_ASSERT(! r6) ;
  }

} ;
