diff --git a/src/body.cpp b/src/body.cpp index 67eeeab..d594ba7 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -12,4 +12,4 @@ float DENSITY = 4E3; // Constructor definition Body::Body(const std::array* initial_position, const std::array* initial_velocity, double mass, float radius) - : Particle(initial_position, initial_velocity, mass), radius(radius == 0 ? pow(3 * mass / (4 * M_PI * DENSITY), 1/3) : radius) {}; + : Particle(initial_position, initial_velocity, mass), radius((radius == 0) ? pow(3 * mass / (4 * M_PI * DENSITY), 1./3.) : radius) {}; diff --git a/test/test_body.cpp b/test/test_body.cpp new file mode 100644 index 0000000..81e4a9b --- /dev/null +++ b/test/test_body.cpp @@ -0,0 +1,67 @@ +#define _USE_MATH_DEFINES + +#include +#include + +#include "../include/doctest.h" +#include "../include/body.h" + +TEST_CASE("Body Initialization - WITH Radius") { + + // Initialize particle specs + double mass = 1; + std::array initial_position1 = {0, 0, 0}; + std::array initial_velocity1 = {0, 0, 0}; + float radius = 1E5; + + // Define the particle + Body body1(&initial_position1, &initial_velocity1, mass, radius); + + CHECK(body1.position[0] == 0); + CHECK(body1.position[1] == 0); + CHECK(body1.position[2] == 0); + CHECK(body1.mass == 1); + CHECK(body1.radius == 1E5); +} + +TEST_CASE("Body Initialization - WITHOUT Radius") { + + // Initialize particle specs + double mass = 1; + std::array initial_position1 = {0, 0, 0}; + std::array initial_velocity1 = {0, 0, 0}; + + // Define the particle + Body body1(&initial_position1, &initial_velocity1, mass); + + CHECK(body1.position[0] == 0); + CHECK(body1.position[1] == 0); + CHECK(body1.position[2] == 0); + CHECK(body1.mass == 1); + CHECK((body1.radius - pow(3 * mass / (4 * M_PI * 4E3), 1./3.)) < 0.000001); +} + + +TEST_CASE("Body Update") { + + // Parameters for a timestep + double timestep = 1; + + // Initialize particle specs + double mass = 1; + float radius = 1E5; + std::array initial_position1 = {0, 0, 0}; + std::array initial_velocity1 = {0, 0, 0}; + + // Define the force + std::array force = {3, 0, 0}; + + // Define the particle + Body body1(&initial_position1, &initial_velocity1, mass, radius); + + // Update particle + body1.update(&force, timestep); + + // Check the update works + CHECK(body1.position[0] == 1.5); +} \ No newline at end of file