From 941bf61f283a34ba00af59f5eb99335f324cfeb1 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Thu, 26 Jan 2023 13:26:11 +0100 Subject: [PATCH] constant number of assertions in unittests --- unittests/hal/testCommandExecutor.cpp | 33 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/unittests/hal/testCommandExecutor.cpp b/unittests/hal/testCommandExecutor.cpp index 89011922b..d565ff7f9 100644 --- a/unittests/hal/testCommandExecutor.cpp +++ b/unittests/hal/testCommandExecutor.cpp @@ -45,9 +45,16 @@ TEST_CASE("Command Executor", "[hal][linux]") { while (result != CommandExecutor::EXECUTION_FINISHED) { limitIdx++; result = cmdExecutor.check(bytesHaveBeenRead); - REQUIRE(result != CommandExecutor::COMMAND_ERROR); + // to have constant assertion count, we first check condition + // and then force a failed assertion + if (result == CommandExecutor::COMMAND_ERROR) { + REQUIRE(result != CommandExecutor::COMMAND_ERROR); + } usleep(500); - REQUIRE(limitIdx < 500); + // same as above + if (limitIdx >= 500) { + REQUIRE(limitIdx < 500); + } } limitIdx = 0; @@ -75,10 +82,17 @@ TEST_CASE("Command Executor", "[hal][linux]") { while (result != CommandExecutor::EXECUTION_FINISHED) { limitIdx++; result = cmdExecutor.check(bytesHaveBeenRead); - REQUIRE(result != CommandExecutor::COMMAND_ERROR); + // to have constant assertion count, we first check condition + // and then force a failed assertion + if (result == CommandExecutor::COMMAND_ERROR) { + REQUIRE(result != CommandExecutor::COMMAND_ERROR); + } // This ensures that the tests do not block indefinitely usleep(500); - REQUIRE(limitIdx < 500); + // same as above + if (limitIdx >= 500) { + REQUIRE(limitIdx < 500); + } } limitIdx = 0; CHECK(bytesHaveBeenRead == true); @@ -116,10 +130,17 @@ TEST_CASE("Command Executor", "[hal][linux]") { while (result != CommandExecutor::EXECUTION_FINISHED and result != returnvalue::FAILED) { limitIdx++; result = cmdExecutor.check(bytesHaveBeenRead); - REQUIRE(result != CommandExecutor::COMMAND_ERROR); + // to have constant assertion count, we first check condition + // and then force a failed assertion + if (result == CommandExecutor::COMMAND_ERROR) { + REQUIRE(result != CommandExecutor::COMMAND_ERROR); + } // This ensures that the tests do not block indefinitely usleep(500); - REQUIRE(limitIdx < 500); + // same as above + if (limitIdx >= 500) { + REQUIRE(limitIdx < 500); + } } REQUIRE(result == returnvalue::FAILED); REQUIRE(cmdExecutor.getLastError() == 1);