Added tests for error handler macros

Fixed missing value parameter for exception + log configuration
This commit is contained in:
John Wellbelove 2022-07-24 12:52:58 +01:00
parent c2ceca3d8f
commit 4316a4d7af
9 changed files with 694 additions and 17 deletions

View File

@ -283,7 +283,7 @@ namespace etl
#define ETL_ASSERT_FAIL(e) {etl::error_handler::error((e)); throw((e));} // Calls the error handler then throws an exception.
#define ETL_ASSERT_FAIL_AND_RETURN(e) {etl::error_handler::error((e)); throw((e)); return;} // Calls the error handler then throws an exception.
#define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e) {etl::error_handler::error((e)); throw((e)); return(v);} // Calls the error handler then throws an exception.
#define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::error_handler::error((e)); throw((e)); return(v);} // Calls the error handler then throws an exception.
#else
#define ETL_ASSERT(b, e) {if (!(b)) {throw((e));}} // If the condition fails, throws an exception.
#define ETL_ASSERT_AND_RETURN(b, e) {if (!(b)) {throw((e)); return;}} // If the condition fails, throws an exception.

View File

@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5.0)
project(etl_error_handler_unit_tests)
add_definitions(-DETL_DEBUG)
add_definitions(-DETL_THROW_EXCEPTIONS)
include_directories(${PROJECT_SOURCE_DIR}/../../../include)
set(TEST_SOURCE_FILES
test_error_handler.cpp
)
add_executable(etl_tests
${TEST_SOURCE_FILES}
)
target_include_directories(etl_tests
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Enable the 'make test' CMake target using the executable defined above
add_test(etl_error_handler_unit_tests etl_tests)
# Since ctest will only show you the results of the single executable
# define a target that will output all of the failing or passing tests
# as they appear from UnitTest++
add_custom_target(test_verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
#RSG
set_property(TARGET etl_tests PROPERTY CXX_STANDARD 17)

View File

@ -0,0 +1,223 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/std
https://www.etlcpp.com
Copyright(c) 2022 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "etl/error_handler.h"
#include <stdio.h>
#include <iostream>
//*****************************************************************************
int exception_count = 0;
int return_count = 0;
//*****************************************************************************
class test_exception : public etl::exception
{
public:
test_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//*****************************************************************************
class test_exception_1 : public test_exception
{
public:
test_exception_1(string_type file_name_, numeric_type line_number_)
: test_exception(ETL_ERROR_TEXT("Test Exception 1", "1A"), file_name_, line_number_)
{
}
};
//*****************************************************************************
void Assert(bool state)
{
ETL_ASSERT(state, ETL_ERROR(test_exception_1));
}
//*****************************************************************************
void AssertFail()
{
ETL_ASSERT_FAIL(ETL_ERROR(test_exception_1));
}
//*****************************************************************************
void AssertAndReturn(bool state)
{
ETL_ASSERT_AND_RETURN(state, ETL_ERROR(test_exception_1));
++return_count;
}
//*****************************************************************************
void AssertFailAndReturn()
{
ETL_ASSERT_FAIL_AND_RETURN(ETL_ERROR(test_exception_1));
++return_count;
}
//*****************************************************************************
bool AssertAndReturnValue(bool state)
{
ETL_ASSERT_AND_RETURN_VALUE(state, ETL_ERROR(test_exception_1), true);
++return_count;
return false;
}
//*****************************************************************************
bool AssertFailAndReturnValue()
{
ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(test_exception_1), true);
++return_count;
return false;
}
//*****************************************************************************
int main()
{
try
{
Assert(false);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
Assert(true);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertFail();
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturn(false);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturn(true);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertFailAndReturn();
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
if (AssertAndReturnValue(false))
{
++return_count;
}
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
if (AssertAndReturnValue(true))
{
++return_count;
}
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
if (AssertFailAndReturnValue())
{
++return_count;
}
}
catch (test_exception_1 e)
{
++exception_count;
}
bool exception_count_passed = (exception_count == 6);
if (exception_count_passed)
{
std::cout << "Exception Count Passed\n";
}
else
{
std::cout << "Exception Count Failed\n";
}
bool return_count_passed = (return_count == 2);
if (return_count_passed)
{
std::cout << "Return Count Passed\n";
}
else
{
std::cout << "Return Count Failed\n";
}
return (exception_count_passed && return_count_passed) ? 0 : 1;
}

View File

@ -33,24 +33,20 @@ SOFTWARE.
etl::error_handler eh;
//#define ETL_ASSERT_FAIL(e) assert(false) // Asserts.
//#define ETL_ASSERT_FAIL_AND_RETURN(e) {assert(false); return;} // Asserts.
//#define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v)
//*****************************************************************************
struct ErrorLog
{
ErrorLog()
: exception_count(0)
: log_count(0)
{
}
void Log(const etl::exception& e)
{
++exception_count;
++log_count;
}
int exception_count;
int log_count;
};
int assert_return_count = 0;
@ -153,28 +149,28 @@ int main()
++assert_return_count;
}
bool error_count_passed = (error_log.exception_count == 6);
bool log_count_passed = (error_log.log_count == 6);
if (error_count_passed)
if (log_count_passed)
{
std::cout << "Error Count Passed\n";
std::cout << "Log Count Passed\n";
}
else
{
std::cout << "Error Count Failed\n";
std::cout << "Log Count Failed\n";
}
bool assert_return_count_passed = (assert_return_count == 4);
bool return_count_passed = (assert_return_count == 4);
if (assert_return_count_passed)
if (return_count_passed)
{
std::cout << "Assert Return Count Passed\n";
std::cout << "Return Count Passed\n";
}
else
{
std::cout << "Assert Return Count Failed\n";
std::cout << "Return Count Failed\n";
}
return (error_count_passed && assert_return_count_passed) ? 0 : 1;
return (log_count_passed && return_count_passed) ? 0 : 1;
}

View File

@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.5.0)
project(etl_error_handler_unit_tests)
add_definitions(-DETL_DEBUG)
add_definitions(-DETL_THROW_EXCEPTIONS)
add_definitions(-DETL_LOG_ERRORS)
include_directories(${PROJECT_SOURCE_DIR}/../../../include)
set(TEST_SOURCE_FILES
test_error_handler.cpp
)
add_executable(etl_tests
${TEST_SOURCE_FILES}
)
target_include_directories(etl_tests
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Enable the 'make test' CMake target using the executable defined above
add_test(etl_error_handler_unit_tests etl_tests)
# Since ctest will only show you the results of the single executable
# define a target that will output all of the failing or passing tests
# as they appear from UnitTest++
add_custom_target(test_verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
#RSG
set_property(TARGET etl_tests PROPERTY CXX_STANDARD 17)

View File

@ -0,0 +1,246 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/std
https://www.etlcpp.com
Copyright(c) 2022 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "etl/error_handler.h"
#include <stdio.h>
#include <iostream>
etl::error_handler eh;
//*****************************************************************************
struct ErrorLog
{
ErrorLog()
: log_count(0)
{
}
void Log(const etl::exception& e)
{
++log_count;
}
int log_count;
};
int exception_count = 0;
int return_count = 0;
//*****************************************************************************
class test_exception : public etl::exception
{
public:
test_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//*****************************************************************************
class test_exception_1 : public test_exception
{
public:
test_exception_1(string_type file_name_, numeric_type line_number_)
: test_exception(ETL_ERROR_TEXT("Test Exception 1", "1A"), file_name_, line_number_)
{
}
};
//*****************************************************************************
void Assert(bool state)
{
ETL_ASSERT(state, ETL_ERROR(test_exception_1));
}
//*****************************************************************************
void AssertFail()
{
ETL_ASSERT_FAIL(ETL_ERROR(test_exception_1));
}
//*****************************************************************************
void AssertAndReturn(bool state)
{
ETL_ASSERT_AND_RETURN(state, ETL_ERROR(test_exception_1));
++return_count;
}
//*****************************************************************************
void AssertFailAndReturn()
{
ETL_ASSERT_FAIL_AND_RETURN(ETL_ERROR(test_exception_1));
++return_count;
}
//*****************************************************************************
bool AssertAndReturnValue(bool state)
{
ETL_ASSERT_AND_RETURN_VALUE(state, ETL_ERROR(test_exception_1), true);
++return_count;
return false;
}
//*****************************************************************************
bool AssertFailAndReturnValue()
{
ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(test_exception_1), true);
++return_count;
return false;
}
//*****************************************************************************
int main()
{
ErrorLog error_log;
etl::error_handler::set_callback<ErrorLog, &ErrorLog::Log>(error_log);
try
{
Assert(false);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
Assert(true);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertFail();
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturn(false);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturn(true);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertFailAndReturn();
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturnValue(false);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertAndReturnValue(true);
}
catch (test_exception_1 e)
{
++exception_count;
}
try
{
AssertFailAndReturnValue();
}
catch (test_exception_1 e)
{
++exception_count;
}
bool log_count_passed = (error_log.log_count == 6);
if (log_count_passed)
{
std::cout << "Log Count Passed\n";
}
else
{
std::cout << "Log Count Failed\n";
}
bool return_count_passed = (return_count == 2);
if (return_count_passed)
{
std::cout << "Return Count Passed\n";
}
else
{
std::cout << "Return Count Failed\n";
}
bool exception_count_passed = (exception_count == 6);
if (exception_count_passed)
{
std::cout << "Exception Count Passed\n";
}
else
{
std::cout << "Exception Count Failed\n";
}
return (log_count_passed && return_count_passed && exception_count_passed) ? 0 : 1;
}

View File

@ -218,6 +218,151 @@ else
echo "****************\n**** Failed ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " GCC - Error macros 'log_errors' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../etl_error_handler/log_errors
mkdir -p build-make || exit 1
cd build-make || exit 1
gcc --version | grep gcc | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="g++" -DCMAKE_C_COMPILER="gcc" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'log_errors' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors' ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " GCC - Error macros 'exceptions' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../../etl_error_handler/exceptions
mkdir -p build-make || exit 1
cd build-make || exit 1
gcc --version | grep gcc | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="g++" -DCMAKE_C_COMPILER="gcc" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'exceptions' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " GCC - Error macros 'log_errors and exceptions' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../../etl_error_handler/log_errors_and_exceptions
mkdir -p build-make || exit 1
cd build-make || exit 1
gcc --version | grep gcc | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="g++" -DCMAKE_C_COMPILER="gcc" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'log_errors and exceptions' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors and exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors and exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " Clang - Error macros 'log_errors' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../../etl_error_handler/log_errors
mkdir -p build-make || exit 1
cd build-make || exit 1
clang --version | grep clang | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="clang++" -DCMAKE_C_COMPILER="clang" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'log_errors' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors' ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " Clang - Error macros 'exceptions' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../../etl_error_handler/exceptions
mkdir -p build-make || exit 1
cd build-make || exit 1
clang --version | grep clang | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="clang++" -DCMAKE_C_COMPILER="clang" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'exceptions' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
echo ""
echo "-----------------------------------------------" | tee -a log.txt
echo " Clang - Error macros 'log_errors and exceptions' test" | tee -a log.txt
echo "-----------------------------------------------" | tee -a log.txt
cd ../../../etl_error_handler/log_errors_and_exceptions
mkdir -p build-make || exit 1
cd build-make || exit 1
clang --version | grep clang | tee -a log.txt
cmake --cmake-clean-cache -DCMAKE_CXX_COMPILER="clang++" -DCMAKE_C_COMPILER="clang" ..
make -j8
if [ $? -eq 0 ]; then
echo "<<<< Passed Error macros 'log_errors and exceptions' Compilation >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors and exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
./etl_tests
if [ $? -eq 0 ]; then
echo "<<<< Passed Tests >>>>"
else
echo "****************\n**** Failed Error macros 'log_errors and exceptions' ****\n****************" | tee -a ../log.txt
exit $?
fi
cd ../..
echo ""