/** @file test_suite.cpp
 *
 *  @author Cory Alexander Balaton (coryab)
 *  @author Janita Ovidie Sandtrøen Willumsen (janitaws)
 *
 *  @version 1.0
 *
 *  @brief The test suite for the project
 *
 *  @bug No known bugs
 * */

#include <iomanip>
#include <sstream>
#include <string>

#include "PenningTrap.hpp"
#include "constants.hpp"
#include "utils.hpp"

/** @brief Test class for the Penning trap.
 * */
class PenningTrapTest
{
public:
    /** @brief Test that the external E field gives correct values.
     * */
    void test_external_E_field()
    {
        PenningTrap trap;

        // Vector containing inputs and expected results
        std::vector<std::pair<vec3, vec3>> tests;

        tests.push_back(std::make_pair(vec3{0., 0., 0.}, vec3{0., 0., 0.}));

        tests.push_back(
            std::make_pair(vec3{10., 0., 0.}, vec3{96.4852558, 0., 0.}));

        tests.push_back(
            std::make_pair(vec3{10., 0., 0.}, vec3{96.4852558, 0., 0.}));

        tests.push_back(
            std::make_pair(vec3{0., 10., 0.}, vec3{0., 96.4852558, 0.}));

        tests.push_back(
            std::make_pair(vec3{0., 0., 10.}, vec3{0., 0., -192.9705116}));

        vec3 result;
        vec3 v;
        std::stringstream msg;
        for (size_t i = 0; i < tests.size(); i++) {
            v = tests.at(i).first;
            result = trap.external_E_field(v);

            msg.str("");
            msg << "Testing the external E field at (" << std::setprecision(2)
                << v(0) << "," << v(1) << "," << v(2) << ").";

            ASSERT(close_to(result, tests.at(i).second), msg.str());
        }
    }

    /** @brief Test that the external B field gives correct values.
     * */
    void test_external_B_field()
    {
        // No point in testing at different points since it's not dependent
        // on position.
        PenningTrap trap;
        vec3 expected{0., 0., T};
        vec3 result = trap.external_B_field(vec3{0., 0., 0.});
        ASSERT(close_to(expected, result),
               "Testing the external B field at (0,0,0)");
    }

    /** @brief Test that the force between particles gives expected
     * results.
     * */
    void test_force_on_particle()
    {
        PenningTrap trap;
        vec3 v{0., 0., 0.};

        // Add particles to test
        trap.add_particle(Particle(vec3{0., 0., 0.}, v));
        trap.add_particle(Particle(vec3{1., 0., 0.}, v));
        trap.add_particle(Particle(vec3{0., 3., 4.}, v));

        // Test p0 and p1
        vec3 expected{-1., 0., 0.};
        vec3 result = trap.force_on_particle(0, 1);
        ASSERT(close_to(expected, result),
               "Testing the force on a particle at (0,0,0) from a "
               "particle at (1,0,0).");

        // Test p0 and p2
        expected = vec3{0, -.024, -.032};
        result = trap.force_on_particle(0, 2);
        ASSERT(close_to(expected, result),
               "Testing the force on a particle at (0,0,0) from a "
               "particle at (0,3,4).");
    }

    /** @brief Test that the total external force returns expected results
     * */
    void test_total_force_external()
    {
        PenningTrap trap;
        trap.add_particle(Particle(vec3{1., 2., 3.}, vec3{3., 4., 5.}));

        vec3 expected{395.58954878, -270.15871624, -57.89115348};
        vec3 result = trap.total_force_external(0);
        ASSERT(close_to(expected, result),
               "Testing the total external force on a particle at "
               "(1,2,3) with velocity (3,4,5)");
    }

    /** @brief Test that the total force of all particles on a single
     * particle returns expected results.
     * */
    void test_total_force_particles()
    {
        PenningTrap trap;
        trap.add_particle(Particle(vec3{0., 0., 0.}, vec3{0., 0., 0.}));

        vec3 expected{0., 0., 0.};
        vec3 result = trap.total_force_particles(0);
        ASSERT(close_to(expected, result),
               "Testing the total force of all particles on particle 0 "
               "with only a single particle");

        trap.add_particle(Particle(vec3{1., 0., 0.}, vec3{0., 0., 0.}));
        trap.add_particle(Particle(vec3{0., 1., 0.}, vec3{0., 0., 0.}));
        trap.add_particle(Particle(vec3{0., 0., 1.}, vec3{0., 0., 0.}));

        expected = vec3().fill(-138935.333);
        result = trap.total_force_particles(0);
        ASSERT(close_to(expected, result),
               "Testing the total force of all particles on particle 0 "
               "with 3 other particles.");
    }
};

int main()
{
    PenningTrapTest test;
    test.test_external_E_field();
    test.test_external_B_field();
    test.test_force_on_particle();
    test.test_total_force_external();
    test.test_total_force_particles();
    return 0;
}
