Add mj_copyBack for copying real-valued arrays from mjModel back to mjSpec.

Also add `SaveAndReadXML` function to test fixture using `mjSpec` as input.

PiperOrigin-RevId: 765393821
Change-Id: Ic257addd2fc89678fc11b226c168c077626cc51e
This commit is contained in:
Alessio Quaglino
2025-05-30 17:16:20 -07:00
committed by Copybara-Service
parent 072c872deb
commit 84ad22a590
18 changed files with 139 additions and 14 deletions
+16 -4
View File
@@ -130,9 +130,7 @@ std::string GetFileContents(const char* path) {
return sstream.str();
}
std::string SaveAndReadXml(const mjModel* model) {
EXPECT_THAT(model, testing::NotNull());
std::string SaveAndReadXmlImpl(const mjModel* model, const mjSpec* spec) {
constexpr int kMaxPathLen = 1024;
std::string path_template =
std::filesystem::temp_directory_path().append("tmp.XXXXXX").string();
@@ -148,7 +146,11 @@ std::string SaveAndReadXml(const mjModel* model) {
EXPECT_NE(_mktemp_s(filepath), EINVAL);
#endif
mj_saveLastXML(filepath, model, nullptr, 0);
if (spec) {
mj_saveXML(spec, filepath, nullptr, 0);
} else if (model) {
mj_saveLastXML(filepath, model, nullptr, 0);
}
std::string contents = GetFileContents(filepath);
#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
@@ -159,6 +161,16 @@ std::string SaveAndReadXml(const mjModel* model) {
return contents;
}
std::string SaveAndReadXml(const mjModel* model) {
EXPECT_THAT(model, testing::NotNull());
return SaveAndReadXmlImpl(model, nullptr);
}
std::string SaveAndReadXml(const mjSpec* spec) {
EXPECT_THAT(spec, testing::NotNull());
return SaveAndReadXmlImpl(nullptr, spec);
}
std::vector<mjtNum> GetCtrlNoise(const mjModel* m, int nsteps,
mjtNum ctrlnoise) {
std::vector<mjtNum> ctrl;