Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c0c273283051
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b9665a83afd3
Choose a head ref
  • 3 commits
  • 7 files changed
  • 1 contributor

Commits on Mar 11, 2017

  1. Reinstate interface test.

    pleroy committed Mar 11, 2017
    Copy the full SHA
    ed077b3 View commit details

Commits on Mar 12, 2017

  1. Use auxiliary files.

    pleroy committed Mar 12, 2017
    Copy the full SHA
    835eb05 View commit details
  2. Merge pull request #1247 from pleroy/InterfaceTest

    Reinstate the interface test.
    pleroy authored Mar 12, 2017
    Copy the full SHA
    b9665a8 View commit details
159 changes: 99 additions & 60 deletions ksp_plugin_test/interface_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

#include "ksp_plugin/interface.hpp"

#if THE_INTERFACE_TESTS_WORK

#include <limits>
#include <string>

@@ -15,6 +13,7 @@
#include "gtest/gtest.h"
#include "journal/recorder.hpp"
#include "ksp_plugin/frames.hpp"
#include "ksp_plugin/identification.hpp"
#include "ksp_plugin_test/mock_flight_plan.hpp"
#include "ksp_plugin_test/mock_manœuvre.hpp"
#include "ksp_plugin_test/mock_plugin.hpp"
@@ -51,6 +50,7 @@ using ksp_plugin::Navball;
using ksp_plugin::Navigation;
using ksp_plugin::NavigationManœuvre;
using ksp_plugin::Part;
using ksp_plugin::PartId;
using ksp_plugin::World;
using ksp_plugin::WorldSun;
using physics::CoordinateFrameField;
@@ -96,33 +96,16 @@ using ::testing::SetArgPointee;
using ::testing::StrictMock;
using ::testing::_;

char const serialized_boring_plugin[] =
"\x12\xD2\x1\b\0\x12\xCD\x1\n\xF\n\r\b\x83\xF0\x1\x11\0\0\0\0\0\0\xF0?\x12"
"\xB9\x1\n\xAE\x1\n\x12\n\xE\x12\f\b\x80\b\x11\0\0\0\0\0\0\0\0\x12\0\x12"
"\x97\x1\n\xE\x12\f\b\x80\b\x11\0\0\0\0\0\0\0\0\x12\x84\x1\n>\x12<\n:\n-\n"
"\r\x12\v\b\x1\x11\0\0\0\0\0\0\0\0\x12\r\x12\v\b\x1\x11\0\0\0\0\0\0\0\0\x1A"
"\r\x12\v\b\x1\x11\0\0\0\0\0\0\0\0\"\t\r\xAF\x1F\xB1y\x10\x3\x18\x1\x12"
"B\n@\n3\n\xF\x12\r\b\x81\xF8\x1\x11\0\0\0\0\0\0\0\0\x12\xF\x12\r\b\x81\xF8"
"\x1\x11\0\0\0\0\0\0\0\0\x1A\xF\x12\r\b\x81\xF8\x1\x11\0\0\0\0\0\0\0\0\"\t"
"\r\xAF\x1F\xB1y\x10\x3\x18\x1\x12\x6\n\x4\b\0\x10\0\x1A\x2\n\0\"\x10\b\x80"
"\x80\x80\x80\x80\x1\x11\0\0\0\0\0\0\0\0*\xE\x12\f\b\x80\b\x11\0\0\0\0\0\0"
"\0\0""0\0";

char const hexadecimal_boring_plugin[] =
"12D201080012CD010A0F0A0D0883F00111000000000000F03F12B9010AAE010A120A0E120C"
"08800811000000000000000012001297010A0E120C0880081100000000000000001284010A"
"3E123C0A3A0A2D0A0D120B0801110000000000000000120D120B0801110000000000000000"
"1A0D120B080111000000000000000022090DAF1FB1791003180112420A400A330A0F120D08"
"81F801110000000000000000120F120D0881F8011100000000000000001A0F120D0881F801"
"11000000000000000022090DAF1FB1791003180112060A04080010001A020A002210088080"
"808080011100000000000000002A0E120C0880081100000000000000003000";

char const vessel_guid[] = "NCC-1701-D";
char const part_name[] = "Picard's chair";
char const vessel_guid[] = "123-456";
char const vessel_name[] = "NCC-1701-D";

Index const celestial_index = 1;
Index const parent_index = 2;
Index const unused = 666;

PartId const part_id = 42;

double const planetarium_rotation = 10;
double const time = 11;

@@ -159,9 +142,49 @@ class InterfaceTest : public testing::Test {
journal::Recorder::Deactivate();
}

InterfaceTest() : plugin_(make_not_null_unique<StrictMock<MockPlugin>>()) {}
InterfaceTest()
: plugin_(make_not_null_unique<StrictMock<MockPlugin>>()),
hexadecimal_simple_plugin_(
ReadFromHexadecimalFile("simple_plugin.proto.hex")),
serialized_simple_plugin_(
ReadFromBinaryFile("simple_plugin.proto.bin")) {}

static std::string ReadFromBinaryFile(std::string const& filename) {
std::fstream file =
std::fstream(SOLUTION_DIR / "ksp_plugin_test" / filename,
std::ios::in | std::ios::binary);
CHECK(file.good());
std::string binary;
while (!file.eof()) {
char c;
file.get(c);
binary.append(1, c);
}
file.close();
return binary;
}

static std::string ReadFromHexadecimalFile(std::string const& filename) {
std::fstream file =
std::fstream(SOLUTION_DIR / "ksp_plugin_test" / filename);
CHECK(file.good());
std::string hex;
while (!file.eof()) {
std::string line;
std::getline(file, line);
for (auto const c : line) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
hex.append(1, c);
}
}
}
file.close();
return hex;
}

not_null<std::unique_ptr<StrictMock<MockPlugin>>> plugin_;
std::string const hexadecimal_simple_plugin_;
std::string const serialized_simple_plugin_;
Instant const t0_;
static journal::Recorder* recorder_;
};
@@ -183,12 +206,20 @@ TEST_F(InterfaceDeathTest, Errors) {
principia__UpdateCelestialHierarchy(plugin, celestial_index, parent_index);
}, "plugin.*non NULL");
EXPECT_DEATH({
principia__InsertOrKeepVessel(plugin, vessel_guid, parent_index);
}, "plugin.*non NULL");
bool inserted;
principia__InsertOrKeepVessel(plugin,
vessel_guid,
vessel_name,
parent_index,
/*loaded=*/false,
&inserted);
}, "plugin.*non NULL");
EXPECT_DEATH({
principia__SetVesselStateOffset(plugin,
vessel_guid,
parent_relative_degrees_of_freedom);
principia__InsertUnloadedPart(plugin,
part_id,
part_name,
vessel_guid,
parent_relative_degrees_of_freedom);
}, "plugin.*non NULL");
EXPECT_DEATH({
principia__VesselFromParent(plugin, vessel_guid);
@@ -348,19 +379,31 @@ TEST_F(InterfaceTest, EndInitialization) {
}

TEST_F(InterfaceTest, InsertOrKeepVessel) {
bool inserted;
EXPECT_CALL(*plugin_,
InsertOrKeepVessel(vessel_guid, parent_index));
InsertOrKeepVessel(vessel_guid,
vessel_name,
parent_index,
/*loaded=*/false,
Ref(inserted)));
EXPECT_CALL(*plugin_, HasVessel(vessel_guid))
.WillOnce(Return(false))
.WillOnce(Return(true));
EXPECT_FALSE(plugin_->HasVessel(vessel_guid));
principia__InsertOrKeepVessel(plugin_.get(), vessel_guid, parent_index);
principia__InsertOrKeepVessel(plugin_.get(),
vessel_guid,
vessel_name,
parent_index,
/*loaded=*/false,
&inserted);
EXPECT_TRUE(plugin_->HasVessel(vessel_guid));
}

TEST_F(InterfaceTest, SetVesselStateOffset) {
TEST_F(InterfaceTest, InsertUnloadedPart) {
EXPECT_CALL(*plugin_,
SetVesselStateOffset(
InsertUnloadedPart(
part_id,
part_name,
vessel_guid,
RelativeDegreesOfFreedom<AliceSun>(
Displacement<AliceSun>(
@@ -371,9 +414,11 @@ TEST_F(InterfaceTest, SetVesselStateOffset) {
{parent_velocity.x * SIUnit<Speed>(),
parent_velocity.y * SIUnit<Speed>(),
parent_velocity.z * SIUnit<Speed>()}))));
principia__SetVesselStateOffset(plugin_.get(),
vessel_guid,
parent_relative_degrees_of_freedom);
principia__InsertUnloadedPart(plugin_.get(),
part_id,
part_name,
vessel_guid,
parent_relative_degrees_of_freedom);
}

TEST_F(InterfaceTest, AdvanceTime) {
@@ -731,24 +776,6 @@ TEST_F(InterfaceTest, CurrentTime) {
EXPECT_THAT(t0_ + current_time * Second, Eq(mjd0));
}

TEST_F(InterfaceTest, SerializePlugin) {
PullSerializer* serializer = nullptr;
std::string const message_bytes =
std::string(serialized_boring_plugin,
(sizeof(serialized_boring_plugin) - 1) / sizeof(char));
principia::serialization::Plugin message;
message.ParseFromString(message_bytes);

EXPECT_CALL(*plugin_, WriteToMessage(_)).WillOnce(SetArgPointee<0>(message));
char const* serialization =
principia__SerializePlugin(plugin_.get(), &serializer);
EXPECT_STREQ(hexadecimal_boring_plugin, serialization);
EXPECT_EQ(nullptr, principia__SerializePlugin(plugin_.get(), &serializer));
principia__DeleteString(&serialization);
EXPECT_THAT(serialization, IsNull());
}


TEST_F(InterfaceTest, Apocalypse) {
char const* details;
EXPECT_CALL(*plugin_, HasEncounteredApocalypse(_)).WillOnce(Return(false));
@@ -763,20 +790,33 @@ TEST_F(InterfaceTest, Apocalypse) {
EXPECT_THAT(details, IsNull());
}

TEST_F(InterfaceTest, SerializePlugin) {
PullSerializer* serializer = nullptr;
principia::serialization::Plugin message;
message.ParseFromString(serialized_simple_plugin_);

EXPECT_CALL(*plugin_, WriteToMessage(_)).WillOnce(SetArgPointee<0>(message));
char const* serialization =
principia__SerializePlugin(plugin_.get(), &serializer);
EXPECT_STREQ(hexadecimal_simple_plugin_.c_str(), serialization);
EXPECT_EQ(nullptr, principia__SerializePlugin(plugin_.get(), &serializer));
principia__DeleteString(&serialization);
EXPECT_THAT(serialization, IsNull());
}

TEST_F(InterfaceTest, DeserializePlugin) {
PushDeserializer* deserializer = nullptr;
Plugin const* plugin = nullptr;
principia__DeserializePlugin(
hexadecimal_boring_plugin,
(sizeof(hexadecimal_boring_plugin) - 1) / sizeof(char),
hexadecimal_simple_plugin_.c_str(),
hexadecimal_simple_plugin_.size(),
&deserializer,
&plugin);
principia__DeserializePlugin(hexadecimal_boring_plugin,
principia__DeserializePlugin(hexadecimal_simple_plugin_.c_str(),
0,
&deserializer,
&plugin);
EXPECT_THAT(plugin, NotNull());
EXPECT_EQ(Instant(), plugin->CurrentTime());
principia__DeletePlugin(&plugin);
}

@@ -1032,4 +1072,3 @@ TEST_F(InterfaceTest, FlightPlan) {

} // namespace interface
} // namespace principia
#endif
4 changes: 4 additions & 0 deletions ksp_plugin_test/ksp_plugin_test.vcxproj
Original file line number Diff line number Diff line change
@@ -67,6 +67,10 @@
<ItemGroup>
<ClInclude Include="mock_vessel.hpp" />
</ItemGroup>
<ItemGroup>
<None Include="simple_plugin.proto.bin" />
<None Include="simple_plugin.proto.hex" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{A942ADF0-62F4-435C-85B2-934D5B666DB8}</ProjectGuid>
<RootNamespace>ksp_plugin_test</RootNamespace>
8 changes: 8 additions & 0 deletions ksp_plugin_test/ksp_plugin_test.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -112,4 +112,12 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="simple_plugin.proto.bin">
<Filter>Resource Files</Filter>
</None>
<None Include="simple_plugin.proto.hex">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
</Project>
14 changes: 10 additions & 4 deletions ksp_plugin_test/mock_plugin.hpp
Original file line number Diff line number Diff line change
@@ -41,11 +41,17 @@ class MockPlugin : public Plugin {
void(Index celestial_index,
Index parent_index));

MOCK_METHOD2(InsertOrKeepVessel,
bool(GUID const& vessel_guid, Index parent_index));

MOCK_METHOD2(SetVesselStateOffset,
MOCK_METHOD5(InsertOrKeepVessel,
void(GUID const& vessel_guid,
std::string const& vessel_name,
Index parent_index,
bool loaded,
bool& inserted));

MOCK_METHOD4(InsertUnloadedPart,
void(PartId part_id,
std::string const& name,
GUID const& vessel_guid,
RelativeDegreesOfFreedom<AliceSun> const& from_parent));

MOCK_METHOD2(AdvanceTime,
47 changes: 43 additions & 4 deletions ksp_plugin_test/plugin_test.cpp
Original file line number Diff line number Diff line change
@@ -243,10 +243,11 @@ class PluginTest : public testing::Test {
valid_ephemeris_message_.mutable_fixed_step_parameters());
using ODE = Ephemeris<Barycentric>::NewtonianMotionEquation;
ODE::SystemState initial_state;
McLachlanAtela1992Order5Optimal<Position<Barycentric>>().NewInstance(
IntegrationProblem<ODE>{ODE{}, &initial_state},
[](typename ODE::SystemState const&) {},
1.0 * Second)->WriteToMessage(valid_ephemeris_message_.mutable_instance());
McLachlanAtela1992Order5Optimal<Position<Barycentric>>().
NewInstance(IntegrationProblem<ODE>{ODE{}, &initial_state},
[](typename ODE::SystemState const&) {},
1.0 * Second)->WriteToMessage(
valid_ephemeris_message_.mutable_instance());
}

void InsertAllSolarSystemBodies() {
@@ -311,6 +312,38 @@ class PluginTest : public testing::Test {
++number_of_new_vessels;
}

void PrintSerializedPlugin(const Plugin& plugin) {
serialization::Plugin message;
plugin.WriteToMessage(&message);
std::string const serialized = message.SerializeAsString();

std::fstream file = std::fstream(
SOLUTION_DIR / "ksp_plugin_test" / "simple_plugin.proto.bin",
std::ios::binary | std::ios::out);
CHECK(file.good());
file.write(serialized.c_str(), serialized.size());
file.close();

file = std::fstream(
SOLUTION_DIR / "ksp_plugin_test" / "simple_plugin.proto.hex",
std::ios::out);
CHECK(file.good());
int index = 0;
for (unsigned char c : serialized) {
file << std::hex << std::uppercase << std::setw(2) << std::setfill('0')
<< static_cast<int>(c);
++index;
if (index == 40) {
file << '\n';
index = 0;
}
}
if (index != 0) {
file << '\n';
}
file.close();
}

RotatingBody<Barycentric>::Parameters body_rotation_;
static RigidMotion<ICRFJ2000Equator, Barycentric> const id_icrf_barycentric_;
not_null<std::unique_ptr<SolarSystem<ICRFJ2000Equator>>> solar_system_;
@@ -409,6 +442,12 @@ TEST_F(PluginTest, Serialization) {
Instant const time = initial_time_ + shift;
plugin->AdvanceTime(time, Angle());

#if 0
// Uncomment this block to print out a serialized "simple" plugin for
// interface_test.cpp.
PrintSerializedPlugin(*plugin);
#endif

// Add a handful of points to the history and then forget some of them. This
// is the most convenient way to check that forgetting works as expected.
plugin->InsertOrKeepVessel(satellite,
Binary file added ksp_plugin_test/simple_plugin.proto.bin
Binary file not shown.
Loading