Add recompilation test to user_api_test.

PiperOrigin-RevId: 613192683
Change-Id: Ibb44e1c2d63ac0afb7b5f3a15dff26378deb6c82
This commit is contained in:
Alessio Quaglino
2024-03-06 06:26:26 -08:00
committed by Copybara-Service
parent a00f1b5f30
commit 9ac048e262
8 changed files with 160 additions and 23 deletions
+63
View File
@@ -14,16 +14,25 @@
// Tests for user/user_api.cc.
#include <array>
#include <filesystem>
#include <string>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <absl/strings/match.h>
#include <mujoco/mujoco.h>
#include "src/user/user_api.h"
#include "src/xml/xml.h"
#include "src/xml/xml_numeric_format.h"
#include "test/fixture.h"
namespace mujoco {
namespace {
using ::testing::HasSubstr;
using ::testing::NotNull;
// ----------------------------- test set/get --------------------------------
@@ -53,5 +62,59 @@ TEST_F(MujocoTest, ReadWriteData) {
mjm_deleteModel(model);
}
// ------------------- test recompilation multiple files ----------------------
TEST_F(PluginTest, RecompileCompare) {
// full precision float printing
FullFloatPrecision increase_precision;
// loop over all xml files in data
std::vector<std::string> paths = {GetTestDataFilePath("."),
GetModelPath(".")};
std::string ext(".xml");
for (auto const& path : paths) {
for (auto &p : std::filesystem::recursive_directory_iterator(path)) {
if (p.path().extension() == ext) {
std::string xml = p.path().string();
// if file is meant to fail, skip it
if (absl::StrContains(p.path().string(), "malformed_") ||
absl::StrContains(p.path().string(), "touch_grid") ||
absl::StrContains(p.path().string(), "cow") ||
absl::StrContains(p.path().string(), "discardvisual")) {
continue;
}
// load model
std::array<char, 1000> error;
mjmModel* model =
mjParseXML(xml.c_str(), nullptr, error.data(), error.size());
// compile twice
mjModel* m_old = mjm_compileModel(model, nullptr);
mjModel* m_new = mjm_compileModel(model, nullptr);
ASSERT_THAT(m_old, NotNull())
<< "Failed to compile " << xml << ": " << error.data();
ASSERT_THAT(m_new, NotNull())
<< "Failed to recompile " << xml << ": " << error.data();
// compare and delete
std::string field = "";
mjtNum result = CompareModel(m_old, m_new, field);
mjtNum tol = 0;
EXPECT_LE(result, tol)
<< "Loaded and saved models are different!\n"
<< "Affected file " << p.path().string() << '\n'
<< "Different field: " << field << '\n';
// delete models
mjm_deleteModel(model);
mj_deleteModel(m_old);
mj_deleteModel(m_new);
}
}
}
}
} // namespace
} // namespace mujoco