Skip to content

Commit

Permalink
fix: default predicate value on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
domire8 committed Oct 16, 2024
1 parent cc85c64 commit 3ec1d71
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
8 changes: 3 additions & 5 deletions source/modulo_core/include/modulo_core/Predicate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ namespace modulo_core {
*/
class Predicate {
public:
explicit Predicate(const std::function<bool(void)>& predicate_function) : predicate_(std::move(predicate_function)) {
previous_value_ = !predicate_();
}
explicit Predicate(const std::function<bool(void)>& predicate_function) : predicate_(std::move(predicate_function)) {}

bool get_value() const { return predicate_(); }

void set_predicate(const std::function<bool(void)>& predicate_function) { predicate_ = predicate_function; }

std::optional<bool> query() {
if (const auto new_value = predicate_(); new_value != previous_value_) {
if (const auto new_value = predicate_(); !previous_value_ || new_value != *previous_value_) {
previous_value_ = new_value;
return new_value;
}
Expand All @@ -30,7 +28,7 @@ class Predicate {

private:
std::function<bool(void)> predicate_;
bool previous_value_;
std::optional<bool> previous_value_;
};

}// namespace modulo_core
2 changes: 1 addition & 1 deletion source/modulo_core/modulo_core/predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, predicate_function: Callable[[], bool]):
value = predicate_function()
if not isinstance(value, bool):
raise CoreError("Predicate function does not return a bool")
self.__previous_value = not value
self.__previous_value: Optional[bool] = None

def get_value(self) -> bool:
return self.__predicate()
Expand Down
9 changes: 9 additions & 0 deletions source/modulo_core/test/cpp/test_predicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ TEST(PredicateTest, SimplePredicate) {
value = predicate.query();
EXPECT_FALSE(value);
}

TEST(PredicateTest, ChangeBeforeQuery) {
auto predicate = Predicate([]() { return true; });
predicate.set_predicate([]() { return false; });
EXPECT_FALSE(predicate.get_value());
auto value = predicate.query();
EXPECT_TRUE(value);
EXPECT_FALSE(*value);
}
23 changes: 23 additions & 0 deletions source/modulo_core/test/python/test_predicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from modulo_core import Predicate


def test_simple_predicate():
predicate = Predicate(lambda: True)
assert predicate.get_value()
assert predicate.query()
assert predicate.get_value()
assert predicate.query() is None

predicate.set_predicate(lambda: False)
assert predicate.get_value() is False
assert predicate.query() is False
assert predicate.get_value() is False
assert predicate.query() is None


def test_predicate_change_before_query():
predicate = Predicate(lambda: False)
predicate.set_predicate(lambda: True)
assert predicate.get_value()
assert predicate.query()
assert predicate.query() is None

0 comments on commit 3ec1d71

Please sign in to comment.