Migrate tests to use MockWarningHandler for warning interception.
PiperOrigin-RevId: 933656759 Change-Id: Ia21514c302799034bf598445933dd90427284737
This commit is contained in:
committed by
Copybara-Service
parent
38c3388046
commit
cedaa47d61
@@ -31,7 +31,6 @@
|
||||
#include <mujoco/mjtype.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include <mujoco/mjxmacro.h>
|
||||
#include "src/cc/array_safety.h"
|
||||
#include "src/engine/engine_callback.h"
|
||||
#include "src/engine/engine_core_util.h"
|
||||
#include "src/engine/engine_io.h"
|
||||
@@ -68,10 +67,11 @@ static const char* const kTendonForceClamp =
|
||||
|
||||
using ::testing::Pointwise;
|
||||
|
||||
using ::testing::Ne;
|
||||
using ::testing::HasSubstr;
|
||||
using ::testing::NotNull;
|
||||
using ::testing::_;
|
||||
using ::testing::Gt;
|
||||
using ::testing::HasSubstr;
|
||||
using ::testing::Ne;
|
||||
using ::testing::NotNull;
|
||||
|
||||
// --------------------------- activation limits -------------------------------
|
||||
|
||||
@@ -997,32 +997,28 @@ TEST_F(ForwardTest, ControlClamping) {
|
||||
// data->ctrl[1] remains pristine
|
||||
EXPECT_EQ(data->ctrl[1], 2);
|
||||
|
||||
// install warning handler
|
||||
static char warning[1024];
|
||||
warning[0] = '\0';
|
||||
mju_user_warning = [](const char* msg) {
|
||||
util::strcpy_arr(warning, msg);
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
|
||||
// for the unclamped actuator, huge raises warning
|
||||
warning_handler.ExpectWarnings(
|
||||
"Nan, Inf or huge value in CTRL at ACTUATOR 0");
|
||||
data->ctrl[0] = 10*mjMAXVAL;
|
||||
mj_forward(model, data);
|
||||
EXPECT_THAT(warning,
|
||||
HasSubstr("Nan, Inf or huge value in CTRL at ACTUATOR 0"));
|
||||
testing::Mock::VerifyAndClearExpectations(&warning_handler);
|
||||
|
||||
// for the clamped actuator, huge does not raise warning
|
||||
EXPECT_CALL(warning_handler, Warn(_)).Times(0);
|
||||
mj_resetData(model, data);
|
||||
warning[0] = '\0';
|
||||
data->ctrl[1] = 10*mjMAXVAL;
|
||||
mj_forward(model, data);
|
||||
EXPECT_EQ(warning[0], '\0');
|
||||
testing::Mock::VerifyAndClearExpectations(&warning_handler);
|
||||
|
||||
// for the clamped actuator, NaN raises warning
|
||||
warning_handler.ExpectWarnings(
|
||||
"Nan, Inf or huge value in CTRL at ACTUATOR 1");
|
||||
mj_resetData(model, data);
|
||||
data->ctrl[1] = std::numeric_limits<double>::quiet_NaN();
|
||||
mj_forward(model, data);
|
||||
EXPECT_THAT(warning,
|
||||
HasSubstr("Nan, Inf or huge value in CTRL at ACTUATOR 1"));
|
||||
|
||||
mj_deleteData(data);
|
||||
mj_deleteModel(model);
|
||||
@@ -2865,7 +2861,8 @@ TEST_F(ActEarlyTest, RemovesOneStepDelay) {
|
||||
mj_step(model, data);
|
||||
for (int j = 0; j < model->nu / 2; j++) {
|
||||
// this is true for torque actuators
|
||||
EXPECT_NEAR(last_qfrc[2 * j], data->qfrc_actuator[2 * j + 1], MjTol(1e-3, 1e-1))
|
||||
EXPECT_NEAR(last_qfrc[2 * j], data->qfrc_actuator[2 * j + 1],
|
||||
MjTol(1e-3, 1e-1))
|
||||
<< "there should be a 1 step delay between qfrc for "
|
||||
<< mj_id2name(model, mjOBJ_ACTUATOR, 2 * j);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#include <absl/strings/str_format.h>
|
||||
#include <mujoco/mjxmacro.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "src/engine/engine_util_errmem.h"
|
||||
#include "src/engine/engine_thread.h"
|
||||
#include "test/fixture.h"
|
||||
|
||||
@@ -130,14 +129,10 @@ TEST_F(EngineIoTest, MakeDataReturnsNullOnFailure) {
|
||||
|
||||
// fail mj_makeData intentionally with a bad size
|
||||
model->nbody = -1;
|
||||
static bool warning;
|
||||
warning = false;
|
||||
mju_user_warning = [](const char* error) {
|
||||
warning = true;
|
||||
};
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
mjData* data = mj_makeData(model);
|
||||
EXPECT_THAT(data, IsNull());
|
||||
EXPECT_TRUE(warning) << "Expecting warning to be triggered.";
|
||||
|
||||
mj_deleteData(data);
|
||||
mj_deleteModel(model);
|
||||
@@ -964,18 +959,14 @@ TEST_F(EngineIoTest, LoadModelBufferRejectsOverflowingSizes) {
|
||||
// nbuffer mismatch check — but the overflow should be caught earlier
|
||||
// in safeAddToBufferSize/mj_makeModel before we reach that check.
|
||||
|
||||
// intercept mju_warning because the test framework translates it to ADD_FAILURE
|
||||
static bool warning_triggered = false;
|
||||
warning_triggered = false;
|
||||
mju_user_warning = [](const char* msg) {
|
||||
warning_triggered = true;
|
||||
};
|
||||
// Intercept warnings to prevent them from failing the test.
|
||||
MockWarningHandler warning_handler;
|
||||
warning_handler.ExpectWarnings();
|
||||
|
||||
// attempt to load — should return NULL, not crash
|
||||
mjModel* bad_model = mj_loadModelBuffer(buffer.data(), bufsize);
|
||||
EXPECT_THAT(bad_model, IsNull())
|
||||
<< "Expected mj_loadModelBuffer to reject overflow-inducing sizes";
|
||||
EXPECT_TRUE(warning_triggered) << "Expected a warning about invalid sizes";
|
||||
|
||||
// clean up if somehow it succeeded
|
||||
if (bad_model) {
|
||||
|
||||
Reference in New Issue
Block a user