Skip to content

Commit

Permalink
Add seq in tick tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hit9 committed Apr 28, 2024
1 parent e8d5331 commit 65627fa
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 125 deletions.
10 changes: 6 additions & 4 deletions bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class Node {
std::string name;
// cache priority for current tick.
unsigned int priorityCurrentTick = 0;
unsigned int priorityCurrentTickSeq = 0;

protected:
NodeId id = 0;
Expand Down Expand Up @@ -345,8 +346,12 @@ class Node {

// Internal method to query priority of this node in current tick.
unsigned int GetPriorityCurrentTick(const Context& ctx) {
if (ctx.seq != priorityCurrentTickSeq) priorityCurrentTick = 0;
// try cache in this tick firstly.
if (!priorityCurrentTick) priorityCurrentTick = Priority(ctx);
if (!priorityCurrentTick) {
priorityCurrentTick = Priority(ctx);
priorityCurrentTickSeq = ctx.seq;
}
return priorityCurrentTick;
}

Expand Down Expand Up @@ -378,9 +383,6 @@ class Node {
OnTerminate(ctx, status);
b->running = false; // reset
}

// Clears priority for this tick.
priorityCurrentTick = 0;
return status;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/blob_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
// e1: Tick#1
root.BindTreeBlob(e1.blob);
bb->shouldA = bt::Status::FAILURE;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
REQUIRE(bb->counterA == 1);
Expand All @@ -71,6 +72,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
// e2: Tick#1
root.BindTreeBlob(e2.blob);
bb->shouldA = bt::Status::SUCCESS;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
REQUIRE(bb->counterA == 2); // +1
Expand All @@ -81,6 +83,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
root.BindTreeBlob(e3.blob);
bb->shouldA = bt::Status::FAILURE;
bb->shouldB = bt::Status::FAILURE;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
REQUIRE(bb->counterA == 3); // +1
Expand All @@ -92,6 +95,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
// e1: Tick#2
root.BindTreeBlob(e1.blob);
bb->shouldB = bt::Status::FAILURE;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
REQUIRE(bb->counterA == 3); // +0
Expand All @@ -102,6 +106,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
// e2: Tick#2
root.BindTreeBlob(e2.blob);
bb->shouldA = bt::Status::SUCCESS;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
REQUIRE(bb->counterA == 4); // +1
Expand All @@ -112,6 +117,7 @@ TEMPLATE_TEST_CASE("Blob/2", "[multiple entites]", Entity,
// e3: Tick#2
root.BindTreeBlob(e3.blob);
bb->shouldE = bt::Status::FAILURE;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);
REQUIRE(bb->counterA == 4); // +0
Expand Down
2 changes: 2 additions & 0 deletions tests/builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ TEMPLATE_TEST_CASE("Builder/1", "[extend a custom decorator to builder]", Entity
// Tick#1

root.BindTreeBlob(e.blob);
++ctx.seq;
root.Tick(ctx);
REQUIRE(bb->counterA == 1);
REQUIRE(bb->customDecoratorCounter == 1);
Expand All @@ -55,6 +56,7 @@ TEMPLATE_TEST_CASE("Builder/1", "[extend a custom decorator to builder]", Entity
// Tick#2
root.BindTreeBlob(e.blob);
bb->shouldA = bt::Status::SUCCESS;
++ctx.seq;
root.Tick(ctx);
REQUIRE(bb->counterA == 2);
REQUIRE(bb->counterB == 1);
Expand Down
20 changes: 20 additions & 0 deletions tests/condition_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ TEST_CASE("Condition/1", "[simplest condition - constructed from template]") {
// Tick#1
root.BindTreeBlob(e.blob);
std::cout << "tick1" << std::endl;
++ctx.seq;
root.Tick(ctx);
// A should not started.
REQUIRE(bb->counterA == 0);
Expand All @@ -35,6 +36,7 @@ TEST_CASE("Condition/1", "[simplest condition - constructed from template]") {
// Tick#2: Make C true.
root.BindTreeBlob(e.blob);
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
// A should started running.
REQUIRE(bb->counterA == 1);
Expand All @@ -47,6 +49,7 @@ TEST_CASE("Condition/1", "[simplest condition - constructed from template]") {
// Tick#3: Make A Success.
root.BindTreeBlob(e.blob);
bb->shouldA = bt::Status::SUCCESS;
++ctx.seq;
root.Tick(ctx);
// The whole tree should Success.
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
Expand Down Expand Up @@ -76,13 +79,15 @@ TEST_CASE("Condition/2", "[simplest condition - constructed from lambda]") {
// Tick#1
root.BindTreeBlob(e.blob);

++ctx.seq;
root.Tick(ctx);
// A should not started.
REQUIRE(bb->counterA == 0);
REQUIRE(bb->statusA == bt::Status::UNDEFINED);

// Tick#2: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
// A should started running.
REQUIRE(bb->counterA == 1);
Expand Down Expand Up @@ -114,13 +119,15 @@ TEST_CASE("Condition/3", "[simplest condition - if]") {
REQUIRE(bb->shouldC == false);

// Tick#1
++ctx.seq;
root.Tick(ctx);
// A should not started.
REQUIRE(bb->counterA == 0);
REQUIRE(bb->statusA == bt::Status::UNDEFINED);

// Tick#2: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
// A should started running.
REQUIRE(bb->counterA == 1);
Expand All @@ -131,6 +138,7 @@ TEST_CASE("Condition/3", "[simplest condition - if]") {

// Tick#2: Make C false.
bb->shouldC = false;
++ctx.seq;
root.Tick(ctx);
// A should not change.
REQUIRE(bb->counterA == 1);
Expand Down Expand Up @@ -160,18 +168,21 @@ TEST_CASE("Condition/4", "[simplest condition - and]") {
root.BindTreeBlob(e.blob);

// Tick#1
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);

// Tick#2: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);

// Tick#2: Make All false.
bb->shouldC = true;
bb->shouldD = true;
bb->shouldF = true;
++ctx.seq;
root.Tick(ctx);
// The whole tree should Success
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
Expand All @@ -198,24 +209,28 @@ TEST_CASE("Condition/5", "[simplest condition - or]") {
root.BindTreeBlob(e.blob);

// Tick#1
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);

// Tick#2: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);

// Tick#3: Make only D true.
bb->shouldC = false;
bb->shouldD = true;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);

// Tick#4: Make All false.
bb->shouldC = true;
bb->shouldD = true;
bb->shouldF = true;
++ctx.seq;
root.Tick(ctx);
// The whole tree should Success
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
Expand All @@ -240,11 +255,13 @@ TEST_CASE("Condition/6", "[simplest condition - not]") {
root.BindTreeBlob(e.blob);

// Tick#1
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);

// Tick#2: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);

Expand All @@ -271,18 +288,21 @@ TEST_CASE("Condition/7", "[simplest condition - not2]") {
REQUIRE(!bb->shouldC);

// Tick#1
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
REQUIRE(bb->counterA == 1);

// Tick#2: Make A Success.
bb->shouldA = bt::Status::SUCCESS;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
REQUIRE(bb->counterA == 2);

// Tick#3: Make C true.
bb->shouldC = true;
++ctx.seq;
root.Tick(ctx);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);
REQUIRE(bb->counterA == 2); // not ticked
Expand Down
4 changes: 2 additions & 2 deletions tests/delay_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEMPLATE_TEST_CASE("Delay/1", "[simple delay]", Entity,
REQUIRE(bb->counterA == 0);
// Tick#1: A is not started.
root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);;
REQUIRE(bb->counterA == 0);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
root.UnbindTreeBlob();
Expand All @@ -36,7 +36,7 @@ TEMPLATE_TEST_CASE("Delay/1", "[simple delay]", Entity,

// Tick#2: A is started.
root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);;
REQUIRE(bb->counterA == 1);
root.UnbindTreeBlob();
}
6 changes: 3 additions & 3 deletions tests/hook_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEMPLATE_TEST_CASE("Hook/2", "[hook OnEnter]", Entity, (EntityFixedBlob<16>)) {
TestType e;

root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->onEnterCalledA);
root.UnbindTreeBlob();
}
Expand All @@ -62,13 +62,13 @@ TEMPLATE_TEST_CASE("Hook/3", "[hook OnTerminate]", Entity, (EntityFixedBlob<16>)
TestType e;

root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(!bb->onTerminatedCalledA);
root.UnbindTreeBlob();

root.BindTreeBlob(e.blob);
bb->shouldA = bt::Status::FAILURE;
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->onTerminatedCalledA);
root.UnbindTreeBlob();
}
8 changes: 4 additions & 4 deletions tests/invert_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_CASE("Invert/1", "[invert once]") {

// Tick#1: A is running
root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->counterA == 1);
REQUIRE(bb->statusA == bt::Status::RUNNING);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
Expand All @@ -31,7 +31,7 @@ TEST_CASE("Invert/1", "[invert once]") {
// Tick#2: A is success.
root.BindTreeBlob(e.blob);
bb->shouldA = bt::Status::SUCCESS;
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->counterA == 2);
REQUIRE(bb->statusA == bt::Status::SUCCESS);
REQUIRE(root.LastStatus() == bt::Status::FAILURE);
Expand All @@ -58,7 +58,7 @@ TEST_CASE("Invert/2", "[invert twice]") {

// Tick#1: A is running
root.BindTreeBlob(e.blob);
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->counterA == 1);
REQUIRE(bb->statusA == bt::Status::RUNNING);
REQUIRE(root.LastStatus() == bt::Status::RUNNING);
Expand All @@ -67,7 +67,7 @@ TEST_CASE("Invert/2", "[invert twice]") {
// Tick#2: A is success.
root.BindTreeBlob(e.blob);
bb->shouldA = bt::Status::SUCCESS;
root.Tick(ctx);
++ctx.seq;root.Tick(ctx);
REQUIRE(bb->counterA == 2);
REQUIRE(bb->statusA == bt::Status::SUCCESS);
REQUIRE(root.LastStatus() == bt::Status::SUCCESS);
Expand Down
Loading

0 comments on commit 65627fa

Please sign in to comment.