BTSetVar: Cover operations in unit tests & fix bugs

This commit is contained in:
Serhii Snitsaruk 2023-11-15 16:35:56 +01:00
parent 554ae5a165
commit c48eeef081
3 changed files with 89 additions and 2 deletions

View File

@ -15,6 +15,7 @@
#include "modules/limboai/util/limbo_utility.h"
#include "core/variant/callable.h"
#include "core/variant/variant.h"
String BTSetVar::_generate_name() const {
if (variable.is_empty()) {
@ -39,6 +40,7 @@ BT::Status BTSetVar::_tick(double p_delta) {
Variant left_value = get_blackboard()->get_var(variable, error_result);
ERR_FAIL_COND_V_MSG(left_value == error_result, FAILURE, vformat("BTSetVar: Failed to get \"%s\" blackboard variable. Returning FAILURE.", variable));
result = LimboUtility::get_singleton()->perform_operation(operation, left_value, right_value);
ERR_FAIL_COND_V_MSG(result == Variant(), FAILURE, "BTSetVar: Operation not valid. Returning FAILURE.");
}
get_blackboard()->set_var(variable, result);
return SUCCESS;

View File

@ -43,7 +43,7 @@ TEST_CASE("[Modules][LimboAI] BTSetVar") {
sv->set_value(value);
sv->set_variable("var");
SUBCASE("When setting to a provided value") {
SUBCASE("When assigning a raw value") {
value->set_value_source(BBParam::SAVED_VALUE);
value->set_saved_value(123);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
@ -74,6 +74,91 @@ TEST_CASE("[Modules][LimboAI] BTSetVar") {
CHECK(bb->get_var("var", 0) == Variant(123));
}
}
SUBCASE("When performing an operation") {
bb->set_var("var", 8);
value->set_value_source(BBParam::SAVED_VALUE);
value->set_saved_value(3);
SUBCASE("Addition") {
sv->set_operation(LimboUtility::OPERATION_ADDITION);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(11));
}
SUBCASE("Subtraction") {
sv->set_operation(LimboUtility::OPERATION_SUBTRACTION);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(5));
}
SUBCASE("Multiplication") {
sv->set_operation(LimboUtility::OPERATION_MULTIPLICATION);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(24));
}
SUBCASE("Division") {
sv->set_operation(LimboUtility::OPERATION_DIVISION);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(2));
}
SUBCASE("Modulo") {
sv->set_operation(LimboUtility::OPERATION_MODULO);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(2));
}
SUBCASE("Power") {
sv->set_operation(LimboUtility::OPERATION_POWER);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(512));
}
SUBCASE("Bitwise shift left") {
sv->set_operation(LimboUtility::OPERATION_BIT_SHIFT_LEFT);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(64));
}
SUBCASE("Bitwise shift right") {
sv->set_operation(LimboUtility::OPERATION_BIT_SHIFT_RIGHT);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(1));
}
SUBCASE("Bitwise AND") {
bb->set_var("var", 6);
sv->set_operation(LimboUtility::OPERATION_BIT_AND);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(2));
}
SUBCASE("Bitwise OR") {
bb->set_var("var", 6);
sv->set_operation(LimboUtility::OPERATION_BIT_OR);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(7));
}
SUBCASE("Bitwise XOR") {
bb->set_var("var", 6);
sv->set_operation(LimboUtility::OPERATION_BIT_XOR);
CHECK(sv->execute(0.01666) == BTTask::SUCCESS);
CHECK(bb->get_var("var", 0) == Variant(5));
}
}
SUBCASE("Performing an operation when assigned variable doesn't exist.") {
value->set_value_source(BBParam::SAVED_VALUE);
value->set_saved_value(3);
sv->set_operation(LimboUtility::OPERATION_ADDITION);
ERR_PRINT_OFF;
CHECK(sv->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK_FALSE(bb->has_var("var"));
}
SUBCASE("Performing an operation with incompatible operand types.") {
bb->set_var("var", 2); // int
value->set_value_source(BBParam::SAVED_VALUE);
value->set_saved_value("3"); // String
sv->set_operation(LimboUtility::OPERATION_ADDITION);
ERR_PRINT_OFF;
CHECK(sv->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(bb->get_var("var", 0) == Variant(2));
}
}
}

View File

@ -211,7 +211,7 @@ Variant LimboUtility::perform_operation(Operation p_operation, const Variant &le
return Variant::evaluate(Variant::OP_MODULE, left_value, right_value);
} break;
case OPERATION_POWER: {
return Variant::evaluate(Variant::OP_MODULE, left_value, right_value);
return Variant::evaluate(Variant::OP_POWER, left_value, right_value);
} break;
case OPERATION_BIT_SHIFT_LEFT: {
return Variant::evaluate(Variant::OP_SHIFT_LEFT, left_value, right_value);