Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Aug 25, 2023
1 parent cc2e097 commit be535b1
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 143 deletions.
219 changes: 145 additions & 74 deletions wpilibc/src/test/native/cpp/event/BooleanEventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,89 @@ TEST(BooleanEventTest, BinaryCompositions) {
EXPECT_EQ(1, orCounter);
}

/**
* Tests that composed edge events only execute on edges (two
* rising edge events composed with and() should only execute when both signals
* are on the rising edge)
*/
TEST(BooleanEventTest, BinaryCompositionsWithEdgeDecorators) {
EventLoop loop;
bool boolean1 = false;
bool boolean2 = false;
bool boolean3 = false;
bool boolean4 = false;
int counter = 0;

auto event1 = BooleanEvent(&loop, [&] { return boolean1; }).Rising();
auto event2 = BooleanEvent(&loop, [&] { return boolean2; }).Rising();
auto event3 = BooleanEvent(&loop, [&] { return boolean3; }).Rising();
auto event4 = BooleanEvent(&loop, [&] { return boolean4; }).Rising();
(event1 && event2).IfHigh([&] { ++counter; });
(event3 || event4).IfHigh([&] { ++counter; });
EXPECT_EQ(0, counter);

boolean1 = true;
boolean2 = true;
boolean3 = true;
boolean4 = true;
loop.Poll(); // Both actions execute

EXPECT_EQ(2, counter);

loop.Poll(); // Nothing should happen since nothing is on rising edge

EXPECT_EQ(2, counter);

boolean1 = false;
boolean2 = false;
boolean3 = false;
boolean4 = false;
loop.Poll(); // Nothing should happen

EXPECT_EQ(2, counter);

boolean1 = true;
loop.Poll(); // Nothing should happen since only Bool 1 is on rising edge

EXPECT_EQ(2, counter);

boolean2 = true;
loop.Poll(); // Bool 2 is on rising edge, but Bool 1 isn't, nothing should
// happen

EXPECT_EQ(2, counter);

boolean1 = false;
boolean2 = false;
loop.Poll(); // Nothing should happen

EXPECT_EQ(2, counter);

boolean1 = true;
boolean2 = true;
loop.Poll(); // Bool 1 and 2 are on rising edge, increments counter once

EXPECT_EQ(3, counter);

boolean3 = true;
loop.Poll(); // Bool 3 is on rising edge, increments counter once

EXPECT_EQ(4, counter);

loop.Poll(); // Nothing should happen, Bool 3 isn't on rising edge

EXPECT_EQ(4, counter);

boolean4 = true;
loop.Poll(); // Bool 4 is on rising edge, increments counter once

EXPECT_EQ(5, counter);

loop.Poll(); // Nothing should happen, Bool 4 isn't on rising edge

EXPECT_EQ(5, counter);
}

TEST(BooleanEventTest, BinaryCompositionLoopSemantics) {
EventLoop loop1;
EventLoop loop2;
Expand All @@ -49,90 +132,104 @@ TEST(BooleanEventTest, BinaryCompositionLoopSemantics) {
EXPECT_EQ(0, counter1);
EXPECT_EQ(0, counter2);

loop1.Poll();
loop1
.Poll(); // 1st event executes, Bool 1 and 2 are true, increments counter

EXPECT_EQ(1, counter1);
EXPECT_EQ(0, counter2);

loop2.Poll();
loop2
.Poll(); // 2nd event executes, Bool 1 and 2 are true, increments counter

EXPECT_EQ(1, counter1);
EXPECT_EQ(1, counter2);

boolean2 = false;
loop1.Poll();
loop1.Poll(); // 1st event executes, Bool 2 is still true because loop 2
// hasn't updated it, increments counter

EXPECT_EQ(2, counter1);
EXPECT_EQ(1, counter2);

loop2.Poll();
loop2.Poll(); // 2nd event executes, Bool 2 is now false because loop 1
// updated it, does nothing

EXPECT_EQ(2, counter1);
EXPECT_EQ(1, counter2);

loop1.Poll();
loop1.Poll(); // All bools are updated at this point, nothing should happen

EXPECT_EQ(2, counter1);
EXPECT_EQ(1, counter2);

boolean2 = true;
loop2.Poll();
loop2.Poll(); // 2nd event executes, Bool 2 is still false because loop 1
// hasn't updated it, does nothing

EXPECT_EQ(2, counter1);
EXPECT_EQ(1, counter2);

loop1.Poll();
loop1.Poll(); // 1st event executes, Bool 2 is true because loop 2 updated
// it, increments counter

EXPECT_EQ(3, counter1);
EXPECT_EQ(1, counter2);

boolean1 = false;
loop2.Poll();
loop2.Poll(); // 2nd event executes, Bool 1 is false because this loop
// updated it, does nothing

EXPECT_EQ(3, counter1);
EXPECT_EQ(1, counter2);

loop1.Poll();
loop1.Poll(); // 1st event executes, Bool 1 is false because this loop
// updated it, does nothing

EXPECT_EQ(3, counter1);
EXPECT_EQ(1, counter2);

loop2.Poll();
loop2.Poll(); // All bools are updated at this point, nothing should happen

EXPECT_EQ(3, counter1);
EXPECT_EQ(1, counter2);
}

/**
* When a BooleanEvent is constructed, an action is bound to the event loop to
* update an internal state variable. This state variable is checked during loop
* polls to determine whether or not to execute an action. If a condition is
* changed during the loop poll but before the state variable gets updated, the
* changed condition is used for the state variable.
*/
TEST(BooleanEventTest, EventConstructionOrdering) {
/** Tests the order of actions bound to an event loop. */
TEST(BooleanEventTest, PollOrdering) {
EventLoop loop;
bool boolean1 = true;
bool boolean2 = true;
int counter1 = 0;
int counter2 = 0;
bool enableAssert = false;
int counter = 0;

(BooleanEvent(&loop,
[&] {
boolean2 = false;
return boolean1;
}) &&
(BooleanEvent( // This event binds an action to the event loop first
&loop,
[&] {
if (enableAssert) {
++counter;
EXPECT_EQ(1, counter % 3);
}
return boolean1;
}) && // The composed event binds an action to the event loop third
// This event binds an action to the event loop second
BooleanEvent(&loop, [&] {
if (enableAssert) {
++counter;
EXPECT_EQ(2, counter % 3);
}
return boolean2;
})).IfHigh([&] { ++counter1; });

EXPECT_EQ(0, counter1);
EXPECT_EQ(0, counter2);

// This binds an action to the event loop fourth
})).IfHigh([&] {
if (enableAssert) {
++counter;
EXPECT_EQ(0, counter % 3);
}
});
enableAssert = true;
loop.Poll();
loop.Poll();
loop.Poll();
loop.Poll();

EXPECT_EQ(0, counter1);
EXPECT_EQ(0, counter2);
}

TEST(BooleanEventTest, EdgeDecorators) {
Expand Down Expand Up @@ -290,9 +387,7 @@ TEST(BooleanEventTest, MidLoopBooleanChange) {

/**
* Tests that all actions bound to composed events will still execute even if
* the composed signal changes during the loop poll. Also tests that composed
* edge events only execute on edges (two rising edge events composed with and()
* should only execute when both signals are on the rising edge)
* the composed signal changes during the loop poll.
*/
TEST(BooleanEventTest, MidLoopBooleanChangeWithComposedEvents) {
EventLoop loop;
Expand All @@ -302,23 +397,20 @@ TEST(BooleanEventTest, MidLoopBooleanChangeWithComposedEvents) {
bool boolean4 = false;
int counter = 0;

auto event1 = BooleanEvent(&loop, [&] { return boolean1; }).Rising();
auto event2 = BooleanEvent(&loop, [&] { return boolean2; }).Rising();
auto event3 = BooleanEvent(&loop, [&] { return boolean3; }).Rising();
auto event4 = BooleanEvent(&loop, [&] { return boolean4; }).Rising();
auto event1 = BooleanEvent(&loop, [&] { return boolean1; });
auto event2 = BooleanEvent(&loop, [&] { return boolean2; });
auto event3 = BooleanEvent(&loop, [&] { return boolean3; });
auto event4 = BooleanEvent(&loop, [&] { return boolean4; });
event1.IfHigh([&] {
// Executes only when Bool 1 is on rising edge
boolean2 = false;
boolean3 = false;
++counter;
});
(event3 || event4).IfHigh([&] {
// Executes only when Bool 3 or 4 are on rising edge
boolean1 = false;
++counter;
});
(event1 && event2).IfHigh([&] {
// Executes only when Bool 1 and 2 are on rising edge
boolean3 = false;
boolean4 = false;
++counter;
});
Expand All @@ -339,48 +431,27 @@ TEST(BooleanEventTest, MidLoopBooleanChangeWithComposedEvents) {
EXPECT_EQ(3, counter);

boolean1 = true;
loop.Poll(); // Bool 1 is on rising edge, increments counter once

EXPECT_EQ(4, counter);

loop.Poll(); // Nothing should happen, Bool 1 is true, but not on rising edge

EXPECT_EQ(4, counter);

boolean2 = true;
loop.Poll(); // Nothing should happen, Bool 2 is on rising edge, but Bool 1
// isn't
loop.Poll(); // Bool 1 and 2 are true, increments counter twice, Bool 2 gets
// set to false

EXPECT_EQ(5, counter);

EXPECT_EQ(4, counter);
boolean1 = false;
boolean2 = false;
loop.Poll(); // Nothing should happen

EXPECT_EQ(4, counter);
EXPECT_EQ(5, counter);

boolean1 = true;
boolean2 = true;
loop.Poll(); // Bool 1 and 2 are on rising edge, so counter is incremented
// twice, and Bool 2 is
// reset to false

EXPECT_EQ(6, counter);

boolean3 = true;
loop.Poll(); // Bool 3 is on rising edge, increments counter once

EXPECT_EQ(7, counter);

loop.Poll(); // Nothing should happen, Bool 3 isn't on rising edge
loop.Poll(); // Bool 1 and 3 are true, increments counter twice, Bool 3 gets
// set to false

EXPECT_EQ(7, counter);

boolean1 = false;
boolean4 = true;
loop.Poll(); // Bool 4 is on rising edge, increments counter once

EXPECT_EQ(8, counter);

loop.Poll(); // Nothing should happen, Bool 4 isn't on rising edge
loop.Poll(); // Bool 4 is true, increments counter once

EXPECT_EQ(8, counter);
}
Expand Down
Loading

0 comments on commit be535b1

Please sign in to comment.