Skip to content

Commit

Permalink
Reorganize is_true/false and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Jul 8, 2018
1 parent d29178a commit 44a9534
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/alia/data_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,22 @@ struct loop_block : noncopyable
// takes the context as its first argument. The other form has no trailing
// underscore and assumes that the context is a variable named 'ctx'.

// is_true(x) evaluates x in a boolean context.
template<class T>
std::enable_if_t<!is_readable_signal_type<T>::value, bool>
is_true(T x)
{
return x ? true : false;
}

// is_false(x) evaluates x in a boolean context and inverts it.
template<class T>
std::enable_if_t<!is_readable_signal_type<T>::value, bool>
is_false(T x)
{
return x ? false : true;
}

// if, else_if, else

#define alia_if_(ctx, condition) \
Expand Down
15 changes: 15 additions & 0 deletions unit_tests/signals/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <catch.hpp>

#include <alia/signals/basic.hpp>

struct unreadable_regular_signal
: alia::regular_signal<int, alia::read_only_signal>
{
Expand Down Expand Up @@ -91,3 +93,16 @@ TEST_CASE("lazy_reader", "[signals]")
REQUIRE(reader.read(generator) == 12);
REQUIRE(n == 1);
}

TEST_CASE("is_true/false", "[signals]")
{
using namespace alia;

REQUIRE(is_true(value(true)));
REQUIRE(!is_true(value(false)));
REQUIRE(!is_true(empty<bool>()));

REQUIRE(is_false(value(false)));
REQUIRE(!is_false(value(true)));
REQUIRE(!is_false(empty<bool>()));
}

0 comments on commit 44a9534

Please sign in to comment.