111 lines
3.2 KiB
C++
111 lines
3.2 KiB
C++
#include <gtest/gtest.h>
|
|
#include "body.hpp"
|
|
#include <cmath>
|
|
#include <memory>
|
|
|
|
class BodyTest : public testing::Test {
|
|
protected:
|
|
BodyTest() {
|
|
test_body = std::make_unique<Body>(
|
|
Position{Decimal(0), Decimal(0), Decimal(0)},
|
|
Velocity{Decimal(0), Decimal(0), Decimal(0)},
|
|
Mass(1.0),
|
|
"test_body"
|
|
);
|
|
}
|
|
|
|
std::unique_ptr<Body> test_body;
|
|
};
|
|
|
|
TEST_F(BodyTest, AddAcceleration) {
|
|
Acceleration new_A{Decimal(1.0), Decimal(2.0), Decimal(3.0)};
|
|
test_body->addAcceleration(new_A);
|
|
|
|
const auto& A = test_body->getAcceleration();
|
|
EXPECT_DOUBLE_EQ(A[0], 1.0);
|
|
EXPECT_DOUBLE_EQ(A[1], 2.0);
|
|
EXPECT_DOUBLE_EQ(A[2], 3.0);
|
|
|
|
// Add another acceleration
|
|
Acceleration another_A{Decimal(2.0), Decimal(3.0), Decimal(4.0)};
|
|
test_body->addAcceleration(another_A);
|
|
|
|
EXPECT_DOUBLE_EQ(A[0], 3.0);
|
|
EXPECT_DOUBLE_EQ(A[1], 5.0);
|
|
EXPECT_DOUBLE_EQ(A[2], 7.0);
|
|
}
|
|
|
|
TEST_F(BodyTest, Step) {
|
|
// Set initial velocity and acceleration
|
|
test_body->setAcceleration(Acceleration{Decimal(1.0), Decimal(2.0), Decimal(3.0)});
|
|
|
|
// Take a step
|
|
test_body->step(1.0);
|
|
|
|
// Check new position
|
|
const auto& X = test_body->getPosition();
|
|
EXPECT_DOUBLE_EQ(X[0], 0.0);
|
|
EXPECT_DOUBLE_EQ(X[1], 0.0);
|
|
EXPECT_DOUBLE_EQ(X[2], 0.0);
|
|
|
|
// Check new velocity
|
|
const auto& V = test_body->getVelocity();
|
|
EXPECT_DOUBLE_EQ(V[0], 1.0);
|
|
EXPECT_DOUBLE_EQ(V[1], 2.0);
|
|
EXPECT_DOUBLE_EQ(V[2], 3.0);
|
|
|
|
// Check acceleration is reset
|
|
const auto& A = test_body->getAcceleration();
|
|
EXPECT_DOUBLE_EQ(A[0], 0.0);
|
|
EXPECT_DOUBLE_EQ(A[1], 0.0);
|
|
EXPECT_DOUBLE_EQ(A[2], 0.0);
|
|
|
|
}
|
|
|
|
TEST_F(BodyTest, Energy) {
|
|
// Set position and velocity through save/load
|
|
auto state = std::make_tuple(
|
|
Position{Decimal(1.0), Decimal(0.0), Decimal(0.0)},
|
|
Velocity{Decimal(1.0), Decimal(0.0), Decimal(0.0)},
|
|
Mass(1.0)
|
|
);
|
|
test_body = std::make_unique<Body>(Body::load(state));
|
|
|
|
// Calculate expected values
|
|
Decimal expected_ke = Decimal(0.5); // 0.5 * m * v^2
|
|
Decimal expected_pe = Decimal(-1.0); // -m/r
|
|
Decimal expected_total = expected_ke + expected_pe;
|
|
|
|
EXPECT_DOUBLE_EQ(test_body->ke(), expected_ke);
|
|
EXPECT_DOUBLE_EQ(test_body->pe(), expected_pe);
|
|
EXPECT_DOUBLE_EQ(test_body->E(), expected_total);
|
|
}
|
|
|
|
TEST_F(BodyTest, SaveAndLoad) {
|
|
// Set some values through save/load
|
|
auto state = std::make_tuple(
|
|
Position{Decimal(1.0), Decimal(2.0), Decimal(3.0)},
|
|
Velocity{Decimal(4.0), Decimal(5.0), Decimal(6.0)},
|
|
Mass(7.0)
|
|
);
|
|
test_body = std::make_unique<Body>(Body::load(state));
|
|
|
|
// Save state
|
|
auto saved_state = test_body->save();
|
|
|
|
// Create new body from saved state
|
|
Body loaded_body = Body::load(saved_state);
|
|
|
|
// Verify loaded values
|
|
const auto& X = loaded_body.getPosition();
|
|
const auto& V = loaded_body.getVelocity();
|
|
const auto& m = loaded_body.getMass();
|
|
|
|
EXPECT_DOUBLE_EQ(X[0], 1.0);
|
|
EXPECT_DOUBLE_EQ(X[1], 2.0);
|
|
EXPECT_DOUBLE_EQ(X[2], 3.0);
|
|
EXPECT_DOUBLE_EQ(V[0], 4.0);
|
|
EXPECT_DOUBLE_EQ(V[1], 5.0);
|
|
EXPECT_DOUBLE_EQ(V[2], 6.0);
|
|
EXPECT_DOUBLE_EQ(m, 7.0);
|
|
}
|