/*
 * 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 "measurement.hh"

class Measurement_test: public CxxTest::TestSuite
{
public:

  void test_constructors(void)
  {
    // Default constructor
    Measurement m00 ;
    std::map<pkt_id_t, ss_t> vi1 ;
    Measurement m01(nullptr) ;
    m01.add_ss_list(vi1) ;
    TS_ASSERT_EQUALS(m00, m01) ;

    // Copy constructor
    CapturePoint ap1 ;
    Measurement m1(&ap1) ;
    Measurement m2(m1) ;
    TS_ASSERT_EQUALS(m1, m2) ;
  }


  void test_accessors(void)
  {
    // Simple read accessors
    CapturePoint ap1 ;
    std::map<pkt_id_t, ss_t> vi1 ;
    Measurement m1(&ap1) ;
    m1.add_ss_list(vi1) ;
    TS_ASSERT_EQUALS(m1.get_cp(), &ap1) ;
    TS_ASSERT_EQUALS(m1.get_nb_ss(), 0) ;
    TS_ASSERT_EQUALS(m1.get_average_dbm(), 0) ;

    // Write & read accessors
    CapturePoint ap2 ;
    m1.set_cp(&ap2) ;
    TS_ASSERT_EQUALS(m1.get_cp(), &ap2) ;

    m1.add_ss(1, -33) ;
    m1.add_ss(2, -78) ;
    m1.add_ss(3, -21) ;
    TS_ASSERT_EQUALS(m1.get_nb_ss(), 3) ;
    TS_ASSERT_EQUALS(m1.get_ss(1), -33) ;
    TS_ASSERT_EQUALS(m1.get_ss(2), -78) ;
    TS_ASSERT_EQUALS(m1.get_ss(3), -21) ;

    /* *** Average computation ***
     *
     * We need to convert all dBm values into mW, compute the average
     * and convert mW back to dBm.
     * (1) P[mW] = 10^(P[dBm] / 10)
     * (2) P[dBm] = 10 log10(P[mW])
     *
     * So for the dBm values -33, -78, -21, we have:
     * -33 dBm = 10^(-33/10) mW = 0.0005012 mW
     * -78 dBm = 10^(-78/10) mW = 0.00000001585 mW
     * -21 dBm = 10^(-21/10) mW = 0.0079433 mW
     * And:
     * ( 10^(-33/10) + 10^(-78/10) + 10^(-21/10) ) / 3 = 0.0028148 mW
     * Back to dBm:
     * 0.0028148 mW = -25.505481 dBm
     */
    TS_ASSERT_DELTA(m1.get_average_dbm(), -25.505481, 0.0001) ;

    Measurement m2(&ap2) ;
    std::map<pkt_id_t, ss_t> vi2 ;
    vi2[3] = -54 ;
    vi2[4] = -1 ;
    m2.add_ss_list(vi2) ;
    TS_ASSERT_EQUALS(m2.get_nb_ss(), vi2.size()) ;
    TS_ASSERT_EQUALS(m2.get_ss(3), -54) ;
    TS_ASSERT_EQUALS(m2.get_ss(4), -1) ;
    TS_ASSERT_DELTA(m2.get_average_dbm(), -4.0102782, 0.0001) ;

    m1.merge(m2) ;
    TS_ASSERT_EQUALS(m1.get_nb_ss(), 4) ;
    TS_ASSERT_EQUALS(m1.get_ss(1), -33) ;
    TS_ASSERT_EQUALS(m1.get_ss(2), -78) ;
    TS_ASSERT_EQUALS(m1.get_ss(3), -54) ;
    TS_ASSERT_EQUALS(m1.get_ss(4), -1) ;

    m1.clear() ;
    Measurement m3 ;
    TS_ASSERT_EQUALS(m1, m3) ;
  }


  void test_ss_square_distance(void)
  {
    float ss = -78 ;

    Measurement measurement1, measurement2 ;
    measurement1.add_ss(1, ss) ;
    measurement2.add_ss(1, ss) ;
    TS_ASSERT_EQUALS(0, measurement1.ss_square_distance(measurement2)) ;
    TS_ASSERT_EQUALS(0, measurement1.ss_square_distance(ss)) ;

    /* Distance computation:
     * (-42 - (-78))^2 == 1296
     */
    ss = -42 ;
    measurement2.clear() ;
    measurement2.add_ss(1, ss) ;
    TS_ASSERT_EQUALS(1296, measurement1.ss_square_distance(measurement2)) ;
    TS_ASSERT_EQUALS(1296, measurement1.ss_square_distance(ss)) ;
  }


  void test_operators(void)
  {
    // ==
    CapturePoint ap1 ;
    Measurement m1(&ap1) ;
    Measurement m2(&ap1) ;
    TS_ASSERT_EQUALS(m1, m2) ;

    // !=
    CapturePoint ap2 ;
    Measurement m3(&ap2) ;
    TS_ASSERT(m1 != m3) ;

    // =
    m2 = m3 ;
    TS_ASSERT_EQUALS(m2, m3) ;

    // bool
    TS_ASSERT(m1) ;
    Measurement m4 ;
    TS_ASSERT(! m4) ;
  }

} ;
