Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/AP_Test.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415//16/// @file AP_Test.h17/// @brief A simple unit test framework.18///19/// AP_Test provides the usual test start, condition validation and reporting20/// functions in a compact form.21///22/// Each test must be contained within a block; either a standalone function or23/// a block within a function. The TEST macro is used to start a test; it creates24/// the local object which will track the results of the test and saves the name25/// for later reporting. Only one test may be performed within each block.26///27/// Within the test, use the REQUIRE macro to describe a condition that must be28/// met for the test to pass. If the condition within the macro is not met,29/// the condition will be output as a diagnostic and the test will be considered30/// to have failed.31///32/// The test ends at the end of the block, and the result of the test will be33/// output as a diagnostic.34///35/// Optionally at the end of the test suite, the Test::report method may be used36/// to summarize the results of all of the tests that were performed.37///3839/// Unit test state and methods.40///41class Test42{43public:44/// Constructor - creates a new test.45///46/// Normally called by the TEST macro.47///48/// @param name The name of the test being started.49///50Test(const char *name);5152/// Destructor - ends the test.53///54~Test();5556/// Perform a success check.57///58/// @param expr If false, the test has failed.59/// @param source The expression source; emitted in the diagnostic60/// indicating test failure.61///62void require(bool expr, const char *source);6364/// Report the overall number of tests/pass/fails.65///66static void report();6768private:69const char *_name; ///< name of the current test70bool _fail; ///< set if any ::require calls indicate the test failed71static int16_t _passed; ///< global pass count72static int16_t _failed; ///< global fail count73};7475/// Constructor76///77Test::Test(const char *name) :78_name(name),79_fail(false)80{81}8283/// Destructor84///85Test::~Test()86{87Serial.printf("%s: %s\n", _fail ? "FAILED" : "passed", _name);88if (_fail) {89_failed++;90} else {91_passed++;92}93}9495/// Success check96///97void98Test::require(bool expr, const char *source)99{100if (!expr) {101_fail = true;102Serial.printf("%s: fail: %s\n", _name, source);103}104}105106/// Summary report107///108void109Test::report()110{111Serial.printf("\n%d passed %d failed\n", _passed, _failed);112}113114int16_t Test::_passed = 0;115int16_t Test::_failed = 0;116117/// Start a new test.118///119/// This should be invoked at the beginning of a block, before any REQUIRE120/// statements. A new test called name is started, and subsequent REQUIRE121/// statements will be applied to the test. The test will continue until122/// the end of the block (or until the _test object that is created otherwise123/// goes out of scope).124///125#define TEST(name) Test _test(# name)126127/// Attach an expression to the test's success criteria.128///129/// The expression expr must evaluate true for the test to pass. If130/// it does not, the text of the expression is output as a diagnostic131/// and the test is marked as a failure.132///133#define REQUIRE(expr) _test.require(expr, # expr)134135136137