Skip to content

Commit

Permalink
Fix signal logical operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Jul 6, 2018
1 parent e9fb330 commit 4632413
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/alia/signals/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(+)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(-)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(*)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(/)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(^)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR (^)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(%)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(&)
ALIA_DEFINE_BINARY_SIGNAL_OPERATOR(|)
Expand Down Expand Up @@ -65,9 +65,9 @@ struct logical_or_signal : signal<bool, read_only_signal>
{
}
id_interface const&
id() const
value_id() const
{
id_ = combine_ids(ref(arg0_.id()), ref(arg1_.id()));
id_ = combine_ids(ref(arg0_.value_id()), ref(arg1_.value_id()));
return id_;
}
bool
Expand Down Expand Up @@ -127,9 +127,9 @@ struct logical_and_signal : signal<bool, read_only_signal>
{
}
id_interface const&
id() const
value_id() const
{
id_ = combine_ids(ref(arg0_.id()), ref(arg1_.id()));
id_ = combine_ids(ref(arg0_.value_id()), ref(arg1_.value_id()));
return id_;
}
bool
Expand Down
54 changes: 53 additions & 1 deletion unit_tests/signals/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <alia/signals/lambdas.hpp>
#include <alia/signals/utilities.hpp>

TEST_CASE("signal operators", "[signals]")
TEST_CASE("basic signal operators", "[signals]")
{
using namespace alia;

Expand Down Expand Up @@ -40,6 +40,58 @@ TEST_CASE("signal operators", "[signals]")
REQUIRE(is_false(!(value(2) == value(2))));
}

TEST_CASE("signal &&", "[signals]")
{
using namespace alia;

REQUIRE(is_true(value(true) && value(true)));
REQUIRE(is_false(value(true) && value(false)));
REQUIRE(is_false(value(false) && value(true)));
REQUIRE(is_false(value(false) && value(false)));

// Check that unreadable signals are treated properly.
REQUIRE(!signal_is_readable(empty<bool>() && empty<bool>()));
REQUIRE(!signal_is_readable(value(true) && empty<bool>()));
REQUIRE(!signal_is_readable(empty<bool>() && value(true)));
REQUIRE(is_false(value(false) && empty<bool>()));
REQUIRE(is_false(empty<bool>() && value(false)));

// Check that && short circuits.
int access_count = 0;
auto access_counting_signal = lambda_input(always_readable, [&]() {
++access_count;
return true;
});
REQUIRE(is_false(value(false) && access_counting_signal));
REQUIRE(access_count == 0);
}

TEST_CASE("signal ||", "[signals]")
{
using namespace alia;

REQUIRE(is_true(value(true) || value(true)));
REQUIRE(is_true(value(true) || value(false)));
REQUIRE(is_true(value(false) || value(true)));
REQUIRE(is_false(value(false) || value(false)));

// Check that unreadable signals are treated properly.
REQUIRE(!signal_is_readable(empty<bool>() || empty<bool>()));
REQUIRE(!signal_is_readable(value(false) || empty<bool>()));
REQUIRE(!signal_is_readable(empty<bool>() || value(false)));
REQUIRE(is_true(value(true) || empty<bool>()));
REQUIRE(is_true(empty<bool>() || value(true)));

// Check that || short circuits.
int access_count = 0;
auto access_counting_signal = lambda_input(always_readable, [&]() {
++access_count;
return false;
});
REQUIRE(is_true(value(true) || access_counting_signal));
REQUIRE(access_count == 0);
}

TEST_CASE("select_signal", "[signals]")
{
using namespace alia;
Expand Down

0 comments on commit 4632413

Please sign in to comment.